$(window).load(function(){
	initPopups();
	initHomeGallery();
	//initTooltips()
	//initGallery();
	//initZoom();
});

function initZoom(){
	var _zClass = 'z-active';
	var _animSpeed = 750;
	$('.product-holder:has(.zoom-area)').each(function(){
		var _holder = $(this);
		var _zoomHold = $('.product-visual', _holder);
		var _zoom = $('.zoom-area', _holder);
		var _zoomBtn = $('.zoom-link', _zoom);
		var _img = $('.slide', _zoom);
		var _toFade = $('.info', _holder);
		var _gallery = $('.p-gallery', _holder);
		var _thumb_a=$('li a',_gallery);
		var _thumb_prev=$('a.prev',_gallery);
		var _thumb_next=$('a.next',_gallery);

		var _zoomW = _zoom.width();
		var _zoomH = _zoom.height();
		var _width = _holder.width();
		var _k = _width/_zoomW;
		var _imgH = _img.height();
		var _galleryW = _gallery.outerWidth();
		function zoomIn(){
			_toFade.fadeOut(_animSpeed, function(){
				_zoomHold.animate({
					'width':_width
				}, _animSpeed)
				_holder.addClass(_zClass);
				_img.animate({
					'width':'100%',
					'height':_imgH * _k
				}, _animSpeed);
				_gallery.animate({
					'margin-left': (_width - _galleryW)/2
				}, _animSpeed)
			});
		}
		function zoomOut(){
			_zoomHold.animate({
				'width':_zoomW
			}, _animSpeed, function(){_toFade.fadeIn(_animSpeed)});
			_gallery.animate({
				'margin-left': 0
			}, _animSpeed);
			_img.animate({
				'width':'100%',
				'height':_imgH
			}, _animSpeed);
			_holder.removeClass(_zClass);
		}

		_zoom.click(function(){
			if (!_holder.hasClass(_zClass) && !_zoomHold.is(':animated')){
				zoomIn();
			}
			return false;
		})
		_zoomBtn.click(function(){
			if (!_holder.hasClass(_zClass) && !_zoomHold.is(':animated')){
				zoomIn();
			}
			else if (_holder.hasClass(_zClass) && !_zoomHold.is(':animated')){
				zoomOut();
			}
			return false;
		})
		
		_thumb_a.each(function(){
		    var _a=$(this);
		    _a.click(function(){
		        _a.attr("href","javascript:void(0);");
		        _img.hide().attr("src",_a.attr("rel")).fadeIn();
		    });		    
		});
		
		var _pi=0;
		var _itemWidth=_thumb_a.eq(0).parent().width()+parseInt(_thumb_a.eq(0).parent().css("paddingLeft"))+parseInt(_thumb_a.eq(0).parent().css("paddingRight"));
		var _sumCount=_thumb_a.size();
		var _scrollCount=5;
		var _screenCount=5;
		var _thumb_hold=$('ul',_gallery);
		
		if(_sumCount>_screenCount)
		{
		    _thumb_next.click(function(){
		        $(this).attr("href","javascript:void(0);");
    		    _pi=_pi+_screenCount+_scrollCount>_sumCount?_sumCount-5:_pi+_scrollCount;
    		    _thumb_hold.animate({marginLeft:-_pi*_itemWidth+'px'},{duration:1000});
		    });
		
		
		    _thumb_prev.click(function(){
		        $(this).attr("href","javascript:void(0);");
		        _pi=_pi-_scrollCount<0?0:_pi-_scrollCount;
    		    _thumb_hold.animate({marginLeft:-_pi*_itemWidth+'px'},{duration:1000});
		    });
		
		}
		
	})
}


