// JavaScript Document

	var arrDataObj;
	var arrImg;
	var current_page_item=-1;
	
	
	
	/*script utils*/
	/*script utils*/
	/*script utils*/
	/*script utils*/
	
	function cloneArray(_arr){
		var newArr = new Array();
		for(l=0;l<_arr.length;l++){
		/*	if(_arr[l].Constructor== Array){
				 newArr[l]=cloneArray(_arr[l]);
			}else{*/
			 	newArr[l] = _arr[l];
			//}
		}
		return newArr;
	}
	
	
	/*mosaico*/
	/*mosaico*/
	/*mosaico*/
	/*mosaico*/
	/*mosaico*/
	
	function tinteggia(obj){
		$(obj._div).addClass("effectAlpha");
		if($.browser.msie){
		  	$(obj._img).addClass("effectTinted");
		} else {
			var options3 = {average : false};
			$(obj._img).pixastic("desaturate",options3);
			obj._canvas=options3.resultCanvas;
		}  
		
	}
	
	function deillumina(cat){		
		for(l=0;l<arrDataObj.length;l++){
			if(arrDataObj[l]._category==cat){
				tinteggia(arrDataObj[l]);		
			}
		}
		
	}
	
	
	function illumina(cat){
		var obj;
		
		for(l=0;l<arrDataObj.length;l++){
			if(arrDataObj[l]._category==cat){
				obj=arrDataObj[l];
				$(obj._div).removeClass("effectAlpha");
				
				if($.browser.msie){
					$(obj._img).removeClass("effectTinted");
				} else {					
					Pixastic.revert(arrDataObj[l]._canvas);
				}
			}
			
		}		
	}
	
	
	
	var ww_wind,hh_wind,cell_riga;
	
	
	function pulisciMosaico(num){
		
		//$('body,html').scrollTop(0);
		
		$('#mosaico').css("top","0");
		
		var daelim;
		if(num>0){
			daelim=num;
		}else{
			if(arrDataObj.length>numCelleMin){
			daelim=arrDataObj.length-numCelleMin;
			}
			
		}
		var arrToRem=arrDataObj.splice(0,daelim);
		
		for(l=0;l<arrToRem.length;l++){
			$(arrToRem[l]._div).remove();
		}
			
		//$('body,html').scrollTop(0);
		/*	$('body,html').stop().animate({  
					scrollTop: 0  
				},  
				100);*/
	}
	
	function resizaMask(){	
					
		
		ww_wind=$(window).width();
		hh_wind=$(window).height()-330;	
		$('#mosaico').width(Math.ceil(ww_wind/150)*150);		
		
		cell_riga=Math.ceil(ww_wind/150);
		numCelleMin=(cell_riga*Math.ceil(hh_wind/150));	
		$('#maschera').width("100%");
		$('#maschera').height(hh_wind);
		
	}
	
	var t;
	var g;
	
	function resizaMosaico(){
		/*clearTimeout(t);
		clearTimeout(g);
		t=0;
		g=0;*/
		
		/*
		arrDataObj=new Array();
		$('#mosaico').empty();
		numCelleMin=0;
		numCelleTot=0;
		countCell=0;
		countArrayMosaico();
		resizaMask();
		creaMosaico();*/
		if(arrDataObj.length>=numCelleMin){		
			pulisciMosaico();
			//scrolla();
		}else{
			creaMosaico(numCelleMin-arrDataObj.length);
		}
		
	}
	var numCelleMin=0;
	var numCelleTot=0;
	var countCell=0;
	function countArrayMosaico(){
		
		arrDataObj=new Array();
		
		arrImg=new Array();
		for(d=0; d<arrDati.length; d++){
			
			arrImg[d]=cloneArray(arrDati[d]);
			
			var arrCount=new Array();
		}
	}
	
	
	function creaMosaico(num){	
	
		if(num>0){
			numCelleTot+=num;	
			//alert(numCelleTot)
		}else{	
			
			numCelleTot=numCelleMin;
		}	
		
		//alert("creaMosaico")
		
		creaCella();				
	}
	
	function creaCella(){
			
		if(numCelleTot>0){
			countCell++;
			numCelleTot--;
			
			
			
			var idCategory;
			var numCategory;
			var numItem;
			
			numCategory=Math.floor(Math.random()*arrImg.length);
			numItem=Math.floor(Math.random()*(arrImg[numCategory].length-1));
			idCategory=arrImg[numCategory][arrImg[numCategory].length-1];
			
			var src =arrImg[numCategory][numItem];
			
			
			
			
			arrImg[numCategory].splice(numItem,1);
			if(arrImg[numCategory].length==1){
				arrImg.splice(numCategory,1);
			}
			if(arrImg.length==0){
				arrImg=new Array();
				for(d=0; d<arrDati.length; d++){
					arrImg[d]=cloneArray(arrDati[d]);
				}	
			}
			
			
			
			
			$('#mosaico').append('<div id="'+countCell+'" class="tessera category'+idCategory+'"></div>');	
			
			//if(current_page_item!=idCategory)$('div#'+countCell).addClass("effectTinted");
			
			var img = new Image();
			
			
			var objCelletta={};
			objCelletta._id=countCell;
			objCelletta._category=idCategory;
			objCelletta._div=$('#mosaico div#'+countCell);
			objCelletta._img=img;
			
			
			arrDataObj.push(objCelletta);
			
			img.name=idCategory;
			img.id=countCell;
			//arrImgCelle.push(img);
			img.onload = function() {
				if(current_page_item!=this.name){
					for(l=0;l<arrDataObj.length;l++){
						if(arrDataObj[l]){
							if(arrDataObj[l]._id==this.id){
								
								tinteggia(arrDataObj[l]);
							}
						}
					}
				}
				creaCella();
			}
			$('#mosaico div#'+countCell).append(img);
			
			
			
			
			img.src = src;
			
		}else{
			
		t=setTimeout("scrolla()",1500);
		}
		
	}
	
	function scrolla(){
		
		clearTimeout(t);
		creaMosaico(cell_riga);
		
		/*$('body,html').stop().animate({  
			scrollTop: 150  
		},  
		500);*/
		
		$('#mosaico').stop().animate({  
			top:-150
		},  
		500);
		g=setTimeout("pulisciMosaico(cell_riga);",600);
		
	}
	
	
	
	
	/*testi testi testi testi*/
	/*testi testi testi testi*/
	/*testi testi testi testi*/
	/*testi testi testi testi*/
	/*testi testi testi testi*/
	function esceBenvenuto(){
		$('p.benvenuto').hide("normal");
		inbenvenuto=false;
	}
	
	function entraBenvenuto(){
		$('p.benvenuto').show("normal");
		inbenvenuto=true;
	}
	
	function esceTesto(num){
		$('a#cat'+num).parent().next().empty();
		$('a#cat'+num).parent().next().hide("normal");

	}
	function entraTesto(num){
		//$('a#cat'+num).parent().next().append(arrTesti[num][0]+'<br><em>'+arrTesti[num][1]+'</em>');
		$('a#cat'+num).parent().next().append(arrTesti[num][0]+'<br>');
		$('a#cat'+num).parent().next().show("normal");


	}


	
	
	
	/*menu cursore*/		
	/*menu cursore*/		
	/*menu cursore*/		
	/*menu cursore*/		
	/*menu cursore*/		
	/*menu cursore*/
	var $cursor;
	function setDataCursor(numCat){
		var posX;
		if(numCat>=0)posX=$('a#cat'+current_page_item).position().left + ($('a#cat'+current_page_item).width()/2) - $cursor.width()/2 -3;
		else posX=-270;
		$cursor.css("left",posX);	
	}
	
	function spostaCursore(numCat){
		var posX;
		if(numCat>=0)posX=$('a#cat'+numCat).position().left + ($('a#cat'+numCat).width()/2) - $cursor.width()/2 -3;
		else posX=-270;
		$cursor.stop().animate({
            left:posX 
        });
	}
	
	
	/*lingua*/
	/*lingua*/
	/*lingua*/
	/*lingua*/
	function settalingua(str){
		$("#lingua a").removeClass("linguaattiva");
		$("#lingua a#"+str).addClass("linguaattiva");
	}
	
	
	
	
	
	inbenvenuto=true;
	$(document).ready(function () {
								
		/*menu cursore*/		
		/*menu cursore*/		
		/*menu cursore*/		
		/*menu cursore*/		
		/*menu cursore*/		
		/*menu cursore*/
		
		$("dl.menu").append('<dt id="cursor"></dt>');
		$cursor= $('dl dt#cursor');
		setDataCursor(-1);
								
								
		
		/*eventi menu*/
		/*eventi menu*/
		/*eventi menu*/
		/*eventi menu*/
		/*eventi menu*/
		/*eventi menu*/
		for(k=0; k<=11;k++){
			$("a#cat"+k).data("categoryNum",k);
			$("a#cat"+k).bind("click",function(e){
											   
											   	e.preventDefault();
											   	if(inbenvenuto)esceBenvenuto();
												esceTesto(current_page_item);
												deillumina(current_page_item);
												
												current_page_item=$(this).data("categoryNum");
												
												entraTesto(current_page_item);
												illumina(current_page_item);
												
												spostaCursore(current_page_item);
												//setDataCursor(current_page_item);
												});
			
			
			
			$("a h1").bind("click",function(e){
											 e.preventDefault();
											if(!inbenvenuto)entraBenvenuto();
											esceTesto(current_page_item);
											deillumina(current_page_item);
											
											current_page_item=-1;
												spostaCursore(current_page_item);
										  
										  });
			
		/*	$("a#cat"+k).hover(function(){
												spostaCursore($(this).data("categoryNum"));
										 },
							   function(){
												spostaCursore(current_page_item);
							             });*/
			
			esceTesto(k);
			
		}
		
								
								
		/*immagini mosaico*/
		/*immagini mosaico*/
		/*immagini mosaico*/
		/*immagini mosaico*/
		/*immagini mosaico*/
		
		
		
		countArrayMosaico();
		resizaMask();
		creaMosaico();
		$(window).resize(function() {
								  
			resizaMask();
			if(timeId!=null)clearTimeout(timeId);	
			timeId=setTimeout("resizaMosaico()",1000);
							
		});
		
		//
		
		settalingua(lingua_str);
		$("#lingua a").bind("click",function(e){
										 settalingua(this.id);
										});
		
	});	
	var timeId;
