/* InterAKT prototype updates */

Element.InterAKTMethods = {
  getChildren: function(element, childname) {
    element = $(element);
    var result = [];
    if (typeof(childname) == 'undefined')
      childname = '*';
    childname = childname.toLowerCase();
    if (!element.childNodes)
      return result;
    for (var i = 0; i < element.childNodes.length; i++) {
      var node = element.childNodes[i];
      if (node.nodeType == 1) {
        if (typeof(node.tagName) != 'undefined' && node.tagName.toLowerCase() == childname || childname == '*') {
          result.push(node);
        }
      }
    }
    return result;
  }, 
  createElement: function(type, attribs, wnd) {
  	var elem = (wnd ? wnd.document : document).createElement(type);
  	attribs = attribs || {};
	for (var i in attribs) {
		switch ( true ) {
			case ( i == 'text' )  : 
				elem.appendChild( document.createTextNode( attribs[i] ) ); 
				break;
			case ( i == 'class' ) : 
				elem.className = attribs[i]; 
				break;
			case ( i == 'id' ) : 
				elem.id = attribs[i]; 
				break;
			case ( i == 'type' ) : 
				if (type.toLowerCase() == "input" && /Konqueror|Safari|KHTML/.test(navigator.userAgent)) {
					elem.setAttribute('type', attribs[i]);
				} else {
					elem.type = attribs[i]; 
				}
				break;
			case ( i == 'style' ) : 
				elem.style.cssText = attribs[i]; 
				break;
			default : 
				try {
					elem.setAttribute(i, attribs[i] );
					elem[i] = attribs[i];
				} catch(e) {}
		}
	}
	if (attribs['value']) {
		elem.value = attribs['value'];
	}
	return elem;
  }, 
  getParentByTagName: function(t, parentName) {
	if (t.nodeName.toLowerCase() == parentName.toLowerCase()) {
		return t;
	}

	while (t.parentNode
			&& t.parentNode.nodeName.toLowerCase() != parentName.toLowerCase()
			&& t.parentNode.nodeName != 'BODY') {
		t = t.parentNode;
	}

	if (t.parentNode && t.parentNode.nodeName.toLowerCase() == parentName.toLowerCase()) {
		return t.parentNode;
	} else {
		return null;
	}
  }
}
Object.extend(Element, Element.InterAKTMethods);

var $E = Element.createElement;

if (typeof UIDGenerator == 'undefined') {
	UIDGenerator = Class.create();
	UIDGenerator.prototype = {
	  initialize: function(name) {
      name = name || 'iaktuid_' + Math.random().toString().substring(2, 6) + '_';
	    this.name = name;
	    this.counter = 1;
	  }, 
	  generate: function(detail) {
      detail = detail || '';
	    return (this.name + detail + this.counter++ + '_');
	  }
	};
}

