/**
* convenience function libraries
* Version 0.22 (30.05.2006)
*/


// convenience method to get Controller
function C(){
	return getController();
	}

// convenience method to get registered modules
function M(title){
	return C().getWindow({id: title});
	}
	
function WIN(title){
	return C().getWindow({id: title});
	}
	
function B(title){
	return C().getBClass(title);
	}
	
function BODY(){
	return document.getElementsByTagName("body").item(0);
}

function cP(text){
	return cDOM({tag: 'p', innerHTML: text});
}

function cBR(){
	return cDOM({tag: 'br'});
}

function cDIV(content){
	
	if(isString(content))
		return cDOM({tag: 'div', innerHTML: content});
	
	else{
		var div = cDOM({tag: 'div'});
		if(isArray(content)){
			for(var i = 0; i<content.length; i++)
				div.appendChild(content[i]);
		}
		else
			div.appendChild(content);
	}
}

// create link, add Observer and return link
// default tag is <a/>
// default behavior is onclick
// default id is randomized id
function cLINK(properties){
			
		if(!properties.tag)
		properties.tag = 'a';
		
		var link = cDOM(properties)
		
		link.command = properties.command;
		
		if(properties.properties)
		link.properties = $H(properties.properties);
		
		if(!properties.behavior)
		properties.behavior = 'onclick';
		
		if(!properties.target)
			var target = this;
		else
			var target = properties.target;
		
		var router = event.getObserver(link, properties.behavior);
		router.addListener(properties.notify, target, properties.notify, properties.command, properties.pars);
		return link;
}

// function to quickly create buttons
// buttons are divs and belong to class 'button'
function cBUTTON(properties){
		
		properties.tag = 'div';
		
		if(properties.className)
		properties.className = properties.className+' button';
		else
		properties.className = 'button';
		
		properties.behavior = 'onclick';
		
		var element = cDOM(properties)
		element.command = properties.command;
		
		if(properties.target)
		element.target = properties.target;
		
		if(properties.properties)
		element.properties = $H(properties.properties);
		
			if(!properties.target)
			var target = this;
		else
			var target = properties.target;
		
		var router = event.getObserver(element, properties.behavior);
		router.addListener(properties.notify, target, properties.notify, properties.command, properties.pars);
		return element;

}

// create DOM Element
// default id is randomized
function cDOM(properties){
		var element = document.createElement(properties.tag);
		
		if(properties.parent)
		element.parent = properties.parent;
		
		if(properties.title)
		element.title = properties.title;
		
		if(properties.innerHTML)
		element.innerHTML = properties.innerHTML;
		
		if(properties.src)
		element.src = properties.src;
		
		if(!properties.fixedId){
			if(!properties.id)
			properties.id = properties.tag;
			element.id = properties.id+'_'+getRandom();
		}
		
		else
		element.id = properties.id;
		
		if(properties.className)
		element.className = properties.className;
		return element;
}

// create Table
// expects columns and data array, each row is array
// (Array('title', 'version',..), Array(Array('Word', '0.92')
// , Array('Photoshop', '1.24')), properties) 
// @properties: className, id

function cTABLE(cols, data, properties){
		
	if (!data)
		return null;
	
	if(properties){
		
		var className;
		if(!(className = properties.className))
			var className = 'table';
		
		var id;
		if(!(id = properties.id))
			id = 'table'+getRandom();
	}
	
	var table = cDOM({tag: 'table', 'id': id, 'className': className});
	
	// compile title row
	var tr = cDOM({tag: 'tr'});
	for(var i=0; i<cols.length; i++){
		tr.appendChild(cDOM({tag: 'th', innerHTML: cols[i]}));
	}
	table.appendChild(tr);
	
	// insert data rows
	for (var i = 0; i < data.length; i++){
		var tr = cDOM({tag: 'tr'});
		
			var row = data[i];
			for(var j = 0; j < row.length; j++){
				var td = cDOM({tag: 'td'});
					
					if(isString(row[j]))
						td.innerHTML = row[j];
					else if(isObject(row[j]))
						td.appendChild(row[j]);
				tr.appendChild(td);
			}
		
		table.appendChild(tr);
	}
	
	return table;
}

function getStyle(element, key){
	return document.defaultView.getComputedStyle(element, "").getPropertyValue(key);
}

function getMouseXY(e) {
	var posx = 0;
	var posy = 0;
	if (!e) var e = window.event;
	if (e.pageX || e.pageY)
	{
		posx = e.pageX;
		posy = e.pageY;
	}
	else if (e.clientX || e.clientY)
	{
		posx = e.clientX + document.body.scrollLeft;
		posy = e.clientY + document.body.scrollTop;
	} 
 return [posx, posy];
}

function getWindowSize() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  return [myWidth,myHeight];
}

function getRandom(){
	return Math.floor(Math.random()*1000);
}

/**
* HashList-Class
*
*/

var Hash = Class.create();
Hash.prototype = {
	
	initialize: function(){
	this.items = new Array();
	},
	
	add: function(key, value){
		this.items.push([key,value]);
		return value;
	},
	
	/**
	* checks for key and returns key->value-pair
	*/
	has: function(key){
		for(var i = 0; i<this.items.length; i++){
			if(this.items[i][0] == key)
				return this.items[i][1];
		}
		return false;
	},
	
	/**
	* deletes pair and returns true or false
	*/
	del: function(key){
		for(var i = 0; i<this.items.length; i++){
			if(this.items[i][0] == key){
				this.items.splice(i,1);
				return true;
			}
		}
		return false;
	},
	
	/**
	* empties array and returns ONE pair by each call, null if empty
	*/
	empty: function(){
		if(this.items.length > 0)
		return this.items.pop();
		return null;
	}

};

function isAlien(a) {
   return isObject(a) && typeof a.constructor != 'function';
}

function isArray(a) {
    return isObject(a) && a.constructor == Array;
}

function isBoolean(a) {
    return typeof a == 'boolean';
}

function isEmpty(o) {
    var i, v;
    if (isObject(o)) {
        for (i in o) {
            v = o[i];
            if (isUndefined(v) && isFunction(v)) {
                return false;
            }
        }
    }
    return true;
}


// some typeChecking functions
function isFunction(a) {
    return typeof a == 'function';
}

function isNull(a) {
    return typeof a == 'object' && !a;
}

function isNumber(a) {
    return typeof a == 'number' && isFinite(a);
}

function isObject(a) {
    return (a && typeof a == 'object') || isFunction(a);
}

function isString(a) {
    return typeof a == 'string';
}

function isUndefined(a) {
    return typeof a == 'undefined';
} 

function getImageWidth(myImage) {
	var x, obj;
	if (document.layers) {
		var img = getImage(myImage);
		return img.width;
	} else {
		return getElementWidth(myImage);
	}
	return -1;
}

function getImageHeight(myImage) {
	var y, obj;
	if (document.layers) {
		var img = getImage(myImage);
		return img.height;
	} else {
		return getElementHeight(myImage);
	}
	return -1;
}

