var common = {

	init:function(){

		function initSearchInput(){
			var input = document.getElementById('searchinput');
			if(input.value == ''){
				input.value = input.title;
				common.addClassName('standby', input);

			}
			input.onfocus = function(){
				if(this.value == this.title){
					this.value = '';
					common.removeClassName('standby', input);
				}
			}
			input.onblur = function(){
				if(this.value == ''){
					this.value = this.title;
					common.addClassName('standby', input);
				}
			}
		}
		
		function initAnchors(){
			var anchors = document.getElementsByTagName('a');
			
			for(var i=0; i<anchors.length; i++){
				if(common.hasClassName('anchor', anchors[i])){
					
					anchors[i].rel = anchors[i].innerHTML;
					anchors[i].innerHTML = '<span>'+anchors[i].innerHTML + '</span><span class="sharp">#</span>';
					
					
					anchors[i].onmouseover = function(){
						this.firstChild.innerHTML = this.id;
						
					}
					anchors[i].onmouseout = function(){
						this.firstChild.innerHTML = this.rel;
					}
				}
			}
		}

		function initRoundedBoxes(){

			var elements = new Array('div', 'img', 'p', 'form', 'pre', 'table');
			var boxes;
			var i;

			for(var e=0; e<elements.length; e++){
				boxes = document.getElementsByTagName(elements[e]);
				for(i=0; i<boxes.length; i++){
					if(common.hasClassName('frame',boxes[i]) || elements[e] == 'pre' || elements[e] == 'table'){
						boxes[i].style.backgroundImage = 'url('+common.getMetaContent('basehref')+'shell/layout/box'+boxes[i].offsetWidth+'x'+boxes[i].offsetHeight+'.png)';
					}
				}
			}
		}

		common.addEvent(window,'load',initSearchInput);
		common.addEvent(window,'load',initRoundedBoxes);
		common.addEvent(window,'load',initAnchors);

	},
	parseInt:function(value){
		var intval = parseInt(value);
		return 	isNaN(intval) ? 0 : intval;
	},
	addEvent:function(element, eventName, func){
		if(element.addEventListener){
			element.addEventListener(eventName, func, false);
		}else if (element.attachEvent){
			element.attachEvent('on'+eventName, func);
		}
	},
	removeClassName:function(cName,node){
		node.className = node.className.replace(new RegExp("\\s?"+cName+"\\b"), "");
	},
	addClassName:function(cName,node){
		if(common.hasClassName(cName,node)){1
			return;
		}
		node.className += (node.className == "" ? cName : " "+cName);
	},
	hasClassName:function(cName,node){
		return (node.className.match(new RegExp("\\s?"+cName+"\\b")));
	},
	getFirstChildByName:function(cName,node){
		var nodes = node.childNodes;
		for(var j=0;j<nodes.length;j++){
			if(nodes[j].nodeName == cName){
				return nodes[j];
			}
		}
		return false;
	},
	strReplace:function(search,replace,subject){
		var arr = subject.split(search);
		return arr.join(replace);
	},
	getMetaContent:function(name){
		var arr = document.getElementsByTagName('meta');

		for(var i=0; i<arr.length; i++){
			if(arr[i].name == name){
				return arr[i].content;
			}
		}
		return '';
	},
	createCookie:function(name,value,days) {
		if (days) {
			var date = new Date();
			date.setTime(date.getTime()+(days*24*60*60*1000));
			var expires = "; expires="+date.toGMTString();
		}
		else var expires = "";
		document.cookie = name+"="+value+expires+"; path=/";
	},
	readCookie:function(name) {
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		for(var i=0;i < ca.length;i++) {
			var c = ca[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
		}
		return null;
	},
	eraseCookie:function(name) {
		common.createCookie(name,"",-1);
	}
}

common.init();
