jQuery(function(){
	hideFormText();
	initPlugins();
});

function initPlugins(){
	jQuery('div.slide-show').galleryCircle({
		btnPrev: 'a.prev',
		btnNext: 'a.next',
		slidesHolder :'.gallery-holder',
		slider:'ul',
		fadeSlides:'ul.slide-list > li',
		switcher:'.gallery-holder ul li',
		autoSlide: true,
		generateSwitcher:false,
		vertical:false,
		autoHeight:false,
		switchTime: 3000,
		easing:'linear',
		duration : 700,
		pauseOnHover:true,
		asynchronous:false
	});
}

function hideFormText() {
	var _inputs = document.getElementsByTagName('input');
	var _txt = document.getElementsByTagName('textarea');
	var _value = [];
	
	if (_inputs) {
		for(var i=0; i<_inputs.length; i++) {
			if (_inputs[i].type == 'text' || _inputs[i].type == 'password') {
				
				_inputs[i].index = i;
				_value[i] = _inputs[i].value;
				
				_inputs[i].onfocus = function(){
					if (this.value == _value[this.index])
						this.value = '';
				}
				_inputs[i].onblur = function(){
					if (this.value == '')
						this.value = _value[this.index];
				}
			}
		}
	}
	if (_txt) {
		for(var i=0; i<_txt.length; i++) {
			_txt[i].index = i;
			_value['txt'+i] = _txt[i].value;
			
			_txt[i].onfocus = function(){
				if (this.value == _value['txt'+this.index])
					this.value = '';
			}
			_txt[i].onblur = function(){
				if (this.value == '')
					this.value = _value['txt'+this.index];
			}
		}
	}
}