// custom (detail gallery code)
function initCustomGallery() {
	// settings
	var _autoSlide = true;
	var _waitAnimation = true;
	var _easing = 'swing';
	var _activeClass = 'active';
	var _switchTime = 12000;
	var _tagBoxDelay = 300;
	var _speed = 1000;

	$('div.product-box, div.intro').each(function(){
		// gallery options
		var _holder = $(this);
		var _pagerLinks = _holder.find('div.swicher li');
		var _btnPrev = _holder.find('a.in-prev');
		var _btnNext = _holder.find('a.in-next');
		var _slidesHolder = _holder.find('div.ig-box');
		var _slider = _slidesHolder.find('>ul');
		var _slides = _slider.children();
		var _slidesCount = _slides.length;
		var _slideWidth = _slides.eq(0).outerWidth(true);
		var _visibleCount = Math.round(_slidesHolder.width() / _slideWidth);
		var _currentIndex = 0;
		var _oldIndex = _currentIndex;
		var _animating = false;
		var _direction;
		var _timer;
		
		_autoSlide=_slidesCount>1;
		if(_slidesCount<=1)
		{
		    _btnPrev.hide();
		    _btnNext.hide();
		    _holder.find('div.swicher').hide();
		}

		// slider height
		_holder.css({position:'relative'});
		_slider.css({height:_slides.eq(0).outerHeight(true)});
		_slides.show().css({position:'absolute',top:0,left:_slideWidth});
		_slides.eq(_currentIndex).css({left:0});

		// gallery control
		_pagerLinks.each(function(_ind){
			$(this).click(function(){
				if(_animating) return false;
				if(_ind != _currentIndex) {
					_oldIndex = _currentIndex;
					_currentIndex = _ind;
					_direction = (_oldIndex < _currentIndex);
					switchSlide();
				}
				return false;
			});
		});
		_btnPrev.click(function(){
			prevSlide();
			return false;
		});
		_btnNext.click(function(){
			nextSlide();
			return false;
		});

		// hover effect
		var _hoverSpeed = 400;
		_holder.find('a.in-next, a.in-prev').each(function(){
			var _btn = $(this);
			var _d = _btn.find('span'); $('<em>').appendTo(_btn);
			var _h = _btn.find('em').css({display:'block',opacity:0, zIndex:1});
			_btn.hover(function(){
				_h.animate({opacity:1},{duration:_hoverSpeed,queue:false});
				_d.animate({opacity:0},{duration:_hoverSpeed,queue:false});
			},function(){
				_h.animate({opacity:0},{duration:_hoverSpeed,queue:false});
				_d.animate({opacity:1},{duration:_hoverSpeed,queue:false});
			})
		})

		// gallery functions
		function prevSlide() {
			if(_animating) return;
			_oldIndex = _currentIndex;
			if(_currentIndex > 0) _currentIndex--;
			else _currentIndex = _slidesCount-1;
			_direction = false;
			switchSlide();
		}
		function nextSlide() {
			if(_animating) return;
			_oldIndex = _currentIndex;
			if(_currentIndex < _slidesCount-1) _currentIndex++;
			else _currentIndex = 0;
			_direction = true;
			switchSlide();
		}

		// gallery animation
		function refreshStatus() {
			_pagerLinks.removeClass(_activeClass).eq(_currentIndex).addClass(_activeClass);
		}
		
		function switchSlide() {
			if(_waitAnimation) _animating = true;
			var _tagBox = _slides.eq(_currentIndex).find('.tag');
			if(_tagBox.length) _tagBox.css({right:-_tagBox.width()})

			_slides.eq(_currentIndex).css({left:(_direction ? _slideWidth : -_slideWidth)}).animate({left:0},{duration:_speed, queue:false,easing:_easing,complete:function(){
				if(_tagBox.length) setTimeout(function(){_tagBox.animate({right:($.browser.msie && $.browser.version < 7 ? -1 : 0)},{duration:_slideWidth})},_tagBoxDelay);
				_animating = false;
			}});
			_slides.eq(_oldIndex).animate({left:(_direction ? -_slideWidth : _slideWidth)},{duration:_speed, queue:false,easing:_easing});
			refreshStatus();
			autoSlide();
		}
		function autoSlide() {
			if(!_autoSlide) return;
			if(_timer) clearTimeout(_timer);
			_timer = setTimeout(nextSlide,_switchTime);
		}
		refreshStatus();
		autoSlide();
	});
}

function initSlideDrop(){
	var duration = 500;
	$('ul.product-nav > li').each(function(){
		var li = $(this);
		var drop = $('div.drop',li);
		var slide = $('> div',drop);
		var slideW = drop.show().outerWidth();
		drop.hide();
		slide.css({marginLeft:-slideW});
		
		li.mouseenter(function(){
			drop.show();
			slide.stop().animate({marginLeft:0},duration);
		}).mouseleave(function(){
			slide.stop().animate({marginLeft:-slideW},duration,function(){
				drop.hide();
			});
		})
	});	
}

