//jQuery(function(){
//	initPlugins();
//	initSearch();
//	$('.video-gallery').gallery({
//		listOfSlides: '.video-inner>li',
//		disableBtn: 'disable',
//		switcher: '.swicher>li'
//	});
//	initTabs();
//});




function initTabs() {
	$('ul.tabset-list').each(function(){
		var _list = $(this);
		var _links = _list.find('a.tab');
		_links.each(function() {
			var _link = $(this);
			var _href = _link.get(0).hash;
			var _tab = $(_href);

			if(_link.hasClass('active')) _tab.show();
			else _tab.hide();

			_link.click(function(e){
				_links.filter('.active').each(function(){
					$($(this).removeClass('active').get(0).hash).hide();
				});
				_link.addClass('active');
				_tab.show();
				e.preventDefault();
			});
		});
	});
}
function initPlugins(){
	clearFormFields({
		clearInputs: false,
		clearTextareas: true,
		passwordFieldText: false,
		addClassFocus: "focus",
		filterClass: "default"
	});
    //home page slideshow
//	jQuery('div.slideshow').fadeGallery({
//		slideElements:'.slideshow-holder > li',
//		pagerLinks:'.swicher li',
//		ie:false,
//		autoRotation:true,
//		switchTime:5000,
//		duration:500,
//		event:'click'
//	});
	jQuery('div.gallery').fadeGallery({
		slideElements:'div.gallery-holder > ul > li',
		btnNext:'a.more',
		ie:false,
		autoRotation:false,
		autoHeight:true,
		switchTime:5000,
		duration:500,
		event:'click'
	});
//	jQuery('.product-gallery').fadeGallery({
//		slideElements:'.holder li',
//		pagerLinks:'div.paging-items a',
//		generatePagination:'div.paging-items',
//		autoRotation:true,
//		switchTime:5000,
//		duration:800,
//		event:'click'
//     });
    jQuery('.video-gallery').gallery({
		listOfSlides: '.video-inner>li',
		disableBtn: 'disable',
		switcher: '.swicher>li'
	});
}

function initSearch(){
	jQuery('.search-box').each(function(){
		var _this = jQuery(this).css({width: 'auto'}),
			_text = jQuery('input:text', _this),
			_submit = jQuery('input:submit', _this).show(),
			_subState = _submit.width(),
			_defState = _text.width(),
			_actState,
			_duration = 500;
			_text.addClass('focus');
			_actState = _text.width();
			_text.css({width: _defState});
			_submit.css({opacity: 0, width: 0});
			_text.removeClass('focus');
		_text.focus(function(){
			_submit.animate({width: _subState}, _duration);
			_text.animate({width: _actState}, _duration, function(){
				_submit.animate({opacity: 1}, _duration);
			});
		});
		_text.blur(function(){
			_text.animate({width: _defState}, _duration);
			_submit.animate({opacity: 0}, _duration / 2, function(){
				_submit.animate({width: 0}, _duration);
			});
		});
	});
}

