/**
	SW Javascript Plugin,	http://www.icafe8.com
	2009-3-5  
*/



var SWCUtil={
	controls:[],
	getCurrentLocId:function(){
		return 'sw_' + Math.round(Math.random()*10000) + "_" + Math.round(Math.random()*10000);
	},
	delControl:function(_locid){
		try{
			delete this.controls[_locid];
		}catch(e){}
	},
	create:function(_funid, _fun, _option){
		var ev;
		if(typeof _option == "string"){
			_option = _option.replace("{","{ownerId:\'"+_funid+"\',");			
			ev = "new "+_fun+"("+_option+")";			
		}else if(typeof _option == "object"){
			_option = this.obj2str(_option);
			ev = "new "+_fun+"("+_option+")";
		}else if(typeof _option =="undefined"){
		 	ev = "new "+_fun+"()";
		}
		
		this.com[_funid] = eval(ev);
	},
	obj2str:function(o){ 
		var r = [];
		if(o===null) return "null";
		else if(!o.sort){
			for(var i in o)
				r.push(i+":"+this.obj2str(o[i]))
			r="{"+r.join()+"}"
		}else{
			for(var i =0;i<o.length;i++)
				r.push(this.obj2str(o[i]))
			r="["+r.join()+"]"
		}
		return r;
	},
	com:[],
	remove: function(_index){
		delete this.com[_index];
	},
	removeAll: function(){
		for(i in this.com){
			delete this.com[i];
		}
	}
}
 

function SWControl(property,option,event){
	if(option)
		this.option = option;
	else
		this.option = [];
	if(property)
		this.property = property;
	else
		this.property=[];
	if(event)
		this.event = event;
	else
		this.event=[];
	
	
	this.valField;
	
	if(property && property.valField)
		this.valField=property.valField;
	
	this.validate;
	if(property && property.validate)
		this.validate=property.validate;
	
	this._remove = this.remove;
	this.remove = function(){
		if(this._remove)this._remove.call(this);
		var ownerid = this.ownerId;
		SWCUtil.delControl(this.locid);
		if(ownerid){
			SWCUtil.remove(ownerid);
		}
	}
		
	BeanUtil.copyPro(this,property);  
	this.regist();
	
}

SWControl.prototype.reset = function(){
	this.setValue("");
}

SWControl.prototype.regist=function(){
	this.locid = SWCUtil.getCurrentLocId();
	SWCUtil.controls[this.locid] = this;
}

SWControl.prototype.getFunString = function(fun){
	var f ='';
	if(fun.indexOf("(")<0)f='()';					
	return 'SWCUtil.controls[\''+this.locid +'\'].'+fun + f;
}

SWControl.prototype.draw = function(obj){
	if(!obj){
		if(!this.loc){
			if(this.field)
				obj = this.field +"__Loc";
			else
				obj = this.id +"__Loc";
		}else{
			obj = this.loc +"__Loc";
		}
	}
	BeanUtil.draw(this.toString(),obj);
}


/**
 * @author
 */
function CString(){
    this.__string__ = [];
}
CString.prototype.append = function(str){
    this.__string__.push(str);
}
CString.prototype.toString = function(){
    return this.__string__.join("");
}