function initSlideDrop2(){
	var duration = 500;
	$('ul.sub-nav > li').each(function(){
		var li = $(this);
		var drop = $('div.drop',li);
		var slide = $('> div',drop);
		var slideW = drop.show().outerWidth();
		drop.hide();
		slide.css({marginLeft:-slideW});
		
		li.mouseenter(function(){
			drop.show();
			slide.stop().animate({marginLeft:0},duration);
		}).mouseleave(function(){
			slide.stop().animate({marginLeft:-slideW},duration,function(){
				drop.hide();
			});
		})
	});	
}

function initPopups(){
	$('a.with-popup').each(function(){
		$(this).pvlightbox();
	});
}

function initTooltips(){
	var duration = 350;
	$('a.with-tooltip').each(function(){
		var link = $(this);
		var tooltip = $(link.attr('href')).css({opacity:0});
		link.mouseenter(function(){
			tooltip.css({display:'block'}).stop().animate({opacity:1},duration);		
		}).mouseleave(function(){
			tooltip.stop().animate({opacity:0},duration,function(){
				tooltip.css({display:'none'});
			})
		});
	})
}

function initGallery(){
	$('div.blog-gallery').scrollGallery();
	$('div.category-box').scrollGallery({
		sliderHolder: 'div.overflower',
		duration:1500
	})
}

// scrolling gallery plugin
jQuery.fn.scrollGallery = function(_options){
	var _options = jQuery.extend({
		sliderHolder: 'div.bg-box',
		slider:'>ul',
		slides: '>li',
		pagerLinks:'div.pager a',
		btnPrev:'a.btn-prev',
		btnNext:'a.btn-next',
		activeClass:'active',
		disabledClass:'disabled',
		generatePagination:'div.pg-holder',
		curNum:'em.scur-num',
		allNum:'em.sall-num',
		circleSlide:true,
		pauseClass:'gallery-paused',
		pauseButton:'none',
		pauseOnHover:true,
		autoRotation:true,
		stopAfterClick:false,
		switchTime:7000,
		duration:650,
		easing:'swing',
		event:'click',
		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 _btnPrev = jQuery(_options.btnPrev, _this);
		var _btnNext = jQuery(_options.btnNext, _this);
		var _pagerLinks = jQuery(_options.pagerLinks, _this);
		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;

		// gallery init
		if(!_slides.length) return;
		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(_generatePagination.length) {
			_generatePagination.empty();
			recalcOffsets();
			var _list = $('<ul />');
			for(var i=0; i<_stepCount; i++) $('<li><a href="#">'+(i+1)+'</a></li>').appendTo(_list);
			_list.appendTo(_generatePagination);
			_pagerLinks = _list.children();
		}
		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);
	});
}

