//init page
$(function(){
	positionFixed();
	initClearInput();
	buttonScroller();
	initGallery();
});
function positionFixed(){
	var aside = $('.mover');
	if(aside.length){
		var left = aside.offset().left;
		var top = aside.offset().top;
		var win = $(window);
		
		win.resize(function(){
			aside.css({position: 'static'});
			left = aside.offset().left;	
			reposition();
		});
		
		function reposition() {
			var scrollNow = win.scrollTop();
			if(scrollNow < top){
				aside.css({position: 'static', left:left, top:0});
			} else {
				aside.css({position: 'fixed',left:left, top:0});
			}		
		}
		win.scroll(reposition);	
	}
}
// init gallery
function initGallery(){
	$('div.slide-container').slideshow({
		autoPlay:true,
		effect:'slideX',//fade, slideX, slideY,
		switchTime:7000 //ms
	});
	$('div.slide-show').slideshow({
		slides:'div.frame > div.slide-item',
		pagingHolder:'ul.scroller',
		pagingTag:'li',
		caption:'div.info',
		createPaging:false,
		nextBtn:false,
		prevBtn:false,
		autoPlay:true,
		effect:'fade',
		switchTime:7000, //ms
		animSpeed:600 //ms
	});
};

// scrolling box init
function buttonScroller() {
	var scroller = jQuery('ul.scroller');
	var slides = scroller.children();
	var w = 0;
	slides.each(function(){
		w += jQuery(this).outerWidth(true);
	});
	scroller.css({width:w});
	jQuery('div.slide-show div.gallery').buttonScroller();
} 

// scrolling box plugin
jQuery.fn.buttonScroller = function(_options){
	// default options
	var _options = jQuery.extend({
		slideHolder:'div.holder',
		slideContainer:'ul.scroller',
		btnUp:'a.prev',
		btnDown:'a.next',
		disabledClass:'disable',
		eventStart:'mouseenter',
		eventStop:'mouseleave',
		scrollSpeed: 0.5
	},_options);

	return this.each(function(){
		// options
		var _holder = jQuery(this);
		var _scrollSpeed = _options.scrollSpeed;
		var _disabledClass = _options.disabledClass;
		var _slideHolder = jQuery(_options.slideHolder, _holder);
		var _slideContainer = jQuery(_options.slideContainer, _slideHolder);
		var _slide = _slideContainer.children();
		var _btnUp = jQuery(_options.btnUp, _holder);
		var _btnDown = jQuery(_options.btnDown, _holder);
		var _eventStart = _options.eventStart;
		var _eventStop = _options.eventStop;

		// scroller init
		var _holderWidth = _slideHolder.width();
		var _sliderWidth = _slideContainer.outerWidth(true);
		var _maxOffset = _sliderWidth - _holderWidth;
		var _marginOffset = parseInt(_slide.css('marginRight'));
		var _duration;
		var _k;
		
		_slideContainer.css('marginLeft',0);
		_duration = _sliderWidth / _scrollSpeed;

		function stopScroll() {
			_slideContainer.stop();
			return false;
		}
		function refreshButtons() {
			var _offset = parseInt(_slideContainer.css('marginLeft'));
			if(_offset == 0) _btnUp.addClass(_disabledClass); else _btnUp.removeClass(_disabledClass);
			if(_offset == -_maxOffset) _btnDown.addClass(_disabledClass); else _btnDown.removeClass(_disabledClass);
		}

		_btnUp.bind(_eventStart,function(){
			_k = 1-(_maxOffset + parseInt(_slideContainer.css('marginLeft')))/_maxOffset;
			_slideContainer.animate({marginLeft:0},{duration: _duration*_k,queue:false, easing:'linear',step:refreshButtons,complete:refreshButtons});
		}).bind(_eventStop,stopScroll).click(stopScroll);

		_btnDown.bind(_eventStart, function(){
			if(_sliderWidth < _holderWidth) return false;
			_k = (_maxOffset + parseInt(_slideContainer.css('marginLeft')))/_maxOffset;
			_slideContainer.animate({marginLeft:-_maxOffset+_marginOffset},{duration: _duration*_k,queue:false, easing:'linear',step:refreshButtons,complete:refreshButtons});
		}).bind(_eventStop,stopScroll).click(stopScroll);
		refreshButtons();
	});
};

//init clear input
function initClearInput(){
	clearFormFields({
		clearInputs: false,
		clearTextareas: false,
		passwordFieldText: true,
		addClassFocus: "focus",
		filterClass: "default"
	});
}
//clear form fields module
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";
	}
};

//create jQuery plugin
jQuery.fn.slideshow = function(options){return new slideshow(this, options);}

//constructor
function slideshow(obj, options){this.init(obj,options)}

