// script demo
jQuery(function(){
	clearFormFields({
		clearInputs: true,
		clearTextareas: true,
		passwordFieldText: true,
		addClassFocus: "focus",
		filterClass: "default"
	});
	initDropDown();
	$('div.accordion.align-left').uniAccordion({
		activeClass:'item-active',
		items:'>div.acc-item',
		opener:'>a.acc-opener',
		slider:'>div.acc-slide',
		animSpeed: 300,
		event:'click'
	});
	
	jQuery('.countdown').countdown(new Date("may 02, 2012, 17:00"));
});

// clearFormFields
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";
	}
}

// initDropDown
function initDropDown() {
	var nav = document.getElementById("nav");
	if(nav) {
		var lis = nav.getElementsByTagName("li");
		for (var i=0; i<lis.length; i++) {
			if(lis[i].getElementsByTagName("ul").length > 0) {
				lis[i].className += " has-drop-down"
				lis[i].getElementsByTagName("a")[0].className += " has-drop-down-a"
			}
			lis[i].onmouseover = function()	{
				this.className += " hover";
			}
			lis[i].onmouseout = function() {
				this.className = this.className.replace(" hover", "");
			}
		}
	}
}

// horizontal accordion plugin
jQuery.fn.uniAccordion = function(o){
	// default options
	var o = jQuery.extend({
		activeClass:'active',
		opener:'>.opener',
		slider:'>.slide',
		items:'>*',
		easing:'linear',
		animSpeed: 500,
		openerPosition: 'left', // right
		event:'click'
	},o);

	return this.each(function(){
		// options
		var accordion = jQuery(this), animProps,
			accItems = jQuery(o.items, accordion);

		// revert slides order
		if(!accItems.length) return;
		if(o.openerPosition == 'right') {
			var itemsParent = accItems.eq(0).parent();
			accItems.each(function(ind){
				accItems.eq(accItems.length - 1 - ind).appendTo(itemsParent);
			});
			accItems = jQuery(o.items, accordion);
		} else if(o.openerPosition !== 'left') {
			o.openerPosition = 'left'
		}

		// plugin variables
		accordion.css({position:'relative',overflow:'hidden'});
		var accWidth = 0,
			accOpenerWidth = 0,
			accOpenersWidth = 0,
			accFreeWidth = 0,
			oldIndex = 0,
			curIndex = accItems.index(accItems.filter('.'+o.activeClass));
		if(curIndex < 0) curIndex = 0;



		// event handlers
		accItems.each(function(i){
			var item = jQuery(this).css({display:'block',position:'absolute'});
			item.data('opener',jQuery(o.opener, this));
			item.data('slider',jQuery(o.slider, this));
			item.data('opener').bind(o.event, function(){
				var activeSlide = accItems.filter('.'+o.activeClass);
				if(i != accItems.index(activeSlide)) {
					setActiveSlide(i);
				}
				return false;
			});
		});

		// element animations
		function setActiveSlide(setNum) {
			oldIndex = curIndex;
			curIndex = setNum;
			accItems.eq(oldIndex).removeClass(o.activeClass);
			accItems.eq(curIndex).addClass(o.activeClass)
			accItems.each(function(curNum){
				if(curNum > Math.min(oldIndex,curIndex) && curNum <= Math.max(oldIndex,curIndex)) {
					animProps = {};
					animProps[o.openerPosition] = curNum*accOpenerWidth + (curNum <= curIndex ? 0 : accFreeWidth);
					accItems.eq(curNum).stop().animate(animProps,{duration:o.animSpeed,easing:o.easing,queue:false});
				}
			});
		}
		function repositionElements() {
			// recalculate dimensions
			accWidth = accordion.width();
			accOpenersWidth = 0;
			accItems.each(function(){
				accOpenerWidth = jQuery(this).data('opener').outerWidth(true);
				accOpenersWidth += accOpenerWidth;
			});
			accFreeWidth = accWidth - accOpenersWidth;

			// set styles
			accItems.each(function(i){
				jQuery(this).data('slider').css({width:accFreeWidth});
				animProps = {zIndex:i+1, width:accFreeWidth + accOpenerWidth};
				animProps[o.openerPosition] = i*accOpenerWidth + (i <= curIndex ? 0 : accFreeWidth)
				jQuery(this).css(animProps);
			});
		}

		repositionElements();
		jQuery(window).resize(repositionElements);
	});
}

jQuery.fn.countdown = function (date, options) {
	options = jQuery.extend({
		finish: 'Stop'
	}, options);

	var timeDifference = function(begin, end) {
		if (end < begin) {
			return false;
		}
		var diff = {
			seconds: [end.getSeconds() - begin.getSeconds(), 60],
			minutes: [end.getMinutes() - begin.getMinutes(), 60],
			hours: [end.getHours() - begin.getHours(), 24],
			days: [parseInt((end.getTime() - begin.getTime()) / (1000 * 60 * 60 * 24), 10)]
		};
		
		var result = new Array();
		var flag = false;
		for (i in diff) {
			if (flag) {
				diff[i][0]--;
				flag = false;
			}
			if (diff[i][0] < 0) {
				flag = true;
				diff[i][0] += diff[i][1];
			}
			if (!diff[i][0]) {
				result.push(0);
				continue;
			}
			result.push(diff[i][0]);
		}
		return result;
	};
	var elem = $(this);
	var d = $('.days', this).text('0');
	var h = $('.hours', this).text('0');
	var m = $('.minutes', this).text('0');
	var s = $('.seconds', this).text('0');
	var timeUpdate = function () {
		var t = timeDifference(new Date(), date);
		if (t.length) {
			d.text(t[3]);
			h.text(t[2]);
			m.text(t[1]);
			s.text(t[0]);
		} else {
			clearInterval(timer);
			elem.html(options.finish);
		}
	};
	timeUpdate();
	var timer = setInterval(timeUpdate, 1000);
};
