/*
 * JavaScript v1.0
 * http://www.palomarnewmedia.it/
 *
 * Copyright (c) 2009 Palomar New Media
 * Author: Giampaolo Petrosemolo
 * Date: 2009-10-13
 *
 */

var oldgal=0;
var oldfoto=0;
var sqrState = Array();

$(document).ready(function(){ 
	redim(); 
	$(window).resize(function(){ redim() });
	$("#container").scroll(function(){ redim() });
	$(document).bind("contextmenu",function(e){ alert("tasto destro disabilitato"); return false; }); 
}); 

function redim(){
  var setop = 102;
  var pos = $('#topfix .menu').offset();     
  var wid = $('#topfix .menu').innerWidth(); 
  var foot= $("#footer").offset();
  var seba= $("#sudest").offset();
  var seheight = $(window).height()-setop-18;
  var seheight2 = foot.top-seba.top-4;
  var seleft = pos.left+wid-85;
  if(seheight2<seheight) seheight=seheight2;
  $('#sudest').css({  
      position: 'absolute', 
      zIndex: 5000,
      left: seleft,  
      top: setop,
	height: seheight,
	overflow: 'hidden'
  });

 
  var sewidth = $(window).width()-seleft-18;
  if(sewidth>=0) $('#sudest').css({ width: sewidth });  
  else $('#sudest').hide();
  $('#sudest').show();
}

/*
function scrolla(){
	var foot= $("#footer").offset();
	var seba= $("#sudest").offset();
	var sehe= $('#sudest').height();
	//foot.top
	if((sehe+seba.top)>(foot.top)){
		var hfin=foot.top-seba.top;
		alert(hfin);
		$('#sudest').css({  
			height: hfin
 		});
	}
}
*/
function zoom(gal, foto, dida){
	if((oldgal!="")&&(oldfoto!="")){
		if((oldgal!=gal)||((oldgal==gal)&&(oldfoto==foto))){
			resetZoom(oldgal, oldfoto, gal);
			if((oldgal==gal)&&(oldfoto==foto)){
				oldfoto=0;
				return false;
			}
			oldfoto=0;
		}
	}else{
		scrollPage(gal);
	}
	accendiSqr(gal, foto);
	var img = new Image();
	$(img)
	.load(function(){
		$(this).hide();
		$('#temp'+gal).remove();
		$('#foto'+gal).append('<div class="tempimg" id="temp'+gal+'"></div>');
		$('#temp'+gal).append(this);
		$(this).show();    
		ll=$(this).width();
		hh=$(this).height();
		if(dida!="") hh=hh+30;
		$('#temp'+gal).hide();
		$('#temp'+gal).width(ll);
		$('#temp'+gal).height(hh);
		$('#thumb'+gal).fadeOut();
		$('#foto'+gal).animate({ 
        		width: ll+"px",
        		height: hh+"px"
      	}, 1000, function(){ 
				$('#temp'+gal).fadeIn(500); 
				$('#text'+gal).fadeIn(500); 
				if(dida!="") $('#temp'+gal).append('<div class="dida">'+dida+'</div>');
			});
		
	})
	.attr('src', 'foto/'+gal+'/'+foto+'.jpg');
	oldgal=gal;
	oldfoto=foto;
	return false;
}

function resetZoom(gal, foto, newgal){
	accendiSqr(gal, 0);
	sqrState[gal]="0_on";
	swapImgRestore(gal, 0);
	$('#text'+gal).fadeOut(100);
	$('#thumb'+gal).fadeIn();
	ll2=$('#thumb'+gal).width();
	hh2=$('#thumb'+gal).height();
	$('#temp'+gal).fadeOut();
	$('#temp'+gal).remove();
	$('#foto'+gal).animate({ 
        	width: ll2+"px",
        	height: hh2+"px"
      }, 500, function(){ scrollPage(newgal); });
	return false;
}

function accendiSqr(qualegal, qualefoto){
	//var kids=$('#elem'+qualegal+' .squares .sqr').children();
	var imgkids=$('#elem'+qualegal+' .squares .sqr').find("img");
	for(x=0; x<imgkids.length; x++){
		imgkids[x].src="images/square.png";
	}
	sqrState[qualegal]=qualefoto+"_white";
	$('#sq'+qualegal+'_'+qualefoto).attr("src","images/square_white.png"); 
}

function scrollPage(gal){
	//alert(gal);
	var zero = $('#elem0').offset();
	var pos = $('#elem'+gal).offset();
	var yscroll=pos.top-zero.top;
	//alert(180-$('#logone').height());
	var diffe=272-270+$('#topfix .menu').height();
	//$('#container .content').css({ 'margin-top' : diffe+'px' });
	$('#container .content').animate({ 'marginTop': diffe+'px' }, 1000 );
	$("#container").scrollTo( yscroll+"px", 800, { offset:{ top:-20,left:0 } } );
}



function imagePreview(gal, foto){
	var xOffset = 0;
	var yOffset = 0;
	var topCont = $("#container div").offset().top;
	//topCont = topCont-(92+$('#logone').height());
	topCont = topCont-$('#topfix').height();
	//alert(topCont);
	var pos = $('#sq'+gal+'_'+foto).offset();
	$("#container").append("<p id='preview'><img src='foto/"+gal+"/"+foto+"_pw.jpg' alt='' /></p>");
	yOffset = pos.top+18-topCont;
	xOffset = pos.left-$("#preview").width()+10;
	$("#preview").css("top", yOffset+"px");
	$("#preview").css("left", xOffset+"px"); 
	$("#preview").fadeIn("fast");			
}
function delPreview(gal, foto){
	$("#preview").remove();
}

function chiudiTop(xxx){
	//alert($('#topfix').height());
	if($('#topfix .menu').height()==270){
		$('#topfix .menu').height(68);
		$('#topfix').css({ 'min-height' : '70px' });
		$('#topfix').height(70);
		$('#logone').hide();
		$('#imgChiudi img').attr("src","images/mostra"+xxx+".png");
	}else{
		$('#topfix').css({ 'min-height' : '272px' });
		$('#topfix').height(272);
		$('#topfix .menu').height(270);
		$('#logone').show();
		$('#imgChiudi img').attr("src","images/nascondi"+xxx+".png");
	}
	scrollPage(oldgal);
	return false;
}


function swapImgRestore(g, i){
	temp=sqrState[g].split("_");
	if(temp[0]!=i){
		$('#sq'+g+'_'+i).attr("src","images/square.png"); 
	}else{
		$('#sq'+g+'_'+i).attr("src","images/square_"+temp[1]+".png");
	}
}

function swapImage(g, i){
	if((sqrState[g]==undefined)||(sqrState[g]=="")){
		sqrState[g]="0_on";
	}
	$('#sq'+g+'_'+i).attr("src","images/freccetta.png"); 
}


/* Dreamweaver code: preload/swap/restore Img */

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}