/**
 * 
 *
 * @version $Id$
 * @copyright 2006 
 **/

_global = this;
_load_classes = [];

 
function Core(framework_path)
{
	this._framework_path = framework_path;
	this._script_html_incode = '<script language="javascript1.6" type="text/javascript" src="%s"></script>';
	
	for (var i in _global._load_classes)
	{
		this.load(_global._load_classes[i]);
	}
	
	_global.using = this.using;
	_global.createNamespace = this.createNamespace;
	_global.classExtends = this.classExtends;
	
//	alert('Core::constractor()');
}

Core.prototype =
{
	//_instanceHashCodeCounter: Number = 1,
	
	frameworkRun: function(framework_path, classesArr)
	{
		if (typeof(classesArr) != "array")
		{
			_global._load_classes = classesArr;
		}
		
		_global.core = new Core(framework_path);
		_global.core.capability();
	},
	
	load: function(className)
	{
		var class_path = className;
		class_path = class_path.replace(/\./g, '/');
		class_path+= '.js';
//alert (this._framework_path + class_path);
		_global.document.write(
			this._script_html_incode.replace(
				/%s/, 
				this._framework_path + 
				class_path
			) + 
			"\n"
		);
	},
	
	using: function(namespace)
	{
		if (typeof namespace == 'object')
		{
			for (var classFun in namespace)
			{
				if (typeof(namespace[classFun]) == 'function') 
				{
					var flg = false;
					var equal;
					
					try
					{
						eval('equal = ' + classFun + ' == namespace[classFun];');
					}
					catch(e)
					{
						flg = true;
					}
					
					if (flg)
					{
						eval(classFun + ' = namespace[classFun]');
					} 
					//if (equal!=undefined && !equal) 
					if (typeof equal != "" && !equal) 
					{
						throw new Error('Class objects override!');
					}
				}
			}
		}
		else
		{
			throw new Error('This is not a namespace!');
		}
	},
	
	createNamespace: function(namespaceStr)
	{
		var cobj = _global;
		var arr = namespaceStr.split('.');
		var ns = '';
		for (var i=0; i < arr.length; i++) 
		{
			if (ns!='') { ns+= '.' }
			ns+= arr[i];
			if (typeof(cobj[ arr[i] ]) != "object")//'undefined')
			{
				cobj[ arr[i] ] = new Object();
				cobj[ arr[i] ]._namespace = ns;
				cobj[ arr[i] ].classExtends = this.classExtends;
				cobj = cobj[ arr[i] ];
			}
		}
	},
	
	classExtends: function(className, parentClass, classConstructor)
	{
		//co = com.govo.classExtends('Test', Object, function() {});  //extends
		//co.static.varr = '12345'; //declear static var 
		//this._parent.apply(this, arguments); //call parent contructor
		//this._parent.prototype.run.apply(this, arguments); //call parent method
		//ClassName.prototype.method(); //call method as static
		//ClassName.varr; //use static vars
		
		_prototypeFlag = true;
		
		if (!parentClass) 
		{
			parentClass = Object;
		}
		
		var superClass = new parentClass;

		var realConstructor = function()
		{
			if (_prototypeFlag) { return; }
			
			//parentClass.apply(this, arguments); //autorun parent constructor
			
			if (classConstructor)
			{
				classConstructor.apply(this, arguments);
			}
		}

		var cp = realConstructor.prototype = superClass;

		// cp.static = realConstructor;
		cp._className = className;
		
		cp.inherits = function()
		{
			for(var i=0; i<arguments.length; i++)
			{
				var baseClass = arguments[i];

				if (baseClass && baseClass.prototype)
				{
					for(var fieldName in baseClass.prototype)
					{
						if (this[fieldName ]==undefined)
						{
							this[fieldName] = baseClass.prototype[fieldName];
						}
					}
				}
			}

			return this;
		}
		
		//if (this._namespace!=undefined)
		//alert(typeof     this._namespace);
		if (typeof(this._namespace) == "string")
		{
			this[className] = realConstructor;
		}
		cp._parent = parentClass;
		
		_prototypeFlag = false;
		
		return cp;
	},
	
	capability: function()
	{
		try
		{
			if (document.images) 
			{
				this.isCSS = (document.body && document.body.style) ? true : false;
				this.isW3C = (this.isCSS && document.getElementById) ? true : false;
				this.isIE4 = (this.isCSS && document.all) ? true : false;
				this.isNN4 = (document.layers) ? true : false;
				this.isIE6CSS = (document.compatMode && document.compatMode.indexOf("CSS1") >= 0) ? true : false;
			} 
			else 
			{
				this.isCSS = false;
				this.isW3C = false;
				this.isIE4 = false;
				this.isNN4 = false;
				this.isIE6CSS = false;
			}
			
			if (navigator.plugins && navigator.mimeTypes && navigator.mimeTypes.length) // netscape plugin architecture	
			{
				this.isNNPlugins = true;
			}
			else
			{
				this.isNNPlugins = false;
			}
				
			/*this._w3c = document.getElementById && !document.all;
			this._userAgent = navigator.userAgent.toLowerCase();
			this.is_firefox = (this._userAgent.indexOf('firefox') != -1);
			this.is_opera = (this._userAgent.indexOf('opera') != -1);
			this.is_saf = ((this._userAgent.indexOf('applewebkit') != -1) || 
				(navigator.vendor == 'Apple Computer, Inc.'));
			this.is_webtv = (this._userAgent.indexOf('webtv') != -1);
			this.is_ie = ((this._userAgent.indexOf('msie') != -1) && 
				(!this.is_opera) && (!this.is_saf) && (!this.is_webtv) && (!this.is_firefox));
			this._ie = document.all;
			*/
			
			//fix IE setInterval "feature" :)
			if (document.uniqueID)
			{
				_global.nativeSetInterval = window.setInterval;
				_global.setInterval = function(fn, ms)
				{
					var param = [];
		
					if (arguments.length<=2)
					{
						return nativeSetInterval(fn,ms);
					}
					else
					{
						for (var i=2;i<arguments.length;i+=1)
						{
							param[i-2] =  arguments[i];
						}	
					}
		
					if (typeof(fn)=='function')
					{
						return (function (fn,ms,param)
						{
							var fo = function ()
							{								
								fn.apply(_global,param);
							};			
							return nativeSetInterval(fo,ms);
						})(fn,ms,param);
					}
					else if(typeof(fn)=='string')
					{
						return  nativeSetInterval(fn,ms);
					}
					else
					{
						throw Error('setInterval Error\nInvalid function type');
					};
				}
			}
		}
		catch(e)
		{
		}
	}
	
}

FrameworkRun = Core.prototype.frameworkRun;