function clearFormFields(o){
	if (o.clearInputs == null) o.clearInputs = true;
	if (o.clearTextareas == null) o.clearTextareas = true;
	if (o.passwordFieldText == null) o.passwordFieldText = false;
	if (o.addClassFocus == null) o.addClassFocus = false;
	if (!o.filterClass) o.filterClass = "default";
	if(o.clearInputs) {
		var inputs = document.getElementsByTagName("input");
		for (var i = 0; i < inputs.length; i++ ) {
			if((inputs[i].type == "text" || inputs[i].type == "password") && inputs[i].className.indexOf(o.filterClass) == -1) {
				inputs[i].valueHtml = inputs[i].value;
				inputs[i].onfocus = function ()	{
					if(this.valueHtml == this.value) this.value = "";
					if(this.fake) {
						inputsSwap(this, this.previousSibling);
						this.previousSibling.focus();
					}
					if(o.addClassFocus && !this.fake) {
						this.className += " " + o.addClassFocus;
						this.parentNode.className += " parent-" + o.addClassFocus;
					}
				}
				inputs[i].onblur = function () {
					if(this.value == "") {
						this.value = this.valueHtml;
						if(o.passwordFieldText && this.type == "password") inputsSwap(this, this.nextSibling);
					}
					if(o.addClassFocus) {
						this.className = this.className.replace(o.addClassFocus, "");
						this.parentNode.className = this.parentNode.className.replace("parent-"+o.addClassFocus, "");
					}
				}
				if(o.passwordFieldText && inputs[i].type == "password") {
					var fakeInput = document.createElement("input");
					fakeInput.type = "text";
					fakeInput.value = inputs[i].value;
					fakeInput.className = inputs[i].className;
					fakeInput.fake = true;
					inputs[i].parentNode.insertBefore(fakeInput, inputs[i].nextSibling);
					inputsSwap(inputs[i], null);
				}
			}
		}
	}
	if(o.clearTextareas) {
		var textareas = document.getElementsByTagName("textarea");
		for(var i=0; i<textareas.length; i++) {
			if(textareas[i].className.indexOf(o.filterClass) == -1) {
				textareas[i].valueHtml = textareas[i].value;
				textareas[i].onfocus = function() {
					if(this.value == this.valueHtml) this.value = "";
					if(o.addClassFocus) {
						this.className += " " + o.addClassFocus;
						this.parentNode.className += " parent-" + o.addClassFocus;
					}
				}
				textareas[i].onblur = function() {
					if(this.value == "") this.value = this.valueHtml;
					if(o.addClassFocus) {
						this.className = this.className.replace(o.addClassFocus, "");
						this.parentNode.className = this.parentNode.className.replace("parent-"+o.addClassFocus, "");
					}
				}
			}
		}
	}
	function inputsSwap(el, el2) {
		if(el) el.style.display = "none";
		if(el2) el2.style.display = "inline";
	}
}

