jQuery(function(){
	initPage();
	initGallery();
})

function initGallery(){
	jQuery('.gallery').cyclingGall({
		slideEl: '.frame>ul',
		slides: '>li',
		autoSlide: false,
		duration: 700
	})
}

function initPage()
{
	clearFormFields({
		clearInputs: true,
		clearTextareas: true,
		passwordFieldText: false,
		addClassFocus: "focus",
		filterClass: "default"
	});
}

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.fn.cyclingGall = function(_opt){
	var _options = jQuery.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
	}, _opt);
	var _enabledClass = 'slide-enabled';

	return this.each(function(){
		if ($(this).hasClass(_enabledClass)) return;
		var _holder = $(this),
			_animSpeed = _options.duration,
			_duration = _options.autoSlide,
			_slideEl = jQuery(_options.slideEl, _holder),
			_slides = jQuery(_options.slides, _slideEl),
			_btNext = jQuery(_options.btNext, _holder),
			_btPrev = jQuery(_options.btPrev, _holder),
			_switcher = _options.switcher ? jQuery(_options.switcher, _holder) : false,
			_duration = _options.autoSlide,
			_activeClass = _options.activeClass,
			_direction = _options.direction,
			_frame = _options.changeHeight ? jQuery(_options.changeHeight, _holder) : false;
		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-1;
						_slideEl.css('margin-top',-_offset - _step*_active)
					}
					else if (_active > _slides.length-1) {
						_active = 0;
						_slideEl.css('margin-top',-_offset)
					}
				})
			}
			else {
				_slideEl.animate({
					'margin-left': -_step*_to - _offset
				}, _animSpeed, function(){
					_active = _to;
					if (_active < 0) {
						_active = _slides.length-1;
						_slideEl.css('margin-left',-_offset - _step*_active)
					}
					else if (_active > _slides.length-1) {
						_active = 0;
						_slideEl.css('margin-left',-_offset)
					}
				})
			}
		}
		
		if (_duration) {
			_t = setInterval(function(){
				changeSlide(_active+1)
			}, _duration)
			
			_holder.mouseenter(function(){
				if (_t) clearInterval(_t);
			}).mouseleave(function(){
				if (_t) clearInterval(_t);
				_t = setInterval(function(){
					changeSlide(_active+1)
				}, _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+1);
				}
				return false;
			})
		}
		if (_btPrev) {
			_btPrev.click(function(){
				if (!_slideEl.is(':animated')) {
					changeSlide(_active-1);
				}
				return false;
			})
		}
	});
}
