jQuery(function(){
	initPlugins();
	ieHover('.switcher li, .nav li div', 'hover');
});
// jquery ie6 hover pack
function ieHover(obj, actClass){
	var elements = obj;
	var hoverClass = actClass;
	if(jQuery.browser.msie && parseInt(jQuery.browser.version) == 6){
		jQuery(elements).hover(function(){
			jQuery(this).addClass(hoverClass);
		}, function(){
			jQuery(this).removeClass(hoverClass);
		});
	}
}

// init plugins function
function initPlugins(){
	jQuery('div.block-gallery').scrollGallery({
		sliderHolder: 'div.holder',
		switchTime:5000, 
		duration:1000,
		autoRotation:true
	});
	jQuery('ul.tabset').jqueryTabs({
		addToParent:true,
		holdHeight:false,
		activeClass:'active',
		tabLinks:'a.tab',
		fadeSpeed:0
	});
	jQuery('ul.subnav li').OpenClose({
		activeClass:'active',
		opener:'>a.opener',
		slider:'>ul.slide',
		slideSpeed: 400
	});
	jQuery('a.has-popup').simplebox();
}

(function(jQuery) {
	jQuery.fn.simplebox = function(options) { 
		return new Simplebox(this, options); 
	};
	
	function Simplebox(context, options) { this.init(context, options); };
	
	Simplebox.prototype = {
		options:{},
		init: function (context, options){
			this.options = jQuery.extend({
				duration: 300,
				linkClose: 'a.close, a.btn-close',
				divFader: 'fader',
				faderColor: 'black',
				opacity: 0.7,
				wrapper: '#wrapper',
				linkPopap: '.link-submit'
			}, options || {});
			this.btn = jQuery(context);
			this.select = jQuery(this.options.wrapper).find('select');
			this.initFader();
			this.btnEvent(this, this.btn);
		},
		btnEvent: function(jQuerythis, el){
			el.click(function(){
				if (jQuery(this).attr('href')) jQuerythis.toPrepare(jQuery(this).attr('href'));
				else jQuerythis.toPrepare(jQuery(this).attr('title'));
				return false;
			});
		},
		calcWinWidth: function(){
			this.winWidth = jQuery('body').width();
			if (jQuery(this.options.wrapper).width() > this.winWidth) this.winWidth = jQuery(this.options.wrapper).width();
		},
		toPrepare: function(obj){
			this.popup = jQuery(obj);
			this.btnClose = this.popup.find(this.options.linkClose);
			this.submitBtn = this.popup.find(this.options.linkPopap);
			
			if (jQuery.browser.msie) this.select.css({visibility: 'hidden'});
			this.calcWinWidth();
			this.winHeight = jQuery(window).height();
			this.winScroll = jQuery(window).scrollTop();
			
			this.popupTop = this.winScroll + (this.winHeight/2) - this.popup.outerHeight(true)/2;
			if (this.popupTop < 0) this.popupTop = 0;
			this.faderHeight = jQuery(this.options.wrapper).outerHeight();
			if (jQuery(window).height() > this.faderHeight) this.faderHeight = jQuery(window).height();
			
			this.popup.css({
				top: this.popupTop,
				left: this.winWidth/2 - this.popup.outerWidth(true)/2
			}).hide();
			this.fader.css({
				width: this.winWidth,
				height: this.faderHeight
			});
			this.initAnimate(this);
			this.initCloseEvent(this, this.btnClose, true);
			this.initCloseEvent(this, this.submitBtn, false);
			this.initCloseEvent(this, this.fader, true);
		},
		initCloseEvent: function(jQuerythis, el, flag){
			el.click(function(){
				jQuery('body > div.outtaHere').removeClass('optionsDivVisible').addClass('optionsDivInvisible')
				jQuerythis.popup.fadeOut(jQuerythis.options.duration, function(){
					jQuerythis.popup.css({left: '-9999px'}).show();
					if (jQuery.browser.msie) jQuerythis.select.css({visibility: 'visible'});
					jQuerythis.submitBtn.unbind('click');
					jQuerythis.fader.unbind('click');
					jQuerythis.btnClose.unbind('click');
					jQuery(window).unbind('resize');
					if (flag) jQuerythis.fader.fadeOut(jQuerythis.options.duration);
					else {
						if (jQuerythis.submitBtn.attr('href')) jQuerythis.toPrepare(jQuerythis.submitBtn.attr('href'));
						else jQuerythis.toPrepare(jQuerythis.submitBtn.attr('title'));
					}
				});
				return false;
			});
		},
		initAnimate:function (jQuerythis){
			jQuerythis.fader.fadeIn(jQuerythis.options.duration, function(){
				jQuerythis.popup.fadeIn(jQuerythis.options.duration);
			});
			jQuery(window).resize(function(){
				jQuerythis.calcWinWidth();
				jQuerythis.popup.animate({
					left: jQuerythis.winWidth/2 - jQuerythis.popup.outerWidth(true)/2
				}, {queue:false, duration: jQuerythis.options.duration});
				jQuerythis.fader.css({width: jQuerythis.winWidth});
			});
		},
		initFader: function(){
			if (jQuery(this.options.divFader).length > 0) this.fader = jQuery(this.options.divFader);
			else{
				this.fader = jQuery('<div class="'+this.options.divFader+'"></div>');
				jQuery('body').append(this.fader);
				this.fader.css({
					position: 'absolute',
					zIndex: 999,
					left:0,
					top:0,
					background: this.options.faderColor,
					opacity: this.options.opacity
				}).hide();
			}
		}
	}
}(jQuery));

