
/**
 * DynInterface
 */
DynInterface = {}

DynInterface.className   = 'DynInterface';
DynInterface.version     = '3.19';

DynInterface.path        = 'DynInterface/';
DynInterface.classes     = new Object();
DynInterface.supported   = false;
DynInterface.initialized = false;
DynInterface.loaded      = false;
DynInterface.objects     = [];

DynInterface.setPath = function(path) { DynInterface.path = path; }

DynInterface.load = function(path) {
	DynInterface.setPath(path);
	DynInterface._import("DynLayer");
}

DynInterface._import = function(cName,cPath) {
	if (!cPath) cPath = 'core';
	if (typeof DynInterface.classes[cName] == 'undefined') {
		DynInterface.getClass(cName,DynInterface.path + cPath);
		DynInterface.classes[cName] = [cPath,true]
	}
}

DynInterface.getClass = function(name,path) { document.write('<script type="text\/javascript" src="' + path + '/' + name + '.js"><\/script>'); }

DynInterface.initialize = function() {

	var DI = DynInterface;
	var DE = DynEvent;

	if (DI.initialized || !DI.supported) return;

	DI.onload = new DE();
	DI.onunload = new DE();
	DI.onresize = new DE();

	var ddo = new DynObject(document,null);
	DI.onmousemove = ddo.onmousemove = new DE();
	DI.onmousedown = ddo.onmousedown = new DE();
	DI.onmouseup   = ddo.onmouseup   = new DE();
	DI.onclick     = ddo.onclick     = new DE();
	DI.ondblclick  = ddo.ondblclick  = new DE();
	document.dynObjectIndex  = ddo.index;

	if (isValid(window.onload))   DI.onload.addClient(onload);
	if (isValid(window.onunload)) DI.onunload.addClient(onunload);
	if (isValid(window.onresize)) DI.onresize.addClient(onresize);

	onload = DI.handleOnload;
	onunload = function() { DynInterface.onunload.invoke(); for (var i = 0; i < DynInterface.objects.length; i++) { if (DynInterface.objects[i].destroy) DynInterface.objects[i].destroy(); } }
	onresize = function() { DynInterface.onresize.invoke(); }

	DynInterface.initialized = true;
}

DynInterface.handleOnload = function() {

	var d = document;
	var DI = DynInterface;
	var DE = DynEvent;
	
	if (d.body) { d.body.dynObjectIndex = d.dynObjectIndex; }

	if (isValid(d.onmousemove)) DI.onmousemove.addClient(d.onmousemove);
	if (isValid(d.onmousedown)) DI.onmousedown.addClient(d.onmousedown);
	if (isValid(d.onmouseup))   DI.onmouseup.addClient(d.onmouseup);
	if (isValid(d.onclick))     DI.onclick.addClient(d.onclick);
	if (isValid(d.ondblclick))  DI.ondblclick.addClient(d.ondblclick);

	d.onmousemove = function(e) { return DI.getObject(d.dynObjectIndex).onmousemove.handle(e,true); }
	d.onmousedown = DE.handle;
	d.onmouseup   = DE.handle;
	d.onclick     = DE.handle;
	d.ondblclick  = DE.handle;

	DynInterface.onload.invoke();
	DynInterface.loaded = true;
}

DynInterface.getObject = function(index) { return DynInterface.objects[index]; }

d = document;
n = navigator;
v = n.appVersion.toUpperCase();
ua = n.userAgent.toUpperCase();

ns    = (ua.indexOf("GECKO") > -1);
opera = (ua.indexOf("OPERA") > -1);
ie    = (ua.indexOf("MSIE") > -1 && !opera);
ie5   = (ie && v.indexOf("IE 5") > -1);
ie55  = (ie && v.indexOf("IE 5.5") > -1);
ie6   = (ie && v.indexOf("IE 6") > -1);
dom   = (d.createElement && d.appendChild) ? true : false;
win   = (ua.indexOf("WIN") > -1);
mac   = (ua.indexOf("MAC") > -1);

xhtml = false;

cookiesEnabled = (typeof n.cookieEnabled != 'undefined') ? n.cookieEnabled : null;

