var $speed = 1000;
var minscreen = 901;
var $top=0;
var $left=0;
var $blink = '/en/description/';
var $elink = '/?iframe=true&width=850&height=550';

function checkShaders() {
	var screenwidth = $("body").width();
	var screenheight = $("body").height();
	if(screenwidth>minscreen) {
		var dwidth = Math.ceil((screenwidth-minscreen)/2);
		$("#leftshader").css({position:"absolute",top:0,left:0,height:screenheight+"px",width:dwidth+"px"});
		$("#rightshader").css({position:"absolute",top:0,right:0,height:screenheight+"px",width:dwidth+"px"});
	}
	else {
		$("#leftshader").css({position:"absolute",top:0,left:0,height:"0px",width:"0px"});
		$("#rightshader").css({position:"absolute",top:0,right:0,height:"0px",width:"0px"});
	}
}

$(function(){
	
	$(".bags li img").each(function(){
		var $c_height=$(this).parent().height();
		var $c_width=$(this).parent().width();
		var $width=$(this).width();
		var $height=$(this).height();
		$top = Math.round(($c_height-$height)/2);
		$left = Math.round(($c_width-$width)/2);
		$(this).css({position: 'absolute', top:$top, left:$left});
	});
	
	$("a[rel^='prettyPhoto']").prettyPhoto({
		showTitle: false,
		allowresize: false,
		theme: 'dark_rounded',
		padding: 5
	});
	
	$('#slideshow').cycle({
		timeout: 2500,
		speed: 500,
		before: onBefore
	});
	
	function onBefore() {
		$('#title')
		.html(this.alt);
	};
	
	$('.bags li').hover(function() {
		$(this).addClass('m_over');
	}, function() {
		$(this).removeClass('m_over');
	});
	
	$(".bags li").click(function () {
		var $c_height=$('#bag').parent().height();
		var $c_width=$('#bag').parent().width();
		var $width=$("span.width",this).text();
		var $height=$("span.height",this).text();
		var $ext_l=$("span.ext_l",this).text();
		var $title =$('img',this).attr('alt');
		var $bagid = $(this).attr('id');
		jQuery ('<img src="/files/photos/bag_'+$bagid+'.'+$ext_l + '" />'); 
		$top = Math.round(($c_height-$height)/2);
		$left = Math.round(($c_width-$width)/2);
		$('#bag_title').text($title);
		if(!$(this).hasClass("active")) {
			$(".bags li").removeClass("active");
			$(this).addClass("active");
			$('#bag').fadeOut($speed,function (){
				$('#bag').attr('src','/files/photos/bag_'+$bagid+'.'+$ext_l);
				$('#bag').css({position: 'absolute', top:$top, left:$left});
				$(this).fadeIn($speed);
			});
			$(".details").attr("href",$blink + $(this).attr('id') + $elink);
			$(".illustration a").attr("href",$blink + $(this).attr('id') + $elink);
			$(".button").click(function(){
				window.location = '/en/order/'+$bagid;
			});
		}
	});
	
	var $item = $(".bags li:first")
	var $c_height=$('#bag').parent().height();
	var $c_width=$('#bag').parent().width();
	var $width=$("span.width",$item).text();
	var $height=$("span.height",$item).text();
	var $ext_l=$("span.ext_l",$item).text();
	var $title =$('img',$item).attr('alt');
	var $bagid = $($item).attr("id")
	$top = Math.round(($c_height-$height)/2);
	$left = Math.round(($c_width-$width)/2);
	$('#bag_title').text($title);
	$($item).addClass("active");
	$('#bag').css({position: 'absolute', top:$top, left:$left});
	$('#bag').attr('src','/files/photos/bag_'+$bagid+'.'+$ext_l);
	$(".details").attr("href",$blink + $($item).attr('id') + $elink);
	$(".illustration a").attr("href",$blink + $($item).attr('id') + $elink);
	$('#bag').fadeIn($speed);
	$(".button_order").click(function(){
		window.location = '/en/order/'+$bagid;
	});

});
