/*
	VacuuWeb 3000
	
	Author: Volontaire 			 	www.volontaire.se
	Client: Ballerina Kladdkaka 	www.ballerinakladdkaka.se
	
	Goes great with getData.php!
	 
*/

// SUPER VARS
goRes = "";
giRes = "";
twRes = "";
thePageGo = 0;
thePageTw = 1;
thePageGi = 0;

$(function(){
	
	// Get Stuff
	getData("go", "#googleSearch", thePageGo);
	getData("tw", "#twitterSearch", thePageTw);
	getData("gi", "#googleImages", thePageGi);

	// HEADERS RELOAD
/*
	$("#go").click(function(){getData("go", "#googleSearch", thePageGo);$("#goKaka").show();});
	$("#tw").click(function(){getData("tw", "#twitterSearch", thePageTw);$("#twKaka").show();});
	$("#gi").click(function(){getData("gi", "#googleImages", thePageGi);$("#giKaka").show();});
*/

	// NEXT/PREV Functions
	$("#Plus").click(function(){
		thePageGo=thePageGo+8;
		thePageGi=thePageGi+8;
		thePageTw++;
		getData("go", "#googleSearch", thePageGo);
		getData("tw", "#twitterSearch", thePageTw);
		getData("gi", "#googleImages", thePageGi);

		});
	
	$(window).scroll(function(){
        if  ($(window).scrollTop() == $(document).height() - $(window).height()){
        thePageGo=thePageGo+8;
		thePageGi=thePageGi+8;
		thePageTw++;
		getData("go", "#googleSearch", thePageGo);
		getData("tw", "#twitterSearch", thePageTw);
		getData("gi", "#googleImages", thePageGi);
        }
	});

	$("#dopp").fancybox().trigger('click');
	$("noscript").html("");

});


// Super VacuuWeb Getter. Now with Extra Special Switch Statement++
function getData(srvc, selector, starting){
	//$(selector + " .results").html("");
	$.getJSON("xhr/getData.php", {query: "ballerina+kladdkaka", country: "SE", service: srvc, start: starting},
		function(json){
			switch(srvc) {
			
	// GOOGLE
				case 'go': 	
				if (starting>=24) {$("#Plus").hide();}
				
					$.each(json.responseData.results, function(i, gResults){  
						var goTitle 	= gResults.titleNoFormatting;
						var goDisp 		= gResults.url;
						var goUrl 		= gResults.unescapedUrl;
						var goVis 		= gResults.visibleUrl;
						var goSum 		= gResults.content;
							goRes 		= goRes + "<div class=\"go-res\"><a href=\"" + goUrl + "\" target=\"_blank\">" + goTitle + "</a>" +
						"<p class=\"sum\">" + goSum + "</p>"+
						"<p class=\"dispurl\">" + goVis + "</p><br />"+
						"</div>";
						
						});
					$("#goKaka").fadeOut();	
					$(selector + " .results").append(goRes);
					goRes="";
					$(".go-res").fadeIn();
				break;
	// TWITTER
				case 'tw':
					$.each(json.results, function(i,results){       
						var tweetTime = parseDate(results.created_at)
						var twPic = results.profile_image_url;
						// Parse Twitter
						twRes = 
						twRes +
						"<div class=\"tweet "+ i +"\"><a class=\"twitter-user\" href=\"http://twitter.com/" + results.from_user + "\" target=\"_blank\">"+
						"<img src=\"" + twPic + "\" class=\"alignleft\" title=\"" + tweetTime+"\" />" + 
						 "</a>  "+
						"" + results.text + "" + 
						//"<span class=\"postTime\"> | "+ tweetTime +"</span></div>"; 
						"</div>"
					
						});
					$("#twKaka").fadeOut();	
					$(selector + " .results").append(twRes); 
					$(selector + " .results").autolink(); 
					twRes="";
					$(".tweet").fadeIn();
				break;
				
	// GOOGLE IMAGES
				case 'gi':
				if (starting>=24) {$("#Plus").hide();}
				
					$.each(json.responseData.results, function(i,resultset_web){  
					var giTitle = resultset_web.content;
					var giPic = resultset_web.tbUrl;
					var giOrig = resultset_web.originalContextUrl;
					var giUrl = resultset_web.url;
					var giPage = "<a href="+giOrig+" target=_blank>View in context</a>";
						
						giRes = giRes + "<div class=\"gi-res\"><div class=\"gi-img\" style=\"background: #000 url('"+giUrl+"') "+
						"50% 50% no-repeat;width:270px;height:120px;\">"+
						"<a href=\"" + giOrig + "\" target=\"_blank\" style=\"display:block;height:130px\" title=\"Visa sida\"> </a></div></div>";
				
					$("#giKaka").fadeOut();	
						
				});
				$(selector + " .results").append(giRes);
				$(".gi-res").fadeIn();
				giRes="";
				break;
			}
		
		//goRes = "";	
			
		});
	
}


// Make Twitter inlinks clickable
jQuery.fn.autolink = function () {
	return this.each( function(){
		var re = /((http|https|ftp):\/\/[\w?=&.\/-;#~%-]+(?![\w\s?&.\/;#~%"=-]*>))/g;
		$(this).html( $(this).html().replace(re, '<a href="$1" target="_blank">$1</a> ') );
		var twU = /\@([a-z0-9\_]+)(?![-a-z0-9\_])/ig;
		$(this).html( $(this).html().replace(twU, '<a href="http://twitter.com/$1" target="_blank">@$1</a> ') );
	});
} 

// Makes the JSON response from Dates look nice
function parseDate(dateToParse) {
	// Parse Dates
					var dateUnparsed = Date.parse(dateToParse);
					var thisTime = "";
					var curdate = new Date();
					var timeSince = curdate.getTime() - dateUnparsed;
					var inSeconds = timeSince / 1000;
					var inMinutes = timeSince / 1000 / 60;
					var inHours = timeSince / 1000 / 60 / 60;
					var inDays = timeSince / 1000 / 60 / 60 / 24;
					var inYears = timeSince / 1000 / 60 / 60 / 24 / 365;
					// in seconds
					if(Math.round(inSeconds) == 1){
						thisTime = "1 sec ago"
						return thisTime;
					}
					else if(inMinutes < 1.01){
						thisTime = (Math.round(inSeconds) + " sekunder sen");
						return thisTime;
					}
					// in minutes
					else if(Math.round(inMinutes) == 1){
						thisTime = "1 min ago";
						return thisTime;
					}
					else if(inHours < 1.01){
						thisTime = (Math.round(inMinutes) + " minuter sen ");
						return thisTime;
					}
	 
					// in hours
					else if(Math.round(inHours) == 1){
						thisTime = "1 h ago";
						return thisTime;
					}
					else if(inDays < 1.01){
						thisTime = (Math.round(inHours) + " timmar sen");
						return thisTime;
					}
					
					// in days
					else if(Math.round(inDays) == 1){
						thisTime = "1 day ago";
						return thisTime;
					}
					else if(inYears < 1.01){
						thisTime = (Math.round(inDays) + " dagar sen");
						return thisTime;
					}
					
					// in years
					else if(Math.round(inYears) == 1){
						thisTime = "1 year ago";
						return thisTime;
					}
					else
					{
						thisTime = (Math.round(inYears) + " år sen");
						return thisTime;
					}
	
	}