// scrolling gallery plugin
jQuery.fn.scrollGallery = function(_options){
	var _options = jQuery.extend({
		sliderHolder: '>div',
		slider:'>ul',
		slides: '>li',
		pagerLinks:'div.pager a',
		generatePagination:'div.paging',
		paginationUlClass:'switcher',
		switcherClasses:'',
		switcherClassTo:'a',
		btnPrev:'a.link-prev',
		btnNext:'a.link-next',
		activeClass:'active',
		disabledClass:'disabled',
		switcherClassName:'switcher',
		curNum:'em.scur-num',
		allNum:'em.sall-num',
		circleSlide:true,
		pauseClass:'gallery-paused',
		pauseButton:'none',
		pauseOnHover:true,
		autoRotation:false,
		stopAfterClick:false,
		switchTime:5000,
		duration:650,
		easing:'swing',
		event:'click',
		splitCount:false,
		afterInit:false,
		vertical:false,
		step:false
	},_options);

	return this.each(function(){
		// gallery options
		var _this = jQuery(this);
		var _sliderHolder = jQuery(_options.sliderHolder, _this);
		var _slider = jQuery(_options.slider, _sliderHolder);
		var _slides = jQuery(_options.slides, _slider);
		var _generatePagination = jQuery(_options.generatePagination, _this);
		var _paginationUlClass = _options.paginationUlClass;
		var _switcherClasses = _options.switcherClasses;
		var _switcherClassTo = _options.switcherClassTo;
		var _btnPrev = jQuery(_options.btnPrev, _this);
		var _btnNext = jQuery(_options.btnNext, _this);
		var _pagerLinks = jQuery(_options.pagerLinks, _this);
		var _switcherClassName = _options.switcherClassName;
		var _generatePagination = jQuery(_options.generatePagination, _this);
		var _curNum = jQuery(_options.curNum, _this);
		var _allNum = jQuery(_options.allNum, _this);
		var _pauseButton = jQuery(_options.pauseButton, _this);
		var _pauseOnHover = _options.pauseOnHover;
		var _pauseClass = _options.pauseClass;
		var _autoRotation = _options.autoRotation;
		var _activeClass = _options.activeClass;
		var _disabledClass = _options.disabledClass;
		var _easing = _options.easing;
		var _duration = _options.duration;
		var _switchTime = _options.switchTime;
		var _controlEvent = _options.event;
		var _step = _options.step;
		var _vertical = _options.vertical;
		var _circleSlide = _options.circleSlide;
		var _stopAfterClick = _options.stopAfterClick;
		var _afterInit = _options.afterInit;
		var _splitCount = _options.splitCount;
		
		if(_switcherClasses){_switcherClasses = _switcherClasses.split((/\s*,\s*/));}
		
		// gallery init
		if(!_slides.length) return;

		var _switcherIndex = 0;

		if(_generatePagination.length) {
			_generatePagination.empty();
			var list = jQuery('<ul class="'+_paginationUlClass+'">');
			var classTagLi = '';
			var classTagA = '';
			for(var i=0; i<_slides.length; i++){
				if(_switcherClasses.length && _switcherClassTo == 'li'){
					classTagLi = ' class="'+_switcherClasses[_switcherIndex]+'"';
				}else if(_switcherClasses.length && _switcherClassTo == 'a'){
					classTagA = ' class="'+_switcherClasses[_switcherIndex]+'"';
				}
				jQuery('<li'+classTagLi+'><a'+classTagA+' href="#">'+(i+1)+'</a><div class="popup"><div class="popup-holder"><img src="'+_slides.eq(i).attr('rel')+'" width="149" height="80"></div></div></li>').appendTo(list);
				if(_switcherIndex < _switcherClasses.length -1){_switcherIndex++;}else{_switcherIndex = 0;}
			}
			list.appendTo(_generatePagination);
			_pagerLinks = list.children();
		}

		if(_splitCount) {
			var curStep = 0;
			var newSlide = $('<slide>').addClass('split-slide');
			_slides.each(function(){
				newSlide.append(this);
				curStep++;
				if(curStep > _splitCount-1) {
					curStep = 0;
					_slider.append(newSlide);
					newSlide = $('<slide>').addClass('split-slide');
				}
			});
			if(curStep) _slider.append(newSlide);
			_slides = _slider.children();
		}
		
		var _currentStep = 0;
		var _sumWidth = 0;
		var _sumHeight = 0;
		var _hover = false;
		var _stepWidth;
		var _stepHeight;
		var _stepCount;
		var _offset;
		var _timer;

		_slides.each(function(){
			_sumWidth+=$(this).outerWidth(true);
			_sumHeight+=$(this).outerHeight(true);
		});

		// calculate gallery offset
		function recalcOffsets() {
			if(_vertical) {
				if(_step) {
					_stepHeight = _slides.eq(_currentStep).outerHeight(true);
					_stepCount = Math.ceil((_sumHeight-_sliderHolder.height())/_stepHeight)+1;
					_offset = -_stepHeight*_currentStep;
				} else {
					_stepHeight = _sliderHolder.height();
					_stepCount = Math.ceil(_sumHeight/_stepHeight);
					_offset = -_stepHeight*_currentStep;
					if(_offset < _stepHeight-_sumHeight) _offset = _stepHeight-_sumHeight;
				}
			} else {
				if(_step) {
					_stepWidth = _slides.eq(_currentStep).outerWidth(true)*_step;
					_stepCount = Math.ceil((_sumWidth-_sliderHolder.width())/_stepWidth)+1;
					_offset = -_stepWidth*_currentStep;
					if(_offset < _sliderHolder.width()-_sumWidth) _offset = _sliderHolder.width()-_sumWidth;
				} else {
					_stepWidth = _sliderHolder.width();
					_stepCount = Math.ceil(_sumWidth/_stepWidth);
					_offset = -_stepWidth*_currentStep;
					if(_offset < _stepWidth-_sumWidth) _offset = _stepWidth-_sumWidth;
				}
			}
		}

		// gallery control
		if(_btnPrev.length) {
			_btnPrev.bind(_controlEvent,function(){
				if(_stopAfterClick) stopAutoSlide();
				prevSlide();
				return false;
			});
		}
		if(_btnNext.length) {
			_btnNext.bind(_controlEvent,function(){
				if(_stopAfterClick) stopAutoSlide();
				nextSlide();
				return false;
			});
		}
		if(_pagerLinks.length) {
			_pagerLinks.each(function(_ind){
				jQuery(this).bind(_controlEvent,function(){
					if(_currentStep != _ind) {
						if(_stopAfterClick) stopAutoSlide();
						_currentStep = _ind;
						switchSlide();
					}
					return false;
				});
			});
		}

		// gallery animation
		function prevSlide() {
			recalcOffsets();
			if(_currentStep > 0) _currentStep--;
			else if(_circleSlide) _currentStep = _stepCount-1;  
			switchSlide();
		}
		function nextSlide() {
			recalcOffsets();
			if(_currentStep < _stepCount-1) _currentStep++;
			else if(_circleSlide) _currentStep = 0; 
			switchSlide();
		}
		function refreshStatus() {
			if(_pagerLinks.length) _pagerLinks.removeClass(_activeClass).eq(_currentStep).addClass(_activeClass);
			if(!_circleSlide) {
				_btnPrev.removeClass(_disabledClass);
				_btnNext.removeClass(_disabledClass);
				if(_currentStep == 0) _btnPrev.addClass(_disabledClass);
				if(_currentStep == _stepCount-1) _btnNext.addClass(_disabledClass);
			}
			if(_curNum.length) _curNum.text(_currentStep+1);
			if(_allNum.length) _allNum.text(_stepCount);
		}
		function switchSlide() {
			recalcOffsets();
			if(_vertical) _slider.animate({marginTop:_offset},{duration:_duration,queue:false,easing:_easing});
			else _slider.animate({marginLeft:_offset},{duration:_duration,queue:false,easing:_easing});
			refreshStatus();
			autoSlide();
		}

		// autoslide function
		function stopAutoSlide() {
			if(_timer) clearTimeout(_timer);
			_autoRotation = false;
		}
		function autoSlide() {
			if(!_autoRotation || _hover) return;
			if(_timer) clearTimeout(_timer);
			_timer = setTimeout(nextSlide,_switchTime+_duration);
		}
		if(_pauseOnHover) {
			_this.hover(function(){
				_hover = true;
				if(_timer) clearTimeout(_timer);
			},function(){
				_hover = false;
				autoSlide();
			});
		}
		recalcOffsets();
		refreshStatus();
		autoSlide();

		// pause buttton
		if(_pauseButton.length) {
			_pauseButton.click(function(){
				if(_this.hasClass(_pauseClass)) {
					_this.removeClass(_pauseClass);
					_autoRotation = true;
					autoSlide();
				} else {
					_this.addClass(_pauseClass);
					stopAutoSlide();
				}
				return false;
			});
		}

		if(_afterInit && typeof _afterInit === 'function') _afterInit(_this, _slides);
	});
}

