var IsIE   = (typeof(window.attachEvent)!='undefined');
var IsIE7 = (IsIE)? (typeof(document.documentElement.style.msInterpolationMode)!='undefined') : false; 
var $;
if( document.getElementById ){
 $ = function (id){ return document.getElementById(id); };
}else{
 $ = function(){ return null; };
}

function createXMLHttpRequest()
{
	var httprq = null;
	if (window.XMLHttpRequest) {
		httprq = new XMLHttpRequest();
	}else{
		try { 
			httprq = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try { 
				httprq = new ActiveXObject("Microsoft.XMLHTTP"); 
			}	catch (e2){ httprq = null; }
		}
	}
	return httprq;
}

function httpRequest(url,onLoad,async)
{
	var httprq = createXMLHttpRequest();
	
	if(!httprq){ return -1; }
	httprq.onreadystatechange = function onrecv(){
			if (httprq.readyState == 4) {
					if( onLoad ){ 
						onLoad( (httprq.status == 200),httprq.responseText); 
					}
			}
	}
	if( typeof(async) == 'undefined' ){ async = true; }
	httprq.open("GET", url , (async)? true : false);
	httprq.send(null);
  	return 0;
}


function httpPost(url,param,onLoad,async)
{
	var httprq = createXMLHttpRequest();
	
	if(!httprq){ return -1; }
	
	httprq.onreadystatechange = function onrecv(){
			if (httprq.readyState == 4) {
					if( onLoad ){ onLoad((httprq.status == 200),httprq.responseText); }
			}
	}
	if( typeof(async) == 'undefined' ){ async = true; }
	var post_data = [];
	if( param ){
		for( var p in param ){
			post_data.push( p + "=" + encodeURIComponent(''+param[p]) );
		}
		post_data = post_data.join('&');
	}
	httprq.open("POST", url , ((async)? true : false));
	
	if(typeof(httprq.setRequestHeader) !=  'undefined' ){
		httprq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	}

	httprq.send(post_data);
	return 0;
}

function httpPost2(url,param,onLoad)
{
	return httpPost(url,param,function(s,res)
	{
		if( !onLoad ){ return; }
		var dat = null;
		if( res ){ eval("dat="+res+";"); }
		onLoad(s,dat);
	},true);
}



function httpRawPost(url,qstr,onLoad,async)
{
	var httprq = createXMLHttpRequest();
	
	if(!httprq){ return -1; }
	
	httprq.onreadystatechange = function onrecv(){
			if (httprq.readyState == 4) {
					if( onLoad ){ onLoad((httprq.status == 200),httprq.responseText); }
			}
	}
	if( typeof(async) == 'undefined' ){ async = true; }
	var post_data = qstr || null;
	
	httprq.open("POST", url , ((async)? true : false));
	
	if(typeof(httprq.setRequestHeader) !=  'undefined' ){
		httprq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	}

	httprq.send(post_data);
	return 0;
}


function appendCSS(cssfile,win)
{
	win = (win)? win : window;
	var doc = win.document;
	var eLink  = doc.createElement('link');
	eLink.rel  = "StyleSheet";
	eLink.type = "text/css";
	eLink.href = cssfile;
	eLink.setAttribute('extattr','__REMOTE_CSS__');
	var eHead = doc.getElementsByTagName('head');
	if( !eHead ){
		return null;
	}
	eHead[0].appendChild(eLink);
	return eLink;
}

function appendScript(src,enc,win)
{
	win = (win)? win : window;
	var doc = win.document;
	var script  = doc.createElement('script');
	script.type = "text/javascript";
	if( enc ){ 
		script.charset = enc;
	}
	script.src = src;
	var head = doc.getElementsByTagName('head');
	
	if( head && head[0] ){
		head[0].appendChild(script);
	}else{
		doc.body.appendChild(script);
	}
}

function jsonp(url,params)
{
	var src = url;
	if( params ){
		var qs = [];
		for( var v in params ){
			qs.push(v+"="+encodeURIComponent(params[v]));	
		}
		src += "?" + qs.join("&");
	}
	appendScript(src);
}

var $N = function (tag,params,attr)
{
	if(!tag){ tag = "div"; }
	if( !params ){ params = {}; }
	var e = document.createElement(tag);
	if( attr ){
		for( var a in attr ){
			e.setAttribute(a,attr[a]);
		}
	}
	if( params.id ){ e.id = params.id; }
	if( params.className ){ e.className = params.className; }
	if( params.innerHTML ){ e.innerHTML = params.innerHTML; }
	extend(e);
	if( params.parent && typeof(params.parent.appendChild)!='undefined' ){
		params.parent.appendChild(e);
	}
	return e;
}

function extend(e)
{
	e.setSize = e_setSize;
	e.setPosition = e_setPosition;
	e.show = e_show;
	e.hide = e_hide;
	return e;
}

function e_setSize(w,h)
{
	if( !isNaN(w) ){ this.style.width = w+"px"; }
	if( !isNaN(h) ){ this.style.height = h+"px"; }
}
function e_setPosition(x,y)
{
	//this.style.position = IsIE? "absolute" : "fixed";
	this.style.position = "absolute";
	if( !isNaN(x) ){ this.style.left = x+"px"; }
	if( !isNaN(y) ){ this.style.top = y+"px"; }
}
function e_show()
{
	this.style.display = "block";
}
function e_hide()
{
	this.style.display = "none";
}

function getAbsolutePosition(elem,from)
{
	if( !elem ){ return null; }
	var e = elem;
	var x = 0;
	var y = 0;
	while( e && e != from ){
		x += e.offsetLeft;
		y += e.offsetTop;
		e = e.offsetParent;
	}
	return {x:x,y:y};
}

