var timer;
var timer2;

$(document).ready(function() {
	$.ajaxSetup({
		type:"POST",
		url:link_prefix+"index.php",
		cache:false,
		dataType:"html",
		scriptCharset:"UTF-8",
		error:ajaxError
	}); // global ajax setup
	
	$(".box_content img:not(.no_op)").live("mouseover",function(){
		$(this).animate({opacity:1},200);
	});
	$(".box_content img:not(.no_op)").live("mouseout",function(){
		$(this).stop().animate({opacity:0.5},200);
	});
	
	$(".t_c").click(function(){toggle2Boxes(0,6);return false;});
	$("#refresh").click(function(){doUpdate();return false;});
	
	// initialize portfolio tooltips
	$(".tt2").each(function() {
		var title=$(this).attr("title");
		var tp=title.split(" ::: ");
		$(this).attr("title","");
		$(this).qtip({
			content:{
				text:"<p>"+tp[1]+"</p><p>"+tp[2]+"</p>"+tp[3]+"</p>",
				title:{text:tp[0]}
			},
			position:{
				corner:{target:"topRight",tooltip:"topLeft"},
				adjust:{x:5,y:5}
			},
			style:{
				name:"dark",
				width:250,
				border:{width:4},
				classes:{content:"port_desc",title:"port_title"}
			},
			show:{
				when:"mouseover",
				solo:true,
				effect:{type:"fade",length:80}
			},
			hide:{
				when:"mouseout",
				fixed:true,
				delay:250,
				effect:{type:"fade",length:250}
			}
		});
	});
	
	init();
	
	timer=window.setTimeout("doUpdate();",1000);
	timer2=window.setTimeout('doUpdateTagline();',90*1000);
});

function init() {
	$(".box_content a img:not(.no_op)").css('opacity',0.5);
	$(".tumblr a.image,.flickr a").fancybox({
		'zoomSpeedIn':500,
		'zoomSpeedOut':800,
		'zoomSpeedChange':800,
		'easingIn':'easeOutBack',
		'easingOut':'easeOutBounce',
		'easingChange':'easeInOutBack',
		'padding':0,
		'hideOnContentClick':true,
		'zoomOpacity':false,
		'centerOnScroll':false,
		'imageScale':false,
		'overlayShow':false 
	});
	// initialize h3 tooltips
	$(".tt").each(function() {
		var title=$(this).attr("title");
		if(title.length>0) {
			$(this).attr("title","");
			$(this).qtip({
				content:title,
				position:{
					corner:{target:'topLeft',tooltip:'bottomLeft'},
					adjust:{x:18}
				},
				style:{name:"dark",tip:"bottomLeft"},
				show:{solo:true}
			});
		}
	});
	// initialize tumblr tooltips 
	$(".ttt").each(function() {
		var title=$(this).attr("title");
		if(title.length>0) {
			$(this).attr("title","");
			$(this).qtip({
				content:title,
				position:{
					corner:{target:'bottomLeft',tooltip:'topLeft'},
					adjust:{x:0,y:2}
				},
				style:{
					name:"dark",
					width:145,
					classes:{content:"port_desc"}
				},
				show:{solo:true}
			});
		}
	});
	// init last.fm info popups
	$("a.track_").unbind();
	$("a.track_").click(function(){
		$($(this).attr("id")).slideToggle(200);
		return false;
	});
	if($("#nowplaying_cover:visible").length==0) {
		$("#nowplaying div div").slideDown(200);
	}
}

function doUpdate() {
	$(".loader").show();
	$("#refresh").hide();
	$.ajax({
		data:"a=u_all&last_tweet_id="+last_tweet_id
				+"&last_tumblr_id="+last_tumblr_id
				+"&last_flickr_id="+last_flickr_id
				+"&last_lastfm_id="+last_lastfm_id,
		success:updateComplete
	});
}

function updateComplete(html) {
	results=html.split(":|:");
	updateContent(2,results[1]);
	updateContent(3,results[2]);
	updateContent(4,results[3]);
	updateContent(5,results[4]);
	window.clearTimeout(timer);
	timer=window.setTimeout("doUpdate();",60*1000);
	init();
	$("#refresh").show();
}

function updateContent(box_id,html) {
	$("#box_content_"+box_id+" .loader").hide();
	if(html!="") {
		$("#box_content_"+box_id).empty().html(html);
	}
}

function doUpdateTagline() {
	$.ajax({
		data:"a=u_tl&id="+$("#tagline_id").html(),
		success:function(html){
			if(html!='') {
				results=html.split(":|:");
				var tagline=$("#tagline");
				tagline.animate({opacity:0},500,"linear",function(){
					$("#tagline_id").html(results[0]);
					tagline.html(results[1]);
					tagline.animate({opacity:1},500);
					window.setTimeout('doUpdateTagline();',90*1000);
				});
			}
		}
	});
}

function ajaxError(XMLHttpRequest,textStatus,errorThrown) {
	XMLHttpRequest.abort();
	//alert("OOPS! An AJAX error occured! Not your fault and you also can't do anything about it....");
}

function toggle2Boxes(id_1,id_2) {
	if($("#box_wrapper_"+id_1+":visible").length>0) {
		$("#box_wrapper_"+id_1).animate({opacity:"toggle"},200,"linear",function() { $("#box_wrapper_"+id_2).animate({opacity:"toggle"},200,"linear",function(){}); });
	} else {
		$("#box_wrapper_"+id_2).animate({opacity:"toggle"},200,"linear",function() { $("#box_wrapper_"+id_1).animate({opacity:"toggle"},200,"linear",function(){}); });
	}
}
