var time_diff;

$(document).ready(function() {
	getDiff();
	$.post("board/get_nick.php", {}, function(data) {
		if(data!=""){
			checkInbox();
			startTimer();
		}
	});
});
/*
$('#log-me-in').live('click',function(event){
	var str=$('#login_form').serialize();
	$.post("login.php",str,function(data){
		var h=$(document).height();;
		$('body').append("<div class=\"ui-widget-overlay\" style=\"width: 100%; height: "+h+"px; z-index: 1003; position:fixed; top:0; left:0;\"></div>");
		$('body').append("<div class=\"ui-corner-all\" style=\"background:#ccc; opacity:0.8; width: 600px; margin-left:-300px; height: 150px; z-index: 1004; position:fixed; top:100px; left:50%; \">"+data+"</div>");
		var t=setTimeout('window.location.reload()', 3000 );
	});
});*/

$('#register').live('click',function(event){
	window.open("registrati.php",'_self');
});


/*
$('#tb_login').live('click',function(event){
	$('#tool_login').slideToggle(100);
});
*/
/*
$('#tb_logout').live('click',function(event){
	$.post("logout.php", {}, function(data) {
		var h=$(document).height();;
		$('body').append("<div class=\"ui-widget-overlay\" style=\"width: 100%; height: "+h+"px; z-index: 1003; position:fixed; top:0; left:0;\"></div>");
		$('body').append("<div class=\"ui-corner-all\" style=\"background:#ccc; opacity:0.8; width: 600px; margin-left:-300px; height: 150px; z-index: 1004; position:fixed; top:100px; left:50%; \">"+data+"</div>");
		var t=setTimeout( window.location.reload(), 3000 );
	});
});
*/
//-----------------------------------------------------------------------------------------------------------------------------------//

$('#tb_10').live('click',function(event){
	if($('#tool_friend').is(":not(:hidden)")){
		$('#tool_friend').slideToggle(200,function(){
			$("#tf").attr("src","");
		});
	}
	$('#tool_board').slideToggle(200,function(){
		if($('#tool_board').is(":hidden")){
			$('#tb_10').html("<span class=\"tbc clearfix\"><span id=\"boardIcon\" class=\"ui-icon ui-icon-comment\"></span></span>GMBoard");
			$("#tb").attr("src","");
			startTimer();
		}else{
			$('#tb_10').html("<span class=\"tbc clearfix\"><span id=\"boardIcon\" class=\"ui-icon ui-icon-closethick\"></span></span>GMBoard");
			var d=new Date();
			$("#tb").attr("src","board/experimental_exp.php?ss="+d.getTime());
			destroyTimer();
		}
	});
	
});
//-----------------------------------------------------------------------------------------------------------------------------------//

$('#tb_7').live('click',function(event){
	if($('#tool_board').is(":not(:hidden)")){
		$('#tool_board').slideToggle(200,function(){
			$('#tb_10').html("<span class=\"tbc clearfix\"><span id=\"boardIcon\" class=\"ui-icon ui-icon-comment\"></span></span>GMBoard");
			$("#tb").attr("src","");
			startTimer();
		});
	}
	$('#tool_friend').slideToggle(200,function(){
		if($('#tool_friend').is(":hidden")){
			$("#tf").attr("src","");
		}else{
			$("#tf").attr("src","contactlist/contactlist.php");
		}
	});
});
//-----------------------------------------------------------------------------------------------------------------------------------//

$('#tb_4').live('click',function(event){
	$.get('modules/messaggio_nuovo_lite.php',function(data){
		$('#message').html(data);
		$('#message').dialog({
			bgiframe: true,
			modal: true,
			width:450,
			resizable: false,
			draggable: false
		});
		$('#message').bind('dialogclose', function(event, ui) {
		});
	});
});
//-----------------------------------------------------------------------------------------------------------------------------------//
function internalMail(to, title, body){
		$.get('modules/messaggio_nuovo_lite.php',{usr:to,title:title,body:body},function(data){
			var name = new Number(Math.floor(Math.random()*1000000000));
			var tmpname = name.toString();
			$('body').append("<div id=\""+tmpname+"\" title=\"Messaggio\"style=\"display:none;\">"+data+"</div>");
			$("#"+tmpname).dialog({
				bgiframe: true,
				modal: true,
				width:450,
				resizable: false,
				draggable: false,
				close: function(event,ui){
					$("#"+tmpname).remove();
				},
				buttons: [
					{
						text: "Chiudi",
						click: function() { $(this).dialog("close"); }
					}
				]
			});
		});
}

function startTimer(){
	var time;
	$(document).everyTime(10000, "notify", function(i) {
		if ($.cookie("notify")) {
			time = $.cookie("notify");
		} else {
			time = new Date().getTime();
			time = time.toString().substr(0,10);
			time = parseInt(time) + time_diff;
		}
		$.post("modules/notify.php", {time: time}, function(data) {
			var recordtime = new Date().getTime();
			recordtime = recordtime.toString().substr(0,10);
			recordtime = parseInt(recordtime) + time_diff;
			$.cookie("notify",recordtime);
			if (data == "things") {
				blink("destroy","boardIcon");
				blink("start","boardIcon");
			}
		});
		checkInbox();
	});
}

function checkInbox()
{
	$.post("modules/inbox_notify.php", {}, function(in_data) {
		if (in_data != 0) {
			blink("destroy","inboxIcon");
			blink("start","inboxIcon");
			$("#nummsg").html("("+in_data+") ");
		} else {
			blink("destroy","inboxIcon");
			$("#nummsg").html("");
		}
	});
}

function blink(action, obj)
{
	if (action == "start") {
		$("#"+obj).everyTime(500, "blink", function(i) {
			if ($(this).css('display') == "inline-block")
				$(this).css('display','none');
			else 
				$(this).css('display','inline-block');
		});
	} else if (action == "destroy") {
		$("#"+obj).stopTime('blink');
		$("#"+obj).css('display','inline-block');
	}
}

function destroyTimer()
{
	$.cookie("notify",null);
	$(document).stopTime('notify');
	blink("destroy","boardIcon");
}

function getDiff()
{
	$.post("modules/notify.php", {diff: 1}, function(server_time) {
		client_time = new Date().getTime();
		client_time = client_time.toString().substr(0,10);
		time_diff = server_time - parseInt(client_time);
	});
}

function ping(delay)
{
	var rnd=new Number(Math.floor(Math.random()*1000000000)); 
	$.post("board/get_nick.php", {rnd: rnd}, function(data) {
		$.post("contactlist/ping.php", {nick: data, method: "ajax", delay: delay, rnd: rnd});
	});
}

function keepMeAlive()
{
	$(document).everyTime(900000,"keepmealive",function() {
		ping("350");
	});
}

