window.addEvent('domready', function(){	
	clearFormFields({
		clearInputs: true,
		clearTextareas: true,
		passwordFieldText: true,
		addClassFocus: "focus",
		filterClass: "default"
	});
	initCycle();
});

window.addEvent('domready',function(){
	var gallery1 = $('gallery1');
	if (gallery1) {
		new slideshow ('gallery1',{
			slides:'div.gallery > ul > li',
			nextBtn:'a.next',
			prevBtn:'a.prev',
			pagingHolder:'div.switcher-holder > div',
			effect:'slideY'
		});
	}
	var gallery = $$('div.gallery');
	if (gallery.length) {
		new slideshow(gallery[0]);
	}
});

var slideshow = new Class({
	Implements : [Options, Events],
	options:{
		slides:'div.slide',
		nextBtn:false,
		prevBtn:false,
		pagingHolder:'div.switcher',
		pagingTag:'li',
		slidesActiveClass:'active',
		pagingActiveClass:'active',
		autoHeight:false,
		createPaging:false,
		autoPlay:true,
		dynamicLoad:false,
		imgAttr:'alt',
		effect:'slideX',//fade, slideX, slideY,
		startSlide:false,
		switchTime:4000,
		animSpeed:700
	},
	initialize:function(element, options) {
		this.mainHolder = $(element);
		this.setOptions(options);
		this.slides = this.mainHolder.getElements(this.options.slides);		
		if (this.options.nextBtn) this.nextBtn = this.mainHolder.getElement(this.options.nextBtn);
		if (this.options.prevBtn) this.prevBtn = this.mainHolder.getElement(this.options.prevBtn);
		
		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[0].getParent();
		this.slideW = this.slidesParent.getSize().x;
		this.slideH = this.slidesParent.getSize().y;
		this.autoPlay = this.options.autoPlay;
		
		this.initStartSlide();
		this.initPaging();
		this.setStyles();
		this.bindEvents();
		this.showSlide();
	},
	
	initStartSlide: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[i].hasClass(this.options.slidesActiveClass)) {
					active = i;
					break;						
				}
			}
			if (active != -1) this.current = active;
			else this.current = 0;
		}
	},
	
	initPaging:function(){
		this.pagingHolder = this.mainHolder.getElements(this.options.pagingHolder);
		
		if (this.options.createPaging) {
			this.pagingHolder.each(function(paging,i){
				paging.empty();
				var list = new Element('ul');
				var html = '';
				for (var i = 0; i < this.slides.length; i++) {
					html += '<li><a href="#">' + (i + 1) + '</a></li>';
				}
				list.innerHTML = html;
				list.inject(paging);
			}.bind(this));
		}
		
		this.pagingHolder.each(function(paging){
			this.pagingArray.push(paging.getElements(this.options.pagingTag))
		}.bind(this));
	},
	
	setStyles:function(){
		//loader
		if (this.options.dynamicLoad) {
			this.loader = new Element('div').addClass('loader');
			this.loaderDiv = new Element('div').inject(this.loader);
			this.loader.inject(this.slidesParent);
		}
		
		//slides
		this.slides.each(function(slide,i){
			if (this.options.effect == 'fade') {
				if (i != this.current) slide.setStyles({display:'none'});
				else slide.setStyles({display:'block'});
			} else if (this.options.effect == 'slideX'){
				if (i != this.current) slide.setStyles({display: 'none',left:-this.slideW});
				else slide.setStyles({display:'block',left:0});
			} else if (this.options.effect == 'slideY'){
				if (i != this.current) slide.setStyles({display:'none',top:-this.slideH});
				else slide.setStyles({display:'block',top:0});
			}
		}.bind(this));
		
		if (this.options.autoHeight) {
			this.slidesParent.setStyles({
				height:this.slides[this.current].getSize().y
			});
		}
	},
	
	bindEvents:function(){
		if (this.nextBtn) this.nextBtn.addEvent('click',function(){
			if (!this.busy) this.nextSlide();
			return false;
		}.bind(this));
		
		if (this.prevBtn) this.prevBtn.addEvent('click',function(){
			if (!this.busy) this.prevSlide();
			return false;
		}.bind(this));
		this.pagingArray.each(function(paging){
			paging.each(function(btn,i){
				btn.addEvent('click',function(){
					if (i != this.current && !this.busy) {
						this.previous = this.current;
						this.current = i;
						if (this.previous > i) this.direction = -1
						else this.direction = 1;
						this.showSlide();
					}
					return false;
				}.bind(this));
			}.bind(this));
		}.bind(this));
		
		if (this.options.dynamicLoad) this.loader.addEvent('click',this.abortLoading.bind(this));
	},
	
	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(){
		if (this.previous == this.current) return; 
		var _current = this.current;
		this.busy = true;
		clearTimeout(this.timer);
		if (typeof this.loadArray[_current] != 'undefined' || !this.options.dynamicLoad) {
			//slide already loaded
			this.switchSlide();
		} else {
			//slide not loaded
			this.showLoading();
			var images = this.slides[this.current].getElements(this.options.dynamicLoad);
			if (images.length) {
				var counter = 0;
				images.each(function(img){
					var preloader = new Image;
					this.preloader.push(preloader);
					preloader.onload = function(){
						counter++;
						checkImages.apply(this);
					}.bind(this);
					preloader.onerror = function(){
						//ignore errors
						counter++;
						checkImages.apply(this);
					}.bind(this);
					preloader.src = img.getProperty(this.options.imgAttr);
				}.bind(this));
				
				function checkImages(){
					if (counter == images.length) {
						images.each(function(img){
							img.setProperty('src',img.getProperty(this.options.imgAttr));
						}.bind(this));
						successLoad.apply(this);
					}
				}
			} else successLoad.apply(this);
		}
		
		function successLoad(){
			this.loadArray[_current] = 1;
			this.hideLoading();
			this.switchSlide();
		}
	},
	
	switchSlide:function(){
		var obj = this;
		
		if (this.previous != -1) {
			var nextSlide = this.slides[this.current];
			var prevSlide = this.slides[this.previous];
			nextSlide.setStyles({display:'block'});
				
				
			if (this.options.effect == 'slideX'){
				this.slideW = this.slides[this.current].getSize().x;
				var nextFx = new Fx.Morph(nextSlide,{duration:this.options.animSpeed});
				var prevFx = new Fx.Morph(prevSlide,{duration:this.options.animSpeed,onComplete:callback.bind(this)});
				
				nextSlide.setStyles({left:this.slideW*this.direction})
				nextFx.start({left:0});
				prevFx.start({left:-this.slideW*this.direction});
			} else if (this.options.effect == 'slideY'){
				this.slideH = this.slides[this.current].getSize().y
				var nextFx = new Fx.Morph(nextSlide,{duration:this.options.animSpeed});
				var prevFx = new Fx.Morph(prevSlide,{duration:this.options.animSpeed,onComplete:callback.bind(this)});
				
				nextSlide.setStyles({top:this.slideH*this.direction});
				nextFx.start({top:0});
				prevFx.start({top:-this.slideH*this.direction});
			} else {
				var nextFx = new Fx.Morph(nextSlide,{duration:this.options.animSpeed,onComplete:function(){
					nextSlide.setStyles({opacity:'auto'});
				}});
				var prevFx = new Fx.Morph(prevSlide,{duration:this.options.animSpeed,onComplete:callback.bind(this)});
				nextSlide.setStyles({display:'block',opacity:0});
				nextFx.start({opacity:1});
				prevSlide.setStyles({opacity:1});
				prevFx.start({opacity:0});
			}
			
			if (this.options.autoHeight) {
				this.slideH = this.slides[this.current].getSize().y
				var parentFx = new Fx.Morph(this.slidesParent,{duration:this.options.animSpeed});
				parentFx.start({height:this.slideH});
			}
		} else {
			if (this.autoPlay) this.startAutoPlay();
			this.busy = false;
		}
		
		this.refreshStatus();
		
		function callback(){
			prevSlide.setStyles({display:'none'});
			if (this.autoPlay) this.startAutoPlay();
			this.busy = false;
		}
	},
	
	refreshStatus:function(){
		if (this.pagingArray.length) {
			this.pagingArray.each(function(paging){
				if (this.previous != -1) paging[this.previous].removeClass(this.options.pagingActiveClass);
				paging[this.current].addClass(this.options.pagingActiveClass);
			}.bind(this));
		}
		if (this.previous != -1) this.slides[this.previous].removeClass(this.options.pagingActiveClass);
		this.slides[this.current].addClass(this.options.pagingActiveClass);
	},
	
	showLoading:function(){
		this.loader.setStyles({display:'block'});
		clearInterval(this.loadingTimer);
		this.loadingTimer = setInterval(animateLoading.bind(this), 66);
		
		function animateLoading(){
			this.loaderDiv.setStyles({'top': this.loadingFrame * -40});
			this.loadingFrame = (this.loadingFrame + 1) % 12;
		}
	},
	
	hideLoading:function(){
		this.loader.setStyles({display:'none'});
		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(){
		clearTimeout(this.timer);
		this.timer = setTimeout(this.nextSlide.bind(this),this.options.switchTime);
	}
});

