//	Helper
String.prototype.isin = function(s){
	return (this.indexOf(s) != -1);
}

Array.prototype.isin = function(item){
	for(var i=0; i<this.length; i++)if(this[i] == item)return true;
	return false;
}
Array.prototype.remove = function(item){
	var nArray = [];
	for(var i=0; i<this.length; i++)if(this[i] != item)nArray.push(this[i]);
	return nArray;
}

var dPx = function(str){
	if(str != undefined && str.length)return parseInt(str.replace("px",""));
	else return 0;
}


var sushi =	{
	init	: (function(){ window.tmpSushi = this; return true; })(),

	browser : {
		//	Detect ie
		ua : navigator.userAgent,
		ie : function(){
			var ua = navigator.userAgent;
			if(ua.isin('MSIE')){
				window.XMLHttpRequest = function(){ return new ActiveXObject('MSXML2.XMLHTTP.3.0')};
				if(ua.isin('9.0'))return 9;
				else if(ua.isin('8.0'))return 8;
				else if(ua.isin('7.0'))return 7;
				else if(ua.isin('6.0'))return 6;
				else return 5;
			}
			return false;	
		}(),
		konqueror : navigator.userAgent.isin('Konqueror'),
		safari 	  : navigator.userAgent.isin('Safari'),
		opera 	  : navigator.userAgent.isin('Opera'),
		gecko 	  : navigator.userAgent.isin('Gecko'),
		webkit 	  : navigator.userAgent.toLowerCase().isin("webkit")
	},

	event : function(){
		var functions = [];
		this.add  = function(f){if(typeof f == 'function')functions.push(f);}
		this.call = function(e){
			if(!e && window.event)e = event || null;
			for(var i=0; i<functions.length; i++)functions[i](e);
		};
	},

	//	Onload Event
	onload : new (function(base){
		var done = false;
		var func = [];
	
		var fire = function(){
			done = true;
			while(func.length)func.shift()();
			if(!navigator.userAgent.isin("MSIE"))document.removeEventListener("DOMContentLoaded",arguments.callee,false);
		}
		
		this.add = function(f){
			if(!done && typeof f == 'function'){
				func.push(f);
				return true;
			}
			else return false;
		}
	
		if(navigator.userAgent.isin("MSIE")){
			document.onreadystatechange = function(){
				if(document.readyState == 'complete')fire();	
			}
		}
		else document.addEventListener("DOMContentLoaded",fire,false);
	})(),

	//	Load a css file
	includeStyle : function(path){
		var el = document.createElement('link');
		el.type = 'text/css';
		el.rel	= 'stylesheet';
		el.href = path;
		document.getElementsByTagName('head')[0].appendChild(el);
	},

	//	Load as js file
	includeScript : function(scripts){
		if(!sushi.http)return false;
		
		var scrpt  = document.createElement("script");
		scrpt.text = sushi.http.post("getjs/"+scripts.join(" "));
		scrpt.type = "text/javascript";
		document.getElementsByTagName("head")[0].appendChild(scrpt);
		
		return true;
	},

	//	Write alert Message
	alert : function(msg, offsetTop, onOk){
		var body = document.body;
		var html = body.parentNode;
		body.style.overflow = "hidden";
	
		var ov  = document.createElement("div");	//	Overlay
		var box = document.createElement("div");	//	Box
		box.innerHTML = "<pre class=\"sushi_alert_mesg\">"+msg+"</pre><br/>";
		body.appendChild(box);

		

		var ok = document.createElement("div");		//	OK Button
		ok.innerHTML = "OK";
		
		if(sushi.browser.ie && sushi.browser.ie < 8){
			ov.style.setAttribute("cssText","position: absolute; top:0px; left:0px; width:3000px; height:3000px; background:#808080; opacity:0.4; z-index:100; filter:Alpha(opacity=30);");
			ok.style.setAttribute("cssText", "padding:3px 15px 3px 15px; border:2px outset #d3cbba; display:inline; cursor:pointer;");
			box.style.setAttribute("cssText", "text-align:justify; font-size:12px; padding:20px; border:2px outset #d3cbba; background:#f9efdb; position:absolute; top:0px; left:0px; color:black; z-index:1000;");
		}
		else{
			ov.setAttribute("style", "position: absolute; top:0px; left:0px; width:3000px; height:3000px; background:#808080; opacity:0.4; z-index:100; filter:Alpha(opacity=30);");
			ok.setAttribute("style", "padding:3px 15px 3px 15px; border:2px outset #d3cbba; display:inline; cursor:pointer;");
			box.setAttribute("style", "text-align:justify; font-size:12px; padding:20px; border:2px outset #d3cbba; background:#f9efdb; position:absolute; top:0px; left:0px; color:black; z-index:1000;");
		}

		ok.onclick = function(){
			body.removeChild(box);
			body.removeChild(ov);
			body.style.overflow = "visible";
			if(onOk)onOk();
		}

		box.appendChild(ok);
		body.appendChild(ov);
		
		if(box.offsetWidth  < 200)box.style.width  = "150px";
		if(box.offsetHeight < 60 )box.style.height = "60px";
		if(!offsetTop)box.style.top  = (html.offsetHeight - box.offsetHeight)/2 + "px";
		else box.style.top = offsetTop + "px";
		box.style.left = (html.offsetWidth  - box.offsetWidth)/2 + "px";
	},

	//	Check if Element 'child' is Child of 'of'
	isChildOf : function(child,of){
		if(child == of)return false;
		var parent = child.parentNode;
		while((child != document.body.parentNode) && parent){
			if(parent == of)return true;
			parent = parent.parentNode;
		}
		return false;
	},

	preload : function(imgArray){
		var load = function(url){
			var tmpImg = new Image()
			tmpImg.onload = function(){
				if(imgArray.length)load(imgArray.shift());
			}
			tmpImg.src = url;
		}
		if(imgArray.length)load(imgArray.shift());
	},
	
	//	Cheats
	target : function(el,type){
		el.target = type;
	}
};