var BeanUtil ={
	
	/**

	**/
	getCurrentDirectory : function(path){
		if(path=="" || path==undefined || path==null) path = location.href
		var locHref = path;
		var locArray = locHref.split("/");
			delete locArray[locArray.length-1];
			var dirTxt = locArray.join("/");
			return dirTxt;
	},	
	/**
		
	**/
	getPluginDirectory : function(_file){
		var scripts = document.getElementsByTagName("script"); 
		if(!scripts) return;
		for(var i=0;i<scripts.length;i++){
			var jsPath = scripts[i].src;
			var jsFilename = this.getFileNameWithPath(jsPath);
			if(jsFilename==_file){			
				return this.getCurrentDirectory(jsPath);
			}		
		}	
	},
	/**
		
	*/
	getFileNameWithPath : function(path){	
		var pathArray = path.split("/");
		return pathArray[pathArray.length-1];
	
	},	
	/**
		
	*/
	include_js : function(path){
			var sobj = document.createElement('script'); 
			sobj.type = "text/javascript"; 
			sobj.src = icafe_path + path;
			var headobj = document.getElementsByTagName('head')[0]; 
			headobj.appendChild(sobj); 
	},
	/**
		
	*/
	include_js_online : function(path){		
			var sobj = document.createElement('script'); 
			sobj.type = "text/javascript"; 
			sobj.src = path;
			var headobj = document.getElementsByTagName('head')[0]; 
			headobj.appendChild(sobj);
	},
	/**
		
	*/
	include_css : function(path){     		
			var sobj = document.createElement('link'); 
			sobj.type = "text/css"; 
			sobj.rel = "stylesheet";
			sobj.href = icafe_path + path; 
			var headobj = document.getElementsByTagName('head')[0]; 
			headobj.appendChild(sobj); 
	},
	/**

	*/
	copyPro:function(target,src){
		for(var field in src){
			try{
				if(src[field]!==undefined)
					target[field]=src[field];
			}catch(e){
				alert('copyPro error');
			}
		}
	},
	/**

	*/
	classExtend:function(target,src){
		for(var field in src){
			try{
				if(src[field]!==undefined)
					target[field]=src[field];
			}catch(e){
				alert('classExtend error');
			}
		}
		target.toString = src.toString;
	},
	/**
		
	*/
	getNewLocId : function(){
		return "swControl_" + Math.round(Math.random()*50000);
	},
	/**
		
	*/
	getItemTextWithValue : function(_v, _items){
		
		var _reslut = ""; _v = _v + "";
		try{
			for(var i=0;i<_items.length;i++){
				if(_items[i].value == _v) return _items[i].text;
			}
		}catch(e){}
		return _reslut;	
	},
	/**
		
	*/
	delArrWithValue : function(_value, _items){
		var arr = [];
		if(typeof _value == "number"){
			for(var i=0;i<_items.length;i++){
				if(_items[i] && i != _value){arr.push(_items[i]);}
			}
		}else{
			for(var i=0;i<_items.length;i++){
				if(_items[i] && _items[i].value != _value){arr.push(_items[i]);}
			}
		}
		return arr;
	},
	/**
		
	*/
	delArrWithId : function(_id, _items){
		var arr = [];
		for(var i=0;i<_items.length;i++){
			if(_items[i] && _items[i].id != _id){arr.push(_items[i]);}
		}
		return arr;
	},
	/**

	*/
	render:function(_html, afterObj){
		if(afterObj && typeof(afterObj) == "string") afterObj = document.getElementById(afterObj);		
		if(!afterObj){	afterObj = document.body;}
		if(afterObj==document.body){			
			$(afterObj).append(_html); //Safari bug ,
		}else{
			$(afterObj).after(_html); //Safari bug ,
		}
		
	},
	draw:function(_html, afterObj){	
		if(afterObj && typeof(afterObj) == "string") afterObj = document.getElementById(afterObj);		
		if(!afterObj){	afterObj = document.body;}
		if(afterObj){
			$(afterObj).append(_html);
		}
	},
	/**
		run Function
	*/
	runFunction:function(fun, param){
		if(fun){
			
			if(typeof fun == "string"){
				eval(fun);
			}else if(typeof fun == "function"){
				fun(param);
			}
		}
		
	},
	/**
		
	*/
	getTopZindex : function(){
			var bd = document.body;
			var zindex = bd.getAttribute("swtopzindex");
			if(!zindex){
				zindex=this.getTopZindex2();
				bd.setAttribute("swtopzindex",zindex);
			}
			zindex++;
			bd.setAttribute("swtopzindex",zindex);
			return zindex;
	},
	/**
		
	*/
	getTopZindex2 : function(){	
			var divarr = document.getElementsByTagName("div");		 
			var ztop = 1;
			if(divarr && divarr.length){
				for(var i=0;i<divarr.length;i++){
					var z = divarr[i].style.zIndex;
					if(z){			
						z = parseInt(z); 
						if(z>=ztop)ztop=z+1;
					}
				}
			}
			return ztop;
	},
	/**
		Example: onBlurIt(document.getElementById("mywindow"));
	*/
	onBlurIt : function(e){
			if(typeof e == "string"){e = document.getElementById(e)};
			if(typeof e != "object"){swobjBlurObject=null; return false};
			swobjBlurObject = e;
			$(document).bind('mousedown', BeanUtil.__swIsBlurObj);
	},
	__swIsBlurObj : function(e){
		var blurarr = $(swobjBlurObject).find("*").andSelf();
		var inObject = false;
		for(var i=0;i<blurarr.length;i++){ if(blurarr[i] == e.target){inObject = true; break;}}
		if(!inObject){
			$(swobjBlurObject).hide();
			swobjBlurObject = null;
		}
	},
	getInternetExplorerVersion : function()
	{
		 var rv = -1; // Return value assumes failure
		 if (navigator.appName == 'Microsoft Internet Explorer')
		 {
		  var ua = navigator.userAgent;
		  var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
		  if (re.exec(ua) != null)
		   rv = parseFloat( RegExp.$1 );
		 }		
		 return rv;
	}
	

}


var swobjBlurObject = null;
var icafe_path = BeanUtil.getPluginDirectory("util.js");

BeanUtil.include_js("../js_plugin/SWValidate.js");

BeanUtil.include_js("jquery.js");

BeanUtil.include_js("SWButton/SWButton.js");
BeanUtil.include_css("SWButton/css/SWButton.css");

BeanUtil.include_js("SWMenu/SWMenu.js");
BeanUtil.include_css("SWMenu/css/SWMenu.css");

BeanUtil.include_js("SWInput/SWInput.js");
BeanUtil.include_css("SWInput/css/SWInput.css");

BeanUtil.include_js("SWSelectBox/SWSelectBox.js");
BeanUtil.include_css("SWSelectBox/css/SWSelectBox.css");

BeanUtil.include_js("SWCheckBox/SWCheckBox.js");
BeanUtil.include_css("SWCheckBox/css/SWCheckBox.css");

BeanUtil.include_js("SWSingleCitySelect/SWSingleCitySelect.js");
BeanUtil.include_css("SWSingleCitySelect/css/SWSingleCitySelect.css");

BeanUtil.include_js("SWTab/SWTab.js");
BeanUtil.include_css("SWTab/css/SWTab.css");

BeanUtil.include_js("SWGrid/SWGrid.js");
BeanUtil.include_css("SWGrid/css/SWGrid.css");

BeanUtil.include_js("SWWindow/SWWindow.js");
BeanUtil.include_css("SWWindow/css/SWWindow.css");

BeanUtil.include_js("SWDate/SWDate.js");
BeanUtil.include_css("SWDate/css/SWDate.css");

BeanUtil.include_js("SWMultiCitySelect/SWMultiCitySelect.js");
BeanUtil.include_css("SWMultiCitySelect/css/SWMultiCitySelect.css");