// cycle gallery init
function initCycle() {
	$$('div.colored-info').each(function(obj,i){
		var _gallery = new cycleCarousel(obj,{
			pagerLinks: 'ul.tab-nav a',
			sliderHolder: 'div.news-holder',
			slider: 'ul',
			slides: 'li'
		});
	})
}

// cycle gallery code
var cycleCarousel = new Class({
	options: {
		activeClass: 'active',
		btPrev: 'a.top',
		btNext: 'a.bottom',
		pagerLinks: 'ul.tabset li',
		slidesHolder: 'div',
		slider: '.slide',
		slides: 'li',
		switchTime: 3000,
		duration : 450,
		stopAfterClick:false,
		autoRotation:false
	},

	// create class
	initialize: function(element, options){
		this.setOptions(options);
		var _this = this;

		if (this.options.btNext) this.next = element.getElement(this.options.btNext);
		else this.next = false;

		if (this.options.btPrev) this.prev = element.getElement(this.options.btPrev);
		else this.prev = false;

		if (this.options.pagerLinks) this.pagerLinks = element.getElements(this.options.pagerLinks);
		else this.pagerLinks = false;

		this.animated = false;
		this.holder = element.getElement(this.options.slidesHolder);
		this.slider = this.holder.getElement(this.options.slider);
		this.slides = this.holder.getElements(this.options.slides);
		this.stepWidth = this.slides[0].getSize().y;
		this.switchTime = this.options.switchTime;
		this.activeClass = this.options.activeClass;
		this.autoRotation = this.options.autoRotation;
		this.stopAfterClick = this.options.stopAfterClick;
		
		// gallery init
		this.duration = this.options.duration;
		this.slideCount = this.slides.length;
		this.oldIndex = 0;
		this.currentIndex = 0;
		this.timer = false;
		this.direction = false;

		// slides init
		this.slider.setStyles({position:'relative',height:this.slider.getSize().y});
		this.slides.each(function(slide){
			slide.setStyles({position:'absolute',left:0,top:_this.stepWidth});
		})
		this.slides[this.currentIndex].setStyles({top:0});

		// mouseover
		_this.addEvent('mouseover', function(){
			if (_this.timer) clearInterval(_this.timer);
		}).addEvent('mouseout', function(){
			_this.autoSlide();
		});

		// gallery control
		if (this.next) {
			this.next.addEvent('click', function(){
				if (!_this.animated) {
					if(_this.stopAfterClick) {
						_this.autoRotation = false;
						clearTimeout(_this.timer);
					}
					_this.nextSlide();
				}
				return false;
			});
		}
		if (this.prev) {
			this.prev.addEvent('click', function(){
				if (!_this.animated) {
					if(_this.stopAfterClick) {
						_this.autoRotation = false;
						clearTimeout(_this.timer);
					}
					_this.prevSlide();
				}
				return false;
			});
		}
		if (this.pagerLinks) {
			this.pagerLinks.each(function(link,ind){
				link.addEvent('click', function(){
					if (!_this.animated && _this.currentIndex!=ind) {

						if(_this.stopAfterClick) {
							_this.autoRotation = false;
							clearTimeout(_this.timer);
						}
						_this.direction = (_this.currentIndex < ind);
						_this.oldIndex = _this.currentIndex;
						_this.currentIndex = ind;
						_this.switchSlide();
					}
					return false;
				});
			})
		}

		// autoslide
		if (this.options.autoRotation) {
			this.autoSlide();
		}
	},
	nextSlide: function(){
		this.oldIndex = this.currentIndex
		if(this.currentIndex < this.slideCount-1) this.currentIndex++;
		else this.currentIndex = 0;
		this.direction = true;
		this.switchSlide();
	},
	prevSlide: function(){
		this.oldIndex = this.currentIndex
		if(this.currentIndex < this.slideCount-1) this.currentIndex++;
		else this.currentIndex = 0;
		this.direction = false;
		this.switchSlide();
	},
	refreshPager: function() {
		// refresh pagination
		if (this.pagerLinks) {
			this.pagerLinks.each(function(link,ind){
				if(ind != this.currentIndex) {
					link.removeClass(this.activeClass);
				} else {
					link.addClass(this.activeClass);
				}
			}.bind(this));
		}
	},
	switchSlide: function(){
		var _d = (this.direction ? -1 : 1);
		var _this = this;
		var oldSlide = this.slides[this.oldIndex];
		var newSlide = this.slides[this.currentIndex];
		var fxOldSlide = new Fx.Morph(oldSlide, {duration: this.duration});
		var fxNewSlide = new Fx.Morph(newSlide, {duration: this.duration,onComplete:function(){
			_this.animated = false;
		}});
		this.animated = true;

		newSlide.setStyles({top:-this.stepWidth*_d})
		fxOldSlide.start({'top':this.stepWidth*_d});
		fxNewSlide.start({'top':0});
		this.autoSlide();
		this.refreshPager();
	},
	autoSlide : function(){
		if(this.autoRotation) {
			var _this = this;
			if(this.timer) clearTimeout(this.timer);
			this.timer = setInterval(function(){_this.nextSlide()}, this.options.switchTime);
		}
	},

	// add options and events
	Implements : [Options, Events]
});

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.filter) o.filter = "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)) {
				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)) {
				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";
	}
}

function initTabs()
{
	var sets = document.getElementsByTagName("ul");
	for (var i = 0; i < sets.length; i++)
	{
		if (sets[i].className.indexOf("tabset") != -1)
		{
			var tabs = [];
			var links = sets[i].getElementsByTagName("a");
			for (var j = 0; j < links.length; j++)
			{
				if (links[j].className.indexOf("tab") != -1)
				{
					tabs.push(links[j]);
					links[j].tabs = tabs;
					var c = document.getElementById(links[j].href.substr(links[j].href.indexOf("#") + 1));

					if (c) if (links[j].className.indexOf("active") != -1) c.style.display = "block";
					else c.style.display = "none";

					links[j].onclick = function ()
					{
						var c = document.getElementById(this.href.substr(this.href.indexOf("#") + 1));
						if (c)
						{
							for (var i = 0; i < this.tabs.length; i++)
							{
								var tab = document.getElementById(this.tabs[i].href.substr(this.tabs[i].href.indexOf("#") + 1));
								if (tab)
								{
									tab.style.display = "none";
								}
								this.tabs[i].className = this.tabs[i].className.replace("active", "");
							}
							this.className += " active";
							c.style.display = "block";
							return false;
						}
					}
				}
			}
		}
	}
}