// init circle carousel plugin
jQuery.fn.galleryCircle = function(_options){
	// defaults options
	var _options = jQuery.extend({
		btnPrev: 'a.link-prev',
		btnNext: 'a.link-next',
		slidesHolder :'.frame',
		slider:'ul',
		fadeSlides:'ul.fade > li',
		autoSlide: false,
		switcher:'div.swicher .holder .pg-holder',
		generateSwitcher:true,
		vertical:false,
		autoHeight:false,
		switchTime: 6000,
		easing:'linear',
		duration : 700,
		pauseOnHover:false,
		asynchronous:false
	},_options);

	return this.each(function(_galIndex){
		var _holder = jQuery(this);
		var _btnPrev = jQuery(_options.btnPrev, _holder);
		var _btnNext = jQuery(_options.btnNext, _holder);
		var _slidesHolder = jQuery(_options.slidesHolder, _holder);
		var _slider = jQuery(_options.slider, _slidesHolder);
		var _slides = _slider.children();
		var _slidesCount = _slides.length;
		var _slideWidth = _slides.eq(0).outerWidth(true);
		var _slideHeight = _slides.eq(0).outerHeight(true);
		var _currentIndex = 0;
		var _oldIndex = _currentIndex;
		var _animating = false;
		var _timer;
		var _sumWidth = _slidesCount*_slideWidth;
		var _sumHeight = _slidesCount*_slideHeight;
		var switcher = jQuery(_options.switcher, _holder);
		var _vertical = _options.vertical;
		var _waitAnimation = true;
		var _autoSlide = _options.autoSlide;
		var _easing = _options.easing;
		var _switchTime = _options.switchTime;
		var _speed = _options.duration;
		var _autoStopByThumbs = false;
		var _swGenerate = _options.generateSwitcher;
		var _autoHeight = _options.autoHeight;
		var _asynchronous = _options.asynchronous;
		var _fadeSlides = jQuery(_options.fadeSlides);
		var _pauseOnHover = _options.pauseOnHover;
		
		_fadeSlides.hide().eq(_currentIndex).show();
		switcher.each(function(_ind){
			jQuery(this).attr('rel', _ind);
		});
		elNum();
		_slider.append(_slides.clone()).append(_slides.clone());
		_slides = _slider.children();
		var _curr = 0;
		_slides.each(function(){
			jQuery(this).click(function(){
				var _ind = jQuery(this).attr('rel');
				if(_ind != _curr){
					_fadeSlides.fadeOut(_speed).eq(_ind).fadeIn(_speed);
					_curr = _ind;
				}
				return false;
			});
		});
		_holder.css({position:'relative'});
		if(!_vertical){
			_slider.css({
				position: 'relative',
				left: -_sumWidth,
				height:_slides.eq(0).outerHeight(true)
			});
		}else{
			_slider.css({
				position: 'relative',
				top: -_sumHeight,
				height:_slides.eq(0).outerHeight(true)
			});
		}
		_slides.each(function(i, el){
			if(!_vertical){
				_slides.eq(i).css({
					position:'absolute',
					top:0,
					left:_slideWidth*i
				});
			}else{
				_slides.eq(i).css({
					position:'absolute',
					top:_slideHeight*i,
					left:0
				});
			}
		})
		// Switcher
		function elNum(){
			if (switcher && _swGenerate) {
				var switcherEl='';
				var num=1;
				var difference=1;
				if(_swGenerate){
					while (difference <= _slides.length){
						switcherEl+='<li><a href="">'+ num + '</a></li>';
						num++;
						difference++;
					}
					switcher.html('<ul class="switcher">'+ switcherEl+'</ul>');
					if(_currentIndex!=-1){
						switcher.find('li').eq(_currentIndex).addClass('active');
					}
				}
				var link = switcher.find('a');
				link.click(function(){
					if(_currentIndex != link.index(jQuery(this))) {
						_currentIndex=link.index(jQuery(this));
						switchSlide();
					}
					if(_autoStopByThumbs && _timer!=-1) clearTimeout(_timer)
					return false;
				});
				switcher.hover(
					function(){
						if(_autoStopByThumbs && _timer!=-1 ) clearTimeout(_timer)
					},
					function(){
						if(_autoStopByThumbs){
							clearTimeout(_timer);
							autoslide();
						}
					}
				)
			}
		}
		//gallery control
		_btnPrev.click(function(){
			prevSlide();
			return false;
		});
		_btnNext.click(function(){
			nextSlide();
			return false;
		});
		// gallery animation
		function prevSlide() {
			if(_animating) return;
			_currentIndex--;
			switchSlide();
		}
		function nextSlide() {
			if(_animating) return;
			_currentIndex++;
			switchSlide();
		}

		// gallery animation
		function switchSlide(_auto){
			_animating = true;
			if(_autoHeight){_holder.animate({height: _slides.eq(_currentIndex).outerHeight(true)}, {duration:_speed, queue:false});}
			if(!_vertical){
				_slider.animate({ left:-_sumWidth-_currentIndex*_slideWidth},{duration:_speed, queue:false,complete:function(){
					if(_currentIndex == _slidesCount || _currentIndex == -_slidesCount ) {
						_currentIndex = 0;
						_slider.css({ left:-_sumWidth});
					}
					if(_currentIndex == -1  ) {
						_currentIndex=_slidesCount+_currentIndex;
						_slider.css({left:-_sumWidth-_currentIndex*_slideWidth});
					}
					_animating = false;
				}});
			}else{
				_slider.animate({ top:-_sumHeight-_currentIndex*_slideHeight},{duration:_speed, queue:false,complete:function(){
					if(_currentIndex == _slidesCount || _currentIndex == -_slidesCount ) {
						_currentIndex = 0;
						_slider.css({ top:-_sumHeight});
					}
					if(_currentIndex == -1  ) {
						_currentIndex=_slidesCount+_currentIndex;
						_slider.css({top:-_sumHeight-_currentIndex*_slideHeight});
					}
					_animating = false;
				}});
			}
			switcher.find('li').removeClass('active').eq(Math.abs(_currentIndex) == _slidesCount ? 0 : _currentIndex).addClass('active');
			autoSlide();
			autoSlide();
			if(_auto){_fadeSlides.fadeOut(_speed).eq(Math.abs(_currentIndex) == _slidesCount ? 0 : _currentIndex).fadeIn(_speed);}
		}
		function autoSlide() {
			if(!_autoSlide) return;
			if(_timer) clearTimeout(_timer);
			_timer = setTimeout(function(){
				_currentIndex++;
				switchSlide(true);
			},_switchTime);
		}
		if(_pauseOnHover){
			var _save = _autoSlide;
			_holder.hover(function(){
				if(_timer) clearTimeout(_timer);
			}, function(){
				autoSlide();
			})
		}
		if(!_asynchronous || (_asynchronous && _galIndex == 0)){
			autoSlide();
		}else{
			if(_galIndex > 0){
				setTimeout(function(){
					autoSlide();
				}, _asynchronous * _galIndex);
			}
		}
	});
}
