


// jqtweet object
JQTWEET = {
	
	// Set twitter username, number of tweets & id/class to append tweets
	user: 'jellcreative',
	numTweets: 3,
	appendTo: '#tweets ul',

	// core function of jqtweet
	loadTweets: function() {
		$.ajax({
			url: 'http://api.twitter.com/1/statuses/user_timeline.json/',
			type: 'GET',
			dataType: 'jsonp',
			data: {
				screen_name: JQTWEET.user,
				include_rts: true,
				count: JQTWEET.numTweets,
				include_entities: true
			},
			success: function(data, textStatus, xhr) {

				 var html = '<li class="tweet"><a href="http://twitter.com/' + JQTWEET.user + '" target="_blank">@'+JQTWEET.user+'</a> TWEET_TEXT<span class="time"> AGO</span></li>';
		
				 // append tweets into page
				 for (var i = 0; i < data.length; i++) {
				
					 var ent_length = data[i].entities.urls.length;
					 
					 var orig_txt_array = [];
					 var disp_txt_array = [];
					 
					  var disp_url_text = data[i].text
					 
					 for (var j = 0; j < ent_length; j++) {
						//console.log('count = ' + j)
						
						// get the indicies of the text to replace
						var from = data[i].entities.urls[j].indices[0]
						var to = data[i].entities.urls[j].indices[1] 
						
						// get the replacement substrings
						orig_txt_array[j] = [disp_url_text.substring(from, to)] ;
						// get the display urls to replace the substrings
						disp_txt_array[j] = data[i].entities.urls[j].display_url ;
						 
					}
					 
					 // replace the items in the array with this
					 for (k = 0; k < orig_txt_array.length; k++){
						 disp_url_text = disp_url_text.replace(orig_txt_array[k], '<a href="' + orig_txt_array[k] + '" target="_blank">' + disp_txt_array[k] + '</a>');
					 }
					 
		$(JQTWEET.appendTo).append(
						html.replace('TWEET_TEXT', JQTWEET.ify.clean(disp_url_text) )
							.replace(/USER/g, data[i].user.screen_name)
							.replace('AGO', JQTWEET.timeAgo(data[i].created_at) )
							.replace(/ID/g, data[i].id_str)
					);
				}					
			}	

		});
		
	}, 
	
	
	
	
		
	/**
      * relative time calculator FROM TWITTER
      * @param {string} twitter date string returned from Twitter API
      * @return {string} relative time like "2 minutes ago"
      */
    timeAgo: function(dateString) {
		var rightNow = new Date();
		var then = new Date(dateString);
		
		if ($.browser.msie) {
			// IE can't parse these crazy Ruby dates
			then = Date.parse(dateString.replace(/( \+)/, ' UTC$1'));
		}

		var diff = rightNow - then;

		var second = 1000,
		minute = second * 60,
		hour = minute * 60,
		day = hour * 24,
		week = day * 7;

		if (isNaN(diff) || diff < 0) {
			return ""; // return blank string if unknown
		}

		if (diff < second * 2) {
			// within 2 seconds
			return "right now";
		}

		if (diff < minute) {
			return Math.floor(diff / second) + " seconds ago";
		}

		if (diff < minute * 2) {
			return "about 1 minute ago";
		}

		if (diff < hour) {
			return Math.floor(diff / minute) + " minutes ago";
		}

		if (diff < hour * 2) {
			return "about 1 hour ago";
		}

		if (diff < day) {
			return  Math.floor(diff / hour) + " hours ago";
		}

		if (diff > day && diff < day * 2) {
			return "yesterday";
		}

		if (diff < day * 365) {
			return Math.floor(diff / day) + " days ago";
		}

		else {
			return "over a year ago";
		}
	}, // timeAgo()
    
	
    /**
      * The Twitalinkahashifyer!
      * http://www.dustindiaz.com/basement/ify.html
      * Eg:
      * ify.clean('your tweet text');
      */
    ify:  {
      link: function(tweet) {
        return tweet.replace(/\b(((https*\:\/\/)|www\.)[^\"\']+?)(([!?,.\)]+)?(\s|$))/g, function(link, m1, m2, m3, m4) {
          var http = m2.match(/w/) ? 'http://' : '';
          //return '<a class="twtr-hyperlink" target="_blank" href="' + http + m1 + '">' + ((m1.length > 25) ? m1.substr(0, 24) + '...' : m1) + '</a>' + m4;
		  
		 return '<a class="twtr-hyperlink" target="_blank" href="' + http + m1 + '">' + ((m1.length > 25) ? m1.substr(0, 24) + '...' : m1) + '</a>' + m4;
        });
      },

      at: function(tweet) {
        return tweet.replace(/\B[@＠]([a-zA-Z0-9_]{1,20})/g, function(m, username) {
          return '<a target="_blank" class="twtr-atreply" href="http://twitter.com/intent/user?screen_name=' + username + '">@' + username + '</a>';
        });
      },

      list: function(tweet) {
        return tweet.replace(/\B[@＠]([a-zA-Z0-9_]{1,20}\/\w+)/g, function(m, userlist) {
          return '<a target="_blank" class="twtr-atreply" href="http://twitter.com/' + userlist + '">@' + userlist + '</a>';
        });
      },

      hash: function(tweet) {
        return tweet.replace(/(^|\s+)#(\w+)/gi, function(m, before, hash) {
          return before + '<a target="_blank" class="twtr-hashtag" href="http://twitter.com/search?q=%23' + hash + '">#' + hash + '</a>';
        });
      },

      clean: function(tweet) {
        return this.hash(this.at(this.list(this.link(tweet))));
      }
    } // ify

	
};












	
$(document).ready(function(){
						   
	// start jqtweet!
	JQTWEET.loadTweets();
	
						   
						
	/* get tweets 				   
	$('#tweets').twitter({
			from    : 'mediatemple',            
			//replies : false,     
			//retweets: false,           
			limit    : 3      
	});*/	
	/* if IE 6 */
	$('#twitter').mouseover(function() {
		$(this).addClass('sfhover')							 
	});
	$('#twitter').mouseout(function() {
		$(this).removeClass('sfhover')							 
	});
	
	if($(".play_slideshow").length > 0){
		$(".play_slideshow").click(function(e){
			e.preventDefault();
			Modal.show('617', '386', 'modalSlideshow()');
			
			
		});
	}
	
	
});

function setHash(el, attribute){
	window.location.hash = el.attr(attribute)
}


function getHash(){
	var hash=location.hash
	if(hash != ''){
		hash = hash.substring(1)
		//hash = hash.replace("&", "/"); // our custom directory characters with actual directory char
		//hash = hash.replace("%20", " "); // replace with actual whitespace
		return hash;
	} else {
		return false;
	}
}


function clearHash(){
	if ("pushState" in history){ // html5 compatilbe : remove the # too
    	history.pushState("", document.title, window.location.pathname);
	} else { // not html5 : leaves #
        window.location.hash = "";
	}	
}

function write_email(prefix, suffix, alt, classname){
	if(alt){
  		var address_hold ="<a href='mailto:" + prefix + "@" + suffix +"' target='_blank' class='"+ classname +"' >"+ alt +"</a>";
 	}else{
 		var address_hold ="<a href='mailto:" + prefix + "@" + suffix +"' target='_blank' class='"+ classname +"' >" + prefix + "@" + suffix +"</a>";
 	}
	address=document.write(address_hold);
 	return address;
}

GlobalSlideShow = {
	current_slide : 0,
	play_state : 'play'
}
 