//lightbox
$.fn.pvlightbox = function(options){return new pvlightbox($(this).eq(0),options)}
function pvlightbox(link, options) {this.init(link,options)}
pvlightbox.prototype = {
	init:function(link,options){
		var el = this;
		//options
		el.options = $.extend({
			fadeSpeed:400,
			closer:'a.btn-close,a.close',
			scroll:false,
			wrapper:false,
			IE:false,
			zIndex:1000
		},options);	
		//popup & default css styles
		if ($.browser.msie && el.options.IE) el.popup = $(link.attr('href')).css({visibility:'hidden'})
		else el.popup = $(link.attr('href')).css({opacity:0,visibility:'hidden'});
		if (el.options.zIndex) el.popup.css({zIndex : el.options.zIndex});
		el.closer = $(el.popup.find(el.options.closer));
		el.popup.visible = false;
		pvlightbox.prototype.activePopup = false;
		if (!pvlightbox.prototype.firstRun) {
			pvlightbox.prototype.firstRun = 'done'
			if ($.browser.msie && $.browser.version < 7) pvlightbox.prototype.selects = $('select',document);
			//create fader
			if (!$('#fader').length) {
				$('body').append('<div id="fader"></div>');
				pvlightbox.prototype.fader = $('#fader');
				pvlightbox.prototype.fader.css({position:'absolute',top:0,left:0,background:'#000',opacity:0,display:'none'});	
				if (el.options.zIndex) pvlightbox.prototype.fader.css({zIndex : el.options.zIndex-1});
			}
			//fader click event
			pvlightbox.prototype.fader.click(function(){
				if (pvlightbox.prototype.activePopup == false) el.hideFader()
				else pvlightbox.prototype.activePopup.hidePopup(function(){el.hideFader()});
				return false;
			});
			//esc event
			$(document).keydown(function (e) {
				if (e.keyCode == 27) {
					if (pvlightbox.prototype.activePopup == false) el.hideFader()
					else pvlightbox.prototype.activePopup.hidePopup(function(){el.hideFader()});
					return false;
				}
			});
			//minWidth
			pvlightbox.prototype.minWidth = $('body').css('min-width');
			if (pvlightbox.prototype.minWidth == '0px' || typeof(pvlightbox.prototype.minWidth) == 'undefined') {
				if (el.options.wrapper) {
					pvlightbox.prototype.wrapper = $(el.options.wrapper);
					pvlightbox.prototype.minWidth = $(el.options.wrapper).outerWidth();
				}
			}
			pvlightbox.prototype.minWidth = parseInt(pvlightbox.prototype.minWidth);
		}
		if ($.browser.msie && $.browser.version < 7) {
			el.popupSelects = $('select',el.popup);
			pvlightbox.prototype.selects = pvlightbox.prototype.selects.not(el.popupSelects);
		}
		//open event
		link.click(function(){
			if (pvlightbox.prototype.activePopup == el) {return false;}
			if (pvlightbox.prototype.activePopup) {
				pvlightbox.prototype.activePopup.hidePopup(function(){
					el.showPopup()
					el.positioning(true);
				});
			} else {
				el.showFader(function(){el.showPopup()});
				el.positioning(true);
			} 
			return false;
		});
		//close event
		el.closer.click(function(){
			el.hidePopup(function(){el.hideFader()});
			return false;
		});
		//resize event
		$(window).resize(function(){
			if (el.popup.visible) el.positioning(false);
		});
		if (el.options.scroll) {
			$(window).scroll(function(){
				el.positioning(true);
			});
		}
	},
	showPopup:function(){
		var el = this;
		el.popup.visible = true;
		pvlightbox.prototype.activePopup = el;
		if ($.browser.msie && el.options.IE) el.popup.css({visibility:'visible'})
		else el.popup.stop().css({'visibility':'visible'}).animate({opacity:1},el.options.fadeSpeed)
	},
	hidePopup:function(callback){
		var el = this;
		if ($.browser.msie && el.options.IE) {
			el.popup.css({left:'-9999px',top:'-9999px',visibility:'hidden'});
			el.popup.visible = false;
			pvlightbox.prototype.activePopup = false;
			if ($.isFunction(callback)) callback();
		} else {
			el.popup.stop().animate({opacity:0},el.options.fadeSpeed,function(){
				el.popup.css({left:'-9999px',top:'-9999px',visibility:'hidden'});
				el.popup.visible = false;
				pvlightbox.prototype.activePopup = false;
				if ($.isFunction(callback)) callback();
			});
		}
	},
	showFader:function(callback){
		var el = this;
		el.fader.stop().css({display:'block'}).animate({opacity:0.5},el.options.fadeSpeed,function(){
			if ($.isFunction(callback)) callback();
		});
		if ($.browser.msie && $.browser.version < 7) pvlightbox.prototype.selects.css({'visibility': 'hidden'});
	},
	hideFader:function(){
		var el = this;
		el.fader.stop().animate({opacity:0},el.options.fadeSpeed,function(){
			el.fader.css({display:'none'});
			if ($.browser.msie && $.browser.version < 7) pvlightbox.prototype.selects.css({'visibility': 'visible'});
		});
	},
	positioning:function(openFlag){
		var el = this;
		//x offset
		var windowW = $(window).width();
		var popupW = el.popup.outerWidth();
		if (windowW < pvlightbox.prototype.minWidth) {
			el.popup.css({left:pvlightbox.prototype.minWidth/2-popupW/2})
			el.fader.css({width:pvlightbox.prototype.minWidth});
		} else {
			 el.popup.css({left:windowW/2-popupW/2});
			 el.fader.css({width:windowW})
		}
		//y offset
		var docH = $('body').height();
		var popupH = el.popup.outerHeight();
		if (openFlag) {
			var windowH = $(window).height();
			var popupH = el.popup.outerHeight();
			if (popupH < windowH) el.popup.css({top:windowH/2-popupH/2+$(window).scrollTop()});
			else if ($(window).scrollTop()+popupH > docH){
				el.popup.css({top:docH-popupH});
			} else {
				el.popup.css({top:$(window).scrollTop()});
			}
		}
		//el.fader.css({height:docH});
		el.fader.css({height:$(document).height()});
	}
}







