/* SkySoft.js
 * ==============
 * kernel of SkySoft js source, copyright by oursky.net, all rights reserved.
 * any use of this code or part of these code, must keep these lines
 *
 */
 


// {{{ error handling

Function.prototype.getDecl = function() {
	var match=/function\s*([^\(]*)\(([^\)]+)\)/.exec(String(this));
	if (match) {
		return {name:match[1], args:match[2].split(/[, ]+/g)};
	}
	return {name:'',args:[]};
}
Function.prototype.getBody = function() {
	var m = String(this).split(/\{/);
	delete m[0];
	return m.join('{');
}
String.prototype.compatToLine = function() {
	return this.replace(/\s{2,}/g, '').replace(/[\r\n]+/g, '/');
}
String.prototype.cut = function(len) {
	return (this.length > len) ? this.substr(0, len) + "..." : String(this);
}
String.prototype.dump = function() {
	return "'" + this.replace(/\\/g, '\\\\')
		.replace(/\n/g, "\\n")
		.replace(/\r/g, "\\r")
		.replace(/\t/g, "\\t")
		.replace(/'/g, "\\'") + "'";
}
Error.prototype.getBacktrace = function(func) {
	try {
		var x = '';
		var stack = this.stack;
		if (stack) {
			x = stack + "\n";
		}
		else {
			if (!func) return "";
			var decl = func.getDecl();
			x += "- " + decl.name +"(";
			for (var a = 0, c = func.arguments.length; a < c; a ++) {
				if (a > 0) x += ", ";
				x += (decl.args[a]||'') + '=';
				var lm = func.arguments[a];
				switch (typeof(lm)) {
				case 'function':
					x += 'F'; lm = lm.getBody();
				case 'string':
					x += lm.cut(40).dump(); break;
				case 'undefined':
					x += 'N'; break;
				default:
					x += String(lm);
				}
			}
			x += ")\n";
			if (decl.name.indexOf('anony') != -1 || !decl.name) {
				x += "  " + func.getBody().compatToLine().cut(40) + "\n";
			}

			if (func.caller) {
				x += this.getBacktrace(func.caller);
			}
		}
		return x;
	}
	catch(wa) {
		return "[Cannot get stack trace]\n";
	}
}
 
 

function assert(v)
{
	if (!v) {
		throw Error("assertion failed");
	}
}

 
 
// }}}
// {{{ SSObject
var SSObj = function() {
	this.loaded = false;
	this.handlerId = 0;
}

var SSp = SSObj.prototype;

 
 
 
// fix IE memory leak?
SSp.cleanListen = function() {
	for (var i in this.listens) {
		this.listens[i][0].detachEvent('on' + this.listens[i][1]);
	}
}
// {{{ listen: listen object event
// target name [thisobj] handler
SSp.listen = function(target, name, v1, v2) {
	assert(typeof target != 'string');
	if (v2) {
		var thisobj = v1;
		var handler = v2;
	}
	else {
		var thisobj = target;
		var handler = v1;
	}
	var cb = this.makeEventHandler(target, thisobj, handler);
	if (target.addEventListener) {
		target.addEventListener(name, cb, false);
		return cb;
	}
	else if (target.attachEvent) {
		if (!this.listens) {
			this.listens = [];
			var _SS = this;
			this.listen(window, 'unload', function(){_SS.cleanListen()});
		}
		this.listens[this.listens.length] = [target, name];
		target.attachEvent('on' + name, cb);
		return cb;
	}
	else {
		var key = 'on' + name;
		var oldhandler = target[key];
		if (oldhandler) {
			if (typeof oldhandler == 'string') {
				oldhandler = this.makeEventHandler(target, target, oldhandler);
			}
			target[key] = function(e) {
				oldhandler(event);
				cb(event);
			}
		}
		else {
			target[key] = cb;
		}
		return cb;
	}
}
// }}}
// {{{ makeEventHandler: return a optimized event handler
SSp.makeEventHandler = function(target, thisobj, handler) {
	var hid = '__SS_handler__' + (this.handlerId ++);
	if (is.ie) {
		if (handler instanceof Function &&
			handler.constructor.call instanceof Function) {

			var cb = function(event) {
				return handler.call(thisobj, event||window.event, target);
			}
		}
		else {
			if (typeof handler == 'string') {
				handler = function(event) {
					eval(handler);
				}
			}
			thisobj[hid] = handler;
			var cb = function(event) {
				return thisobj[hid](event||window.event, target);
			}
		}
	}
	else {
		var cb = function(event) {
			if (!event) {
				return {}.undefined;
			}
			if(handler instanceof Function) {
				if (handler.call instanceof Function) {
					ret = handler.call(thisobj, event, target);
				}
				else {
					thisobj[hid] = handler;
					ret = thisobj[hid](event, target);
				}
			}
			else if (handler) {
				ret = eval(handler);
			}
			else {
				return {}.undefined;
			}
			if (ret === false) {
				event.returnValue = ret;
				if (event.preventDefault) {
					event.preventDefault();
				}
			}

			if (event.cancelBubble && event.stopPropagation) {
				event.stopPropagation();
			}
			return ret;
		}
	}
	cb.handlerId = hid;
	return cb;
}
// }}}

// dreamwaver's findObject
SSp.obj = SSp.findObject = function(n, d) {
	var p,i,x;
	if(!d) d=document;
	if((p=n.indexOf("?"))>0&&parent.frames.length) {
		d=parent.frames[n.substring(p+1)].document;
		n=n.substring(0,p);
	}
	if(!(x=d[n])&&d.all) x=d.all[n];
	if(!x && d.getElementById)
		x=d.getElementById(n);
	for (i=0;!x&&i<d.forms.length;i++)
		x=d.forms[i][n];

	for(i=0;!x&&d.layers&&i<d.layers.length;i++)
		x=this.findObject(n,d.layers[i].document);
	return x;
}

 

SSp.getBody = function(doc) {
	doc = doc||document;
	return !is.cssCompatMode ? doc.documentElement : doc.body;
}

// create object
var SS, SkySoft;
SS = SkySoft = new SSObj();

// install handler
if (self.DynAPI||self.dynapi) {
	if (!self.dynapi) {
		dynapi = self.DynAPI;
	}
}
// }}}
// {{{ cookie functions
SkySoft.cookies = {
	save : function(name, value, days, path, domain, raw) {
		if (typeof name == 'object') {
			value = name.value;
			days = name.days;
			path = name.path;
			name = name.name;
			domain = name.domain;
			raw = name.raw;
		}
		var cookie;
		if (raw) {
			cookie = name + "=" + value;
		}
		else {
			cookie = encodeURIComponent(name) + "=" + encodeURIComponent(value);
		}
		if (days) {
			var date=new Date();
			date.setTime(date.getTime()+(days*24*60*60*1000));
			cookie += "; expires="+date.toGMTString();
		}
		if (typeof path == 'undefined') {
			cookie += '; path=/';
		}
		else if (path != '.') {
			cookie += '; path=' + path;
		}
		if (domain) {
			cookie += '; domain=' + domain;
		}
		document.cookie = cookie;
	},
	read : function(name) {
		var nameEQ = encodeURIComponent(name) + "=";
		var nameLN = nameEQ.length;
		var ca = document.cookie.split(/[; ]+/);
		for (var i in ca) {
			if (ca[i].substring(0, nameLN) == nameEQ) {
				return decodeURIComponent(ca[i].substring(nameLN));
			}
		}
		return {}.undefined;
	},
	del : function(name) {
		this.save(name,"",-1);
	}
};
SkySoft.cookies.write = SkySoft.cookies.save;

if (self.DynAPI) {
	self.DynAPI.cookies = SkySoft.cookies;
}
// }}}
// {{{ browser detection
/* code from dynapi3, Powered by oursky.net
 */
function Browser() {
	var b = navigator.appName;
	var v = this.version = navigator.appVersion;
	var ua = navigator.userAgent.toLowerCase();
	this.v = parseInt(v);
	this.safari = ua.indexOf("safari") != -1; // always check for safari & opera
	this.opera = ua.indexOf("opera") != -1; // before ns or ie
	this.ns = !this.opera && !this.safari && (b=="Netscape");
	this.ie = !this.opera && (b=="Microsoft Internet Explorer");
	this.gecko = ua.indexOf('gecko') != -1;

	if (this.ns) {
		this.ns4 = (this.v==4);
		this.ns6 = (this.v>=5);
		this.b = "Netscape";
	}
	else if (this.ie) {
		this.v=parseFloat(v.substr(v.indexOf("MSIE")+4));
		if (this.v > 8) {}
		else if (this.v >= 7) {this.ie7 = true;}
		else if (this.v >= 6) {this.ie6 = true;}
		else if (this.v >= 5) {this.ie5 = true;}
		else if (this.v >= 4) {this.ie4 = true;}
		this.b = "MSIE";
	}
	else if (this.opera) {
		this.v=ua.substr(ua.indexOf("opera")+6,1) * 1; // set opera version
		if (this.v >= 7) {this.opera7 = true;}
		else if (this.v >= 6) {this.opera6 = true;}
		this.b = "Opera";
	}
	else if (this.safari) {
		this.ns6 = (this.v>=5);	// ns6 compatible correct?
		this.b = "Safari";
	}
	this.dom = (document.createElement
		&& document.appendChild
		&& document.getElementsByTagName)? true : false;
	this.def = (this.ie||this.dom);
	this.win32 = ua.indexOf("win")>-1;

	if (this.win32) {
		this.win = true;
		if (ua.indexOf("nt")>-1) {
			this.nt = true;
			if (ua.indexOf("nt 5")>-1) {
				this.nt5 = true;
				if (ua.indexOf("nt 5.1")>-1) {
					this.nt51 = true
				}
			}
		}
	}
	else if (ua.indexOf("mac")>-1) { this.mac = true; }
	else if (ua.indexOf("x11")>-1) { this.x = true; }

	this.filter = this.ie && this.v>=6;
	this.other = (!this.win32 && !this.mac);
	this.detectCharset();

	var minver = navigator.appMinorVersion||'';
	if (minver.indexOf('SP1')) this.sp1 = true;

	this.hand = this.ie ? 'hand' : 'pointer';
	this.blankpage = this.opera ?
		(self.siteurl||'') + '/blank.html' : 'about:blank';
	this.cssCompatMode = !document.compatMode||
		document.compatMode=='BackCompat';

	this.supported = (this.def||this.ns4||this.ns6||this.opera);
	if (!this.supported) {
		this.setStatus('无法识别的浏览器. 本站脚本可能不支持该浏览器.');
	}

	if (!window.navigate) {
		window.navigate = function(url) {
			window.location.href = phpurl(url);
		}
	}
}
Browser.prototype.detectCharset = function () {
	var _this = this;
	if (!self.document) { setTimeout(function(){_this.detectCharset()}, 0); return }
	this.en = this.english = self.english;
	this.charset = (document.charset||document.characterSet||"").toLowerCase();
	var c = this.charset;
	this.cht	= (c=="big5"||c=="big5-hkscs"||c=="euc-tw");
	// gb2312 hz-gb-2312 x-gbk
	this.chs	= (c.indexOf("gb")>=0?true:false);
}
Browser.prototype.setStatus = function(str) {
	if (this.ie7) {
		if (!this.docTitle) {
			this.docTitle = document.title;
		}
		document.title = this.docTitle + ' | ' + str;
	}
	else {
		window.status = str;
	}
}

var is = SkySoft.browser = new Browser();
var ie = is.ie;
var ns = is.ns;
// }}}
SS.listen(window, 'load', function() {
	SS.loaded = true;
 
	init_button_overout(true);
	init_button_overout(false);
	var forms = document.forms;
	var forms_l = forms.length;
	for (var i = 0; i < forms_l; i ++) {
		var elements = forms[i].elements;
		var elements_j = elements.length;
		var found = false;
		for (var j = 0; j < elements_j; j ++) {
			if (!found && elements[j].type == 'submit') {
				forms[i].defaultSubmitElement = elements[j];
				found = true;
				if (is.ie) {
					break;
				}
			}
			if (!is.ie) {
				form_init_focus_event(forms[i], elements[j], true);
				form_init_focus_event(forms[i], elements[j], false);
			}
		}
		if (is.ie) {
			form_init_focus_event(forms[i], null, true);
			form_init_focus_event(forms[i], null, false);
		}
	}
	var ae = document.activeElement;
	if (ae && ae.nodeName == '#text') {
		ae = ae.parentNode;
	}
	if (ae && ae.nodeName != 'TEXTAREA' && ae.form && ae.form.defaultSubmitElement) {
		ae.form.defaultSubmitElement.className += ' btnactive';
		ae.form.activeButton = ae.form.defaultSubmitElement;
	}
});
// {{{ over effect
function init_button_overout(over)
{
	SS.listen(document, over ? 'mouseover' : 'mouseout', function(e) {
		e = e||window.event;
		var o = e.srcElement||e.target;
		if (!o) {
			return; // in case when the page is unloading?
		}
		if (o.nodeName == '#text') {
			o = o.parentNode;
		}
		if (over && o.disabled) {
			return;
		}
		var css, reg;
		switch (o.nodeName) {
		case 'INPUT':
			switch (o.type)
			{
			    case 'submit':
			    case 'reset':
			    case 'image':
			    case 'button':
				    css = ' btnover';
				    reg = / ?\bbtnover\b/g;
				    break;
			    case 'text':
				    css = ' inpover';
				    reg = / ?\binpover\b/g;
				    break;
			    default:
				    return;
			}
			break;

//		case 'IMG':
//			css = ' imgover';
//			reg = / ?\bimgover\b/g;
//			break;

		case 'LABEL':
			css = ' lblover';
			reg = / ?\blblover\b/g;
			break;

		default:
			return;
		}
		if (over) {
			o.className += css;
		}
		else {
			o.className = o.className.replace(reg, '');
		}
	});
}
// }}}
// {{{ focus effect
function form_init_focus_event(form, o, isin) {
	SS.listen(o||form, o ? (isin ? 'focus' : 'blur') : (isin ? 'focusin' : 'focusout'),  function(e) {
		e = e||window.event;
		var o = e.srcElement||e.target;
		if (o.nodeName == '#text') {
			o = o.parentNode;
		}
		var updatefocus = function() {
			form.timerFocus = 0;
			var btn = null;
			if (isin) {
				btn = form.defaultSubmitElement;
				switch (o.nodeName) {
				case 'INPUT':
					switch (o.type) {
					case 'submit':
					case 'reset':
					case 'image':
					case 'button':
						btn = o;
					}
					break;

				case 'TEXTAREA':
				case 'IFRAME':
				case 'A':
					btn = null;
					break;
				}
			}
			var ab = form.activeButton;
			if (ab != btn) {
				if (ab)  { ab.className = ab.className.replace(/ ?\bbtnactive\b/g, ''); }
				if (btn) { btn.className += ' btnactive'; }
			}
			form.activeButton = btn;
		}
		if (form.timerFocus) {
			window.clearTimeout(form.timerFocus);
		}
		if (isin) {
			updatefocus();
		}
		else {
			form.timerFocus = window.setTimeout(updatefocus, 1);
		}
	});
}
  
  
 
// }}}
// {{{ preload css backgroundImage
SS.listen(window, 'load', function() {
	try {
		var ss = document.styleSheets;
	} catch(e) {
		return;
	}
	var cnt = 0;
	if (ss) {
		var rs, b;
		for (var i = 0, ic = ss.length; i < ic; i ++) {
			try {
				if (ss[i].cssText && ss[i].cssText.indexOf("url") == -1) {
					continue;
				}
			} catch(e) {
				break;
			}
			rs = ss[i].rules || ss[i].cssRules;
			if (!rs) {
				continue;
			}
			for (var j = 0, jc = rs.length; j < jc; j ++) {
				b = rs[j].style.backgroundImage.toString();
				if (b.match(/^url\(/)) {
					 
				}
				cnt ++;
			}
		}
	}
});
// }}}