// JavaScript Document

var li_w = 109;
var li_row = 6; //numero di elementi visibili in una volta nella lista thumb
var li_max = 0;
var curr_pos = 1;
var curr_click = null;

$(document).ready(
	function()
	{
		if (jQuery.browser.msie) {
			if(parseInt(jQuery.browser.version) <= 7) {
		    	$("#center img.loader").attr('src','images/loader_ie6.png');
		    }
		}
	
		var linum = $("#center div.thumbcont ul li");
		$("#center div.thumbcont ul").css('width',(linum.length *li_w)+'px');
		
		li_max = linum.length;
       
		$("#center div.thumbcont a.arrow-lt").click(function(){
		    scrollThumb('left');
		    return false;
		});
		$("#center div.thumbcont a.arrow-rt").click(function(){
		    scrollThumb('right');
		    return false;
		});
		
		$("#center #pic a.arrow-lt").click(function(){
      		scrollPic('left');
		    return false;
		});
		$("#center #pic a.arrow-rt").click(function(){
      		scrollPic('right');
		    return false;
		});

		$("#center div.thumbcont ul li a").click(function(){
      		chooseThumb(this);

		    return false;
		});
		
		$('#pic #big').load(function() {
        	showPic();
    	});
		
		var achild = $(linum[0]).children('a');
		curr_click = achild[0];
		$(curr_click).addClass('selected');
		chooseThumb(curr_click);
    }
);

function scrollThumb(act) {
	var old_pos = curr_pos;
	switch(act) {
	    case 'left':
	        curr_pos--;
	        if (curr_pos == 0) { curr_pos = 1; }
		break;

		case 'right':
		    curr_pos++;
		    if (curr_pos > li_max) { curr_pos = li_max; }
		break;
	}
	
	//alert(curr_pos);

 	if (curr_pos +li_row -1<= li_max) {
		var thumbs = $("#center div.thumbcont ul li");

		$("#center div.thumbcont div.listcont ul").stop();

		$("#center div.thumbcont div.listcont ul").animate(
	    	{left:'-'+thumbs[curr_pos-1].offsetLeft+'px'},
	    	1000 * 0.7,
	    	jQuery.easing.def = 'easeOutQuart',
			null
	  	);
  	}
  	else { //se non si muove si annulla la modifica della posizione
  		curr_pos = old_pos;
	}
}

function chooseThumb(obj) {
	loadPic($(obj).attr('href'),$(obj).attr('title'));

	$(curr_click).removeClass('selected');
	$(obj).addClass('selected');
	curr_click = obj;
}

function loadPic(urlimg, imgtitle) {

    $('#pic #big').fadeTo(
	  	1000 * 0.7,
	    0,
		function() {
		    $('#pic #big').attr('src',urlimg);
		    $('#pic h3 span').text(imgtitle);
		},
	    jQuery.easing.def = 'easeOutQuint'
  	);
  	
    $('#pic h3').fadeTo(
	  	1000 * 0.7,
	    0,
		null,
	    jQuery.easing.def = 'easeOutQuint'
  	);
}

function showPic() {
	$('#pic #big').fadeTo(
	  	1000 * 0.7,
	    1,
		null,
	    jQuery.easing.def = 'easeOutQuint'
  	);
  	
	$('#pic h3').fadeTo(
	  	1000 * 0.7,
	    1,
		function() {
			if (jQuery.browser.msie) {
				if(parseInt(jQuery.browser.version) <= 7) {
		    		document.getElementById("pic").style.removeAttribute("filter"); //IE7 fade font fix
		    	}
		    }
		},
	    jQuery.easing.def = 'easeOutQuint'
  	);
}

function scrollPic(act) {
	var thumbpos = getCurrThumbPos();
	//alert(thumbpos);
	if (thumbpos != -1) {
	    var thumbs = $("#center div.thumbcont ul li a");
	    
	    if (act == 'left') {
	        thumbpos--;
		}
		else if (act == 'right') {
		    thumbpos++;
		}
		
		if (thumbpos > -1 && thumbpos < thumbs.length) {
			chooseThumb(thumbs[thumbpos]);
		}
	}
}

function getCurrThumbPos() {
	var thumbs = $("#center div.thumbcont ul li a");
	
	var exit = false;
	var counter = -1;
	while (!exit && counter < thumbs.length) {
		counter++;
		if (thumbs[counter] == curr_click) {
		    exit = true;
		}
	}
	if (!exit) { counter = -1; }
	
	return counter;
}

