jQuery(function($){
	var options = {
		literals: {
			big_photo: ".b-big_photo_pic",
			thumb_wrap: ".b-thumb-wrap",
			thumb_pic: ".b-thumb_pic",
			selected_class: "b-thumb_current",
			hover_class: "b-thumb_hover",
			more_photos_class: "b-big_photo_more"
		}
	};
	fixIE6layout();
	preloadBigPhotos();
	slideShow($('.b-cat .b-cat_item'));
	
	function preloadBigPhotos() {
		$.preload( options.literals.thumb_pic, {
			find:/_thumb_?s?/,
			replace:''
		});
		
	}
	
	function slideShow(/*jQuery*/ elements) {
		if (!elements.size()) return;
		activateThumbs(elements);
	}
	
	function activateThumbs(/*jQuery*/ elements) {
		for (var i = 0; i < elements.length; i++) {
			if (!$(options.literals.thumb_wrap, elements[i]).size()) continue;
			var oThumbPanel = new ThumbPanel ($(elements[i]), i);
			oThumbPanel.init();
		}
	}
	
	function ThumbPanel (/*jQuery*/ ThumbPanel, /*Number*/ id) {
		return {
			id: id,
			elt: ThumbPanel,
			literals: options.literals,
			thumbs:[],
			big_photo_elt: undefined,
			current_thumb: undefined,
			init: function(){
				var $thumbs = $(this.literals.thumb_wrap, this.elt);
				this.set_big_photo_elt();
				for (var i = 0; i < $thumbs.length; i++) {
					this.thumbs[i] = new Thumb($($thumbs[i]), this, i);
					this.thumbs[i].init();
				}
				this.set_current_thumb();
				this.activate_big_photo();
			},
			activate_big_photo: function() {
				if(this.thumbs.length <= 1) return;
				var $big_photo = this.big_photo_elt;
				$big_photo.addClass(this.literals.more_photos_class);
				$big_photo.bind("click", {thumbPanel: this}, this.big_photo_click);
				
			},
			big_photo_click: function(/*Event*/ evt) {
				var tp = evt.data.thumbPanel, 
					total_photos = tp.thumbs.length,
					current_photo = tp.get_current_thumb()
					next_photo = current_photo + 1;
				next_photo = next_photo < total_photos ? next_photo : 0;
				tp.update_big_photo(next_photo);
				
			},
			get_big_photo_elt: function() {
				return this.big_photo_elt;
			},
			set_big_photo_elt: function() {
				this.big_photo_elt = $(this.literals.big_photo, this.elt);
			},
			get_id: function() {
				return this.id;
			},
			get_current_thumb: function() {
				return this.current_thumb;
			},
			set_current_thumb: function(/*Number*/ /*id*/) {
				if(arguments.length) {
					this.current_thumb = arguments[0];
				} else {
					this.current_thumb = $(this.literals.thumb_wrap + " " + this.literals.selected_class, this.elt).prevAll().length;
				}
				
			},
			update_big_photo: function(/*Number*/ /*id*/){
				var id = arguments.length ? arguments[0] : this.get_current_thumb();  
				this.change_photos(this.thumbs[id].get_big_src());
				this.update_panel(id);
			},
			update_panel: function(/*Number*/ id) {
				this.unselect_current_thumb();
				this.select_thumb(id);
			},
			unselect_current_thumb: function() {
				this.thumbs[this.get_current_thumb()].set_state(false);
				this.thumbs[this.get_current_thumb()].update();
			},
			select_thumb: function(/*Number*/ id) {
				this.set_current_thumb(id);
				this.thumbs[id].set_state(true);
				this.thumbs[id].update();
			},
			change_photos: function(/*String*/ photo_src) {
				var $big_photo = this.get_big_photo_elt(),
					duration = 200;
				$big_photo.stop().animate(
					{opacity:0},
					{
						duration: duration,
						complete: function() {
							$big_photo
								.attr("src", photo_src)
								.animate({opacity:1},duration);					
						}
					});
			}
		};
	}
	
	function Thumb (/*jQuery*/ Thumb, /*ThumbPanel*/ ThumbPanel, /*Number*/ id){
		return {
			id: id,
			elt: Thumb,
			tp: ThumbPanel,
			literals: options.literals,
			pic_elt: undefined,
			pic_elt_src: {path: undefined, file: undefined},
			selected: undefined,
			init: function() {
				this.pic_elt = $(this.literals.thumb_pic, this.elt);
				this.parse_src();
				this.set_state();
				this.update();
			},
			update: function() {
				if (this.get_state()) {
					this.pic_elt.unbind();
					this.select();
					this.unhover();
				} else {
					this.pic_elt.bind("mouseover", {thumb: this}, this.mouseover);
					this.pic_elt.bind("mouseout", {thumb: this}, this.mouseout);
					this.pic_elt.bind("click", {thumb: this}, this.click);
					this.unselect();
				}
			},
			get_id: function() {
				return this.id;
			},
			select: function() {
				this.elt.addClass(this.literals.selected_class);
				this.change_photo(this.get_select_src());
			},
			unselect: function() {
				this.elt.removeClass(this.literals.selected_class);
				this.change_photo(this.get_thumb_src());
			},
			change_photo: function(/*String*/ sNewSrc) {
				var $thumb_photo = this.pic_elt,
					duration = 50;
				$thumb_photo.attr("src", sNewSrc);
				/*$thumb_photo.stop().animate(
					{opacity:0},
					{
						duration: duration,
						complete: function() {
							$thumb_photo
								.attr("src", sNewSrc)
								.animate({opacity:1},duration);					
						}
					});*/
				
			},
			hover: function(){
				this.elt.addClass(this.literals.hover_class);
			},
			unhover: function(){
				this.elt.removeClass(this.literals.hover_class);
			},
			mouseover: function(/*Event*/ evt) {
				var oThumb = evt.data.thumb;
				oThumb.hover();
			},
			mouseout: function(/*Event*/ evt) {
				var oThumb = evt.data.thumb;
				oThumb.unhover();
			},
			click: function (/*Event*/ evt) {
				var oThumb = evt.data.thumb;
				oThumb.tp.update_big_photo(oThumb.get_id());
			},
			get_pic_src: function() {
				return this.pic_elt_src;
			},
			parse_src: function() {
				var sFileName = this.pic_elt.attr("src").split("/");
				this.pic_elt_src.file = sFileName.pop();
				this.pic_elt_src.path = sFileName.join("/") + "/";
			},
			get_state: function() {
				return this.selected;
			},
			set_state: function(/*Boolean*/ /*state*/) {
				this.selected = arguments.length ? arguments[0] : this.elt.hasClass(this.literals.selected_class);
			},
			get_big_src: function() {
				return this.pic_elt_src.path + this.pic_elt_src.file.replace("_thumb","");
			},
			get_select_src: function() {
				return this.pic_elt_src.path + this.pic_elt_src.file.replace("_thumb","_thumb_s");
			}
			,
			get_thumb_src: function() {
				return this.pic_elt_src.path + this.pic_elt_src.file.replace("_thumb_s","_thumb");
			}
		}
	}
	
	function fixIE6layout() {
		if ($.browser.msie && $.browser.version < 7){
			// colors panel width
			$(".b-cat .b-colors").each(
				function(/*Number*/index) {
					var $color_panel = $(this),
						children = $(".b-color_item", $color_panel),
						parent_padding = 4;
					$color_panel.width(children.size() * children.width() + parent_padding*2);
				}
			);
			
			// blue button
			$(".b-button-wrap .d-sides .d-l").each(
				function() {
					var $button_bg = $(this);
					$button_bg.width($button_bg.parent().width());
				}
			);
		}
	}
});

