/**
********************************************************************************
* global variables
********************************************************************************
*/

var detF = false; // flag per details false e' chiuso
var showF = false; // flag per showAll false -> chiuso
var pH;
var descH;// chiuso
var moreH; // aperto

var getLang;
var mostraTxt;
var mostraTxtClose;
var chiudiTxt;
var chiudiTxtClose;


/**
********************************************************************************
* Anteprime
********************************************************************************
*/
function antOver()
{
	var src = $(this).attr('src');
	if(jQuery.browser.msie) src = $(this).attr('class');

	var u = src.slice(0, -4); // url senza estensione
	var ext = src.slice(-4); // estensione
	var uOver = u+'_over'+ext;
	$(this).attr('src', uOver);
	$(this).after('<div class=\'antNumb\'><p style=\'color: '+mColor+'\'>'+$(this).attr('id')+'</p></div>');
}

function antOut()
{
	var src = $(this).attr('src');
	if(jQuery.browser.msie) src = $(this).attr('class');
	var u = src.slice(0, -9); // url senza estensione
	var ext = src.slice(-4); // estensione
	var uOut = u+ext;
	$(this).attr('src', uOut);
	$(this).parent().children('.antNumb').remove();	
}

/**
********************************************************************************
* ShowAll/Close
********************************************************************************
*/

function closeOver()
{
	$(this).children('p').css({ color: '#999'});	
}

function closeOut()
{
	
	$(this).children('p').css({ color: (typeof mColor != 'undefined')?mColor:'#495173'});		
}

function closeClick()
{

	var t = $(this).children('p').text();
	var f;
	var o;
	
	//if(t == 'CLOSE')
	if(showF)
	{ // il div e' aperto
	
		$('#antCont').animate(
			{
				opacity: 0
			},
			500,
			close
			);
	}
	else
	{
		$('#antCur').animate(
			{
				opacity: 0
			},
			500,
			showAll
			);
		}
}



function showAll()
{
	showF = true;
	$("#close p").text(chiudiTxtClose);
	if(typeof mColor != 'undefined') $("#close p").css('color', mColor);
	$("#antCur").hide();
	
	$('#antCont').animate(
		{
			opacity:1
		},
		500
		);
}

function close()
{
	$("#close p").text(chiudiTxt);
	if(typeof mColor != 'undefined') $("#close p").css('color', mColor);
	$("#antCur").animate({opacity: 1}, 500);
	$("#antCur").show();
	showF = false;
}

/**
********************************************************************************
* Show Details
********************************************************************************
*/

function detailsClick()
{
	if(!detF)// e' chiuso
	{
		$('#desc').animate(
			{
				top: (pH-moreH)/2
			},
			500,
			function()
				{
					$('#moreDet').css({display:'block'});
					$("#detButt p").text(mostraTxtClose);
					$('#moreDet').animate({opacity:1}, 500);
					detF = true;
				}
			);
		//$(this).parent().slideUp("slow", function(){});
	}
	else
	{
		$('#moreDet').animate({opacity:0}, 500,
			function()
			{
				detF = false
				$('#moreDet').css({display:'none'});
				$("#detButt p").text(mostraTxt);
				$('#desc').animate(
					{
						top: (pH-descH)/2
					},
					500
					);		
			}
			
			);
	}
}

function setDescPos()
{
	pH = $('#page').height() + $('#menu1').height() + 110;
	descH =  $('#desc').height(); // chiuso
	moreH = $('#desc').height() + $('#moreDet').height(); // aperto

	$('#desc').css({top: (pH-descH)/2});
}

function anteprimaClick()
{
	curImg = Number($(this).attr('id')) - 1;
	$(this).oneTime(500, "preload", scrollImg);	
}

// Nasconde il livello delle anteprime se ce ne' solo una
function hideAntDiv()
{
	if(typeof gallery != 'undefined')
	{
		if(gallery.length <= 1) $('#showAll').hide();
	}
}

// Nasconde il livello delle anteprime se ce ne' solo una
function showAntDiv()
{
	if(typeof gallery != 'undefined')
	{
		if(gallery.length > 1) $('#showAll').show();
	}
}

// legge i param GET nel url e li mette in un array assoc
function getUrlVars()
{
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}

/**
********************************************************************************
* document ready
********************************************************************************
*/



$(document).ready(function()
{
	setDescPos();
	//hideAntDiv();
	showAntDiv();
	
	//$("#detButt p").text('MOSTRA DIMENSIONI');
	//$("#close p").text('MOSTRA TUTTI');

	getLang = getUrlVars()["lang"];
	mostraTxt = $("#detButt p").text();
	mostraTxtClose = (getLang == 'eng')?'HIDE DIMENSIONS':'CHIUDI DIMENSIONI';
	chiudiTxt = (getLang == 'eng')?'SHOW ALL':'MOSTRA TUTTI';
	chiudiTxtClose = (getLang == 'eng')?'CLOSE':'CHIUDI';
	
	if(typeof mColor != 'undefined') $("#close p").css('color', mColor);
	
	// :TODO: fare preload over anteprime
	$('#moreDet').css({opacity: 0});
	$('#antCont').css({opacity: 0});
	close();

	$(".anteprima").children().hover(antOver, antOut);
	$(".anteprima").children().click(anteprimaClick);
	$("#close").hover(closeOver, closeOut);
	$("#close").click(closeClick);
	$('#detButt').click(detailsClick);
	
}); // <- document ready end

$(window).bind("resize", function()
	{
		setDescPos();
        });
