$(function(){
	if($('#color_options').length>0){
		$.fn.colorSelector = function(){
			this.setBigImage = function(){this.bigImage = $('.image img')};
			this.setBigImage();
			this.options = new Array;
			this.imagesSource=$("#color_pictures");
			this.colorContainer = $("<table id='color_container'><thead><tr><th colspan='2'>Цвета:</th></tr></thead><tbody></tbody></table>");
			this.current=-1;
			this.prettyPhotoLink = $("#pretty_photo_link").attr('href',$("#pretty_photo_link img").attr('src'));
			init=function(el){
				el.find("tbody tr").each(function(i,e){
					el.options[i] = {};
					el.options[i].input = $(e).find("input");
					var h=$(e).find(".object_name").html();
					el.options[i].variants = h.split('/');
				});
				el.imagesSource.find("img").each(function(i,e){if(el.options[i])el.options[i].image = $(e)});
				var ccBody=el.colorContainer.find("tbody");
				for(i=0;i<el.options.length;i++){
					var ccTR = $("<tr class='color_select"+i+"' />"), ccTD1 = $("<td/>"), ccTD2 = $("<td/>");
					ccTR.append(ccTD1).append(ccTD2);
					ccBody.append(ccTR);
					for (var variant in el.options[i].variants){
						var pair = el.options[i].variants[variant].split('#');
						ccTD1.append((variant==0 ? "" : " / ")+pair[0]);
						ccTD2.append("<div style='float:left;margin-right:5px;width:15px;height:15px;padding:0;background:#"
							+pair[1]+";'></div>");
					}
				}
				el.find("table").hide();
				el.append(el.colorContainer);
				for(i=0;i<el.options.length;i++){
					$(".color_select"+i).bind("click",{el:el,img:i},function(event){event.data.el.setPicture(event.data.img)});
				}
				el.setPicture(0);
			}
			this.setPicture = function(n){
				if(n==this.current)return;
				if(!this.options[n].image)return;
				this.bigImage.replaceWith(this.options[n].image);
				this.setBigImage();
				this.prettyPhotoLink.attr('href',this.bigImage.attr('src'));
				this.current=n;
				this.options[n].input.attr('checked', 'checked');
			}
			
			
			init(this);
		}
		$('#color_options').colorSelector();
		$("a[rel^='prettyPhoto']").prettyPhoto({theme: 'light_square', gallery_markup:''});
	}
	if($("#pic_gallery").length>0){
		$.fn.slideShow=function(img_src){
			this.pics = img_src.find("img").map(function(el){return this.src}).get();
			this.count = this.pics.length;
			this.current=0;
			this.layers = new Array();
			this.html("");
			for(i in this.pics){this.layers[i] = $("<div>").css({background:"url('"+this.pics[i]+"') no-repeat", height:200,width:710,position:"absolute",top:0}).hide().appendTo(this);}
			this.layers[this.current].show();
			this.run = function(){
				this.layers[this.current].fadeOut();
				this.current=this.current==this.count-1?0:this.current+1;
				this.layers[this.current].fadeIn();
			}
			var self=this;
			window.setInterval(function(){self.run.call(self)}, 7000);
		}
		$("#pic_gallery").slideShow($("#pic_source"));
	}
	if($("#pictures").length>0){
		var tmp_pictures = '';
		$('#pictures').find('img').each(
			function (){
				tmp_pictures += '<a class="pics" href="'+$(this).attr('src')+'" rel="prettyPhoto[mixed]"><img src="'+$(this).attr('src')+'"></a>';
			}
		);
		$('#pictures').hide();
		$('#main_img').html(tmp_pictures);
		$('#main_img').find('a').first().addClass('fst');
		$("a[rel^='prettyPhoto']").prettyPhoto({theme: 'light_square', gallery_markup:''});
	}
});
