/* Cufon */
function initCufon() {
	Cufon.set('fontFamily', 'Capture it'); 
	Cufon.replace('.content h1 span');
	Cufon.replace('.content h2 span');
	Cufon.replace('.twocol h3');
	Cufon.replace('#sidebar h3');
	Cufon.replace('.slogan');
	Cufon.replace('#footer .heading h3');
}

$(function(){
	initCufon();
	initNav();
	initAcc('ul.accordion');
	clearFormFields({
		clearInputs: true,
		clearTextareas: true,
		passwordFieldText: true,
		addClassFocus: "focus",
		filterClass: "default"
	});
});

//initNav
function initNav(){
	var _duration = 300; //ms
	_holder = $('div.navigation > ul > li');
	_holder.each(function(){
		var _li = $(this);
		var _drop = $('div.drop', _li);
		if (!$.browser.msie) _drop.css('opacity',0);
		_li.mouseenter(function(){
			_drop.stop().css('display','block')
			if (!$.browser.msie) _drop.animate({opacity : 1},_duration);
			_holder.removeClass('hover');
			$(this).addClass('hover');
		}).mouseleave(function(){
			if (!$.browser.msie) {
				_drop.stop().animate({opacity : 0},_duration,function(){
					$(this).css('display','none');
					_li.removeClass('hover');
				});
			} else {
				_drop.stop().css('display','none');
				_li.removeClass('hover');
			}
		});
	});
}

//accordion
function initAcc(_hold){
	_hold = $(_hold);
	_hold.each(function(){
		var _list = $(this).children();
		var _a = _list.index(_list.filter('.selected:eq(0)'));
		if(_a != -1){
			_list.removeClass('selected').eq(_a).addClass('selected');
		}
		_list.each(function(_i){
			var _el = $(this);
			_el.removeClass('selected');
			this._btn = _el.children('div.title').find('> a');
			this._box = _el.children('div.slide');
			if(this._btn.length && this._box.length){
				this._h = this._box.height();
				if (_i == _a) {
					this._box.show();
					this._box.parent().addClass('selected');
				}
				else this._box.hide();
				this._btn.click(function(){
					if(_a != -1){
						_list.eq(_a).removeClass('selected');
						_list.get(_a)._box.stop().animate({height: 0}, 400, function(){
							$(this).css({display: 'none', height: 'auto'});
						});
					}
					if(_a != _i){
						_list.eq(_i).addClass('selected');
						if(_list.get(_i)._box.is(':hidden')){
							_list.get(_i)._box.show();
							_list.get(_i)._h = _list.get(_i)._box.height();
							_list.get(_i)._box.height(0);
						}
						_list.get(_i)._box.stop().animate({ height: _list.get(_i)._h}, 400, function(){
							$(this).height('auto');
						});
						_a = _i;
					}
					else{
						_a = -1;
					}
					return false;
				});
			}
		});
	});
}

//clear inputs
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";
	}
}
