﻿$(document).ready(function(){
	$('div.carousel > div.hold > ul > li').eq($_REQUEST).removeClass('top').addClass('active').addClass('top').find('> h4 > a').addClass('active');
	initMenu();
	
	var t1 = $('div.slide-tabs').gallery({
		infinite: true,
		duration: 2000,
		autoRotation: 5000,
		listOfSlides: 'div.carousel div.hold > ul > li',
		nextBtn: 'div.slider-next, div.carousel div.next',
		prevBtn: 'div.slider-prev, div.carousel div.prev',
		direction: true,
		onStart: function(){
			t1.options.duration = 500;
		}
	});
	$('div.slide-tabs').each(function(){
		var hold = $(this);
		var pause = hold.find('div.pause, div.carousel div.pause');
		var play = hold.find('div.play, div.carousel div.play');
		var next= hold.find('div.slider-next, div.carousel div.next');
		var prev = hold.find('div.slider-prev, div.carousel div.prev');
		var t = t1.options.duration;
		
		prev.click(function(){
			t1.stop();
			pause.css({display: 'none'});
			play.css({display: 'block'});
			return false;
		});
		
		next.click(function(){
			t1.stop();
			pause.css({display: 'none'});
			play.css({display: 'block'});
			return false;
		});
		
		pause.click(function(){
			t1.stop();
			pause.css({display: 'none'});
			play.css({display: 'block'});
		});
		play.click(function(){
			t1.options.duration = t;
			t1.play();
			t1.toPrepare(t1, true);
			play.css({display: 'none'});
			pause.css({display: 'block'});
		});
	});
	$('div.slide-tabs').gallery({
		duration: 500,
		listOfSlides: 'div.block-holder > div.block',
		switcher: 'div.carousel ul > li > h4 > a',
		effect: true,
		autoHeight: true
	});
	$('body').gallery({
		duration: 500,
		listOfSlides: 'div.promo-hold > div',
		switcher: 'div.carousel ul > li > h4 > a',
		effect: true,
		autoHeight: true,
		block: 'div.aside-l ul.l-menu > li > a > span'
	});
	resPage();
});

function initMenu(){
	$('table.menu').each(function(){
		var hold = $(this);
		var link = hold.find('tr > td');
		var active = link.index(link.filter('.active:eq(0)'));
		
		link.hover(function(){
			if (active != -1) link.removeClass('active');
		}, function(){
			if (active != -1) link.eq(active).addClass('active');
		});
	});
}

function resPage(){
	var hold = $('div.mainContent > div.block-holder');
	var block = hold.find('> div.block');
	var w;
	
	function resizePage(){
		w = hold.outerWidth(true);
		block.css({width:w});
	}
	resizePage();
	$(window).resize(function(){
		resizePage();
	});
}

//function initFooter(){
//	var footer = $('div.footer');
//	var footerPlace = $('div.footer-place');
//	var h = footer.outerHeight();
//	
//	footer.css({marginTop:-h});
//	footerPlace.css({height:h});
//}


$.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,								
			duration: 700,									
			slideElement: 1,								
			autoRotation: false,							
			effect: false,									
			listOfSlides: 'ul > li',						
			switcher: false,								
			disableBtn: false,								
			nextBtn: 'a.link-next, a.btn-next, a.next',		
			prevBtn: 'a.link-prev, a.btn-prev, a.prev',		
			circle: true,									
			direction: false,								
			event: 'click',									
			IE: false,										
			autoHeight: false,
			disebled: false,
			block: false,								
			onStart: function(){}
		}, options || {});
		var _el = $(context).find(this.options.listOfSlides);
		if (this.options.effect) this.list = _el;
		else this.list = _el.parent();
		if (this.options.switcher) this.switcher = $(context).find(this.options.switcher);
		if (this.options.block) this.block = $(context).find(this.options.block);
		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);
			if ($.browser.msie && $.browser.version <8) this.woh = _el.outerHeight(true) + 3;
			this.installDirections(this.list.parent().height());
		}
		
		if (!this.options.effect) {
			this.rew = this.count - this.wrapHolderW + 1;
			if (!this.options.direction) this.anim = '{marginLeft: -(this.woh * this.active)}';
			else this.anim = '{marginTop: -(this.woh * this.active)}';
			eval('this.list.css('+this.anim+')');
		}
		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++;
			this.active = this.count + this.active;
			this.list.append(_el.clone());
			this.list.append(_el.clone());
			eval('this.list.css('+this.anim+')');
		}
		
		if(this.options.block){
			if(this.active == 0) this.block.removeClass('checked').eq(0).addClass('checked');
			if(this.active == 1 || this.active == 2) this.block.removeClass('checked').eq(1).addClass('checked');
			if(this.active == 3 || this.active == 4 || this.active == 5) this.block.removeClass('checked').eq(2).addClass('checked');
			if(this.active == 6 || this.active == 7 || this.active == 8) this.block.removeClass('checked').eq(3).addClass('checked');
		}
		
		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);
	},
	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 || 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(this.options.block){
			if(this.active == 0) this.block.removeClass('checked').eq(0).addClass('checked');
			if(this.active == 1 || this.active == 2) this.block.removeClass('checked').eq(1).addClass('checked');
			if(this.active == 3 || this.active == 4 || this.active == 5) this.block.removeClass('checked').eq(2).addClass('checked');
			if(this.active == 6 || this.active == 7 || this.active == 8) this.block.removeClass('checked').eq(3).addClass('checked');
		}
		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.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) eval('$this.list.animate('+$this.anim+', {queue:false, easing: "linear", duration: $this.options.duration});');
		else eval('$this.list.animate('+$this.anim+', $this.options.duration, "linear", function(){ $this.flag = true; $this.list.find("li h4 > a").eq($this.active).trigger("click"); });');
		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;
			$this.toPrepare($this, true);
		}, this.options.autoRotation);
	},
	initEventSwitcher: function($this, el){
		el.bind($this.options.event, function(){
			$this.active = $this.switcher.index($(this)) * $this.options.slideElement;
			el.parents('li').removeClass('top')
			$(this).parents('li').addClass('top')
			if ($this.active >= $this.count) $this.active = $this.active - ($this.count+1)
			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){
				$this.options.onStart();
				if ($this.options.infinite) $this.flag = false;
				if($this._t) clearTimeout($this._t);
				$this.toPrepare($this, dir);
				if ($this.options.autoRotation && !($this.disebled)) $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.active >= $this.count + $this.count-1 && side) $this.active -= $this.count;
			if ($this.active <= $this.count-1 && !side) $this.active += $this.count;
			eval('$this.list.css('+$this.anim+')');
			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(){
		this.disebled = true;
		if (this._t) clearTimeout(this._t);
	},
	play: function(){
		this.disebled = false;
		if (this._t) clearTimeout(this._t);
		if (this.options.autoRotation) this.runTimer(this);
	}
}