function initHomeGallery(){
	// settings
	var _waitAnimation = true;
	var _autoSlide = true;
	var _easing = 'linear';
	var _activeClass = 'active';
	var _loadingClass = 'ajax-loading';
	var _loadedClass = 'loaded';
	var _switchTime = 5000;
	var _speed = 600;

	$('div.slideshow').each(function(){
		// gallery options
		var _holder = $(this);
		var _btnPrev = _holder.find('a.prev');
		var _btnNext = _holder.find('a.next');
		var _slidesHolder = _holder.find('div.main');
		var _slider = _slidesHolder.find('>ul');
		var _slides = _slider.children();
		var _slidesCount = _slides.length;
		var _slideWidth = _slides.eq(0).outerWidth(true);
		var _currentIndex = 0;
		var _oldIndex = _currentIndex;
		var _animating = false;
		var _timer;
		var _navPanel = _holder.find('.controls');
		var _btnPlay = _navPanel.find('.pause');
		var _thumbs=_holder.find('.thumbs').eq(0).children();
		var _dynamicImageLoad = 'img:eq(0)';
		var _dynamicImageLoadAttr = 'alt';
		var _txt;
		// slider height
		_holder.css({position:'relative'});
		_slider.css({height:_slides.eq(0).outerHeight(true)});
		_slides.hide().css({position:'absolute',top:0,left:0});
		_slides.eq(_currentIndex).show();
		//User panel
		if(_navPanel.length){
			var _animTxt = true;
			_navPanel.css({
				bottom: 0
			})
			function animPanel(){
				if(_animTxt){
					_navPanel.animate({
						bottom: 0
					}, { queue: false, duration: _speed })
				}
				else {
					_navPanel.animate({
						bottom: -_navPanel.outerHeight(true)
					}, { queue: false, duration: _speed, complete: function(){
						switchText();
					} })
				}
			}
		}
		function prevSlide() {
			if(!_animating) {
				_oldIndex = _currentIndex;
				if(_currentIndex > 0) _currentIndex--;
				else _currentIndex = _slidesCount-1;
				switchSlide();
			}
			return false;
		}
		function nextSlide() {
			if(!_animating) {
				_oldIndex = _currentIndex;
				if(_currentIndex < _slidesCount-1) _currentIndex++;
				else _currentIndex = 0;
				switchSlide();
			}
			return false;
		}
		_btnPrev.click(prevSlide);
		_btnNext.click(nextSlide);
		_thumbs.each(function(){
		    var _item=$(this);
		    var _index=parseInt(_item.attr("rel"));
		    if(!_animating)
		    {
		        _item.click(function(){
		            _oldIndex = _currentIndex;
		            _currentIndex=_index;
		            switchSlide();
		        });
		    }
		});
		//Buttin pause-play
		//Buttin pause-play
		if( _btnPlay.length ){
			_btnPlay.bind('click', function(){
				if(_autoSlide){
					if(_timer) clearTimeout(_timer);
					_autoSlide = false;
					$(this).removeClass('pause').addClass('play');
				}
				else {
					_autoSlide = true;
					$(this).removeClass('play').addClass('pause');
					autoSlide();
				}
				return false;
			})
		}

		// gallery animation
		function refreshStatus() {
			//if(_dynamicImageLoad) loadSlide(_slides.eq(_currentIndex));
		}
		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,_speed);
						}
						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:_speed,complete:function(){
								_animTxt = true;
								animPanel();
								if(jQuery.browser.msie && jQuery.browser.version < 9) jQuery(this).css({opacity:'auto'})
							}});
							slide.addClass(_loadedClass)
						}
					}
				}
			}
		}
		//Text box include
		//var _txtWrap = _holder.find('.descr:eq(0)');
		function switchText(){
//			_txt = _slides.eq(_currentIndex).find('div.txt').html();
//			_txtWrap.html(_txt)
		}
		switchText();
		function switchSlide(){
			if(_waitAnimation) _animating = true;
			_animTxt = false;
			animPanel();
			_slides.eq(_currentIndex).fadeIn(_speed, function(){
				_animating = false;
				_animTxt = true;
				animPanel();
			})
			_slides.eq(_oldIndex).fadeOut(_speed);
			refreshStatus();
			autoSlide();
		}
		function autoSlide(){
			if(!_autoSlide) return;
			if(_timer) clearTimeout(_timer);
			_timer = setTimeout(nextSlide,_switchTime);
		}
		refreshStatus();
		
		
		//if slide
		if(_slidesCount<=1)
		{
		    //_navPanel.find("div.links").hide();
		}
		else
		{
		    autoSlide();
		}
		
	});
}

