﻿/// <reference path="JQuery/jquery-1.3.2-vsdoc2.js" />

/**
** Created by Chris.mullany@greatfridays.com
**/

String.prototype.parseURL = function() {
	return this.replace(/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&\?\/.=]+/g, function(url) {
		return url.link(url);
	});
};

String.prototype.parseUsername = function() {
    return this.replace(/[@]+[A-Za-z0-9-_]+/g, function(u) {
        var username = u.replace("@", "");
        return u.link("http://twitter.com/" + username);
    });
};

String.prototype.parseHashtag = function() {
    return this.replace(/[#]+[A-Za-z0-9-_]+/g, function(t) {
        var tag = t.replace("#", "%23");
        return t.link("http://search.twitter.com/search?q=" + tag);
    });
};


$j(document).ready(function() {
    renderTwitter();
    // renderFlickr();
});

function renderTwitter() {
    $j(".tweets").html("<img src=\"_Client/Images/Global/loader.gif\" alt=\"loading tweets\" />");
    $j.getJSON("/Networks/GetLatestTweets", null,
        function(data) {
            $j(".tweets").html("");
            var count = 0;
            var maxTweets = 3;
            $j.each(data, function(index, data) {
                if (count == maxTweets) return;
                var date = new Date();
                var dateInt = data.CreatedDate.split('(')[1].split(')')[0];
                date.setTime(dateInt);
                var tweetText = (((data.Text.parseURL()).parseUsername()).parseHashtag());
                $j(".tweets").append("<p class='date dark'>" + date.getHours() + ":" + date.getMinutes() + " on " + date.getDate() + "/" + (date.getMonth() +1)+ "/" + date.getFullYear() + "</p><p>" + tweetText + "</p>");
                count++;
            });
        }
    );
}

function renderFlickr(){
    $j(".flickr-stream").html("<img src=\"_Client/Images/Global/loader.gif\" alt=\"loading photo stream\" />");
    $j.getJSON("Networks/GetLatestFlickr", null,
        function(data) {
            $j(".flickr-stream").html("");
            var count = 0;
            var maxImages = 12;
            var title = "";
            var months = ['Jan', 'Feb', 'Mar', 'April', 'May', 'June', 'July', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
            $j.each(data, function(index, data) {
                var date = new Date();
                date.setTime(data.Date * 1000);

                $j('.flickrdate').text(months[date.getMonth()] + ', ' + date.getDate() + ', ' + date.getFullYear().toString().slice(2,4));
                if (count == maxImages) return;
                if (data.Title != title) {
                    //$j('.flickr-stream').append('<strong>' + data.Title + '</strong>');
                    title = data.Title;
                }
                $j(".flickr-stream").append("<a target='_blank' href='" + data.ImageInPhotoStreamUrl + "'><img src='" + data.ImageThumbUrl + "' title='" + data.Title + "' alt='" + data.Title + "' /></a>");
                count++;
            });
        }
    );
}