/** 
 * @fileoverview This file is to be used for frame and base class of AJAX Dev Library
 *
 * @author 
 * @version 1.0 
 */
var isDebug = true;
var isMoz = (typeof document.implementation != 'undefined') && (typeof document.implementation.createDocument != 'undefined');
var isIE = (typeof window.ActiveXObject != 'undefined');
var enableMessage = true;
var basePath = "/jcl/res/default/";
var imagePath = basePath + "image/";
/**
 * ext package keyword.
 * @param {String} pkname package name
 * 
 */

function $package(pkname) {
  if(!pkname) return;
  var pkNames   = pkname.split(".");
  var pn,prevpn = "";
  for(var i=0; i<pkNames.length; i++) {
    pn = pkNames[i];
	var _s = "var adl_result=window."+prevpn+pn;
    executeScript(_s);
	if(!adl_result) {
		_s = "window."+prevpn+pn+"=new Object();";
		executeScript(_s);
	}
	prevpn += pn+".";
  }
}

function $id(id) {
	return document.getElementById(id);
}

/**
 * Exec a Script.
 * @class ExecScript is the ext keyword ExecScript.  
 * @param {String} script
 */
function executeScript(script){
	if(isIE)
		return execScript(script, "JavaScript");
	else
		return window.eval(script);
}
$package("js.lang");

js.lang.Object = Object;
/**
 * inherits superclass.
 * @param {Class} superclass
 * @param {Class} _class
 */
function $extends(superclass, _class){
	if(!superclass) return;
	var __class = _class;
	if(!__class) __class = this;
	if(__class.prototype){
		for (var key in superclass.prototype) { 
			if(!__class.prototype[key])
			if (!(key.indexOf("__") == 0)){
				__class.prototype[key] = superclass.prototype[key];
			}
		} 
	}
	else{
		var obj = __class;
		var superobject = superclass;
		if(superclass.prototype){
			superobject = superclass.prototype;
		}
		else{
		}
		obj.constructor = superobject.constructor;
		for (var key in superobject) { 
				if (!(key.indexOf("__") == 0)){
					obj[key] = superobject[key];  
				}
		} 
		superobject = null;
	}
}

$implements = $extends;

js.lang.BaseObject = function(){
}

js.lang.BaseObject.prototype = new Object();

/**
 * get super.
 * @return {Object}
 * 
 */
js.lang.BaseObject.prototype.$super = function () {
    if (!this.constructor)   
        return null;   
	var superclass = this.__superClass;
	if(!superclass)
		superclass = this.constructor.prototype.__superClass;
    if (superclass) {   
		var __EnableMessage = enableMessage;
		enableMessage = false;
        var _super = new superclass();   
		enableMessage = __EnableMessage;
        for (var key in this) {   
	        if (!this[key] && !(key.indexOf("__") == 0) && typeof(this[key])=="function")   
		        _super[key] = this[key];   
        }   
	    return _super;   
    }
	return null;
} 

/**
 * destory
 */
js.lang.BaseObject.prototype.destory = function (){
	browser.destory(this);
}

/**
 * get class name.
 *
 * @return {String}
 */
js.lang.BaseObject.prototype.getClassName = function (object){
	var obj = object;
	if(obj==null)
		obj = this;
	if(obj.className) return obj.className;
	try{
		if(!obj.constructor) 
			return typeof(object);
		var s   = obj.constructor.toString();
		s       = s.replace("function", "");
		var pos = s.indexOf("(");
		if(pos!=-1)
			s   = s.substring(0, pos);
		s = s.Trim();
		obj.className = s;
		return obj.className;
	}
	catch(e){
		return "unknown";
	}
}

/**
 * Object Unique Name.
 * @return {String}
 */
js.lang.BaseObject.prototype.getName = function(){
	if(!this.name){
	    this.name = "autoName"+Math.random();
	    this.name = this.name.replace(".","");
	}
	if(!this.hasRegisterName) {
		var js='window.'+this.name+'=this;';
		eval(js);
		this.hasRegisterName = true;
	}
	return this.name;
}
js.lang.BaseObject.prototype.hashCode = function(){
	if(!this.__HashCode){
	    this.__HashCode = this.GetClassName()+Math.random();
	    this.__HashCode = this.__HashCode.replace(".","");
	}
	return this.__HashCode;
}
$package("js.lang");

/**
 * Construct String.inherits from Object.
 * @class String is the String class.  
 *
 * @see Object Object is the base class for this
 */
js.lang.String = String;
/**
 * Trim space of string.
 * @param {String} str
 * @return {String}
 */
js.lang.String.prototype.trim = function() 
{ 
	return this.replace(/(^\s*)|(\s*$)/g, ""); 
} 
/**
 * LeftTrim space of string.
 * @param {String} str
 * @return {String}
 */
js.lang.String.prototype.leftTrim = function() 
{ 
	return this.replace(/(^\s*)/g, ""); 
} 
/**
 * RightTrim space of string.
 * @param {String} str
 * @return {String}
 */
