(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,
				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
			}, 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().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);
		},
		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) 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;
			});
		},
		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));
(function($) {
	$.fn.gall = 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,
				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
			}, 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.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);
		},
		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) 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;
			});
		},
		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));
function initTabs() {
	$('ul.tabset').each(function(){
		var _list = $(this);
		var _links = _list.find('a.tab');

		_links.each(function() {
			var _link = $(this);
			var _href = _link.attr('href');
			var _tab = $(_href);

			if(_link.hasClass('active')) _tab.fadeIn(500);
			else _tab.fadeOut(300);

			_link.click(function(){
				_links.filter('.active').each(function(){
					$($(this).removeClass('active').attr('href')).fadeOut(300);
				});
				_link.addClass('active');
				if(!$.browser.msie) _tab.fadeIn(500);
				else _tab.fadeIn(500);
				return false;
			});
		});
	});
}
function initHover(){
	$('ul#nav li li, div.gallery-nav ul li').mouseenter(function(){
		$(this).addClass('hover');
	}).mouseleave(function(){
		$(this).removeClass('hover');
	});
}


function initSlideBlock(){
	
	var box = $('#footer div.slide-block');
	var w = box.outerHeight(true);
	var _time;
	// box.height(0);
	$('#footer').find('a.link-sm').mouseenter(function(){
		box.animate({height:w+'px'},{duration:300});
		$(this).addClass('link-sm-close');
	})
      .click(function(){
		_time = setTimeout(function(){
			box.animate({height:0},300);
			$('#footer').find('a.link-sm').removeClass('link-sm-close');
		}, 500);
	});
       
	box.mouseenter(function(){
		if(_time) clearTimeout(_time);
	})
	/*.mouseleave(function(){
		box.animate({height:0},300);
		$('#footer').find('a.link-sm').removeClass('link-sm-close');
	});
	*/
}

function initSlideBlockFrontpage(){
	var box = $('#footer2 div.slide-block2');
	var w = box.outerHeight(true);
	var _time;
	box.height(0);
	$('#footer2').find('a.link-sm2').mouseenter(function(){
		box.animate({height:w+'px'},{duration:300});
		$(this).addClass('link-sm-close2');
	})
      .click(function(){
		_time = setTimeout(function(){
			box.animate({height:0},300);
			$('#footer2').find('a.link-sm2').removeClass('link-sm-close2');
		}, 500);
	});
       
	box.mouseenter(function(){
		if(_time) clearTimeout(_time);
	})
	/*.mouseleave(function(){
		box.animate({height:0},300);
		$('#footer').find('a.link-sm').removeClass('link-sm-close');
	});
	*/
}


