
dynwin_path = 'http://editor.com/dynwin/'
dynwin_cache = []
modules = []

function $(id){
    return document.getElementById(id)
}

function $$(el, doc){
    if(doc == null) return document.getElementsByTagName(el);
    else return doc.getElementsByTagName(el);
}

function _(el, doc){
    if(doc == null) return document.createElement(el);
    else return doc.createElement(el);
}

Function.prototype.bind = function(object){
    var __self = this
    return function() {
	return __self.apply(object, arguments);
    }
}

function fromCache(module, tmpl){
    for(var i in dynwin_cache){
        if(dynwin_cache[i].tmpl == tmpl && 
    	    dynwin_cache[i].module == module)
		return dynwin_cache[i]
    }; return null
}

function moduleLoaded(module){
    for(var i in module.tmpls) if(fromCache(module, module.tmpls[i]) == null) { alert(module + " : "+module.tmpls[i]); return 0}
    for(var i in module.confs) if(fromCache(module, module.confs[i]) == null) { alert(module + " : "+module.tmpls[i]); return 0}
    for(var i in module.images){
	if(typeof(module.images[i]) != 'object' || module.images[i].loaded == 0)
	    return 0
    }
    return 1
}

function initStyle(module){
    var styles = $$('LINK'); for(var i in styles){
	if(styles[i].href == module.style){
	    styles[i].parentNode.removeChild(styles[i])
	} 
    }
    
    var style = _('LINK'); style.rel = 'stylesheet'; style.type = 'text/css';
    style.href = module.style; $$('HEAD')[0].appendChild(style)
}

function browser(){
    var agent = 'unknown'
    navigator.userAgent.indexOf("Firefox") != -1 ? agent = 'ff' :
    navigator.userAgent.indexOf("Opera") != -1 ? agent = 'opera' : 
    navigator.userAgent.indexOf("MSIE") != -1 ? agent = 'ie' : agent = 'unknown'
    return agent
}

function position(obj) {
    var pos = {x: obj.offsetLeft||0, y: obj.offsetTop||0};
    while(obj = obj.offsetParent) {
	if(obj.nodeName == 'TABLE' && browser() == 'ie') {pos.y -= 1; pos.x -= 1; }
	pos.x += obj.offsetLeft||0;
	pos.y += obj.offsetTop||0;
    }; return pos;
}

function random(){
    return (Math.floor(Math.random()*100000)).toString()
}