DynInterface.supported = ( dom || ie5 || ie6 );

/**
 * Constants
 */

HORIZONTAL = 1;
VERTICAL = 2;

UP = 1;
DOWN = 3;

TOP = 1;
RIGHT = 2;
BOTTOM = 3;
LEFT = 4;

/**
 * Core extensions
 */
 
function rVal(v,minV)  { return v > minV ? v : minV };
function rMax(v,maxV)  { return v < maxV ? v : maxV };
function rInt(v)       { v = parseInt(v); return isInt(v) ? v : 0 };

function isDefined(v)  { return typeof v != 'undefined'; }
function notDefined(v) { return typeof v == 'undefined'; }

function isNull(v)     { return v == null && isDefined(v)  && v != 0; }
function notNull(v)    { return v != null || notDefined(v) || v == 0; }

function isValid(v)    { return isDefined(v) && notNull(v) && v; }

function isNumber(v)   { return typeof v == 'number'; }
function notNumber(v)  { return typeof v != 'number'; }

function isString(v)   { return typeof v == 'string' ; }
function isBoolean(v)  { return typeof v == 'boolean'; }
function isObject(v)   { return typeof v == 'object' ; }

function isInt(v)      { return isNumber(v) && parseFloat(parseInt(v)) == v; }
function isFloat(v)    { return isNumber(v); }

function stringIsNumbers(str) { return (str.match(/^\d+$/)  == null) ? false : true; }
function stringIsNotHtml(str) { return (str.match(/[<>\^]/) != null) ? false : true; }

function getElement(id) { return document.getElementById ? document.getElementById(id) : null; }
function getParentNode(obj) { return (obj.parentElement) ? obj.parentElement : obj.parentNode; }

function getW() { return window.innerWidth ? innerWidth : document.documentElement ? document.documentElement.offsetWidth  : ie ? document.body.clientWidth  : innerWidth  }
function getH() { return window.innerHeight ? innerHeight : document.documentElement ? document.documentElement.offsetHeight : ie ? document.body.clientHeight : innerHeight }
function getS() { return ie ? document.body.scrollTop : pageYOffset }

function getContentW() { return ie ? document.body.clientWidth  : innerWidth  }
function getContentH() { return ie ? document.body.clientHeight : innerHeight }

Object.prototype._extends = function(extClass) {
	this.prototype = new extClass(-1);
	if (notDefined(DynInterface.classes[this.className])) {
		DynInterface.classes[this.className] = [null,true,extClass];
	} else {
		DynInterface.classes[this.className][2] = extClass;
	}
}

Function.prototype.bind = function(object) {
	var _method = this;
	return function () {
		return _method.apply(object, arguments);
	};
}

Function.prototype.bindEventListener = function(object) {
    var _method = this;
    return function (event) {
        _method.call(object, event || window.event);
    };
}

if (typeof Function.prototype.call == 'undefined') {
	Object.prototype._call  = null;
	Function.prototype.call = function(obj) { obj._call = this; return obj._call(); }
}

Array.prototype.indexOf = function(o) { for(var i = 0; i < this.length; i++) if(this[i]==o) return i; return -1; };

Array.prototype.removeIndex = function(index) {
	for (var i = index; i < this.length-1; i++) this[i] = this[i+1];
	this.length = this.length-1;
}

if (typeof Array.prototype.push == 'undefined') {
	Array.prototype.push = function() { for (var i = 0; i < arguments.length; i++) this[this.length] = arguments[i]; return this.length; }
	Array.prototype.pop  = function() { this.length = this.length-1; }
}

/**
 * DynObject
 */
DynObject = function(obj,parent) {

	if (obj == -1) return;

	this.index = null;
	this.parent = null;
	this.obj = null;

	if (isObject(obj)) this.obj = obj;
	this.parent = (parent || isNull(parent)) ? parent : DynInterface.getObject(document.dynObjectIndex);

	this.add();
}

DynObject.className = 'DynObject';

DynObject.prototype.add = function() {
	this.index = DynInterface.objects.length;
	DynInterface.objects.push(this);
}

