;(function($){
$(function(){
	clearInputs();
	initGallery();
	initFooterCarousel();
});

function initFooterCarousel(){
	$('.carousel').each(function(){
		var holder = $(this);
		var frame = $('.frame', holder);
		$('<a class="next" href="#">next</a>').appendTo(holder);
		$('<a class="prev" href="#">prev</a>').prependTo(holder);
	})
}

$.fn.cyclingGall = function(_opt){
	var _options = $.extend({
		slideEl: '>ul',
		slides: '>li',
		switcher: false,
		autoSlide: 7000,
		duration: 700,
		btPrev: '.btn-prev, .prev, .prev-btn, .btn-up, .up-btn, .link-prev',
		btNext: '.btn-next, .next, .next-btn, .btn-down, .down-btn, .link-next',
		direction: false,
		activeClass: 'active',
		changeHeight: false,
		step: false
	}, _opt);
	var _enabledClass = 'slide-enabled';

	return this.each(function(){
		if ($(this).hasClass(_enabledClass)) return;
		var _holder = $(this),
			_animSpeed = _options.duration,
			_duration = _options.autoSlide,
			_slideEl = $(_options.slideEl, _holder),
			_slides = $(_options.slides, _slideEl),
			_btNext = $(_options.btNext, _holder),
			_btPrev = $(_options.btPrev, _holder),
			_switcher = _options.switcher ? $(_options.switcher, _holder) : false,
			_duration = _options.autoSlide,
			_activeClass = _options.activeClass,
			_direction = _options.direction,
			_frame = _options.changeHeight ? $(_options.changeHeight, _holder) : false,
			_slidesStep = _options.step ? _options.step : 1;
		var _step = _slides.eq(0).outerWidth(true);
		var _active = 0;
		var _t;
		
		if (!_direction){
			var _offset = _step * (_slides.length);
			_slideEl.css('margin-left', -_offset);
			_slides.clone().appendTo(_slideEl);
			_slides.clone().appendTo(_slideEl);
		}
		else {
			_step = _slides.eq(0).outerHeight(true);
			var _offset = _step * (_slides.length);
			_slideEl.css('margin-top', -_offset);
			_slides.clone().appendTo(_slideEl);
			_slides.clone().appendTo(_slideEl);
		}
		if (_frame){
			if (_frame.height() != _slides.eq(_active).height()) _frame.css({
				'height': _slides.eq(_active).height()
			})
		}
			
		function changeSlide(_to){
			if (_switcher) {
				_switcher.eq(_active).removeClass(_activeClass);
				if (_to < 0) _switcher.eq(_slides.length-1).addClass(_activeClass);
				else if (_to > _slides.length-1) _switcher.eq(0).addClass(_activeClass);
				else _switcher.eq(_to).addClass(_activeClass);
			}
			if (_frame){
				var _toSl = _to;
				if (_to < 0) _toSl = _slides.length-1
				else if (_to > _slides.length-1) _toSl = 0;
				if (_frame.height() != _slides.eq(_toSl).height()) _frame.animate({
					'height': _slides.eq(_toSl).height()
				}, _animSpeed)
			}
			
			if (_direction){
				_slideEl.animate({
					'margin-top': -_step*_to - _offset
				}, _animSpeed, function(){
					_active = _to;
					if (_active < 0) {
						_active = _slides.length + _active;
						_slideEl.css('margin-top',-_offset - _step*_active)
					}
					else if (_active > _slides.length-1) {
						_active = 0 + _active - _slides.length;
						_slideEl.css('margin-top',-_offset)
					}
				})
			}
			else {
				_slideEl.animate({
					'margin-left': -_step*_to - _offset
				}, _animSpeed, function(){
					_active = _to;
					if (_active < 0) {
						_active = _slides.length + _active;
						_slideEl.css('margin-left',-_offset - _step*_active)
						
					}
					else if (_active > _slides.length-1) {
						_active = 0 + _active - _slides.length;
						_slideEl.css('margin-left',-_offset - _step*_active)
					}
				})
			}
		}
		
		if (_duration) {
			_t = setInterval(function(){
				changeSlide(_active+_slidesStep)
			}, _duration)
			
			_holder.mouseenter(function(){
				if (_t) clearInterval(_t);
			}).mouseleave(function(){
				if (_t) clearInterval(_t);
				_t = setInterval(function(){
					changeSlide(_active+_slidesStep)
				}, _duration)
			})
		}
		
		if (_switcher) {
			_switcher.click(function(){
				if (!_slideEl.is(':animated')) {
					changeSlide(_switcher.index($(this)));
				}
				return false;
			})
		}
		
		if (_btNext) {
			_btNext.click(function(){
				if (!_slideEl.is(':animated')) {
					changeSlide(_active+_slidesStep);
				}
				return false;
			})
		}
		if (_btPrev) {
			_btPrev.click(function(){
				if (!_slideEl.is(':animated')) {
					changeSlide(_active-_slidesStep);
				}
				return false;
			})
		}
	});
}

function initGallery(){
	var _animSpeed = 600;
	var _speed = _animSpeed;
	//horizontal gallery
	$('.gallery').each(function(){
		var _holder = $(this);
		var _slider = $('.frame ul', _holder);
		var _slides = _slider.children();
		var _step = _slides.eq(0).outerWidth();
		var _width = _slides.eq(0).width();
		var _height = _slides.eq(0).height();
		var _oW = _slides.eq(0).outerWidth();
		var _top = parseInt(_slider.css('padding-top'));
		var _padding = parseInt(_slides.eq(0).css('padding-right'));
		var _prev = $('.next', _holder);
		var _next = $('.prev', _holder);
		var _t;
		var _timeout = 5000;
		var _hover = false;
		
		if (_slides.length < 3) return;
		var _offset = _slides.length * _step;
		_slider.append(_slides.clone());
		_slider.prepend(_slides.clone());
		var numOffset = _slides.length;
		_slides = _slider.children();
		var _curSlide = 0 + numOffset;
		var _active = _curSlide + 1;
		
		function moveGall(){
			var _pos = (_curSlide - numOffset) * _step;
			_slider.animate({
				'margin-left': -_pos - _offset
			}, _animSpeed, function(){
				if (_curSlide > _slides.length - numOffset-1){
					_curSlide = numOffset;
					_active = _curSlide+1;
					resizeActive();
					setGall();
				}
				else if (_curSlide < numOffset) {
					_curSlide = _slides.length - numOffset-1;
					_active = _curSlide+1;
					resizeActive();
					setGall();
				}
				if (_t) clearTimeout(_t);
				autoRotation();
			});
		}
		
		function setGall(){
			var _pos = (_curSlide - numOffset) * _step;
			_slider.css({
				'margin-left': -_pos - _offset
			});
		}
		
		_prev.click(function(){
			if (!_slider.is(':animated')) {
				_curSlide--
				_new = _curSlide+1;
				moveGall();
				setActive();
			}
		})
		_next.click(function(){
			if (!_slider.is(':animated')) {
				_curSlide++
				_new = _curSlide+1;
				moveGall();
				setActive();
			}
		})

		//set active
		_slides.each(function(ind){
			var _slide = $(this);
			this.left = _slide.position().left;
			this.link = _slide.find('>a:eq(0)');
		})
		function resizeActive(){
			$('.extra', _slider).stop().remove();
			_slides.each(function(ind){
				var _slide = $(this);
				_slide.stop().removeAttr('style');
				this.link.stop().removeAttr('style');
				if (ind == _active) {
					_slide.css({
						'position':'absolute',
						'top':0,
						'left':_slides.eq(_active).get(0).left - _width*0.5 - _padding*2,
						'z-index':100,
						'width': _width*1.4,
						'height': _height*1.4
					})
					_slide.get(0).link.css({
						'width': _width*1.4,
						'height': _height*1.4
					})
				}
			})
			
		}
		
		function setActive(){
			var _extra = false;
			var _offset = 0;
			var _offsetA = 0;
			
			if (!_new) {
				_speed = _animSpeed;
				_animSpeed = 0;
				_extra = $('<li class="extra">').insertAfter(_slides.eq(_active)).css({
					'width': 0,
					'padding-right': 0
				}).animate({
					'width':_width,
					'padding-right':_padding
				}, _animSpeed)
				hideCurrent();
			}
			else if (!_active) {
				_extra = $('<li class="extra">').insertAfter(_slides.eq(_new)).css({
					'width': _width,
					'padding-right': _padding
				}).animate({
					'width':0,
					'padding-right':0
				}, _animSpeed)
				showActive();
			}
			else {
				if (_new > _active) {
					_offset = -_width - _padding
				}
				else _offsetA = -_width - _padding
				_extra = $('<li class="extra">').insertAfter(_slides.eq(_new))
				hideCurrent();
				showActive();
			}
			
			function showActive(){
				_slides.eq(_new).css({
					'position':'absolute',
					'top':_top,
					'left':_slides.eq(_new).get(0).left + _offset,
					'z-index':100
				})
				_slides.eq(_new).animate({
					'top':0,
					'left':_slides.eq(_new).get(0).left - _width*0.5 - _padding*2,
					'width': _width*1.41,
					'height': _height*1.41
				}, _animSpeed, function(){
					if (_extra) _extra.remove();
					_active = _new;
				})
				_slides.eq(_new).get(0).link.animate({
					'width': _width*1.4,
					'height': _height*1.4
				},_animSpeed)
			}
			
			function hideCurrent(){
				_slides.eq(_active).css('z-index',90).animate({
					'top':_top,
					'left':_slides.eq(_active).get(0).left + _offsetA,
					'width': _width,
					'height': _height
				},_animSpeed, function(){
					if (_extra) _extra.remove();
					_slides.eq(_active).removeAttr('style');
				});
				_slides.eq(_active).get(0).link.animate({
					'width': _width,
					'height': _height
				},_animSpeed);
			}
		}
		
		function autoRotation(){
			if (!_hover) {
				if (_t) clearTimeout(_t);
				_t = setTimeout(function(){
					_next.trigger('click');
				}, _timeout)
			}
		}
		
		_holder.mouseenter(function(){
			_hover = true;
			if (_t) clearTimeout(_t);
		}).mouseleave(function(){
			_hover = false;
			if (_t) clearTimeout(_t);
			_t = setTimeout(function(){
				_next.trigger('click');
			}, _timeout)
		})
		
		setGall();
		resizeActive();
		autoRotation();
		
		jQuery(window).focus(function(){
			_hover = true;
			if (_t) clearTimeout(_t);
			_t = setTimeout(function(){
				_next.trigger('click');
			}, _timeout)
		}).blur(function(){
			_hover = false;
			if (_t) clearTimeout(_t);
		})
	})
}
// clear inputs
function clearInputs()
{
	clearFormFields({
		clearInputs: true,
		clearTextareas: true,
		passwordFieldText: true
	});
}
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";
	}
}
})(jQuery);