// slideshow plugin
jQuery.fn.fadeGallery = function(_options){
	var _options = jQuery.extend({
		slideElements:'div.slideset > div',
		pagerLinks:'div.paging a',
		generatePagination:'div.paging',
		paginationUlClass:'switcher',
		switcherClasses:'',
		switcherClassTo:'a',
		currentNumSlide:'.count .current',
		indexOfNumsSlides:'.count .indexof',
		alwaysPutZero:true,
		btnPauseToSwitcher:'false',
		btnNext:'a.next',
		btnPrev:'a.prev',
		btnPlayPause:'a.play-pause',
		btnPlay:'a.play',
		btnPause:'a.pause',
		pausedClass:'paused',
		disabledClass: 'disabled',
		playClass:'playing',
		activeClass:'active',
		loadingClass:'ajax-loading',
		loadedClass:'slide-loaded',
		dynamicImageLoad:false,
		dynamicImageLoadAttr:'alt',
		ie:false,
		currentNum:false,
		allNum:false,
		startSlide:null,
		noCircle:false,
		pauseOnHover:true,
		autoRotation:false,
		autoHeight:false,
		onInit:false,
		onBeforeFade:false,
		onAfterFade:false,
		onChange:false,
		disableWhileAnimating:false,
		switchTime:3000,
		duration:650,
		event:'click'
	},_options);

	return this.each(function(){
		// gallery options
		if(this.slideshowInit) return; else this.slideshowInit;
		var _this = jQuery(this);
		var _slides = jQuery(_options.slideElements, _this);
		var _pagerLinks = jQuery(_options.pagerLinks, _this);
		var _generatePagination = jQuery(_options.generatePagination, _this);
		var _paginationUlClass = _options.paginationUlClass;
		var _switcherClasses = _options.switcherClasses;
		var _switcherClassTo = _options.switcherClassTo;
		var _currentNumSlide = jQuery(_options.currentNumSlide, _this);
		var _indexOfNumsSlides = jQuery(_options.indexOfNumsSlides, _this);
		var _alwaysPutZero = _options.alwaysPutZero;
		var _btnPauseToSwitcher = _options.btnPauseToSwitcher;
		var _btnPrev = jQuery(_options.btnPrev, _this);
		var _btnNext = jQuery(_options.btnNext, _this);
		var _btnPlayPause = jQuery(_options.btnPlayPause, _this);
		var _btnPause = jQuery(_options.btnPause, _this);
		var _btnPlay = jQuery(_options.btnPlay, _this);
		var _pauseOnHover = _options.pauseOnHover;
		var _dynamicImageLoad = _options.dynamicImageLoad;
		var _dynamicImageLoadAttr = _options.dynamicImageLoadAttr;
		var _ie = _options.ie;
		var _autoRotation = _options.autoRotation;
		var _activeClass = _options.activeClass;
		var _loadingClass = _options.loadingClass;
		var _loadedClass = _options.loadedClass;
		var _disabledClass = _options.disabledClass;
		var _pausedClass = _options.pausedClass;
		var _playClass = _options.playClass;
		var _autoHeight = _options.autoHeight;
		var _duration = _options.duration;
		var _switchTime = _options.switchTime;
		var _controlEvent = _options.event;
		var _currentNum = (_options.currentNum ? jQuery(_options.currentNum, _this) : false);
		var _allNum = (_options.allNum ? jQuery(_options.allNum, _this) : false);
		var _startSlide = _options.startSlide;
		var _noCycle = _options.noCircle;
		var _onChange = _options.onChange;
		var _onBeforeFade = _options.onBeforeFade;
		var _onAfterFade = _options.onAfterFade;
		var _onInit = _options.onInit;
		var _disableWhileAnimating = _options.disableWhileAnimating;
		
		if(_switcherClasses){_switcherClasses = _switcherClasses.split((/\s*,\s*/));}
		
		if(_ie && _ie != true){
			if(!(/[^0-9]/i).test(_ie)){
				if(jQuery.browser.msie && parseInt(jQuery.browser.version) == _ie){
					_duration = 0;
				}
			}else if(_ie.indexOf('!') != -1){
				_ie = _ie.replace(/[^0-9]/gi, '');
				if(jQuery.browser.msie && parseInt(jQuery.browser.version) != _ie){
					_duration = 0;
				}
			}else if(_ie.indexOf('>') != -1){
				_ie = _ie.replace(/[^0-9]/gi, '');
				if(jQuery.browser.msie && parseInt(jQuery.browser.version) > _ie){
					_duration = 0;
				}
			}else{
				_ie = _ie.replace(/[^0-9]/gi, '');
				if(jQuery.browser.msie && parseInt(jQuery.browser.version) < _ie){
					_duration = 0;
				}
			}
		}else if(_ie && jQuery.browser.msie){
			_duration = 0;
		}
		
		// gallery init
		var _anim = false;
		var _hover = false;
		var _prevIndex = 0;
		var _currentIndex = 0;
		var _slideCount = _slides.length;
		var _timer;
		var _switcherIndex = 0;
		if(_slideCount < 2) return;
		
		changeNum();
		function changeNum(){
			if(_currentNumSlide.length){
				if(_alwaysPutZero == false){
					_currentNumSlide.text(_currentIndex + 1);
				}else{
					if(_currentIndex + 1 < 10){
						var curIndNum = '0' + (_currentIndex + 1);
						_currentNumSlide.text(curIndNum);
					}
				}
			}
			if(_indexOfNumsSlides){
				if(_alwaysPutZero == false){
					_indexOfNumsSlides.text(_slides.length);
				}else{
					if(_slides.length < 10){
						var curIndNum = '0' + _slides.length;
						_indexOfNumsSlides.text(curIndNum);
					}
				}
			}
		}
		
		if(_generatePagination.length) {
			var list = $('<ul class="'+_paginationUlClass+'">');
			var classTagLi = '';
			var classTagA = '';
			for(var i=0; i<_slideCount; i++){
				if(_switcherClasses.length && _switcherClassTo == 'li'){
					classTagLi = ' class="'+_switcherClasses[_switcherIndex]+'"';
				}else if(_switcherClasses.length && _switcherClassTo == 'a'){
					classTagA = ' class="'+_switcherClasses[_switcherIndex]+'"';
				}
				$('<li'+classTagLi+'><a'+classTagA+' href="#">'+(i+1)+'</a></li>').appendTo(list);
				if(_switcherIndex < _switcherClasses.length -1){_switcherIndex++;}else{_switcherIndex = 0;}
			}
			_generatePagination.empty();
			list.appendTo(_generatePagination);
			if(_switcherClassTo.toLowerCase() == 'li'){
				_pagerLinks = list.children();
			}else{
				_pagerLinks = list.children().find('a');
			}
			if(_btnPauseToSwitcher == 'true'){_btnPause = list.children();}
		}
		
		_prevIndex = _slides.index(_slides.filter('.'+_activeClass));
		if(_prevIndex < 0) _prevIndex = _currentIndex = 0;
		else _currentIndex = _prevIndex;
		if(_startSlide != null) {
			if(_startSlide == 'random') _prevIndex = _currentIndex = Math.floor(Math.random()*_slideCount);
			else _prevIndex = _currentIndex = parseInt(_startSlide);
		}
		_slides.hide().eq(_currentIndex).show();
		if(_autoRotation) _this.removeClass(_pausedClass).addClass(_playClass);
		else _this.removeClass(_playClass).addClass(_pausedClass);


		// gallery control
		if(_btnPrev.length) {
			_btnPrev.bind(_controlEvent,function(){
				prevSlide();
				return false;
			});
		}
		if(_btnNext.length) {
			_btnNext.bind(_controlEvent,function(){
				nextSlide();
				return false;
			});
		}
		if(_pagerLinks.length) {
			_pagerLinks.each(function(_ind){
				jQuery(this).bind(_controlEvent,function(){
					if(_currentIndex != _ind) {
						if(_disableWhileAnimating && _anim) return;
						_prevIndex = _currentIndex;
						_currentIndex = _ind;
						switchSlide();
					}
					return false;
				});
			});
		}

		// play pause section
		if(_btnPlayPause.length) {
			_btnPlayPause.bind(_controlEvent,function(){
				if(_this.hasClass(_pausedClass)) {
					_this.removeClass(_pausedClass).addClass(_playClass);
					_autoRotation = true;
					autoSlide();
				} else {
					_autoRotation = false;
					if(_timer) clearTimeout(_timer);
					_this.removeClass(_playClass).addClass(_pausedClass);
				}
				return false;
			});
		}
		if(_btnPlay.length) {
			_btnPlay.bind(_controlEvent,function(){
				_this.removeClass(_pausedClass).addClass(_playClass);
				_autoRotation = true;
				autoSlide();
				return false;
			});
		}
		if(_btnPause.length) {
			_btnPause.bind(_controlEvent,function(){
				_autoRotation = false;
				if(_timer) clearTimeout(_timer);
				_this.removeClass(_playClass).addClass(_pausedClass);
				return false;
			});
		}

		// dynamic image loading (swap from ATTRIBUTE)
		function loadSlide(slide) {
			if(!slide.hasClass(_loadingClass) && !slide.hasClass(_loadedClass)) {
				var images = slide.find(_dynamicImageLoad) // pass selector here
				var imagesCount = images.length;
				if(imagesCount) {
					slide.addClass(_loadingClass);
					images.each(function(){
						var img = this;
						img.onload = function(){
							img.loaded = true;
							img.onload = null;
							setTimeout(reCalc,_duration);
						}
						img.setAttribute('src', img.getAttribute(_dynamicImageLoadAttr));
						img.setAttribute(_dynamicImageLoadAttr,'');
					}).css({opacity:0});

					function reCalc() {
						var cnt = 0;
						images.each(function(){
							if(this.loaded) cnt++;
						});
						if(cnt == imagesCount) {
							slide.removeClass(_loadingClass);
							images.animate({opacity:1},{duration:_duration,complete:function(){
								if(jQuery.browser.msie && jQuery.browser.version < 9) jQuery(this).css({opacity:'auto'})
							}});
							slide.addClass(_loadedClass)
						}
					}
				}
			}
		}

		// gallery animation
		function prevSlide() {
			if(_disableWhileAnimating && _anim) return;
			_prevIndex = _currentIndex;
			if(_currentIndex > 0) _currentIndex--;
			else {
				if(_noCycle) return;
				else _currentIndex = _slideCount-1;
			}
			switchSlide();
		}
		function nextSlide() {
			if(_disableWhileAnimating && _anim) return;
			_prevIndex = _currentIndex;
			if(_currentIndex < _slideCount-1) _currentIndex++;
			else {
				if(_noCycle) return;
				else _currentIndex = 0;
			}
			switchSlide();
		}
		function refreshStatus() {
			if(_dynamicImageLoad) loadSlide(_slides.eq(_currentIndex));
			if(_pagerLinks.length) _pagerLinks.removeClass(_activeClass).eq(_currentIndex).addClass(_activeClass);
			if(_currentNum) _currentNum.text(_currentIndex+1);
			if(_allNum) _allNum.text(_slideCount);
			_slides.eq(_prevIndex).removeClass(_activeClass);
			_slides.eq(_currentIndex).addClass(_activeClass);
			if(_noCycle) {
				if(_btnPrev.length) {
					if(_currentIndex == 0) _btnPrev.addClass(_disabledClass);
					else _btnPrev.removeClass(_disabledClass);
				}
				if(_btnNext.length) {
					if(_currentIndex == _slideCount-1) _btnNext.addClass(_disabledClass);
					else _btnNext.removeClass(_disabledClass);
				}
			}
			if(typeof _onChange === 'function') {
				_onChange(_this, _slides, _prevIndex, _currentIndex);
			}
		}
		function switchSlide() {
			_anim = true;
			if(typeof _onBeforeFade === 'function') _onBeforeFade(_this, _slides, _prevIndex, _currentIndex);
			_slides.eq(_prevIndex).fadeOut(_duration,function(){
				_anim = false;
			});
			_slides.eq(_currentIndex).fadeIn(_duration,function(){
				if(typeof _onAfterFade === 'function') _onAfterFade(_this, _slides, _prevIndex, _currentIndex);
			});
			if(_autoHeight) _slides.eq(_currentIndex).parent().animate({height:_slides.eq(_currentIndex).outerHeight(true)},{duration:_duration,queue:false});
			refreshStatus();
			autoSlide();
			changeNum();
		}

		// autoslide function
		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();
			});
		}
		refreshStatus();
		autoSlide();
		
		_this.bind('setslide',function(e,h){
			if(_currentIndex != h.num) _prevIndex = _currentIndex;
			_currentIndex = h.num;
			switchSlide();
		})
	});
}
;(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: 500,
				listOfSlides: 'ul > li',
				nextBtn: 'a.link-next, a.btn-next, a.next',
				prevBtn: 'a.link-prev, a.btn-prev, a.prev',
				switcher: false,
				event: 'click'
			}, options || {});
			this.context = $(context);
			this.els = this.context.find(this.options.listOfSlides);
			this.width = this.els.outerWidth();
			this.nextBtn = this.context.find(this.options.nextBtn);
			this.prevBtn = this.context.find(this.options.prevBtn);
			this.count = this.els.index(this.els.filter(':last'));
			this.canMove = true;
			if (this.options.switcher) this.switcher = this.context.find(this.options.switcher);
			
			this.active = 0;
			if (this.active == 0) this.prev = this.count;
			else this.prev = this.active-1;
			
			this.els.css({marginLeft:-this.width}).eq(this.active).addClass('active').css({marginLeft:0});
			
			if (this.options.switcher) this.initEventSwitcher(this, this.switcher);
		},
		scrollElement: function($this,side,speed){
			if (side) {
				this.els.eq(this.active).css({
					marginLeft:this.width
				}).animate({
					marginLeft:0
				},{
					queue:false,
					duration:$this.options.duration,
					complete:function(){
						$this.els.removeClass('active').eq($this.active).addClass('active');
						$this.canMove = true;
					}
				});
				this.els.eq(this.prev).animate({
					marginLeft:-this.width
				},{
					queue:false,
					duration:$this.options.duration
				});
			} else {
				this.els.eq(this.active).css({
					marginLeft: -this.width
				}).animate({
					marginLeft:0
				},{
					queue:false,
					duration:$this.options.duration,
					complete:function(){
						$this.els.removeClass('active').eq($this.active).addClass('active');
						$this.canMove = true;
					}
				});
				this.els.eq(this.prev).animate({
					marginLeft:this.width
				},{
					queue:false,
					duration:$this.options.duration
				});
			}
		},
		initEventSwitcher: function($this, el){
			el.bind($this.options.event, function(){
				if ($this.canMove) {
					$this.canMove = false;
					$this.prev = $this.active;
					$this.active = $this.switcher.index($(this));
					$this.switcher.removeClass('active').eq($this.active).addClass('active');
					if ($this.active == $this.prev) $this.canMove = true;
					if ($this.active > $this.prev) {
						$this.scrollElement($this,true,true);
					}
					if ($this.active < $this.prev) {
						$this.scrollElement($this,false,true);
					}
				}
				return false;
			});
		}
	}
}(jQuery));
