// addThis share button definitions
var addthis_config = {
   addthis_pub: "quodis",
   services_exclude: 'backflip,ballhype,email,fark,faves,diigo,kaboodle,kirtsy,linkagogo,live,netvouz,nujij,hatena,meneme,plaxo,print,segnalo,spurl,simpy,stylehive,thisnext,twitter,yardbarker'
}

var default_search = "google";

// define array for bird queue
var ToriQueue = new Array(0);
var currentQueueIndex = 0;

// Tory Types: CSS name, Max Speed, Min Speed
ToriTypes = new Array(5)
ToriTypes[0] = ["XL", 10000, 5000];
ToriTypes[1] = ["L", 15000, 10000];
ToriTypes[2] = ["M", 20000, 15000];
ToriTypes[3] = ["S", 25000, 20000];
ToriTypes[4] = ["XS", 30000, 25000];

// Cloud Types: CSS name, Speed, Initial Speed
CloudTypes = new Array(3)
CloudTypes[0] = ["L", 35000, 8000];
CloudTypes[1] = ["M", 40000, 20000];
CloudTypes[2] = ["S", 45000, 30000];


$(document).ready(function(){

	// facebox for about tori's eye
	$('a[rel*=facebox]').facebox();
	
	// History
	function pageload(hash) {
/* 		hash = encodeURIComponent(hash); */
		if(hash) {
			fetchFeed('www.googleapis.com/buzz/v1/activities/search', 'callback=?&alt=json&max-results=40&', hash);
/* 			fetchFeed('www.googleapis.com/buzz/v1/activities/search', 'callback=?&alt=json&max-results=50&lat=38.713459&lon=-9.142191&radius=1000&', hash); */
		} else {
			fetchFeed('www.googleapis.com/buzz/v1/activities/search', 'callback=?&alt=json&max-results=40&', default_search);
/* 			fetchFeed('www.googleapis.com/buzz/v1/activities/search', 'callback=?&alt=json&max-results=50&lat=38.713459&lon=-9.142191&radius=1000&', default_hash); */
		}

	}

	$.historyInit(pageload);

	$("form:first").submit(function(){
		var hash = $("#search").val();	
		$.historyLoad(hash);
		return false;
	});

	// Start the never-ending spawn cycle
	spawnTori();
	
	// Start the cloud movement
	for (i = 0; i < CloudTypes.length; ++ i) {
		moveCloud(CloudTypes[i][0], CloudTypes[i][1], CloudTypes[i][2]);
	}
	
	// Search input listener to update submit button icon
	$("#search").keyup( function() {
		$("#sendBttn").removeClass("error");
		$("#sendBttn").addClass("standby");
	});

});

// Cloud movement
function moveCloud(id, speed, initialspeed) {
	if (parseInt($("#cloud" + id).css("left")) == 100) {
		// Second or + movement, final speed
		$("#cloud" + id).css("left", "-220px");
		thisspeed = speed;
	} else {
		// First time it's moving, so start at initial speed
		thisspeed = initialspeed;
	}
		
	$("#cloud" + id).animate({left:"100%"}, thisspeed, "linear");
	setTimeout("moveCloud('" + id + "', "+speed+")", thisspeed+100);
}

