// page init
$(function(){
	initSlideNav();
	initGall();
	initSlideShow();
	initPngFix();
});

/* SlideNav */
function initSlideNav(){
	var z_index = 100;
	var _speed = 300;
	var _showDelay = 0;
	var _timer;
	$('#topnav > li').each(function(){
		var hold = $(this);
		var _flag = true;
		var drop_wrapper = hold.find('.dropArea').eq(0);
		var drop = hold.find('.drop').eq(0);
		if(drop_wrapper.length && drop.length){
			drop_wrapper.css({
				display: 'block'
			});
			hold.addClass('active');
			var drop_wrapper_H = drop_wrapper.outerHeight(true);
			drop.css({
				marginTop: - drop_wrapper_H
			});
			drop_wrapper.css({
				height: 0,
				overflow: 'hidden'
			});
			hold.removeClass('active');
			hold.mousedown(function(){
				$('#topnav li').css({zIndex:2});
				if(hold.hasClass('active')) { _flag = false; }
				hold.css({zIndex:3}).addClass('active');
				if(_timer) { clearTimeout(_timer); }
				_timer = setTimeout(function(){
					drop_wrapper.stop().animate({height:drop_wrapper_H}, {queue:false, duration: _speed});
					drop.stop().animate({marginTop:0}, {queue:false, duration: _speed});
				},_showDelay);
			}).mouseleave(function(){
				if(_timer) { clearTimeout(_timer); }
				drop_wrapper.stop().animate({height:0}, {queue:false, duration: _speed});
				drop.stop().animate({marginTop:-drop_wrapper_H}, {queue:false, duration: _speed,complete:function(){
					hold.removeClass('active');
				}});
			});
		}
	});
}

// init galleries
function initGall(){
	$('.gallery-block').gallery({listOfSlides:'div.mask > ul > li'});
	$('.vertical-gallery').gallery({
		listOfSlides:'div.mask > ul > li',
		direction: true
	});
}

function initSlideShow(){
	$('div.slide-block').each(function(){
		var _this = $(this);
		var list = _this.find('ul.slide', _this),
			count = list.find('> li').length,
			w = list.parent().width(),
			_t,
			a = 0;
		var li = list.find('>li');
		var switcher = _this.find('ul.switcher > li');
		var thumbnails = _this.find('ul.nav a.info-link');
		switcher.eq(a).addClass('active');
		li.css("opacity","0").eq(a).css("opacity","1").addClass('active');
		runTimer();
		
		switcher.click(function(){
			animateSlide(switcher.index($(this)));
			return false;
		});
		thumbnails.each(function(){
			var _link = $(this);
			var img = _link.find('> .image-box');
			_link.mouseenter(function(){
				img.css({display:'block', opacity:0}).animate({opacity:1}, {queue:false, duration: 300});
				animateSlide(thumbnails.index(_link));
				_link.parent().find('.heading').css({
					background:'#d14e24'
				});
			}).mouseleave(function(){
				img.animate({opacity:0}, {queue:false, duration: 300, complete:function(){
					img.css({display:'none'});
				}});
				_link.parent().find('.heading').css({
					background:'none'
				});
			});
		});
		
		function runTimer(){
			_t = setTimeout(function(){
				animateSlide('next');
			}, 6000);
		}
		
		function animateSlide(e){
			if(typeof e == 'number') { a = e; }
			else if(typeof e == 'string' && e == 'next') { a++; }
			else{
				if(e.target.className == 'next') { a++; }
				else if(e.target.className == 'prev') { a--; }
			}
			if(_t) { clearTimeout(_t); }
			if(a == count) { a=0; }
			else if(a == -1) { a=count-1; }
			switcher.removeClass('active').eq(a).addClass('active');
			//li.removeClass('active').eq(a).addClass('active');
			//list.stop().animate({marginLeft:-w*a}, {queue:false, duration:700, complete:runTimer});
			li.stop().animate({opacity:0}, {queue:false, duration:700});
			li.eq(a).animate({opacity:1}, {queue:false, duration:700, complete:runTimer});
			return false;
		}
	});
}

// init png fix
function initPngFix(){
	if($.browser.msie){
		var transparentImage = "images/none.gif";
		var imgs = document.getElementsByTagName("img");
		for (i = 0; i < imgs.length; i++){
			if (imgs[i].src.indexOf(transparentImage) != -1){
				return;
			}
			if (imgs[i].src.indexOf(".png") != -1){
				var src = imgs[i].src;
				imgs[i].src = transparentImage;
				imgs[i].runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "',sizingMethod='scale')";
				imgs[i].style.display = "inline-block";
			}
		}
	}
}

// plugin gallery
(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({
				infinite: false,								//true = infinite gallery
				duration: 700,									//duration of effect it 1000 = 1sec
				slideElement: 1,								//number of elements for a slide
				autoRotation: 5000,							//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, a.btn-previos',		//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
			}, 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;
				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.list.append(_el.clone());
				this.list.append(_el.clone());
				eval('this.list.css('+this.anim+')');
			}
			
			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 ($.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, duration: $this.options.duration});'); }
			else { eval('$this.list.animate('+$this.anim+', $this.options.duration, 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; }
				$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;
				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.active >= $this.count + $this.count && 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(){
			if (this._t) { clearTimeout(this._t); }
		},
		play: function(){
			if (this._t) { clearTimeout(this._t); }
			if (this.options.autoRotation) { this.runTimer(this); }
		}
	};
}(jQuery));
