﻿$(function(){	initGallery();});function initGallery(){	$('.main-gal').each(function(){		var hold = $(this);		var big = hold.find('ul.gal-box');				var left = $('<div class="gal-box-left" />');		var right = $('<div class="gal-box-right" />');		var temp;		var prev = $('<a href="" class="prev" />');		var next = $('<a href="" class="next" />');		var ref = hold.find('div.ref img');		var flagRef = true,			timerotate;				big.parent().append(prev);		big.parent().append(next);				big.find('> li').css({			width: 1150		});		var temp = '<ul class="gal-box">'		big.find('> li').each(function(i){			temp = temp + '<li>' + big.find('> li').eq(big.find('> li').length-1-i).html() + '</li>';		});		temp = temp + '</ul>';				var gLeft = $(temp);		var gRight = $(temp);				gLeft.find('> li').css({			width: 575,			height: 251 		}).find('img').css({			width: 575,			height: 251 		});				left.append(gLeft);		hold.append(left);				gRight.find('> li').css({			width: 575,			height: 251 		}).find('img').css({			width: 575,			height: 251 		});		if($.browser.msie && $.browser.version == 8){			right.css({				width: 575,				height:251,				overflow: 'hidden'			});			left.css({				width: 575,				height:251,				overflow: 'hidden'			});		}		else{			right.css({				width: 575,				height:251,				overflow: 'hidden',				opacity:0.6			});			left.css({				width: 575,				height:251,				overflow: 'hidden',				opacity:0.6			});		}				right.append(gRight);		hold.append(right);				big.find('li').removeClass('active').eq(0).addClass('active');		gLeft.find('li').removeClass('active').eq(0).addClass('active');		gRight.find('li').removeClass('active').eq(-2).addClass('active');		var b = big.gallery({			infinite: true,			duration: 500,			listOfSlides: '> li',			initDop: true		});		var l = gLeft.gallery({			infinite: true,			duration: 500,			listOfSlides: '> li',			dir: false		});		var r = gRight.gallery({			infinite: true,			duration: 500,			listOfSlides: '> li',			dir: false		});		var time=true;		next.click(function(){			if(b.flag && l.flag && r.flag){				ref.animate({rotate: '+=180deg'}, 400);				b.flag = false;				l.flag = false;				r.flag = false;				b.toPrepare(b, true);				l.toPrepare(l, false);				r.toPrepare(r, false);			}			return false;		});		next.hover(function(){			time=true;			right.animate({paddingBottom: 10}, {queue:false, duration: 200, complete: function(){				right.animate({paddingBottom: 6}, {					queue: false,					duration: 200				});			}});			function initTime(){				right.animate({paddingBottom: 10}, {queue:false, duration: 200, complete: function(){					if(time){						$(this).animate({paddingBottom: 6}, {queue:false, duration: 200, complete: function(){							initTime();						}});					}				}});			}			initTime();		}, function(){			time = false;			right.stop().animate({paddingBottom:0}, {queue:false, duration: 200});		});		prev.click(function(){			if(b.flag && l.flag && r.flag){				ref.animate({rotate: '-=180deg'}, 400);				b.flag = false;				l.flag = false;				r.flag = false;				b.toPrepare(b, false);				l.toPrepare(l, true);				r.toPrepare(r, true);			}			return false;		});		prev.hover(function(){			time=true;			left.animate({paddingBottom: 10}, {queue:false, duration: 200, complete: function(){				left.animate({paddingBottom: 6}, {					queue: false,					duration: 200				});			}});			function initTime2(){				left.animate({paddingBottom: 10}, {queue:false, duration: 200, complete: function(){					if(time){						$(this).animate({paddingBottom: 6}, {queue:false, duration: 200, complete: function(){							initTime2();						}});					}				}});			}			initTime2();		}, function(){			time = false;			left.stop().animate({paddingBottom:0}, {queue:false, duration: 200});		});		ref.click(function(){			next.trigger('click');			return false;		});	});}$.fn.gallery = function(options) { return new Gallery(this.get(0), options); };		function Gallery(context, options) { this.init(context, options); };		Gallery.prototype = {		options:{},		init: function (context, options){			this.options = $.extend({				infinite: false,								//true = infinite gallery				duration: 700,									//duration of effect it 1000 = 1sec				slideElement: 1,								//number of elements for a slide				autoRotation: false,							//false = option is disabled; 1000 = 1sec				effect: false,									//false = slide; true = fade				listOfSlides: 'ul > li',						//elements galleries				switcher: false,								//false = option is disabled; 'ul > li' = elements switcher				disableBtn: false,								//false = option is disabled; 'hidden' = class adds an buttons "prev" and "next"				nextBtn: 'a.link-next, a.btn-next, a.next',		//button "next"				prevBtn: 'a.link-prev, a.btn-prev, a.prev',		//button "prev"				circle: true,									//true = cyclic gallery; false = not cyclic gallery				direction: false,								//false = horizontal; true = vertical				event: 'click',									//event for the buttons and switcher				IE: false,										//forced off effect it "fade" in IE				autoHeight: false,								//auto height on fade				easing: 'swing',				dir: true,				initDop: false			}, options || {});			var _el = $(context).find(this.options.listOfSlides);			if (this.options.effect) this.list = _el;			else this.list = _el.parent();			this.switcher = $(context).find(this.options.switcher);			this.nextBtn = $(context).find(this.options.nextBtn);			this.prevBtn = $(context).find(this.options.prevBtn);			this.count = _el.index(_el.filter(':last'));						if (this.options.switcher) this.active = this.switcher.index(this.switcher.filter('.active:eq(0)'));			else this.active = _el.index(_el.filter('.active:eq(0)'));			if (this.active < 0) this.active = 0;			this.last = this.active;						this.woh = _el.outerWidth(true);			if (!this.options.direction) this.installDirections(this.list.parent().width());			else {				this.woh = _el.outerHeight(true);				this.installDirections(this.list.parent().height());			}						if (!this.options.effect) {				this.rew = this.count - this.wrapHolderW + 1;				this.list.css({position: 'relative'}).css(this.dirAnimate());			}			else {				this.rew = this.count;				this.list.css({opacity: 0}).removeClass('active').eq(this.active).addClass('active').css({opacity: 1}).css('opacity', 'auto');				this.switcher.removeClass('active').eq(this.active).addClass('active');				if(this.options.autoHeight) this.list.parent().css({height: this.list.eq(this.active).outerHeight()});			}			this.flag = true;			if (this.options.infinite){				this.count++;				if(!this.options.initDop) this.active += this.count;				this.list.append(_el.clone());				if(!this.options.initDop) this.list.append(_el.clone());				this.list.css(this.dirAnimate());			}						this.initEvent(this, this.nextBtn, true);			this.initEvent(this, this.prevBtn, false);			if (this.options.disableBtn) this.initDisableBtn();			if (this.options.autoRotation) this.runTimer(this);			if (this.options.switcher) this.initEventSwitcher(this, this.switcher);		},		dirAnimate: function(){			if (!this.options.direction) return {left: -(this.woh * this.active)};			else return {top: -(this.woh * this.active)};		},		initDisableBtn: function(){			this.prevBtn.removeClass('prev-'+this.options.disableBtn);			this.nextBtn.removeClass('next-'+this.options.disableBtn);			if (this.active == 0 || this.count+1 == this.wrapHolderW) this.prevBtn.addClass('prev-'+this.options.disableBtn);			if (this.active == 0 && this.count+1 == 1 || this.count+1 <= this.wrapHolderW) this.nextBtn.addClass('next-'+this.options.disableBtn);			if (this.active == this.rew) this.nextBtn.addClass('next-'+this.options.disableBtn);		},		installDirections: function(temp){			this.wrapHolderW = Math.ceil(temp / this.woh);			if (((this.wrapHolderW - 1) * this.woh + this.woh / 2) > temp) this.wrapHolderW--;		},		fadeElement: function(){			if ($.browser.msie && this.options.IE){				this.list.eq(this.last).css({opacity:0});				this.list.removeClass('active').eq(this.active).addClass('active').css({opacity:'auto'});			}			else{				this.list.eq(this.last).animate({opacity:0}, {queue:false, easing: this.options.easing, duration: this.options.duration});				this.list.removeClass('active').eq(this.active).addClass('active').animate({					opacity:1				}, {queue:false, duration: this.options.duration, complete: function(){					$(this).css('opacity','auto');				}});			}			if(this.options.autoHeight) this.list.parent().animate({height: this.list.eq(this.active).outerHeight()}, {queue:false, duration: this.options.duration});			if (this.options.switcher) this.switcher.removeClass('active').eq(this.active).addClass('active');			this.last = this.active;		},		scrollElement: function($this){			if (!$this.options.infinite) $this.list.animate($this.dirAnimate(), {queue:false, easing: $this.options.easing, duration: $this.options.duration});			else $this.list.animate($this.dirAnimate(), $this.options.duration, $this.options.easing, function(){ $this.flag = true });			if ($this.options.switcher) $this.switcher.removeClass('active').eq($this.active / $this.options.slideElement).addClass('active');		},		runTimer: function($this){			if($this._t) clearTimeout($this._t);			$this._t = setInterval(function(){				if ($this.options.infinite) $this.flag = false;				if($this.options.dir) $this.toPrepare($this, true);				else $this.toPrepare($this, false);			}, this.options.autoRotation);		},		initEventSwitcher: function($this, el){			el.bind($this.options.event, function(){				$this.active = $this.switcher.index($(this)) * $this.options.slideElement;				if($this._t) clearTimeout($this._t);				if ($this.options.disableBtn) $this.initDisableBtn();				if (!$this.options.effect) $this.scrollElement($this);				else $this.fadeElement();				if ($this.options.autoRotation) $this.runTimer($this);				return false;			});		},		initEvent: function($this, addEventEl, dir){			addEventEl.bind($this.options.event, function(){				if ($this.flag){					if ($this.options.infinite) $this.flag = false;					if($this._t) clearTimeout($this._t);					$this.toPrepare($this, dir);					if ($this.options.autoRotation) $this.runTimer($this);				}				return false;			});		},		toPrepare: function($this, side){			if (!$this.options.infinite){				if (($this.active == $this.rew) && $this.options.circle && side) $this.active = -$this.options.slideElement;				if (($this.active == 0) && $this.options.circle && !side) $this.active = $this.rew + $this.options.slideElement;				for (var i = 0; i < $this.options.slideElement; i++){					if (side) { if ($this.active + 1 <= $this.rew) $this.active++; }					else { if ($this.active - 1 >= 0) $this.active--; }				};			}			else{				if(!this.options.initDop) {if ($this.active >= $this.count + $this.count && side) $this.active -= $this.count;}				else {if ($this.active >= $this.count && side) $this.active -= $this.count;}				if(!this.options.initDop) {if ($this.active <= $this.count-1 && !side) $this.active += $this.count;}				else {if ($this.active <= 1 && !side) $this.active += $this.count;}				$this.list.css($this.dirAnimate());				if (side) $this.active += $this.options.slideElement;				else $this.active -= $this.options.slideElement;			}			if (this.options.disableBtn) this.initDisableBtn();			if (!$this.options.effect) $this.scrollElement($this);			else $this.fadeElement();		},		stop: function(){			if (this._t) clearTimeout(this._t);		},		play: function(){			if (this._t) clearTimeout(this._t);			if (this.options.autoRotation) this.runTimer(this);		}	}
