ContactForm = Class.create();
ContactForm.prototype = {
	
	initialize: function (form) {
		
		this.markError();
		this.form = $(form);
		this.formElements = $('formElements');
		this.duration = 0.5;
		
		if(this.form.name.value == '' || this.form.name.value == 'Your Name') {
			this.markError("What's your good name please?");
			this.form.name.focus();
			return false;
		}
		if(/^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/.test(this.form.email.value) == false) {
			this.markError("I can't reply unless you give me your email address");
			this.form.email.focus();
			return false;
		}
		if(this.form.msg.value == '' || this.form.msg.value == 'Your Message') {
			this.markError("No message for me? What am I supposed to do with your name and email?");
			this.form.msg.focus();
			return false;
		}
		/*if(!this.form.human.checked) {
			alert("You're not human or you love spamming?");
			this.form.human.focus();
			return false;
		}*/
		
		this.formElements.startWaiting('loading');
		this.form.request({
			onSuccess: function(r) {
				
				this.formElements.stopWaiting();
				
				resp = r.responseText;
				if(resp == '1') {
					this.form.reset();
					new Effect.Fade(this.formElements, {
						duration: this.duration,
						afterFinish:function () {
							this.formElements.update("<p>I got your email. I usually reply back with in a day. So you don't really have to wait so long.<br /><br />Thanks for contacting.</p>");
							new Effect.Appear(this.formElements, {duration: this.duration});
						}.bind(this)
					});
				}
				else if(resp == '0')
					this.markError("Something went wrong with server, you just can't trust machines. Please re submit the form");
				else
					this.markError(resp);
				
			}.bind(this)
		});
		
	},
	
	markError: function (err) {
		
		$('cf_error').update(err);
		
	}
	
}


SS = Class.create();
SS.prototype = {
	
	initialize: function (slides ,controls, holder) {
		
		this.slides = slides;
		this.controls = controls;
		this.holder = holder;
		this.current = 0;
		this.duration = 0.3;
		
		this.controls.first().addClassName('current');
		this.controls.each(function(ctrl, i) {
			
			$(ctrl).observe('click', function () {
				
				if(i == this.current)
					return;
				
				this.clear();
				
				new Effect.Fade(this.holder, {
					duration: this.duration,
					afterFinish: function () {
						this.holder.ancestors().first().startWaiting('loading');
						img = new Image();
						img.onload = function() {
							this.holder.src = this.slides[i];
							new Effect.Appear(this.holder, {
								duration: this.duration
							});
							$(ctrl).addClassName('current');
							this.holder.ancestors().first().stopWaiting();
							this.current = i;
							
						}.bind(this)
						img.src = this.slides[i];
					}.bind(this)
				});
				
			}.bind(this));
			
		}.bind(this));
		
	},
	
	clear: function () {
		this.controls.each(function(ctrl, i) {
			$(ctrl).removeClassName('current');
		}.bind(this));
	}
	
}

D = Class.create();
D.prototype = {
	
	initialize: function (ctrl, id) {
		
		this.duration = 0.2;
		this.id = id;
		this.d = $('post-text-'+id);
		this.ctrl = $(ctrl);
		
		if(this.d.visible())
		{
			new Effect.BlindUp(this.d, {
				duration: this.duration,
				afterFinish: function () {
					this.ctrl.removeClassName('current');
				}.bind(this)
			});
		}
		else
		{
			new Effect.BlindDown(this.d, {
				duration: this.duration,
				afterFinish: function () {
					this.ctrl.addClassName('current');
				}.bind(this)
			});
		}
		
		return false;
	}

}

TN = Class.create();
TN.prototype = {
	
	initialize: function () {
		
		this.boxes = $$('#blognavigation .padder');
		this.widths = [];
		
		this.boxes.each(function(item, index) {
			
			$(item).observe('mouseover', this.expand.bindAsEventListener(this, index));
			$(item).observe('mouseout', this.squeeze.bindAsEventListener(this, index));
			this.widths[index] = $(item).getStyle('width');
			
		}.bind(this));
		//alert(this.boxes);
		
	},
	
	expand: function (e, id) {
		
		this.boxes.each(function(item, index) {
			
			if(index == id) {
				$(item).show();
				$(item).addClassName('fulllength');
				//$(item).setStyle({width: '698px'});
			}
			else {
				$(item).hide();
			}
			
		}.bind(this));
		//alert(id);
		
	},
	
	squeeze: function () {
		
		this.boxes.each(function(item, index) {
			
			$(item).show();
			$(item).removeClassName('fulllength');
			//$(item).setStyle({width: this.widths[index]});
			
		}.bind(this));
		
	}
	
}

