$(document).ready(function(){
	$('.gallery').gallery({
		duration: 500,
		autoRotation: 5000,
		listOfSlides: '.frame > ul > li',
		switcher: '.switcher > li'
	});
});
(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: 700,
				listOfSlides: 'ul > li',
				autoRotation: false,
				nextBtn: 'a.link-next, a.btn-next, a.next',
				prevBtn: 'a.link-prev, a.btn-prev, a.prev',
				disableBtn:false,
				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'));
			
			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.initEvent(this, this.nextBtn, this.prevBtn, true);
			this.initEvent(this, this.prevBtn, this.nextBtn, false);
			
			this.els.css({marginLeft:-this.width}).eq(this.active).addClass('active').css({marginLeft:0});
			
			if  (this.options.disableBtn && this.active == 0) this.prevBtn.addClass(this.options.disableBtn);
			this.nextBtn.removeClass(this.options.disableBtn);
			
			if (this.options.switcher) this.initEventSwitcher(this, this.switcher);
			if (this.options.autoRotation) this.runTimer(this);
		},
		calcPrev: function(){
			if (this.active == 0) this.prev = this.count;
			else this.prev = this.active-1;
		},
		runTimer: function($this){
			if($this._t) clearTimeout($this._t);
			$this._t = setInterval(function(){
				$this.toPrepare(true);
			}, this.options.autoRotation);
		},
		scrollElement: function($this,side){
			if (side) {
				this.els.removeClass('current').eq(this.active).addClass('current').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.els.eq($this.prev).animate({
							marginLeft:-$this.width
						},{
							queue:false,
							duration: $this.options.duration
						});
					}
				});
			} else {
				this.els.removeClass('current').eq(this.active).addClass('current').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.els.eq($this.prev).animate({
							marginLeft:$this.width
						},{
							queue:false,
							duration: $this.options.duration
						});
					}
				});
			}
			if (this.options.switcher) this.switcher.removeClass('active').eq(this.active).addClass('active');
		},
		initEvent: function($this, addEventEl, addDisClass, dir){
			addEventEl.bind($this.options.event, function(){
				if($this._t) clearTimeout($this._t);
				$this.toPrepare(dir);
				if ($this.options.autoRotation) $this.runTimer($this);
				return false;
			});
		},
		initEventSwitcher: function($this, el){
			el.bind($this.options.event, function(){
				if($this._t) clearTimeout($this._t);
				$this.prev = $this.active;
				$this.active = $this.switcher.index($(this));

				if  ($this.options.disableBtn && $this.active == $this.count) {
					$this.nextBtn.addClass($this.options.disableBtn);
					$this.prevBtn.removeClass($this.options.disableBtn);
				}
				if  ($this.options.disableBtn && $this.active == 0) {
					$this.prevBtn.addClass($this.options.disableBtn);
					$this.nextBtn.removeClass($this.options.disableBtn);
				}
				
				if ($this.active > $this.prev) {
					$this.scrollElement($this,true);
				}
				if ($this.active < $this.prev) {
					$this.scrollElement($this,false);
				}
				if ($this.options.autoRotation) $this.runTimer($this);
				return false;
			});
		},
		toPrepare: function(side){
			if (side) {
				if (this.active == this.count) {
					this.active = 0;
				}
				else this.active++;

				if  (this.options.disableBtn && this.active == this.count) this.nextBtn.addClass(this.options.disableBtn);
				this.prevBtn.removeClass(this.options.disableBtn);
				
				if (this.active == 0) this.prev = this.count;
				else this.prev = this.active-1;
			}
			else {
				if (this.active == 0) this.active = this.count;
				else this.active--;

				if  (this.options.disableBtn && this.active == 0) this.prevBtn.addClass(this.options.disableBtn);
				this.nextBtn.removeClass(this.options.disableBtn);
				
				if (this.active == this.count) this.prev = 0;
				else this.prev = this.active+1;
			}
			this.scrollElement(this,side);
		}
	}
}(jQuery));
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";
	}
}
if (window.addEventListener)
	window.addEventListener("load", initPage, false);
else if (window.attachEvent)
	window.attachEvent("onload", initPage);