DynObject.prototype.remove = function() {
	this.destroy();
	DynInterface.objects.removeIndex(this.index);
}

DynObject.prototype.destroy = function() { this.obj = this.parent = null; }

DynObject.prototype.setOnclick     = function() { this.onclick      = new DynEvent(); }
DynObject.prototype.setOndblclick  = function() { this.ondblclick   = new DynEvent(); }
DynObject.prototype.setOnmouseup   = function() { this.onmouseup    = new DynEvent(); }
DynObject.prototype.setOnmousedown = function() { this.onmousedown  = new DynEvent(); }
DynObject.prototype.setOnmouseover = function() { this.onmouseover  = new DynEvent('onmouseover',this.obj); }
DynObject.prototype.setOnmouseout  = function() { this.onmouseout   = new DynEvent('onmouseout',this.obj); }

/**
 * DynEvent
 */
DynEvent = function(type,src) {
	this.clients = [];
	if (type && src) {
		switch (type) {
			case 'onmouseover' : src.onmouseover = DynEvent.handle; break;
			case 'onmouseout'  : src.onmouseout  = DynEvent.handle; break;
		}	
	}
}

DynEvent.className = 'DynEvent';
DynEvent.e = null;

DynEvent.handle = DynEvent.prototype.handle = function(e,_this) {

	if (window.event) e = window.event;
	DynEvent.e = e;

	if (!_this) {
		var _this = DynEvent.getDynEvent(e);
		return isNull(_this) ? true : _this.handle(e,true);
	}

	var r = true;

	for (var i = 0; i < this.clients.length; i++) {
		r = (this.clients[i]() && r);
	}
	return r;
}

DynEvent.getDynEvent = function(e,elm) {

	var curDO;
	var curDE;

	if (elm) curDO = DynEvent.getDynObject(elm);
	else if (isNull(elm)) return null;
	else curDO = DynEvent.getDynObject(DynEvent.getSrcElement());

	if (curDO != null) {
		curDE = DynEvent.getDynEventByType(curDO);
		if (notNull(curDE)) return curDE;
		else if (curDO == DynInterface.getObject(document.dynObjectIndex)) return null;
		else return DynEvent.getDynEvent(e,getParentNode(curDO.obj));
	} else return null;
}

DynEvent.getDynObject = function(elm) { return elm ? ( elm == document || elm.dynObjectIndex ? DynInterface.getObject(elm.dynObjectIndex) : DynEvent.getDynObject(getParentNode(elm)) ) : null; }

DynEvent.getDynEventByType = function(curDO) {

	var curDE = curDO["on" + DynEvent.e.type];
	if (curDE && notNull(curDE)) return curDE;
	else if (notNull(curDO.parent)) { return DynEvent.getDynEventByType(curDO.parent); }
	else return null;
}

DynEvent.prototype.invoke = function() { this.handle(null,true); }
DynEvent.prototype.addClient = function(cMethod,cObj) { this.clients[this.clients.length] = cMethod.bind(cObj); }
DynEvent.prototype.clear = function(c) { this.clients.length = 0; }

DynEvent.getSrcElement = function() { return DynEvent.e.srcElement ? DynEvent.e.srcElement : DynEvent.e.target; }

DynEvent.getMouseX = function() { return DynEvent.e.pageX ? DynEvent.e.pageX : DynEvent.e.clientX + (document.documentElement ? document.documentElement.scrollLeft : document.body.scrollLeft); }
DynEvent.getMouseY = function() { return DynEvent.e.pageY ? DynEvent.e.pageY : DynEvent.e.clientY + (document.documentElement ? document.documentElement.scrollTop : document.body.scrollTop); }
DynEvent.getMouseRelativeX = function() { return DynEvent.e.layerX ? DynEvent.e.layerX : DynEvent.e.offsetX; }
DynEvent.getMouseRelativeY = function() { return DynEvent.e.layerY ? DynEvent.e.layerY : DynEvent.e.offsetY; }

/**
 * DynImage
 */
DynImage = function() { }
DynImage.change = function(id,src) { getElement(id).src = src }
DynImage.getHtml = function(src) { return '<img src="' + src + '" border="0" >'; }