CFO = Class.create();
CFO.prototype = {
	
	Listeners: {
		mouseover: 'enterHover',
		mouseout: 'leaveHover'
	},
	
	initialize: function (ctrl, action) {
		
		this.duration = 0.5;
		this.holder = $('contactbox');
		this.div = $('form');
		this.ctrl = $(ctrl);
		this.action = action;
		this.opened = false;
		this.opening = false;
		
		document.observe("dom:loaded", function() {
			this.ctrl.observe("mouseover", function() {
				this.open();
			}.bind(this))
		}.bind(this));
		
	},
	
	open: function () {
		
		if(!this.opened && !this.opening) {
			
			this.opening = true;
			new Effect.Grow(this.div, {
				direction: 'top-left',
				duration: this.duration,
				beforeStart: function () {
					this.ctrl.addClassName('current');
				}.bind(this),
				afterFinish: function () {
					if(this.div.innerHTML.strip() == '')
						this.loadForm();
					this.opened = true;
					this.opening = false;
					Event.observe($(document.body), "click", this.close.bindAsEventListener(this));
				}.bind(this)
			});
			
		}
		
	},
	
	close: function (e) {
		
		isClicked = this.isMenuClicked(Event.element(e));
		
		if(!isClicked) {
			
			Event.stopObserving($(document.body), "click", this.close.bindAsEventListener(this));
			
			if(this.opened) {
				
				new Effect.Shrink(this.div, {
					direction: 'top-left',
					duration: this.duration,
					afterFinish: function () {
						this.opened = false;
						this.ctrl.removeClassName('current');
					}.bind(this)
				});
				
			}
		} else {
			//e.stop();
		}
		
	},
	
	isMenuClicked: function (ele) {
		
		while (ele!=null && ele.id != this.div.id) {
			
			if(ele.tagName=="BODY")
				return false;
			ele=ele.parentNode;
			
		}
		
		return ele;
	},
	
	loadForm: function () {
		
		this.div.startWaiting('loading');
		new Ajax.Request(this.action, {
			method: 'get',
			parameters: 'show=true',
			onSuccess: function(r) {
				//alert(r.responseText);
				this.div.update(r.responseText);
				$('cf').writeAttribute('action', this.action);
				this.div.stopWaiting();
			}.bind(this)
		});
		
	}

}

Protoload = {
	// the script to wait this amount of msecs until it shows the loading element
	timeUntilShow: 0,
	
	// opacity of loading element
	opacity: 0.0,
	fopacity: 0.8,
	duration: 0.3,

	// Start waiting status - show loading element
	startWaiting: function(element, className, timeUntilShow) {
		if (typeof element == 'string')
			element = document.getElementById(element);
		if (className == undefined)
			className = 'waiting';
		if (timeUntilShow == undefined)
			timeUntilShow = Protoload.timeUntilShow;
		
		element._waiting = true;
		if (!element._loading) {
			var e = document.createElement('div');
			(element.offsetParent || document.body).appendChild(element._loading = e);
			e.style.position = 'absolute';
			try {e.style.opacity = Protoload.opacity;} catch(e) {}
			try {e.style.MozOpacity = Protoload.opacity;} catch(e) {}
			try {e.style.filter = 'alpha(opacity='+Math.round(Protoload.opacity * 100)+')';} catch(e) {}
			try {e.style.KhtmlOpacity = Protoload.opacity;} catch(e) {}
			
			/*var zIndex = 0;
			if (window.UI)
				if (UI.zIndex)
					zIndex = ++UI.zIndex;
			if (!zIndex)
				zIndex = ++Protoload._zIndex;
			e.style.zIndex = zIndex;*/
		}
		element._loading.className = className;
		window.setTimeout((function() {
			if (this._waiting) {
				var left = this.offsetLeft, 
					top = this.offsetTop,
					width = this.offsetWidth,
					height = this.offsetHeight,
					l = this._loading;
					
				l.style.left = left+'px';
				l.style.top = top+'px';
				l.style.width = width+'px';
				l.style.height = height+'px';
				l.style.display = 'inline';
				
				new Effect.Opacity(l, {
					duration: Protoload.duration,
					to: Protoload.fopacity,
					from: 0.0
				});
			}
		}).bind(element), timeUntilShow);
	},
	
	// Stop waiting status - hide loading element
	stopWaiting: function(element) {
		if (element._waiting) {
			/*new Effect.Fade(element._loading, {
				duration: Protoload.duration,
				to: 0.0,
				from: Protoload.fopacity,
				afterFinish: function () {
					//alert(element._loading);
					element._waiting = false;
					element._loading.parentNode.removeChild(element._loading);
					element._loading = null;
				}
			});*/
			element._waiting = false;
			element._loading.parentNode.removeChild(element._loading);
			element._loading = null;
		}
	}/*,
	
	_zIndex: 1000000*/
};

if (Prototype) {
	Element.addMethods(Protoload);
	Object.extend(Element, Protoload);
}

function cf(field, def)
{
	str = $(field).value;
	//alert(str);
	//alert(def);
	if(str == def)
		$(field).value = '';
}

function setFooter()
{
	if($('nav')) {
		if($('nav').descendants().first() && $('nav').descendants().last()) {
			if(($('nav').descendants().first().innerHTML == '' && $('nav').descendants().last().innerHTML == '')) {
				$('nav').remove();
			}
		} else if (($('nav').innerHTML).replace(/^\s+|\s+$/g, '') == '') {
			$('nav').remove();
		}
	}
}