// initPage
$(function(){
	initMain();
	initClear();
	initRating();
	initCycleGallery();
});

// initClear
function initClear(){
	clearFormFields({
		clearInputs: false,
		clearTextareas: false,
		passwordFieldText: false,
		addClassFocus: "focus",
		filterClass: "default"
	});
}

function initCycleGallery(){
	$('div.gallery').cycleGallery();
	$('div.gallery-2').cycleGallery();
	$('div.gallery-3').cycleGallery();
	$('div.playlist-holder').cycleGallery({
		slidesHolder:'div.playlist-content',
		btnLeft:'a.up',
		btnRight:'a.down',
		verticaly:true
	});
}

// slideshow plugin
jQuery.fn.cycleGallery = function(options){
	var options = jQuery.extend({
		btnLeft:'a.prev',
		btnRight:'a.next',
		slidesHolder:'div.gallery-holder',
		slider:'>ul',
		autoSlide:false,
		switchTime:5000,
		verticaly:false,
		speed:450
	},options);

	return this.each(function(){
		// gallery options
		var wrapp = jQuery(this);
		var slidesHolder = jQuery(options.slidesHolder, wrapp);
		var slider = jQuery(options.slider, slidesHolder);
		var btnLeft = jQuery(options.btnLeft, wrapp);
		var btnRight = jQuery(options.btnRight, wrapp);
		var autoSlide = options.autoSlide;
		var switchTime = options.switchTime;
		var speed = options.speed;
		var verticaly = options.verticaly;
		var slides = slider.children();
		var slidesCount = slides.length;
		var slideSize = verticaly ? slides.eq(0).outerHeight(true) : slides.eq(0).outerWidth(true);
		var currentIndex = 0;
		var sumSize = slidesCount*slideSize;
		var animating = false;
		var timer;
		
		slider.append(slides.clone()).append(slides.clone());
		if(verticaly) slider.css({marginTop:-sumSize});
		else slider.css({marginLeft:-sumSize});
		
		btnLeft.click(function(){
			prevSlide();
			return false;
		});
		btnRight.click(function(){
			nextSlide();
			return false;
		});
		function prevSlide() {
			if(animating) return;
			currentIndex--;
			switchSlide();
		}
		function nextSlide() {
			if(animating) return;
			currentIndex++;
			switchSlide();
		}
		
		function switchSlide() {
			animating = true;
			if(verticaly){
				slider.animate({marginTop:-sumSize-currentIndex*slideSize},{duration:speed, queue:false,complete:function(){
					if(currentIndex == slidesCount || currentIndex == -slidesCount) {
						currentIndex = 0;
						slider.css({marginTop:-sumSize});
					}
					animating = false;
				}});
			}else{
				slider.animate({marginLeft:-sumSize-currentIndex*slideSize},{duration:speed, queue:false,complete:function(){
					if(currentIndex == slidesCount || currentIndex == -slidesCount) {
						currentIndex = 0;
						slider.css({marginLeft:-sumSize});
					}
					animating = false;
				}});
			}
			autoChange();
		}
		
		function autoChange() {
			if(!autoSlide) return;
			if(timer) clearTimeout(timer);
			timer = setTimeout(nextSlide,switchTime);
		}
		autoChange();
	});
};

// clearForm
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";
	}
}

// initMain
var _content_box = "image-content";
function initMain(){
	var _div = document.getElementsByTagName("div");
	for(var i=0; i<_div.length; i++){
		if (_div[i].className.indexOf(_content_box) !=-1) {
			var _box = _div[i].getElementsByTagName("div")[0];
			if(_box){
				_div[i].parentNode.parentNode.style.width = _box.offsetWidth + "px";
			}
		}
	}
}

// star rating init
function initRating() {
	var rates = document.getElementsByTagName('ul');
	for (var i = 0; i < rates.length; i ++) {
		if (rates[i].className.indexOf('star-rating') != -1) {
			new StarRating({
				element:rates[i],
				onselect:function(num) {
					// rating setted event
				}
			})
		}
	}
}

// simple star raring module
function StarRating() {
	this.options = {
		activeClass:'active',
		settedClass:'setted',
		element:null,
		items:null,
		onselect:null
	}
	this.init.apply(this,arguments);
}
StarRating.prototype = {
	init: function(opt){
		this.setOptions(opt);
		if(this.element) {
			this.getElements();
			this.addEvents();
		}
	},
	setOptions: function(opt) {
		for(var p in opt) {
			if(opt.hasOwnProperty(p)) {
				this.options[p] = opt[p];
			}
		}
		if(this.options.element) {
			this.element = this.options.element;
		}
	},
	getElements: function() {
		// get switch objects
		if(this.options.items == null) {
			this.items = this.element.children;
		} else {
			if(typeof this.options.items === 'string') {
				this.items = this.element.getElementsByTagName(this.options.items);
			} else if(typeof this.options.items === 'object') {
				this.items = this.options.items;
			}
		}

		// find default active index
		for(var i = 0; i < this.items.length; i++) {
			if(this.hasClass(this.items[i],this.options.activeClass)) {
				this.activeIndex = i;
			}
			if(this.hasClass(this.items[i],this.options.settedClass)) {
				this.settedIndex = i;
			}
		}
	},
	addEvents: function() {
		for(var i = 0; i < this.items.length; i++) {
			this.items[i].onmouseover = this.bind(this.overHandler,this, i);
			this.items[i].onmouseout = this.bind(this.outHandler,this, i);
			this.items[i].onclick = this.bind(this.clickHandler,this, i);
		}
	},
	overHandler: function(ind) {
		this.hovering = true;
		this.hoverIndex = ind;
		this.refreshClasses();
	},
	outHandler: function(ind) {
		this.hovering = false;
		this.refreshClasses();
	},
	clickHandler: function(ind) {
		this.hovering = false;
		this.settedIndex = ind;
		if(typeof this.options.onselect === 'function') {
			this.options.onselect(ind);
		}
		this.refreshClasses();
		return false;
	},
	refreshClasses: function() {
		for(var i = 0; i < this.items.length; i++) {
			this.removeClass(this.items[i],this.options.activeClass);
			this.removeClass(this.items[i],this.options.settedClass);
		}
		if(this.hovering) {
			this.addClass(this.items[this.hoverIndex],this.options.activeClass);
		} else {
			if(typeof this.settedIndex === 'number') {
				this.addClass(this.items[this.settedIndex],this.options.settedClass);
			} else {
				if(typeof this.activeIndex === 'number') {
					this.addClass(this.items[this.activeIndex],this.options.activeClass);
				}
			}
		}
	},
	hasClass: function(el,cls) {
		return el && el.className ? el.className.match(new RegExp('(\\s|^)'+cls+'(\\s|$)')) : false;
	},
	addClass: function(el,cls) {
		if (el && !this.hasClass(el,cls)) el.className += " "+cls;
	},
	removeClass: function(el,cls) {
		if (el && this.hasClass(el,cls)) {el.className=el.className.replace(new RegExp('(\\s|^)'+cls+'(\\s|$)'),' ');}
	},
	bind: function(f, scope, args){
		return function() {return f.apply(scope, [args] || arguments)}
	}
}