if (typeof ObjectStorage == 'undefined') {
  var ObjectStorage = Class.create();
  ObjectStorage.prototype = {
    initialize: function (name) {
      this.storage = {};
      this.gen = new UIDGenerator(name + "_reference_by_id_");
    }, 
    storeObject:  function(obj) {
      var type = obj.constructor.toString().match(/^\s*function\s*([^\s\(]*)\s*\(/i);
      if (!type) {
        type = "unknown_contructor";
      } else {
        type = type[1];
      }
      var newId = this.gen.generate(type);
      obj.id = newId;
      this.storage[newId] = obj;
    }, 
    add: function(obj) {
      var type = obj.constructor.toString().match(/^\s*function\s*([^\s\(]*)\s*\(/i);
      if (!type) {
        type = "unknown_contructor";
      } else {
        type = type[1];
      }
      var newId = this.gen.generate(type);
      obj.id = newId;
      this.storage[newId] = obj;
    }, 
    getObject:  function (id) {
      return this.storage[id];
    }, 
    get: function (id) {
      return this.storage[id];
    }, 
    deleteObject: function (id) {
      delete this.storage[id];
    }, 
    dispose: function () {
      this.storage = null;
    }
  };
}

if (!window.JavascriptDom)
  var JavascriptDom = new Object();

JavascriptDom.loadedCSSes = {};
JavascriptDom.loadedScripts = {};

JavascriptDom.Methods = {
  loadCSSFile: function(url) {
    var sock = new Ajax.Request(url, {
      method: 'get', 
			asynchronous: false
    });
    if (sock.transport.responseText) {
      return sock.transport.responseText;
    } else {
      return '';
    }
  }, 
  loadCSS: function(src) {
    var loc = window.location.toString().replace(/\/[^\/]*$/gi, "/");
    if (loc.indexOf("http://") != 0) {
      src = loc + src;
    }
    if (!JavascriptDom.loadedCSSes[src]) {
      document.createStyleSheet(src);
    } else {
      return true;
    }
  }, 
  loadJSFile: function(url) {
    var sock = new Ajax.Request(url, {
      method: 'get', 
			asynchronous: false
    });
    if (sock.transport.responseText) {
      return sock.transport.responseText;
    } else {
      return '';
    }
  }, 
  loadScript: function(src) {
    var loc = window.location.toString().replace(/\/[^\/]*$/gi, "/");
    src = (loc.indexOf("http://") != 0 ? loc : '') + src;
    var fname = src.substring(src.lastIndexOf('/')+1, src.length-1);
    if (!JavascriptDom.loadedScripts[src] && !JavascriptDom.loadedScripts[fname]) {
      var response = JavascriptDom.loadJSFile(src);
      if (navigator.appVersion.match(/\bMSIE\b/)) {
        try {
          window.execScript(response, "JavaScript");
        } catch(e) { alert(e.message) }
      } else {
        try {
          window.eval(response);
        } catch(e) { alert(e.message) }
      }
      JavascriptDom.loadedScripts[src] = response;
    } else {
      try { window.eval(JavascriptDom.loadedScripts[src]); } 
      catch(e) { jslog.error("could not load script at '"+src+"'"); }
    }
  }, 
  externalizeFunctions: function(s) {
    if (window.execScript) {
      var functions = s.match(/function\s*([a-zA-Z0-9_]*)\s*\(/gi);
      if (functions) {
        for (var j = 0; j < functions.length; j++) {
          var str = functions[j].match(/function\s*([a-zA-Z0-9_]*)\s*\(/i);
          if(str[1]) {
            try {
              window[str[1]] = eval(str[1]);
            } catch(e) { 
              jslog.error("IE : error globalizing function in panel:" + e.message);
            };
          }
        }
      }
    }
  }, 
  evalScript: function(s) {
    if (s.getAttribute('src')) {
      var src = s.getAttribute('src');
      JavascriptDom.loadScript(src);
    } else {
      if (navigator.appVersion.match(/\bMSIE\b/)) {
        try {
          window.execScript(s.text.toString(), "JavaScript");
        } catch(e) { alert(e.message) }
      } else {
        try {
          window.eval(s.text.toString());
        } catch(e) { alert(e.message) }
      }
    }
  }
  
};
Object.extend(JavascriptDom, JavascriptDom.Methods);

Object.extend(String.prototype, {
    trim: function() {
      return this.replace(/^\s*/, "").replace(/\s*$/, "");
    }
});

if (!window.Debugging)
	Debugging = new Object();

Debugging.ws = ' ';
Debugging.passObj = [];

Debugging.Methods = {
	dumpVar: function(obj, tab) {
		tm = "";
		tab = tab || "";
		if (Debugging.passObj.indexOf(obj) >= 0) {
			tm += Debugging.ws + tab + " " + obj/* + "Recursion \n"*/;
			return tm;
		}
		tab += "  ";
		if (typeof(obj) == "object") {
			Debugging.passObj.push(obj);
			for (i in obj) {
				if (typeof(obj[i]) != "function") {
					var isobj = typeof(obj[i]) == "object";
					tm += Debugging.ws + tab + i + ":{ ";
					if(isobj) {
						tm += "\n";
					}
					tm += Debugging.dumpVar(obj[i], tab, true);
					if(isobj) {
						tm += Debugging.ws + tab + " ";
					}
					tm += " }\n";			
				}
			}
			Debugging.passObj.pop();
		} else if (typeof(obj) == "function") {
			tm += Debugging.ws + tab + typeof(obj);
		} else {
			tm += Debugging.ws + tab;
			tm += obj;
		}
		tab = tab.substring(0, tab.length-2);
		return tm;
	}, 
	alert: function(o) {
		alert(Debugging.dumpVar(o));
	}
};
Object.extend(Debugging, Debugging.Methods);
