/* firefly special-purpose javascript
author:		firefly
qq:			251159405
copyright:	2008-2009
*/

//s cross browser common
//firefox xmldom
if(document.implementation&&document.implementation.createDocument){
	XMLDocument.prototype.loadXML=function(x){
		var c=this.childNodes,
			d=new DOMParser().parseFromString(x,"text/xml");
		for(var i=c.length-1;i>=0;--i) this.removeChild(c[i]);
		this.appendChild(this.importNode(d.documentElement,true));
	};

	// check for XPath implementation
	if(document.implementation.hasFeature("XPath", "3.0")){
		XMLDocument.prototype.selectNodes = function(cXPathString, xNode){
			if(!xNode) xNode=this;
			var oNSResolver=this.createNSResolver(this.documentElement);
			var aItems=this.evaluate(cXPathString, xNode, oNSResolver, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
			var aResult=[];
			for( var i=0;i<aItems.snapshotLength;++i){
				aResult[i] = aItems.snapshotItem(i);
			}
			return aResult;
		}
	    XMLDocument.prototype.selectSingleNode = function(cXPathString, xNode){
		    if( !xNode ) xNode = this;
		    var xItems = this.selectNodes(cXPathString, xNode);
		    if( xItems.length > 0 ){
		    	return xItems[0];
		    }else{
		    	return null;
		    }
	    }
		Element.prototype.selectNodes = function(cXPathString){
			if(this.ownerDocument.selectNodes){
				return this.ownerDocument.selectNodes(cXPathString, this);
			}else{
				throw "For XML Elements Only";
			}
		}
	    Element.prototype.selectSingleNode = function(cXPathString){
		    if(this.ownerDocument.selectSingleNode){
		    	return this.ownerDocument.selectSingleNode(cXPathString, this);
		    }else{
		    	throw "For XML Elements Only";
			}
		}
	}
}
String.prototype.trim=function(){
	return this.replace(/(^\s*)|(\s*$)/g, "");
}
//e cross browser

function $F(v){
	return new $Firefly(v);
}

$F.Extends=function(obj){
	for(var i in obj){
		$Firefly.prototype[i]=function(){
			return new obj[i](arguments);
		}
	}
}

function $Firefly(v){
var _this=this,
	_f=v;

this.curStr=function(v){
	var n=(v>2)?2:0;
	if(_f.length>v){
		_f=_f.substring(0,v-n)+"...";
	}
	return this;
}

this.id=function(){
	return document.getElementById(_f);
}

this.write=function(){
	document.write(_f);
}

this.goBack=function(val){
	if(!val) val=-1;
	history.go(val);
}

this.addFavorite=function(url,title){
	try{
		window.external.addFavorite(url,title);
	}catch (e){
		try{
			window.sidebar.addPanel(title,url,"");
		}catch (e){
			alert("加入收藏失败，请使用Ctrl+D进行添加");
		}
	}
}

this.setHomePage=function(obj,url){
	try{
		obj.style.behavior='url(#default#homepage)';
		obj.setHomePage(url);
	}catch(e){
		if(window.netscape){
			try {
				netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect"); 
				var prefs=Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces.nsIPrefBranch);
				prefs.setCharPref('browser.startup.homepage',url);
			}catch(e){ 
				alert("此操作被浏览器拒绝！\n请在浏览器地址栏输入“about:config”并回车\n然后将[signed.applets.codebase_principal_support]设置为'true'"); 
			}
		}
	}
}

this.toString=function(){
	return _f;
}

this.calendar=function(){
	var arrDate=_f.split(" ");
	return {
		"date":function(lang){
			var ret,ch;
			if(!lang) lang=["年","月","日"];
			if(arrDate.length>=1){
				ret=arrDate[0];
				ch=ret.indexOf("-")>0?ch="-":ret.indexOf("/")>0?ch="/":ch="";
				if(ch!=""){
					ret=ret.split(ch);
					for(var i=ret.length-1;i>=0;--i){
						ret[i]+=lang[i];
					}
					ret=ret.join("");
				}else{
					ret="";
				}
			}
			_f=ret;
			return _this;
		},
		"time":function(lang){
			var ret;
			if(!lang) lang=["时","分","秒"];
			if(arrDate.length>=2){
				ret=arrDate[1];
				if(ret.indexOf(":")>0){
					ret=ret.split(":");
					var len=ret.length;
					for(var i=0;i<len;++i){
						ret[i]+=lang[i];
					}
					ret=ret.join("");
				}else{
					ret="";
				}
			}
			_f=ret;
			return _this;
		},
		"datetime":function(lang){
			if(!lang||lang.length!=2) lang=[null,null];
			_f=this.date(lang[0])+" "+this.time(lang[1]);
			return _this;
		}
	}
}

this.image=function(p){
	var s=_f,
		d="";
	for(var i in p){
		d+=" "+i+"=\""+p[i]+"\"";
	}
	return{
		"error":function(u){
			if(!s) s=u;
			d+=" onerror=\"src='"+u+"'\"";
			return this;
		},
		"write":function(){
			_f="<img src=\""+s+"\""+d+"/>";
			_this.write();
		}
	};
}

this.ImageError=function(o){
	o.src="/Zuqan/res/img/nopic.jpg";
	o.onerror=null;
}
this.ImageAutoSize=function(image){
	var wd=image.width;
	var ht=image.height;
	var im=new Image();
	im.src=image.src;
	if(im.width>0 && im.height>0){
		if(im.width/im.height>=wd/ht){
			image.height=im.height/im.width*wd;
		}else{
			image.width=im.width/im.height*ht;
		}
	}
}

function chgRowInput($,ib){
	var inputs,il,ic
	inputs=$.getElementsByTagName('input');
	il=inputs.length;
	ic=ib?function($){$.className+=" hover";}:function($){$.className=$.className.replace(" hover","");};
	for(var i=1;i<il;++i) ic(inputs[i]);
}
function getMarkedRow(){
	if(!document.fir_global_var_marked_row) document.fir_global_var_marked_row=new Object();
	return document.fir_global_var_marked_row;
}

this.activeTable=function(id) {
	var marked_row=getMarkedRow();
	var table=document.getElementById(id);
	var rows = table.getElementsByTagName('tr');
	for ( var i = 0; i < rows.length; ++i ) {
		if ( 'odd' != rows[i].className && 'even' != rows[i].className ) {
			continue;
		}
		if ( navigator.appName == 'Microsoft Internet Explorer' ) {
			rows[i].onmouseover = function() {
				this.className += ' hover';
			}
			rows[i].onmouseout = function() {
				this.className = this.className.replace(/ hover/g, '' );
			}
		}
		rows[i].onmousedown = function(evt) {
			var unique_id;
			var checkbox;
			var event=evt||window.event;
			var src=event.target||event.srcElement;
	
			checkbox=this.getElementsByTagName('input')[0];
			if(src==checkbox||src.tagName.toLowerCase()=="td"){
			}else return true;
	
			if ( checkbox && checkbox.type == 'checkbox' ) {
				unique_id = checkbox.name + checkbox.value;
			} else if ( this.id.length > 0 ) {
				unique_id = this.id;
			} else {
				return;
			}
	
			if ( typeof(marked_row[unique_id]) == 'undefined' || !marked_row[unique_id] ) {
				marked_row[unique_id] = true;
			} else {
				marked_row[unique_id] = false;
			}
	
			if ( marked_row[unique_id] ) {
				this.className += ' marked';
			} else {
				this.className = this.className.replace(' marked', '');
			}
	
			if ( checkbox && checkbox.disabled == false ) {
				checkbox.checked = marked_row[unique_id];
				chgRowInput(this,checkbox.checked);
			}
		}

		// ... and disable label ...
		var labeltag = rows[i].getElementsByTagName('label')[0];
		if ( labeltag ) {
			labeltag.onclick = function() {
				return false;
			}
		}
		// .. and checkbox clicks
		var checkbox = rows[i].getElementsByTagName('input')[0];
		if ( checkbox ) {
			checkbox.onclick = function() {
				// opera does not recognize return false;
				this.checked = ! this.checked;
			}
		}
	}
}

this.checkAll=function(src,id){
	var marked_row=getMarkedRow();
	var rows = document.getElementById(id).getElementsByTagName('tr');
	var check=src.checked;
	var unique_id;
	var checkbox;
	
	for ( var i = 0; i < rows.length; i++ ) {
		checkbox = rows[i].getElementsByTagName( 'input' )[0];
		if ( checkbox && checkbox.type == 'checkbox' ) {
			unique_id = checkbox.name + checkbox.value;
			if ( checkbox.disabled == false ) {
				checkbox.checked=check;
				if( typeof(marked_row[unique_id]) == 'undefined' || !marked_row[unique_id] ) {
					marked_row[unique_id]=false;
				}
				if(check){
					if(!marked_row[unique_id]) rows[i].className+=' marked';
				}else{
					rows[i].className=rows[i].className.replace(" marked",'');
				}
				if(check!=marked_row[unique_id]) chgRowInput(rows[i],check);
				marked_row[unique_id]=check;
			}
		}
	}
	return true;
}

this.resetAll=function(id){
	var marked_row=getMarkedRow();
	var rows = document.getElementById(id).getElementsByTagName('tr');
	for ( var i = 0; i < rows.length; i++ ) {
		checkbox = rows[i].getElementsByTagName( 'input' )[0];
		if ( checkbox && checkbox.type == 'checkbox' ) {
			unique_id = checkbox.name + checkbox.value;
			if ( checkbox.disabled == false ) {
				checkbox.checked = false;
				marked_row[unique_id]=false;
				chgRowInput(rows[i],false)
				rows[i].className=rows[i].className.replace(' marked','');
			}
		}
	}
	return true;
}

this.confirmDel=function(form,action,title){
	if(!title) title="Warning: You determined that must carry out deletes?";
	if(confirm(title)){
		form.action=action;
		form.submit();
	}
}

this.chkAll=function(form){
	var bl=form.isCheckAll.checked;
	var ck=form.id,il;
	if(typeof(ck)=="object"&&ck.length){
		il=ck.length;
		for(var i=0;i<il;++i){
			ck[i].checked=bl;
		}
	}else{
		ck.checked=bl;
	}
}

this.xml=function(){
	return{
		"getText":function(){
			var s="";
			if(window.ActiveXObject){
				s=_f.text;
			}else{
				try{
					s=_f.childNodes[0].nodeValue;
				}catch(e){
					s="";
				}
			}
			return s;
		}
	}
}
this.substring=function(s,e){
	if(e<0) e=_f.length+e;
	_f=_f.substring(s,e);
	return this;
}

this.setProperty=function(obj){
	for(var i in obj){
		this[i]=obj[i];
	}
	return this;
}

this.trim=function(){
	_f.replace(/(^\s*)|(\s*$)/g, "");
	return this;
}
}

