$(document).ready(function (){

	// css3 styles
	$("#previous").css({ "-moz-border-radius-topright" : 7 });
	$("#next").css({ "-moz-border-radius-topleft" : 7 });
	$("#counter").css({ "-moz-border-radius-bottomleft" : 7 });
	$("#gallery-background").css({ opacity: 0.5 });
	
	// markers
	var pos = 0; 
	var current_img = 1;
	var N_imgs = 13;
	
	// objet references
	var $panel = $("#gallery-widget");
	var $c = $("#counter");
	
	$("#gallery-widget img").click(function () {
		if(current_img < N_imgs && $panel.is(":animated") == false) {
			$panel.animate({ left : pos-570 }, 300);
			pos -= 570;
			current_img += 1;
			$c.empty().html(current_img + "/13");	
		}
	});
	
	$("#next").click(function () {
		if(current_img < N_imgs && $panel.is(":animated") == false) {
			$panel.animate({ left : pos-570 }, 300);
			pos -= 570;
			current_img += 1;
			$c.empty().html(current_img + "/13");	
		}
	});
	
	$("#previous").click(function () {
		if(current_img > 1 && $panel.is(":animated") == false) {
			$panel.animate({ left : pos+570 }, 300);
			pos += 570;
			current_img -= 1;
			$c.empty().html(current_img + "/13");	
		}
	});
	
	// thumb controls
	var tpos = 0; 
	var current_th = 1;
	var N_th_slides = Math.round(13 / 3) + 1;

	var $tpanel = $("#gallery-thumbs"); 	
	
	$("#arrow-down").click(function () {
		if(current_th < N_th_slides && $tpanel.is(":animated") == false) {
			$tpanel.animate({ top : tpos-303 }, 300);
			tpos -= 304;
			current_th += 1;
		}
	});

	$("#arrow-up").click(function () {
		if(current_th > 1 && $tpanel.is(":animated") == false) {
			$tpanel.animate({ top : tpos+303 }, 300);
			tpos += 304;
			current_th -= 1;
		}
	});
	
	// thumbnails click function
	var tN = 0;
	var finalX = 0;
	var interval = 0;
	
	$("#gallery-thumbs img").click(function () {
		tN = parseInt($(this).attr("alt")) - 1;
		if (tN+1 != current_img) {
			finalX = tN*570*-1;
			interval = 300 * Math.abs(current_img - (tN + 1));
			$panel.animate({ left : finalX }, interval);
			current_img = tN + 1;
			pos = finalX;
			$c.empty().html(current_img + "/13");
		}
	});
	

});