function switchVariation(code,url){
	$("#showProduct .spDesc h4").text(collection[code].title);
	$("#showProduct .spDesc p").text(collection[code].description);
	var node = collection[code].variations;
	var wariant = 0;
	for(var key=node.length-1;key>=0;key--){
		if(url.search(new RegExp(node[key].front+"$"))!=-1||url.search(new RegExp(node[key].back+"$"))!=-1)wariant=key;	
	}
	if(node[wariant].back != null)$("#showProduct a#mirrorIco").show(); else $("#showProduct a#mirrorIco").hide();
	
	$("#spColors ul li a.current").removeClass("current");
	$("#spColors ul li a").eq(wariant).addClass("current");
	
	colorOut(function(){
		if(node.length<2)$("#spColors").hide(); else $("#spColors").show();
		
		$("#spColors ul li").each(function(i){
			if(i<node.length){
				$($(this).children("a").children("img")).attr("src","/files/photo/showroomSwatch_"+node[i].swatch);
				$(this).show();
			} else $(this).hide();
			colorIn();
		});
	});
	
};
function colorIn(callback){
	$("#spColorsTopLeft").animate({left:0 ,top:0,opacity:1},100,function(){
		$("#spColorsTopRight").animate({left:75, top:0, opacity:1},100,function(){
			$("#spColorsBottomLeft").animate({left:0, top:55, opacity:1},100,function(){
				$("#spColorsBottomRight").animate({left:75, top:55, opacity:1},100,function(){
					if(callback)callback();
				});	
			});	
		});	
	});
};
function colorOut(callback){
	$("#spColors ul li").stop();
	$("#spColorsBottomRight").animate({left:0,top:55, opacity:0},100,function(){
		$("#spColorsBottomLeft").animate({left:0, top:0, opacity:0},100,function(){
			$("#spColorsTopRight").animate({left:0, top:0, opacity:0},100,function(){
				$("#spColorsTopLeft").animate({left:0, top:-55,opacity:0},100,function(){
					if(callback)callback();
				});
			});
		});
	});
};
function switchPic(code, id, donotRotate){
	var node = collection[code].variations;	
	if(donotRotate){
		$("#spPhoto li.current img").stop().animate({opacity:0},150,function(){
			var obj= $(this);
			var img = new Image();
			img.onload = function(){
				obj.attr("src",img.src);
				obj.animate({opacity:1},150);
			};
			img.src="/files/photo/showroomBig_"+node[id].front;
		});
		if(node[id].back != null)$("#showProduct a#mirrorIco").show(); else $("#showProduct a#mirrorIco").hide();
	} else {
		$("#showProduct #spPhoto ul li.current").addClass("noLoading");
		var obj= $("#spPhoto li.current img");
		var img = new Image();
		img.onload = function(){
			$("#spPhoto li.current img").stop().animate({left:128,width:10},100,function(){
				obj.attr("src",img.src);
				obj.animate({left:0,width:267},100,function(){
					$("#showProduct #spPhoto ul li.current").removeClass("noLoading");
				});
			});			
		};
		if((obj.attr("src")).search(new RegExp(node[id].front+"$"))!=-1)img.src="/files/photo/showroomBig_"+node[id].back; else img.src="/files/photo/showroomBig_"+node[id].front;
		
	};
};

$(function(){
	$("#showProduct #spPhoto ul li img").attr("alt","");
	$('<div id="spColors"><h3 class="cufon">Dostępne kolory:<\/h3><ul class="clearfix"><li id="spColorsTopLeft"><a href="#" class="current"><img alt="" src=""><\/a><\/li><li id="spColorsTopRight"><a href="#" class=""><img alt="" src=""><\/a><\/li><li id="spColorsBottomLeft"><a href="#" class=""><img alt="" src=""><\/a><\/li><li id="spColorsBottomRight"><a href="#" class=""><img alt="" src=""><\/a><\/li><\/ul><\/div>').appendTo("#showProduct").hide();
	switchVariation($("#spList li a.current").attr("rel"),$("#spList li a.current").attr("href"));
	$("#showProduct a#mirrorIco").click(function(){
		var cIndex = $("#spColors ul li a").index($("#spColors ul li a.current"));
		
		switchPic($("#spList li a.current").attr("rel"),cIndex, false);
		return false;
	});
	$("#spColors ul li a").click(function(){
		var nIndex = $("#spColors ul li a").index(this);
		var cIndex = $("#spColors ul li a").index($("#spColors ul li a.current"));
		if(nIndex == cIndex) return false;
		
		switchPic($("#spList li a.current").attr("rel"),nIndex, true);
		$("#spColors ul li a.current").removeClass("current");
		$(this).addClass("current");
		
		return false;	
	});
	$("#spList").scrollerMa({
		items:'li',
		duration:500,
		steps:3,
		easing: 'easeOutBack',
		lastFull:true,
		afterInit:function(obj){
			$("#spList li a").click(function(){
				$("#spPhoto").scrollerMa('goto',$("#spList li a").index(this));
				$("#spList li a.current").removeClass("current");
				$(this).addClass("current");
				return false;
			});
		}
	});
	$("#spPhoto").scrollerMa({
		items:'li',
		duration:500,
		steps:1,
		easing: 'easeOutBack',
		lastFull:true,
		onBefore:function(obj,callback,pos){
			$("#spList").scrollerMa('goto',pos);
			
			$("#spList li a.current").removeClass("current");
			
			$("#spPhoto li.current").removeClass("current");
			$("#spPhoto li").eq(pos).addClass("current");
			
			$("#showProduct .spDesc h4").text($("#spList li a img").eq(pos).attr("alt"));
			$("#spList li a").eq(pos).addClass("current");
			
			switchVariation($("#spList li a.current").attr("rel"),$("#spPhoto li.current img").attr("src"));
			
			callback(obj,pos);
		}
	});
	
	
	
	
});