window.addEvent('domready', function() {
    
	var body = $('body');
	body.getElement('#header h1').addEvent('click', function() { window.open('/home', '_self'); return false; }); // attach homepage link
	body.getElements('a[rel^=blank]').addEvent('click', function() { window.open(this.href, '_blank'); return false; }); // fix external links
    
    // fix navigation hover
	body.getElements('#navigation>li').addEvents({
		'mouseenter': function(){ this.addClass('hover');},
		'mouseleave': function(){ this.removeClass('hover'); }
	});
	
	// fix navigation selection
	$('navigation').getElements('ul').addEvents({
		'mouseenter': function(){ body.addClass(this.getParent('li').get('id').substring(3)    + '-hover'); },
		'mouseleave': function(){ body.removeClass(this.getParent('li').get('id').substring(3) + '-hover'); }
	});
	
	// fix menu selection
	body.getElements('div.pod.menu a').each(function(a) {
		if (body.className.indexOf(a.className) != -1) {
			a.addClass('selected');
		}
	});
	
	/* fix list hover/click
	body.getElements('div.list td.link a').each(function(a) {
		a.getParent('tr').addEvents({
			'click': function() {
				window.open(a.href, '_self','',true);
			}
		});
	});*/
	
	// input background	
	setBackground(body.getElement('input.text'), 'email-bg');
});

function setBackground(el, cls) {
	el = $(el);
	if (el) {
		
		if (el.get('value').clean().length == 0)
			el.addClass(cls);
	
		el.addEvents({
			'blur': function() {
				this.set('value', this.get('value').clean());
				if (el.get('value').length == 0) el.addClass(cls);
			},
			'focus': function() { el.removeClass(cls); }
		});
	}
}

function setImageUrl(el, img, url) {
	el = $(el);
	if (el) {
		el.set({
			'styles': {
				'background-image': 'url(' + img + ')'
			},
			'events': {	
				'click': function() {
					window.open(url,'_self','',true);
				}
			}
		});
	}
}

/* extended string methods */
String.implement({
	padLeft: function(length,padChar) {
		if($type(length)!="number"){
			if($type(length)=="string") length.toInt();
			else return this;
		}
		if(length<=this.length) return this;
		else {
			if($type(padChar)!="string") padChar=" "
			tmp=""
			for(i=0;i<(length-this.length);i++) tmp=tmp+padChar
			return tmp+this;
		} 
	},
	htmlEncode: function() {
		var el = new Element('div').set('text', this);
		return el.get('text').replace(/&/, '&amp;').clean();
	},
	toProperCase: function() {
		return this.toLowerCase().replace(/^(.)|\s(.)/g, function($1) { return $1.toUpperCase(); });
	}
});
