//ON READY
$(document).ready(function(){

	$(document).pngFix(); 
	
	var first_row=5; //numero box sulla prima riga
	
	//VARIABILI
	var width_tot_top=0; //larghezza totale top
	var width_tot_bot=0; //larghezza totale bot
	var speed = 300;
	var box_num=post_ID.length; //numero di box
	if(box_num>9){box_num=9;}
	var delay=0; //ritardo apparizione primo box
	var separator=first_row-1;
	var z_index_max = 10;
	box_width = new Array();
	box_height = new Array();
	box_left = new Array();
	box_top = new Array();
	
	//APRIRE UN BOX
	function openbox(box_num, postid){
	$(".overlay-map").hide();
	
		$(".box > .openbox").css({'display':'none'});
		$(".box").css({"z-index":"10"});
		$("#"+postid).addClass("opened");
		$("#"+postid+" .thumb").css({'display':'none', 'zIndex':'20'});
		$("#"+postid+" .post").css({'display':'block', 'zIndex':'50'});
		if($("#"+postid+" .post > div").attr("class")=="box-image"){
			var box_opened_width = $(".image-"+postid).width();
			var box_opened_height = 500;
		} else if($("#"+postid+" .post > div").attr("class")=="box-video"){
			var box_opened_width = 844;
			var box_opened_height = 500;
		} else {
			var box_opened_width = 970;
			var box_opened_height = 500;
		}
		$("#"+postid).css({"z-index":"900"});
		$("#"+postid).animate({ 
			width:box_opened_width+"px",
			height:box_opened_height+"px",
			marginTop:(($("#content").height()-box_opened_height)/2)+"px",
			marginLeft: (($("#content").width()-box_opened_width)/2)+"px"
		},{queue:false, duration:speed*3, "easing":"easeOutExpo"} );
		$(".image-"+postid).css({"marginTop":(-(box_opened_height)/2),"marginLeft":(-box_opened_width/2) });	
		setTimeout(function() {
			$("#"+postid+" > .closebox").css({display:"block"}).animate({top:"0",right:"0"}, speed); //mostra pecetta "chiudi"
		}, speed*3);
	
	      
	
	}

	//CHIUDERE UN BOX
	function closebox(box_num, postid){
		$(".box-"+box_num+" > .closebox").animate({top:"-20px",right:"-100px"},function(){$("#"+i+" > .closebox").css({display:"none"});}); //nascondo la pecetta "chiudi"
		setTimeout(function() {
			$("#"+postid).removeClass("opened");
			$(".box-"+box_num).animate({
				width:""+box_width[box_num]+"px",
				height:""+box_height[box_num]+"px",
				marginLeft:""+box_left[box_num]+"px",
				marginTop:""+box_top[box_num]+"px"
			},{queue:false, duration:speed*3, "easing":"easeOutExpo"});
			$("#"+postid+" .thumb").css({'display':'block', 'zIndex':'50'});
			$("#"+postid+" .post").css({'display':'none', 'zIndex':'20'});

			$(".box").css({"zIndex":"10"});
			$(".box-"+box_num).css({"zIndex":"900"});
		}, speed*0.8);
		setTimeout(function() {
			$(".box > .openbox").css({display:"block"});
		}, speed*2);
	};
	
	//MOSTRA I BOX
	function show_box(box_num, postid){
		delay=box_num*100;
		setTimeout(function() {
			$(".box-"+box_num).show("scale", 200, load_content(box_num, postid));
			
		}, delay);
	}
		function close_box(box_num, postid){
		delay=box_num*200;
		setTimeout(function() {
			$(".box-"+box_num).hide("scale", 200, load_content(box_num, postid));
		}, delay);
	}
	//CARICA IL CONTENUTO
	function load_content(box_num, postid){
			setTimeout(function() {
  				if(box_num<=separator){row=1; } else {row=2; }
				$("#"+postid+" .thumb").load(url+"/load-thumb/?load_id="+postid+"&row="+row, //carica thumb
				function(){ //callback dopo il caricamente di un box
					$(this).parent().css("background-image", "none");
					$(".box-"+box_num+" .mask").css("display","block").fadeOut();
					$(this).parent().toggleClass("post-filled", speed);
				});
				$("#"+postid+" .post").load(url+"/load-post/?load_id="+postid); //carica contenuto esteso
			}, 300);
		}

	//CARICA E POSIZIONA I BOX
	function randomXtoY(minVal,maxVal,floatVal) //funzione per valore random
	{
		var randVal = minVal+(Math.random()*(maxVal-minVal));
		return typeof floatVal=='undefined'?Math.round(randVal):randVal.toFixed(floatVal);
	}

	function dimensions(){
		width_tot_top=0; //larghezza totale top
		width_tot_bot=0; //larghezza totale bot
		$("#content").width(document.body.clientWidth-180);
		if($("#content").width()>1500){$("#content").width(1500)}; //limite massimo larghezza stage
		if($("#content").width()<800){$("#content").width(800)}; //limite minimo larghezza stage
		var center = ($("#content").width())/2;
		$("#content").css("marginLeft",-center);

		var box_width_max=($("#content").width())/(first_row);
		var box_height_max=180;
		var box_width_min=box_width_max-100;
		var box_height_min=120;
	
		for (i=0; i<box_num; i++) { //calcolo dimensioni box
			var postid=parseInt(post_ID[i]);
			width = randomXtoY(box_width_min,box_width_max,0); //larghezza random
			height = randomXtoY(box_height_min,box_height_max,0); //altezza random
			if(i<=4){
				width_tot_top = width_tot_top+Math.round(width)+20; //calcolo larghezza totale prima riga
			} else {
				width_tot_bot = width_tot_bot+Math.round(width)+20; //calcolo larghezza totale seconda riga
			}
			box_width[i] = width; //crea array delle larghezze
			box_height[i] = height; //crea array delle altezze
		}
	}

	function margins(){
		var left = 0;
		var top=0;
		var center = ($("#content").width())/2;
		left =  Math.round(center-(width_tot_top/2));
		left_bot =  Math.round(center-(width_tot_bot/2));
		for (i=0; i<box_num; i++) { //calcolo margini
			box_left[i] = left; // creo array dei margini left
			left = left+parseInt(box_width[i])+25;
			if(i==separator){left=left_bot;}
			if(i<=separator){top=252-box_height[i];} else { top=276;}
			box_top[i] = top; //crea array dei margini top
		}
	}
	
	//APPENDE I BOX
	function append_box(){
		var class_filled = 0;
		var row=0;
		for (i=0; i<box_num; i++) {
			var postid=parseInt(post_ID[i]);
			if(i<=separator){row=1; } else {row=2; }
			$("#content").append("<div id=\""+postid+"\" class=\"box box-"+i+"\" name=\""+i+"\" style=\"width:"+box_width[i]+"px; height:"+box_height[i]+"px; margin-left:"+box_left[i]+"px; margin-top:"+box_top[i]+"px; \"><div class=\"closebox\">CLOSE</div><div class=\"openbox\"></div><div class=\"thumb\"></div><div class=\"post\"></div><div class=\"mask\"></div></div>", show_box(i, postid));
			$(".box > .openbox").animate({opacity:"0"},0);
		}
	}
	
	dimensions(); //CALCOLO DIMENSIONI BOX
	margins();  //CALCOLO DISPOSISIOZNE BOX
	append_box(); //CREO I BOX
	show_box(); //MOSTRO I BOX

	$(".openbox").click(function(){
		var postid = $(this).parent().attr("id");
		var box_num = $(this).parent().attr("name");
		openbox(box_num, postid);
	});
	$(".closebox").click(function(){
		var i = $(this).parent().attr("name");
		var postid = $(this).parent().attr("id");
		closebox(i, postid);
	});

	//ON RESIZE
	var resizeTimer = null;
	$(window).bind('resize', function() {
		if (resizeTimer) clearTimeout(resizeTimer);
		resizeTimer = setTimeout(function() {
			dimensions();
			margins();
			$(".box").each(function() {
				var box_num = $(this).attr("name");
				if($(this).hasClass("opened")){
					$(this).animate({ 
						marginLeft: (($("#content").width()-$(this).innerWidth())/2)+"px"
					},{queue:false, duration:0, "easing":"easeOutExpo"} );
				} else {
					$(this).animate({
						width:""+box_width[box_num]+"px",
						height:""+box_height[box_num]+"px",
						marginLeft:""+box_left[box_num]+"px",
						marginTop:""+box_top[box_num]+"px"
					},{queue:false, duration:speed*5, "easing":"easeOutExpo"});
				}
			});
		}, 500);
	});
	
	$("#cs-tab").click(function(){
	$("#cs2").css({"zIndex":"900"});
		
		$(".box > .openbox").css({'display':'none'});
		if($("#cs").css("top")=="-480px"){
			$("#cs").animate({"top":"20px"},{queue:false,  duration:500});  //duration:speed*5, "easing":"easeOutExpo"
		} else if($("#cs").css("top")=="20px"){
			$(".box > .openbox").css({display:"block"});
			
			$("#cs").animate({"top":"-480px"},{queue:false, duration:500});
		}
		
	 $("#cs2").css({"zIndex":"999"});
	 $("#cs2").animate({"top":"-57px"},{queue:false, duration:50});
	});
	
	
		$("#cs-tab2").click(function(){
		$("#cs").animate({"top":"-480px"},{queue:false, duration:500}, function(){alert("all done");});	
		//$(".box > .openbox").css({'display':'none'});
		if($("#cs2").css("top")=="-57px"){
			$("#cs2").animate({"top":"0px"},{queue:false, duration:500});
		} else if($("#cs2").css("top")=="0px"){
			$(".box > .openbox").css({display:"block"});
			$("#cs2").animate({"top":"-57px"},{queue:false, duration:500});
		}
	});
	/*	$("body").click(function(){
			$(".box > .openbox").css({display:"block"});
			$("#cs").animate({"top":"-480px"},{queue:false, duration:500});
		});

	
	
	   $('.body').click(function(){
	                   
	for (i=0; i<box_num; i++) {
			var postid=parseInt(post_ID[i]);
		      closebox(i,postid);
		        }    
		   //setTimeout(function(){window.location='http://www.artattackadv.com/';},2000);         
	});   	*/

	
	
});