jQuery(document).ready(function(){
	initBodyFade();
	initAccordion();
	initLogo();
	jQuery('div.small-gallery').each(function(){
		$(this).gallery({
			duration: 700,
			autoRotation: 7000,
			diff:true,
			easing: 'easeOutBack',
			listOfSlides: 'li'
		});
	});
	jQuery('div.big-gallery').each(function(){
		$(this).gallery({
			duration: 500,
			autoRotation: 17000,
			listOfSlides: '.frame>ul>li',
			switcher: '.s1>li',
			addSwitcher: '.s2>li',
			effect: true,
			IE:true
		});
	});
});
function initLogo(){
	var wrapper = $('#wrapper');
	var footerHeight = $('#footer').outerHeight();
	var logo = $('.logo');
	var holder = $('.main-holder',wrapper);
	res();
	jQuery(window).resize(function(){res();});
	function res(){
		var holderInd = (wrapper.outerHeight() - holder.outerHeight() - holder.offset().top)/2 + 39;
		logo.css({top: -(holderInd - footerHeight)});
	}
}
function initBodyFade(){
	var lis = $('.accordion>li').css('opacity',0);
	var openerWidth = ('>.opener',lis).outerWidth();
	var elWidth = 863;
	var periodTime = 50;
	var accordionDuration = 500;
	var bodyTime = 1000;
	var bodyDuration = 1000;
	
	setTimeout(function(){
		$('#wrapper,#footer').animate({
			opacity:1
		},{
			queue:false,
			duration:bodyDuration,
			complete:function(){
				$(this).css({
					opacity:'normal'
				});
				var lisCurr = lis.length - 1; 
				Timeout();
				function Timeout(){
					setTimeout(function(){
						if (lisCurr > -1) lisShow(lis.eq(lisCurr),lisCurr);
					},(lis.length-lisCurr)*periodTime);
				}
				function lisShow(_this,_lisCurr){
					_this.css({
						opacity:1
					}).css({
						opacity:'normal'
					}).animate({
						left: openerWidth * _lisCurr + elWidth
					},{
						queue:false,
						easing: 'easeOutBack',
						duration:accordionDuration,
						complete:(function(){
							if (_lisCurr == 0) {
								var cur = $(this).find('>.opener');
								setTimeout(function(){
									cur.trigger('click');
								},accordionDuration*2);
							}
						})
					});
					lisCurr--;
					Timeout();
				}
			}
		});
	},bodyTime);
}
// accordion function
function initAccordion(){
	var activeClass = 'active';
	var hoverClass = 'hover';
	var nextClass = 'next';
	var switchClass = 'switch';
	var slideDuration = 1200;
	var t = 0;
	$('.accordion').each(function(){
		var accondion = $(this);
		var els = $('>li',accondion);
		var elsLength = els.length;
		var slides = $('.slide',accondion).css('width',0);
		var slideWidth = 999;
		var elWidth = 863;
		
		els.each(function(num){
			var el = $(this);
			var openers = $('>.opener',el);
			var openerWidth = openers.outerWidth();
			var slide = $('>.slide',el);
			var otherSlides = $('.slide',accondion).not(slide);
			openers.click(function(){
				if (t) {clearTimeout(t);}
				el.removeClass(hoverClass);
				els.addClass(nextClass).find('.center').stop().css({
					opacity: 1
				});
				
				if (!els.filter(':animated').length) {
					var ind = els.index(el);
					
					if (!$('#main').hasClass('main-bg')) $('#main').addClass('main-bg');
					if (slide.outerWidth() < slideWidth) {
						
						els.removeClass(switchClass).eq(ind).addClass(switchClass);
						slide.animate({
							width:slideWidth
						},{
							queue:false,
							easing: 'easeOutBack',
							duration: slideDuration,
							complete:function(){
								$(this).css({
									'overflow':'visible',
									'paddingBottom':0
								});
								els.filter('.'+switchClass).nextAll().find('.center').animate({
									opacity:0
								},{
									queue:false,
									duration:500,
									complete:function(){
										els.removeClass(nextClass);//.removeClass(activeClass).eq(ind).addClass(activeClass);	
									}
								});
							}
						});
						otherSlides.animate({
							width:0
						},{
							queue:false,
							easing: 'easeOutBack',
							duration: slideDuration,
							complete:function(){
								$(this).css({
									'overflow':'hidden',
									'paddingBottom':'120px'
								});
							}
						});
						for (var i = ind+1; i < elsLength; i++){
							els.eq(i).animate({
								left:  openerWidth * i + elWidth
							}, {queue:false,easing: 'easeOutBack', duration: slideDuration });
						};
						for (var i = 0; i < ind+1; i++){
							els.eq(i).animate({
								left:  openerWidth * i
							}, {queue:false,easing: 'easeOutBack', duration: slideDuration });
						};
					}
				}
				return false;
			});
			
			openers.mouseenter(function(){
				if (!els.filter(':animated').length) {
					if (!el.hasClass(hoverClass)) {
						el.addClass(hoverClass).find('.center').stop().animate({
							opacity:1
						},{
							queue:false,
							duration:500
						});
						if (els.index($(this).parent()) > els.index(els.filter('.switch'))) {
							if (t) {clearTimeout(t);}
							els.eq(els.index(els.filter('.switch'))).nextAll().addClass(nextClass).find('.center').stop().animate({
								opacity:1
							},{
								queue:false,
								duration:500,
								complete:function(){
									$(this).css('opacity','auto');
								}
							});
						}
					}
				}
			}).mouseleave(function(){
				if (!els.filter(':animated').length) {
					if (el.hasClass(hoverClass)) {
						el.removeClass(hoverClass);
					}
					t = setTimeout(function(){
						els.eq(els.index(accondion.find('>.switch'))).nextAll().find('.center').stop().animate({
							opacity:0
						},{
							queue:false,
							duration:500,
							complete:function(){
								els.removeClass(nextClass);
							}
						});
						
					},1000);
				}
			});
			
		});
	});
}
(function($) {
	$.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({
				duration: 700,
				slideElement: 1,
				autoRotation: false,
				effect: false,
				listOfSlides: 'ul > li',
				switcher: false,
				addSwitcher:false,
				disableBtn: false,
				nextBtn: 'a.link-next, a.btn-next, a.next',
				prevBtn: 'a.link-prev, a.btn-prev, a.prev',
				circle: true,
				diff:false,
				easing:false,
				direction: false,
				event: 'click',
				IE: 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.addSwitcher = $(context).find(this.options.addSwitcher);
			this.nextBtn = $(context).find(this.options.nextBtn);
			this.prevBtn = $(context).find(this.options.prevBtn);
			this.count = _el.index(_el.filter(':last'));
			this.els = $(context).find(this.options.listOfSlides);
			
			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;
				if (!this.options.direction) this.list.css({marginLeft: -(this.woh * this.active)});
				else this.list.css({marginTop: -(this.woh * this.active)});
			}
			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.disableBtn) {
				if (this.count < this.wrapHolderW) this.nextBtn.addClass(this.options.disableBtn);
				if (this.active == 0) this.prevBtn.addClass(this.options.disableBtn);
			}
			
			this.initEvent(this, this.nextBtn, this.prevBtn, true);
			this.initEvent(this, this.prevBtn, this.nextBtn, false);
			
			if (this.options.autoRotation) this.runTimer(this);
			
			if (this.options.switcher) this.initEventSwitcher(this, this.switcher);
			if (this.options.addSwitcher) this.initEventAddSwitcher(this, this.addSwitcher);
		},
		installDirections: function(temp){
			this.wrapHolderW = Math.ceil(temp / this.woh);
			if (((this.wrapHolderW - 1) * this.woh + this.woh / 2) > temp) this.wrapHolderWwrapHolderW--;
		},
		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, 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.switcher) this.switcher.removeClass('active').eq(this.active).addClass('active');
			this.last = this.active;
		},
		scrollElement: function(){
			if (!this.options.direction) {
				if (this.options.diff) {
					if (this.options.easing) this.list.animate({marginLeft: -(this.els.eq(this.active).position().left)}, {queue:false, duration: this.options.duration,easing: this.options.easing});
					else this.list.animate({marginLeft: -(this.els.eq(this.active).position().left)}, {queue:false, duration: this.options.duration});
				} else {
					if (this.options.easing) this.list.animate({marginLeft: -(this.woh * this.active)}, {queue:false, duration: this.options.duration,easing: this.options.easing});
					else this.list.animate({marginLeft: -(this.woh * this.active)}, {queue:false, duration: this.options.duration});
				}
			}
			else this.list.animate({marginTop: -(this.woh * this.active)}, {queue:false, duration: this.options.duration});
			if (this.options.switcher) this.switcher.removeClass('active').eq(this.active).addClass('active');
		},
		runTimer: function($this){
			if($this._t) clearTimeout($this._t);
			$this._t = setInterval(function(){
				$this.toPrepare($this, true);
			}, this.options.autoRotation);
		},
		initEventSwitcher: function($this, el){
			el.bind($this.options.event, function(){
				$this.active = $this.switcher.index($(this));
				if($this._t) clearTimeout($this._t);
				if (!$this.options.effect) $this.scrollElement();
				else $this.fadeElement();
				if ($this.options.autoRotation) $this.runTimer($this);
				return false;
			});
		},
		initEventAddSwitcher: function($this, el){
			el.bind($this.options.event, function(){
				var curr = $(this).parent().find('li').index($(this));
				$this.switcher.eq(curr).trigger('click');
				return false;
			});
		},
		initEvent: function($this, addEventEl, addDisClass, dir){
			addEventEl.bind($this.options.event, function(){
				if($this._t) clearTimeout($this._t);
				if ($this.options.disableBtn &&($this.count > $this.wrapHolderW)) addDisClass.removeClass($this.options.disableBtn);
				$this.toPrepare($this, dir);
				if ($this.options.autoRotation) $this.runTimer($this);
				return false;
			});
		},
		toPrepare: function($this, side){
			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) {
						if ($this.options.disableBtn && ($this.count > $this.wrapHolderW)) $this.nextBtn.addClass($this.options.disableBtn);
					}
					else $this.active++;
				}
				else{
					if ($this.active - 1 < 0) {
						if ($this.options.disableBtn && ($this.count > $this.wrapHolderW)) $this.prevBtn.addClass($this.options.disableBtn);
					}
					else $this.active--;
				}
			};
			if ($this.active == $this.rew && side) if ($this.options.disableBtn &&($this.count > $this.wrapHolderW)) $this.nextBtn.addClass($this.options.disableBtn);
			if ($this.active == 0 && !side) if ($this.options.disableBtn &&($this.count > $this.wrapHolderW)) $this.prevBtn.addClass($this.options.disableBtn);
			if (!$this.options.effect) $this.scrollElement();
			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);
		}
	}
}(jQuery));