function initNav(){
	var menu = $('ul#nav > li');
	var _time;
	menu.each(function(){
		var hold = $(this);
		var drop = hold.find('> .dropdown');
		if($.browser.msie) drop.css({top:'-9999px'});
		else drop.css({'opacity':0, top:'-9999px'});
		hold.mouseenter(function(){
			if(hold.find('> .dropdown').length != 0){
				if(_time) clearTimeout(_time);
				drop.css({top:'77px'});
				$(this).addClass('hover');
				if(!$.browser.msie) drop.animate({'opacity':1}, 300);
			}
			else{
				$(this).addClass('hover');
			}
		}).mouseleave(function(){
			var _li = $(this);
			if (hold.find('> .dropdown').length != 0) {
				
					if(!$.browser.msie) drop.animate({
						'opacity': 0
					}, {duration: 300, complete: function(){
							drop.css({top: '-9999px'});
							_li.removeClass('hover');
					}});
					else drop.css({top: '-9999px'});
				
			}
			else{
				$(this).removeClass('hover');
			}
		});
	});
}
jQuery.fn.myPopup = function(_options){
	// defaults options	
	var _options = jQuery.extend({
		duration: 700,
		linkOpenName: '.link-popup',
		linkCloseName: 'a.close, a.btn-close',
		divFader: 'fader',
		wrapper: 'body'
	},_options);

	return this.each(function(){
		var _hold = $(this);
		var _speed = _options.duration;
		var _IE = $.browser.msie;
		var links = _hold.find(_options.linkOpenName);
		var _fader = $('<div class="'+_options.divFader+'"></div>');
		var _select = $(_options.wrapper).find('select');
		var popup;
		$('body').append(_fader);
		_fader.css({
			position: 'absolute',
			top: '0px',
			left: '0px',
			zIndex: 100,
			background: 'url(/wp-content/themes/seuracom/images/123.png)',
			opacity: 0.7
		});
		
		function init(_obj){
			popup = $(_obj);
			var btnClose = popup.find(_options.linkCloseName);
			var submitBtn = popup.find('.link-submit');
			
			if (_IE) _select.css({visibility: 'hidden'});
			
			var w = $('body').width();
			var _w = $(_options.wrapper).width();
			if (_w > w) w =_w;
			var h = $(window).height();
			var _offset = $(window).scrollTop();
			
			var ret = _offset+(h/2) - popup.outerHeight(true)/2;
			if (ret < 0) ret = 0;
			var te = $(_options.wrapper).height();
			if ($(window).height() > te) te = $(window).height();
			
			popup.css({
				top: ret,
				left: w/2 - popup.outerWidth(true)/2
			}).hide();
			_fader.css({
				width: w,
				height: te
			}).fadeIn(300, function(){
				popup.fadeIn(300);
			});
			$(window).resize(function(){
				w = $('body').width();
				_w = $(_options.wrapper).width();
				if (_w > w) w =_w;
				popup.animate({
					left: w/2 - popup.outerWidth(true)/2
				}, {queue:false, duration: 300});
				_fader.css({
					width: w
				});
			});
			function closedPopup(opt1){
				popup.fadeOut(300, function(){
					popup.css({left: '-9999px'}).show();
					if (_IE) _select.css({visibility: 'visible'});
					submitBtn.unbind('click');
					$(window).unbind('resize');
					if (opt1) _fader.hide();
					else {
						if (submitBtn.attr('href')) init(submitBtn.attr('href'));
						else init(submitBtn.attr('title'));
					}
				});
			}
			btnClose.click(function(){
				closedPopup(true);
				return false;
			});
			submitBtn.click(function(){
				closedPopup();
				return false;
			})
			_fader.click(function(){
				closedPopup(true);
				return false;
			});
		}
		links.click(function(){
			if ($(this).attr('href')) init($(this).attr('href'));
			else init($(this).attr('title'));
			return false;
		});
	});
}
function initFilter(){
	$('ul#filter li').each(function(){
		var link = $(this);
		var mass = [];
		link.click(function(){
			var filtred = $(this).find('> a').attr('rel');
			
			if(!$(this).parents('li').hasClass('active')) $('ul#filter li').removeClass('active');
			else $('ul#filter ul li').removeClass('active');
		
			$(this).addClass('active');
			
			$('ul.main-list > li').hide();

			$('ul.main-list > li').each(function(){
				var hold = $(this);
				var a = hold.find('> a').attr('rel').split(',');

				for (var i=0;i<a.length;i++){
					if(a[i]==filtred) hold.delay(210).show();
				}
			});

			if (filtred == 'all') $('ul.main-list > li').delay(210).show();
			
			return false;
		});
	});
}



function fadeHover(){
	if($.browser.msie) $('ul.main-list > li > a > span >img').hide();
	else $('ul.main-list > li > a > span >img').css("opacity","0");
	$('.gallery-page').find('ul.main-list > li > a').mouseenter(function(){
		if($.browser.msie) $(this).find('span').show();
		else $(this).find('span >img').animate({opacity:1},300);
	}).mouseleave(function(){
		if($.browser.msie) $(this).find('span').hide();
		else $(this).find('span >img').animate({opacity:0},300);
	});
}
$(document).ready(function(){
	initTabs();
	initHover();
	initSlideBlock();
	initSlideBlockFrontpage();
	initNav();
	initFilter();
	fadeHover();
	$('body').myPopup();
	$('div.col1').gallery({
		duration: 500,
		listOfSlides: 'div.gallery > div.wrap > ul > li'
	});
	$('div.gal2').gallery({
		duration: 500,
		listOfSlides: 'div.gallery > div.wrap > ul > li'
	});
	$('div.col4').gallery({
		duration: 500,
		listOfSlides: 'div.gallery > div.wrap > ul > li'
	});
	gal = $('div#popup2 > div.wrap').gall({
		duration: 500,
		nextBtn: 'a.btn-next',
		prevBtn: 'a.btn-prev',
		listOfSlides: 'ul.popup-list > li'
	});
	if($('div.wrapper > ul.gallery-list').length){
		$('div.wrapper > ul.gallery-list > li').each(function(){
			var hold = $(this).find('div > a');
			var link = hold.attr('href');
			if (link != '#') {
				var popup = $(link);
				var video_list = popup.find('ul.popup-list > li');
				var w = video_list.width();
				hold.click(function(){
					var ind = $('div.wrapper > ul.gallery-list > li > div > a').index($(this));
					video_list.parent().css({marginLeft:-w*ind});
					gal.active = ind;
				});
			}
			
		});
	}
});