// jquery tabs plugin
jQuery.fn.jqueryTabs = function(_options){
	// default options
	var _options = jQuery.extend({
		addToParent:false,
		holdHeight:false,
		activeClass:'active',
		tabLinks:'a.tab',
		fadeSpeed:300,
		event:'click'
	},_options);

	return this.each(function(){
		var _holder = jQuery(this);
		var _fadeSpeed = _options.fadeSpeed;
		var _activeClass = _options.activeClass;
		var _addToParent = _options.addToParent;
		var _holdHeight = _options.holdHeight;
		var _tabLinks = jQuery(_options.tabLinks, _holder);
		var _tabset = (_addToParent ? _tabLinks.parent() : _tabLinks);
		var _event = _options.event;
		var _animating = false;

		// tabs init
		_tabLinks.each(function(){
			var _tmpLink = jQuery(this);
			var _tmpTab = jQuery(_tmpLink.attr('href'));
			var _classItem = (_addToParent ? _tmpLink.parent() : _tmpLink);
			if(_tmpTab.length) {
				if(_classItem.hasClass(_activeClass)) _tmpTab.show();
				else _tmpTab.hide();
			}
		});

		// tab switcher
		function switchTab(_switcher) {
			if(!_animating) {
				var _link = jQuery(_switcher);
				var _newItem = (_addToParent ? _link.parent() : _link);
				var _newTab = jQuery(_link.attr('href'));
				if(_newItem.hasClass(_activeClass)) return;

				var _oldItem = jQuery(_addToParent ? _tabset : _tabLinks).filter('.'+_activeClass);
				var _oldTab = jQuery(jQuery(_addToParent ? _oldItem.children('a') : _oldItem).attr('href'));
				if(_newTab.length) {
					_animating = true;
					if(_oldItem.length) {
						_newItem.addClass(_activeClass);
						_oldItem.removeClass(_activeClass);

						var _parent = _oldTab.parent();
						if(_holdHeight) _parent.css({height:_parent.height()});

						_oldTab.fadeOut(_fadeSpeed,function(){
							_newTab.fadeIn(_fadeSpeed,function(){
								_animating = false;
							});
							if(_holdHeight) _parent.css({height:'auto'});
						});
					} else {
						_newItem.addClass(_activeClass);
						_newTab.fadeIn(_fadeSpeed,function(){
							_animating = false;
						});
					}
				}
			}
		}

		// control
		_tabLinks.each(function(){
			jQuery(this).bind(_event,function(){
				switchTab(this);
				return false;
			});
		});
	});
}

