﻿// By http://ccyun.com
/* ---------- Common Method Congregate ---------- */
var CommonFunctions = new Object();

CommonFunctions.Interval  = [];
CommonFunctions._isIE	  = window.navigator.appName == "Microsoft Internet Explorer"?true:false;
CommonFunctions._isIE6    = window.navigator.appVersion.indexOf("MSIE 6") != -1;
CommonFunctions._isIE5    = window.navigator.appVersion.indexOf("MSIE 5") != -1;
CommonFunctions._isIE4    = window.navigator.appVersion.indexOf("MSIE 4") != -1;
CommonFunctions._isIE3    = window.navigator.appVersion.indexOf("MSIE 3") != -1;
CommonFunctions._isIE7    = ((navigator.userAgent.indexOf("MSIE 7.0") != -1) && (navigator.userAgent.indexOf("Windows") != -1)); 
CommonFunctions._isOpera  = window.navigator.appName == "Opera"?true:false;
CommonFunctions._isFirefox  = window.navigator.appName == "Netscape"?true:false;
// trim string
CommonFunctions.trim = function(str){
    return str.replace(/(^\s*)|(\s*$)/g, "");
}

// create html element
CommonFunctions.createElement = function(type, parent) {
	var el = null;
	if (document.createElementNS) {		// use the XHTML namespace;
		el = document.createElementNS("http://www.w3.org/1999/xhtml", type);
	} else {
		el = document.createElement(type);
	}
	if (typeof parent != "undefined") {
		parent.appendChild(el);
	}
	return el;
}

CommonFunctions.createSizeString = function(size){
	if(typeof(size) == "number"){
		return(size + "px");
	}
	else if((size*1).toString()==size){
		return(size + "px");
	}
	else{
		return(size);
	}
}

CommonFunctions.getElement = function(ev) {
	if (CommonFunctions._isIE) {
		return window.event.srcElement;
	} else {
		return ev.currentTarget;
	}
};

CommonFunctions.getTargetElement = function(ev) {
	if (CommonFunctions._isIE) {
		return window.event.srcElement;
	} else {
		return ev.target;
	}
};

CommonFunctions.getInnerSize = function(){
	var w = window.innerWidth  || (document.documentElement.offsetWidth  || document.body.offsetWidth);
	var h = window.innerHeight || (document.documentElement.offsetHeight || document.body.offsetHeight);
	w = CommonFunctions._isIE ? document.documentElement.clientWidth : w;
	h = CommonFunctions._isIE ? document.documentElement.clientHeight : h;
	if(CommonFunctions._isIE3 || CommonFunctions._isIE4 || CommonFunctions._isIE5){
		w = document.body.clientWidth;
		h = document.body.clientHeight;
	}
	return [w,h];
}

CommonFunctions.getScrollSize = function(){
	var w = document.documentElement.scrollLeft  || document.body.scrollLeft;
	var h = document.documentElement.scrollTop || document.body.scrollTop;
	return [w,h];
}


CommonFunctions.copyNodeInto	= function(id,newid,target){
	var newnode = $(id).cloneNode(true);
	$(id).id = id + "tmp";
	target.insertBefore(newnode,null);
	$(id).id = newid;
	$(id + "tmp").id = id;
}


CommonFunctions.cutNodeInto	= function(id,target){
	var newnode = $(id).cloneNode(true);
	$(id).outerHTML = '';
	target.insertBefore(newnode,null);
}


CommonFunctions.getCurrentStyle = function(obj, prop) {
	if (obj.currentStyle) {
		return obj.currentStyle[prop];
	}
	else if (window.getComputedStyle) {
		prop = prop.replace (/([A-Z])/g, "-$1");
		prop = prop.toLowerCase ();
		return window.getComputedStyle (obj, "").getPropertyValue(prop);
	}
	return null;
}


CommonFunctions.startDrag = function(ev,target,r,moveevent,offset,limit){
	ev 				= ev || window.event;
	var obj 		= Event.element(ev);
	var x 			= ev.layerX || ev.offsetX,y = ev.layerY || ev.offsetY;
	var offrange 	= !offset?[0,0]:offset;
	var hp 	= vp = 0;
		var hx 	= Event.pointerX(ev) - offrange[0] - x;
		var vy 	= Event.pointerY(ev) - offrange[1] - y;
		hp 		= !r?hx:(hx<r[0]?r[0]:hx>r[1]?r[1]:hx);
		vp 		= !r?vy:(vy<r[2]?r[2]:vy>r[3]?r[3]:vy);
		target.style.left 	= hp + "px";
		target.style.top 	= vp + "px";
	document.onmousemove = function(ev){
		ev 		= ev || window.event;
		var hp 	= vp = 0;
		var hx 	= Event.pointerX(ev) - offrange[0] - x;
		var vy 	= Event.pointerY(ev) - offrange[1] - y;
		hp 		= !r?hx:(hx<r[0]?r[0]:hx>r[1]?r[1]:hx);
		vp 		= !r?vy:(vy<r[2]?r[2]:vy>r[3]?r[3]:vy);
		target.style.left 	= hp + "px";
		target.style.top 	= vp + "px";
		if(moveevent)
			moveevent();

	}
	if(!limit && _isIE){
		if(document.body.setCapture)
			document.body.setCapture();
		else if(window.captureEvents)
			window.captureEvents(Event.MOUSEMOVE|Event.MOUSEUP);
	}

	if(CommonFunctions._isIE){
		document.onselectstart = function(){return false};
	}
	document.onmouseup = CommonFunctions.stopDrag;
}