//prototype
slideshow.prototype = {
	init:function(obj, options) {
		this.options = jQuery.extend({
			slides:'div.slide > div.slide-move',
			nextBtn:'a.next',
			prevBtn:'a.prev',
			pagingHolder:'div.paging >ul',
			pagingTag:'li',
			createPaging:true,
			autoPlay:false,
			autoHeight:false,
			dynamicLoad:false,
			imgAttr:'alt',
			effect:'slideX',//fade, slideX, slideY,
			startSlide:false,
			caption:false,
			switchTime:5000,
			animSpeed:700
		},options);
		
		this.mainHolder = jQuery(obj);
		this.slides = jQuery(this.options.slides,this.mainHolder);		
		this.nextBtn = jQuery(this.options.nextBtn,this.mainHolder);
		this.prevBtn = jQuery(this.options.prevBtn,this.mainHolder);
		this.caption = jQuery(this.options.caption,this.mainHolder);
		this.dynamicLoad = this.options.dynamicLoad;
		this.imgAttr = this.options.imgAttr;
		this.animSpeed = this.options.animSpeed;
		this.switchTime = this.options.switchTime;
		this.effect = this.options.effect;
		this.autoPlay = this.options.autoPlay;
		this.autoHeight = this.options.autoHeight;
		this.previous = -1;
		this.loadingFrame = 1;
		this.busy = false;
		this.direction = 1;
		this.timer;
		this.pagingArray = new Array;
		this.loadArray = new Array;
		this.preloader = new Array;
		this.slidesParent = this.slides.eq(0).parent();
		this.slideW = this.slidesParent.width();
		this.slideH = this.slidesParent.height();
		
		(function(){
			if (this.options.startSlide) this.current = this.options.startSlide
			else {
				var active = -1;
				for(var i = 0; i< this.slides.length-1; i++) {
					if (this.slides.eq(i).hasClass('active')) {
						active = i;
						break;
					}
				}
				if (active != -1) this.current = active;
				else this.current = 0;
			}
		}).apply(this);
		
		this.initPaging();
		this.setStyles();
		this.bindEvents();
		this.showSlide();
	},
	
	initPaging:function(){
		var obj = this;
		this.pagingHolder = jQuery(this.options.pagingHolder,this.mainHolder);
		
		if (this.options.createPaging) {
			this.pagingHolder.each(function(i){
				var _this = jQuery(this);
				_this.empty();
				var list = jQuery('<ul>');
				for (var i = 0; i < obj.slides.length; i++) jQuery('<li><a href="#">' + (i + 1) + '</a></li>').appendTo(_this);
				//_this.append(list);
			});
		}
		
		this.paging = jQuery(this.options.pagingTag, this.pagingHolder);
		var ratio = Math.ceil(this.paging.length / this.slides.length);
		for (var i = 0; i < ratio; i++) {
			this.pagingArray.push(this.paging.slice(i*this.slides.length, (i*this.slides.length)+this.slides.length));
		}
	},
	
	setStyles:function(){
		//loader
		if (this.dynamicLoad) {
			this.loader = jQuery('<div class="loader">');
			this.loaderDiv = jQuery('<div>').appendTo(this.loader)
			this.loader.append(this.loaderDiv).appendTo(this.slidesParent);
		}
		// caption
		if (this.caption){
			this.caption.each(function(){
				var hold = jQuery(this);
				var h = hold.outerHeight();
				hold.css({bottom:-h-1});
			});
			this.caption.eq(this.current).animate({bottom:0},this.animSpeed);
		}
		// auto height
		if (this.autoHeight) {
			this.slides.eq(this.current).parent().css({height:this.slides.eq(this.current).outerHeight(true)});
		}
		//slides
		if (this.effect == 'fade') {
			this.slides.css({display:'none'});
			this.slides.eq(this.current).css({display:'block'});
		} else if (this.effect == 'slideX'){
			this.slides.css({display: 'none',left:-this.slideW});
			this.slides.eq(this.current).css({display:'block',left:0});
		} else if (this.effect == 'slideY'){
			this.slides.css({display:'none',top:-this.slideH});
			this.slides.eq(this.current).css({display:'block',top:0});
		}
	},
	
	bindEvents:function(){
		var obj = this;
		this.nextBtn.bind('click',function(){
			if (!obj.busy) obj.nextSlide();
			return false;
		});
		
		this.prevBtn.bind('click',function(){
			if (!obj.busy) obj.prevSlide();
			return false;
		});
		
		for (var i = 0; i < this.pagingArray.length; i++) {
			this.pagingArray[i].each(function(i){
				jQuery(this).bind('click',function(){
					if (i != obj.current && !obj.busy) {
						obj.previous = obj.current;
						obj.current = i;
						if (obj.previous > i) obj.direction = -1
						else obj.direction = 1;
						obj.showSlide();
					}
					return false;
				});
			});
		}
		
		if (this.dynamicLoad) this.loader.bind('click',function(){
			obj.abortLoading();
		});
	},
	
	nextSlide:function(){
		this.previous = this.current;
		if (this.current < this.slides.length-1) this.current++
		else this.current = 0;
		this.direction = 1;
		this.showSlide();
	},
	
	prevSlide:function(){
		this.previous = this.current;
		if (this.current > 0) this.current--
		else this.current = this.slides.length-1;
		this.direction = -1;
		this.showSlide();
	},
	
	showSlide:function(){
		var obj = this;
		var _current = this.current;
		this.busy = true;
		clearTimeout(this.timer);
		
		if (typeof this.loadArray[_current] != 'undefined' || !this.dynamicLoad) {
			//slide already loaded
			this.switchSlide();
		
		} else {
			//slide not loaded
			this.showLoading();
			var images = jQuery(this.dynamicLoad,this.slides.eq(this.current));
			if (images.length) {
				var counter = 0;
				images.each(function(){
					var preloader = new Image;
					obj.preloader.push(preloader);
					var img = jQuery(this);
					preloader.onload = function(){
						counter++;
						checkImages();
					}
					preloader.onerror = function(){
						//ignore errors
						counter++;
						checkImages();
					}
					preloader.src = img.attr(obj.imgAttr);
				});
				
				function checkImages(){
					if (counter == images.length) {
						images.each(function(){
							var img = jQuery(this);
							img.attr('src',img.attr(obj.imgAttr));
						});
						successLoad();
					}
				}
				
			} else successLoad();
		}
		
		function successLoad(){
			obj.loadArray[_current] = 1;
			obj.hideLoading();
			obj.switchSlide();
		}
	},
	
	switchSlide:function(){
		var obj = this;
		
		if (this.previous != -1) {
			var nextSlide = this.slides.eq(this.current);
			var prevSlide = this.slides.eq(this.previous);
			
			if (this.autoHeight) this.slides.eq(this.current).parent().stop().animate({height:this.slides.eq(this.current).outerHeight(true)},this.animSpeed);
			
			if (this.caption.length) {
				var prevCaption = this.caption.eq(this.previous);
				var nextCaption = this.caption.eq(this.current);
				prevCaption.stop().animate({bottom:-prevCaption.outerHeight()},this.animSpeed*0.5,function(){
					nextSlide.stop().css({display:'block',opacity:0}).animate({opacity:1},obj.animSpeed,function(){
						jQuery(this).css({opacity:'auto'});
						nextCaption.stop().animate({bottom:0},obj.animSpeed*0.5);
					});
					prevSlide.stop().animate({opacity:0},obj.animSpeed,callback);
				});
			} else {
				if (this.effect == 'fade') {
					nextSlide.stop().css({display:'block',opacity:0}).animate({opacity:1},this.animSpeed,function(){
						jQuery(this).css({opacity:'auto'});
					});
					prevSlide.stop().animate({opacity:0},this.animSpeed,callback);
				} else if (this.effect == 'slideX'){
					nextSlide.stop().css({display:'block',left:this.slideW*this.direction}).animate({left:0},this.animSpeed);
					prevSlide.stop().animate({left:-this.slideW*this.direction},this.animSpeed+10,callback);
				} else if (this.effect == 'slideY'){
					nextSlide.stop().css({display:'block',top:this.slideH*this.direction}).animate({top:0},this.animSpeed);
					prevSlide.stop().animate({top:-this.slideH*this.direction},this.animSpeed+10,callback);
				}
			}
		} else {
			if (this.autoPlay) this.startAutoPlay();
			this.busy = false;
		}
		
		this.refreshStatus();
		
		function callback(){
			prevSlide.css({display:'none'});
			if (obj.autoPlay) obj.startAutoPlay();
			obj.busy = false;
		}
	},
	
	refreshStatus:function(){
		for (var i = 0; i < this.pagingArray.length;i++) {
			this.pagingArray[i].eq(this.previous).removeClass('active');
			this.pagingArray[i].eq(this.current).addClass('active');
		}
		this.slides.eq(this.previous).removeClass('active');
		this.slides.eq(this.current).addClass('active');
	},
	
	showLoading:function(){
		var obj = this;
		this.loader.show();
		clearInterval(this.loadingTimer);
		obj.loadingTimer = setInterval(animateLoading, 66);
		
		function animateLoading(){
			obj.loaderDiv.css('top', obj.loadingFrame * -40);
			obj.loadingFrame = (obj.loadingFrame + 1) % 12;
		}
	},
	
	hideLoading:function(){
		this.loader.hide();
		clearInterval(this.loadingTimer);
	},
	
	abortLoading:function(){
		this.busy = false;
		this.hideLoading();
		this.current = this.previous;
		for (var i = 0; i < this.preloader.length; i++) {
			this.preloader[i].onload = null;
			this.preloader[i].onerror = null;
		}
		if (this.autoPlay) this.startAutoPlay();
	},
	
	startAutoPlay:function(){
		var obj = this;
		clearTimeout(obj.timer);
		obj.timer = setTimeout(function(){
			obj.nextSlide();
		},obj.switchTime);
	}
}
// double submit
$("form").each(function(){
var $that = $(this);
$that.submit(function(){
$that.find("input[type='image'],input[type='submit']").click(function(){
return false;
});
});
});