// open-close plugin
jQuery.fn.OpenClose = function(_options){
	// default options
	var _options = jQuery.extend({
		activeClass:'active',
		opener:'.opener',
		slider:'.slide',
		slideSpeed: 400,
		animStart:false,
		animEnd:false,
		event:'click'
	},_options);

	return this.each(function(){
		// options
		var _holder = jQuery(this);
		var _slideSpeed = _options.slideSpeed;
		var _activeClass = _options.activeClass;
		var _opener = jQuery(_options.opener, _holder);
		var _slider = jQuery(_options.slider, _holder);
		var _animStart = _options.animStart;
		var _animEnd = _options.animEnd;
		var _event = _options.event;
		if(_slider.length) {
			_opener.bind(_event,function(){
				if(!_slider.is(':animated')) {
					if(typeof _animStart === 'function') _animStart();
					if(_holder.hasClass(_activeClass)) {
						_slider.slideUp(_slideSpeed,function(){
							if(typeof _animEnd === 'function') _animEnd();
						});
						_holder.removeClass(_activeClass);
					} else {
						_holder.addClass(_activeClass);
						_slider.slideDown(_slideSpeed,function(){
							if(typeof _animEnd === 'function') _animEnd();
						});
					}
				}
				return false;
			});
			if(_holder.hasClass(_activeClass)) _slider.show();
			else _slider.hide();
		}
	});
}