CommonFunctions.stopDrag = function(ev,stopmoveevent){
	ev 		= ev || window.event;
	var obj = Event.element(ev);

	if(stopmoveevent)
		stopmoveevent();
	if(document.body.releaseCapture)
		document.body.releaseCapture();
	else if(window.captureEvents)
		window.captureEvents(Event.MOUSEMOVE|Event.MOUSEUP);
	document.onmousemove 	= null;
	document.onmouseup		= null;
	if(CommonFunctions._isIE){
		document.onselectstart = function(){return true};
	}
}


CommonFunctions.StartFloat = function(interid,target,floatoffset,floatevent){
	var myfoffset 	= floatoffset || 1;
	var mytarget	= target;
	var myevent		= floatevent;
	var _moveLayerBy	= function(l,x,y){
		l.style.left	= (l.offsetLeft	+ x) + "px";
 		l.style.top		= (l.offsetTop	+ y) + "px";
	}
	var _float = function(){
		if  (typeof(mytarget.sX) == "undefined")  { mytarget.sX=0; };
    	if  (typeof(mytarget.sY) == "undefined")  { mytarget.sY=0; };
		var sx = CommonFunctions.getScrollSize()[0],sy = CommonFunctions.getScrollSize()[1];
		if(mytarget.sX != sx|| mytarget.sY != sy){
			var mx 		= sx>mytarget.sX?Math.ceil:Math.floor;
			var my 		= sy>mytarget.sY?Math.ceil:Math.floor;
    	    var dx 		= mx((sx - mytarget.sX)/myfoffset);
			var	dy 		= my((sy - mytarget.sY)/myfoffset);
			mytarget.sX += dx;
			mytarget.sY += dy;
			_moveLayerBy(mytarget,dx,dy);
			if(myevent)
				myevent();
		}
	}
	CommonFunctions.Interval.push({intervalid:interid,interval:eval(interid + " = setInterval(_float,50)")});
}
CommonFunctions.StopFloat = function(interid){
	for(var i=0;i<CommonFunctions.Interval.length;i++){
		if(CommonFunctions.Interval[i].intervalid == interid)
			eval("clearInterval('" + CommonFunctions.Interval[i].interval + "')");
	}
}

CommonFunctions.getAbsolutePoint = function(e){
	var t = e.offsetTop; 
	var l = e.offsetLeft; 
	var w = e.offsetWidth; 
	var h = e.offsetHeight; 
	while(e = e.offsetParent){ 
		t += e.offsetTop; 
		l += e.offsetLeft; 
	}
	return {"l": l, "t": t};
}
CommonFunctions.getPointer = function(e){
	e = e || window.event;
	var x = e.pageX || (e.clientX + (document.documentElement.scrollLeft || document.body.scrollLeft));
	var y = e.pageY || (e.clientY + (document.documentElement.scrollTop || document.body.scrollTop));
	return {"x": x, "y": y};
}
CommonFunctions.preloadImages = function() { //上海人教标A版
	var d=document;
	if(d.images){
		if(!d.MM_p)
			d.CF_p=new Array();
   		var i,j=d.CF_p.length,a=CommonFunctions.preloadImages.arguments;
		for(i=0; i<a.length; i++)
   			if (a[i].indexOf("#")!=0){
				d.CF_p[j]=new Image;
				d.CF_p[j++].src=a[i];
			}
	}
}

if(typeof(HTMLElement)!="undefined" && !window.opera){ 
	HTMLElement.prototype.__defineGetter__("outerHTML",function(){ 
		var a=this.attributes, str="<"+this.tagName, i=0;for(;i<a.length;i++) 
		if(a[i].specified) 
			str+=" "+a[i].name+'="'+a[i].value+'"'; 
		if(!this.canHaveChildren) 
			return str+" />"; 
		return str+">"+this.innerHTML+"</"+this.tagName+">"; 
	}); 
	HTMLElement.prototype.__defineSetter__("outerHTML",function(s){ 
		var r = this.ownerDocument.createRange(); 
		r.setStartBefore(this); 
		var df = r.createContextualFragment(s); 
		this.parentNode.replaceChild(df, this); 
		return s; 
	}); 
	HTMLElement.prototype.__defineGetter__("canHaveChildren",function(){ 
		return !/^(area|base|basefont|col|frame|hr|img|br|input|isindex|link|meta|param)$/.test(this.tagName.toLowerCase()); 
	}); 
}

function listTable_trMouseOver(o){
	o.style.background = "#fafafa";
}
function listTable_trMouseOut(o){
	o.style.background = "#ffffff";
}

function Cookie()
{
  this.SetValue=function(name,value,hours,path,domain,secure){
    var str=new String();
    var nextTime=new Date();
    nextTime.setHours(nextTime.getHours()+hours);
    str=name+"="+escape(value);
    if(hours)
      str+=";expires="+nextTime.toGMTString();
    if(path)
      str+=";path="+path;
    if(domain)
      str+=";domain="+domain;
    if(secure)
      str+=";secure";
    document.cookie=str;
    }
  this.GetValue=function(name){
    var rs=new RegExp("(^|)"+name+"=([^;]*)(;|$)","gi").exec(document.cookie),tmp;
    if(tmp=rs)
      return unescape(tmp[2]);
    return null;
    }
}