/*
 * jQuery plugins
 */

/**
 * jQuery.Preload - Multifunctional preloader
 * Copyright (c) 2008 Ariel Flesler - aflesler(at)gmail(dot)com
 * Dual licensed under MIT and GPL.
 * Date: 3/25/2009
 * @author Ariel Flesler
 * @version 1.0.8
 */
;(function($){var h=$.preload=function(c,d){if(c.split)c=$(c);d=$.extend({},h.defaults,d);var f=$.map(c,function(a){if(!a)return;if(a.split)return d.base+a+d.ext;var b=a.src||a.href;if(typeof d.placeholder=='string'&&a.src)a.src=d.placeholder;if(b&&d.find)b=b.replace(d.find,d.replace);return b||null}),data={loaded:0,failed:0,next:0,done:0,total:f.length};if(!data.total)return finish();var g=$(Array(d.threshold+1).join('<img/>')).load(handler).error(handler).bind('abort',handler).each(fetch);function handler(e){data.element=this;data.found=e.type=='load';data.image=this.src;data.index=this.index;var a=data.original=c[this.index];data[data.found?'loaded':'failed']++;data.done++;if(d.enforceCache)h.cache.push($('<img/>').attr('src',data.image)[0]);if(d.placeholder&&a.src)a.src=data.found?data.image:d.notFound||a.src;if(d.onComplete)d.onComplete(data);if(data.done<data.total)fetch(0,this);else{if(g&&g.unbind)g.unbind('load').unbind('error').unbind('abort');g=null;finish()}};function fetch(i,a,b){if(a.attachEvent&&data.next&&data.next%h.gap==0&&!b){setTimeout(function(){fetch(i,a,1)},0);return!1}if(data.next==data.total)return!1;a.index=data.next;a.src=f[data.next++];if(d.onRequest){data.index=a.index;data.element=a;data.image=a.src;data.original=c[data.next-1];d.onRequest(data)}};function finish(){if(d.onFinish)d.onFinish(data)}};h.gap=14;h.cache=[];h.defaults={threshold:2,base:'',ext:'',replace:''};$.fn.preload=function(a){h(this,a);return this}})(jQuery);