function centering(obj)
{
	if(!obj){ return; }
	var b = document.body;
	var wsize = getWindowSize();
	var osize = getObjectSize(obj);
	var x = Math.floor(0.5*(wsize.width - osize.width));
	var y = Math.floor(0.5*(wsize.height - osize.height));
	
	if( x < 0 ){ x = 0; }
	if( y < 0 ){ y = 0; }

	obj.style.position = (IsIE && !IsIE7 )? "absolute" : "fixed";
	obj.style.left = x+"px";
	obj.style.top  = y+"px";
}

function getObjectSize(obj)
{
	if(!obj){ return {w:0,h:0}; }
	var h = (obj.offsetHeight||obj.style.pixelHeight||0);
	var w = (obj.offsetWidth||obj.style.pixelWidth||0);
	return {width:w,height:h};
}

function getWindowSize()
{
	var w = window;
	if( w.innerWidth ){
		return {width:w.innerWidth,height:w.innerHeight};
	}else{
		var dbody = document.body;
		var delem = document.documentElement;
		if( delem && delem.clientWidth ){
			return {width:delem.clientWidth,height:delem.clientHeight};
		}else if( dbody.clientWidth ){
			return {width:dbody.clientWidth,height:dbody.clientHeight};
		}
	}
	return {width:-1,height:-1};
}

function getKeyCode(e)
{
	if(window.event){ return event.keyCode; }
 	return (e.keyCode!=0)?e.keyCode:e.charCode;
}
function isEnterPressed(e){ return (getKeyCode(e)==13); }
function isEscapePressed(e){ return (getKeyCode(e)==27); }

var evtAddEventListener;
var evtRemoveEventListener;

function getMousePos(event)
{
	event = event || window.event;
	var x = event.pageX || (event.clientX + (document.documentElement.scrollLeft || document.body.scrollLeft));
  	var y = event.pageY || (event.clientY + (document.documentElement.scrollTop || document.body.scrollTop));
	return {x:x,y:y};
}


if( IsIE ){
	evtAddEventListener = function (target,evtname,func)
	{
		target.attachEvent("on"+evtname,func);
	};
	evtRemoveEventListener = function (target,evtname,func)
	{
		target.detachEvent("on"+evtname,func);
	}
}else{
	evtAddEventListener = function (target,evtname,func,bbl)
	{
		target.addEventListener(evtname,func,bbl);
	};
	evtRemoveEventListener = function (target,evtname,func,bbl)
	{
		target.removeEventListener(evtname,func,bbl);
	};
}

var Dragger;
if(!Dragger ){
	Dragger = {
		target: null,
		startDrag: function(target,e,stopFunc)
		{
			this.target = target;
			var self = this;
			var targetStyle = target.style;
			var offsetX, offsetY;
			var moveFunc, upFunc;
			var moveFuncOrig, upFuncOrig;
			
			moveFuncOrig = document.onmousemove;
			upFuncOrig = document.onmouseup;
			
			if( window.event ){
				offsetX = event.x - targetStyle.pixelLeft;
				offsetY = event.y - targetStyle.pixelTop;
				moveFunc = function(){
					targetStyle.left = Math.max(event.x-offsetX,0) + "px";
					targetStyle.top = Math.max(event.y-offsetY,0) + "px";
					return false;
				};
				upFunc = function(){
					document.detachEvent('onmousemove',moveFunc);
					document.detachEvent('onmouseup',upFunc);
					self.target = null;
					if( stopFunc ){
						stopFunc();	
					}
				};
				document.attachEvent('onmousemove',moveFunc);
				document.attachEvent('onmouseup',upFunc);
			}else if( e ){
				offsetX = e.pageX - target.offsetLeft;
				offsetY = e.pageY - target.offsetTop;
				moveFunc = function(e){
					targetStyle.left = (e.pageX-offsetX) + "px";
					targetStyle.top  = (e.pageY-offsetY) + "px";
					return false;
				};
				upFunc = function(){
					document.removeEventListener('mousemove',moveFunc,true);
					document.removeEventListener('mouseup',upFunc,true);
					self.target = null;
					if( stopFunc ){
						stopFunc();	
					}
				};
				document.addEventListener('mousemove',moveFunc,true);
				document.addEventListener('mouseup',upFunc,true);
			}else{ return false; }
			
			return true;
		}
	};
}


var Cookie = {
	vars:null,
	set: function(key, value, options){
		options = options || {};
		value = encodeURIComponent(value);
		if (options.domain) value += '; domain=' + options.domain;
		if (options.path) value += '; path=' + options.path;
		if (options.duration){
			var date = new Date();
			date.setTime(date.getTime() + options.duration * 24 * 60 * 60 * 1000);
			value += '; expires=' + date.toGMTString();
		}
		if (options.secure) value += '; secure';
		document.cookie = key + '=' + value;
		return document.cookie;
	},
	get: function(key){
		var value = document.cookie.match('(?:^|;)\\s*' + key + '=([^;]*)');
		return value ? decodeURIComponent(value[1]) : false;
	},
	getStr:function(){
		return document.cookie;	
	},
	parse:function(){
		var cstr = document.cookie;
		if(!cstr){ return {}; }
		var clist = cstr.split(/;\s*/);
		this.vars = {};
		for( var vs in clist ){
			if( clist[vs].match(/(\S+)\s*=\s*([^;]*)/) ){
				var key = RegExp.$1;
				var val = RegExp.$2;
				if( key && !this.vars[key] ){
					this.vars[key] = decodeURIComponent(val);
				}
			}
		}
	},
	flush: function(){
		for(var v in this.vars ){
			document.cookie = this.value
		}
	}
};