// Parse tweets to make links to links, @usernames and #hashtags
function chat_string_create_urls(input) {
    return input
    .replace(/(ftp|http|https|file):\/\/[\S]+(\b|$)/gim, '<a href="$&" class="my_link" target="_blank">$&</a>')
    .replace(/([^\/])(www[\S]+(\b|$))/gim, '$1<a href="http://$2" class="my_link" target="_blank">$2</a>')
    .replace(/(^|\s)@(\w+)/g, '$1<a href="http://twitter.com/$2" class="my_link" target="_blank">@$2</a>')
    .replace(/(^|\s)#(\S+)/g, '$1<a href="http://search.twitter.com/search?q=%23$2" class="my_link" target="_blank">#$2</a>');
}

// Hover/out dos Tori's
$('.toriWrapper') 
    .livequery(function(){ 
		var previousZ;
		var newSpeed;
        $(this) 
          .hover(function() {
    				if ($(this).find('.tweet').is(':hidden')) {
      					previousLeft = parseInt($(this).css('left'));
      					screenWidth = $(document).width();
    					
    					// Only show tweet if not on edges
    					//if ((previousLeft > 100) && ((screenWidth - previousLeft) > 100)) {*/
    						$(this).stop();
    						previousZ = $(this).css('z-index');
    						previousSpeed = parseInt($(this).attr('speed'));
    						newSpeed = parseInt((previousLeft/(screenWidth + 150)) * previousSpeed);
    						
    						$(this).css('z-index','100');
    						$(this).find('.tweet').show('fast');
    						$(this).stop();
    					//}
    				}
          }, function() { 
    				if ($(this).find('.tweet').is(':visible')) {
    					$(this).find('.tweet').hide('fast');
    					$(this).animate({left:'-150px'}, newSpeed, 'linear', function (){
    						$(this).hide();
    						$(this).remove();
    						$(this).empty();
    					}).css('z-index',previousZ);
    				}
          });
		$(this).click(function () {
				// On Tory Click
				//$(this).fadeOut("slow");					
			});
    }); 


/* define function to spawn tories */
function spawnTori(){
	if ((ToriQueue.length >= currentQueueIndex) && (ToriQueue[currentQueueIndex] != undefined)) {
		
		/* Twit message */
/* 		currentToriText = chat_string_create_urls(ToriQueue[currentQueueIndex][0]); */
		currentToriText = ToriQueue[currentQueueIndex][0];
		currentToriTwittID = ToriQueue[currentQueueIndex][1];
		currentTorifrom_user = ToriQueue[currentQueueIndex][2];
		currentToricreated_at = ToriQueue[currentQueueIndex][3];
		
/* 		console.log(currentToricreated_at); */
		
		currentToriLink = ToriQueue[currentQueueIndex][4];
		currentTorifrom_userUrl = ToriQueue[currentQueueIndex][5];
		
		currentToriDivID = Math.floor(Math.random()*1000000);
		
		/* pick a ori type */
		currentToriType = Math.floor(5*Math.random());
		
		currentToriSpeed = ToriTypes[currentToriType][2] + (Math.floor((ToriTypes[currentToriType][1]-ToriTypes[currentToriType][2])*Math.random()))
		
		// Build Tori HTML
		torihtml = '\
		<div id="'+ currentToriDivID +'" class="toriWrapper tori'+ ToriTypes[currentToriType][0] +'Wrapper" speed="'+currentToriSpeed+'">\
			<div class="toriBouncer">\
				<a href="javascript:void(0);"><img src="assets/img/layout/tori'+ ToriTypes[currentToriType][0] +'.png"></a>\
				<div class="tweet">\
					<h2><strong><a href="' + currentTorifrom_userUrl + '" title="'+currentTorifrom_user+'" target="_blank">'+currentTorifrom_user+'</a></strong> said <i><a class="timeago" href="'+currentToriLink+'" target="_blank"><abbr title="'+currentToricreated_at+'">' + $.timeago(currentToricreated_at) + '</abbr></a></i></h2>\
					<p>'+currentToriText+'</p>\
				</div>\
			</div>\
		</div>';
		
		// Append Tori to #content
		$('#content').append(torihtml);

		// Twitt built, now slide to left and start to bounce
		startHim('#'+currentToriDivID, currentToriSpeed);
		bounceHim('#'+currentToriDivID);
		
		// Set counter to go to next in queue on the next time spawnTori is called
		currentQueueIndex ++;
		
	//} else if(currentQueueIndex > 0)  {
		//RESTART, REQUERY
		
	} else {
		// Nothing good found in Queue, go to beginning
		currentQueueIndex = 0;
	}
	
	// Go to try next in queue, in 2000 to 4000 ms
	setTimeout("spawnTori()",2000 + parseInt(Math.random()*2000));
}

function startHim(id, speed){
	randomHeight = 25-parseInt(Math.random()*50);
	$(id).css('left', $(document).width() + 'px');
	$(id + " .toriBouncer").css('margin-top', randomHeight + 'px');
	$(id).animate({left:"-150px"}, speed, "linear", function (){
		$(this).hide();
		$(this).remove();
		$(this).empty();
	});
}

function bounceHim(id){
	$(id + " .toriBouncer").animate({marginTop:"-=4px"},450).animate({marginTop:"+=4px"},300);
	if ($(id).is(':visible')) {
 		setTimeout("bounceHim('" + id + "')", parseInt(Math.random()*1750));
	}	
}


function fetchFeed(type, parameters, torisQuery) {
  // remove error class upon new request
	$("#sendBttn").removeClass("error");
	// set the button to loading
	$("#sendBttn").addClass("loading");
	// set the search input value
	$("#search").val(decodeURIComponent(torisQuery.replace(/\+/g,  " ")));
	// safari already encodes the url, for everything else we'll encode it
	if ($.safari == 'false') { torisQuery = encodeURIComponent(torisQuery);	}
	
	// do 'The Call'
	$.getJSON('http://' + type + '?' + parameters + 'q=' + torisQuery,  
		function(results) {

		  // debug
  	  if ("console" in window) {
        //console.log('queried: ' + 'http://' + type + '?' + parameters + 'q=' + torisQuery);
  	  	//console.log(data.data.items);
  	  }
  	  
      // we got new results, reset queue and index
  		ToriQueue.length = 0;
  		currentQueueIndex = 0;
      // loaded results! remove thy loading
  		$("#sendBttn").removeClass("loading");
  		// set it on standby
			$("#sendBttn").addClass("standby");
			
			// TODO: error handling heh


  	  // parse results
      $.each(results.data.items, function(i, item) {
        //console.log(item);
        ToriQueue[ToriQueue.length] = [item.object.content, item.actor.id, item.actor.name, item.updated, item.object.links.alternate[0].href, item.actor.profileUrl];
      });
      
		}  
	);
}