js.lang.String.prototype.rightTrim = function() 
{ 
	return this.replace(/(\s*$)/g, ""); 
} 
/**
 * convet string to date.
 * @param {String} token
 * @param {String} mask
 * @return {Date}
 */
 js.lang.String.prototype.toDate = function(token, mask) {
	if(token == null) token = "-";
	if(mask == null) mask = "ymd";
	var dateArray = this.split(token);
	var y = dateArray[mask.indexOf("y")];
	var m = dateArray[mask.indexOf("m")] - 1;
	var d = dateArray[mask.indexOf("d")];
	return new Date(y, m, d);
}


js.lang.Math = Math;

js.lang.Math.isOddNumber = function(value){
  var v = parseInt(value / 2);
  if(v * 2 != value) return true;
  else return false;
}

/**
 * Construct System.inherits from Object.
 * @class System is the System class.  
 *
 * @see Object Object is the base class for this
 */
js.lang.System = function(){
	this.processes = new Array();
	this.windows = new Array();
	this.listeners = new Array();
}

js.lang.System.prototype = new js.lang.BaseObject();


/**
 * Show messagebox.
 * @param {String} message
 */
js.lang.System.prototype.messageBox = function(message, title) 
{ 
	if(js.ui.MessageBox){
		if(!this.messageDlg)
			this.messageDlg = new js.ui.MessageBox();
		if(!title) title = "Info";
		this.messageDlg.showMessage(title, message);
	}
	else
		alert(message);
}

js.lang.System.prototype.openWindow = function(url, parent){
	var win = new js.ui.Window();
	if(!parent) parent = document.body;
	parent.appendChild(win.getUI());
	win.iframe = document.createElement("iframe");
	win.add(win.iframe);
	win.iframe.src = url;
	return win;
}

js.lang.System.prototype.isIE  = isIE;
js.lang.System.prototype.isMoz = isMoz;

var arVersion = navigator.appVersion.split("MSIE");
var version = parseFloat(arVersion[1]);      
if ((version >= 5.5) && (version < 7) && (document.body))
	js.lang.System.prototype.isIE55_6 = true;

/**
 * Get browser type.
 * @return {String}
 */
js.lang.System.prototype.getBrowserType = function () 
{ 
	if(isIE)
		return "IE";
	else if(isMoz)
		return "Moz";
	return "Other";
}
/**
 * destory object
 * @return {js.lang.Object} object
 */
js.lang.System.prototype.destory = function (object){
	object = null;
}

/**
 * destory memory
 */
js.lang.System.prototype.gc = function (){
	if(this.isIE){
		CollectGarbage();
		setTimeout("CollectGarbage();", 1);
	}
}


js.lang.System.prototype.setTimeout = function(callback, timeout, param){
	var args = Array.prototype.slice.call(arguments,2);
	var _cb = function(){
		callback.apply(null,args);
	}
	return setTimeout(_cb, timeout);
} 


js.lang.System.prototype.setInterval = function(callback, interval, param){
	var args = Array.prototype.slice.call(arguments,2);
	var _cb = function(){
		callback.apply(null,args);
	}
	return setInterval(_cb, interval);
} 

js.lang.System.prototype.sendMessage = function(hwnd, msg, params, src){
	if(!enableMessage) return;
	if(hwnd){
		if(hwnd.processMessage)
			hwnd.processMessage(msg, params, src);
	}
	else{
		var hwnd;
		if(this.listeners[msg]){
			for(var i=0; i<this.listeners[msg].length; i++){
				hwnd = this.listeners[msg][i];
				if(hwnd.style) if(hwnd.style.display=="none") continue;
				if(hwnd.processMessage)
					hwnd.processMessage(msg, params, src);
			}
		}
		if(parent!=window){
			parent.browser.sendMessage(null, msg, params, src);
		}
	}

}

js.lang.System.prototype.addListener = function(hwnd, msg){
	if(!this.listeners[msg])
		this.listeners[msg] = new js.lang.Array();
	if(!this.listeners[msg].contains(hwnd)){
		this.listeners[msg].addItem(hwnd);
	}
}

js.lang.System.prototype.removeListener = function(hwnd, msg){
	if(this.listeners[msg])
		this.listeners[msg].removeItem(hwnd);
}

js.lang.System.prototype.language = (navigator.userLanguage==undefined?navigator.language:navigator.userLanguage).replace("-","_").toLowerCase();

var browser = new js.lang.System();

/**
 * convert message or prop.
 */
function props(propname){
	var value;
	if(typeof(ADL_LANGUAGE_PACKAGE)!="undefined")
		value = ADL_LANGUAGE_PACKAGE[propname]; 
	if(!value)
		if(typeof(TOMOS_LANGUAGE_PACKAGE)!="undefined")
			value = TOMOS_LANGUAGE_PACKAGE[propname]; 
	if(!value)
		value = propname;
	return value;
}
