// JavaScript Document
var Utils = function(){};
Utils.debug_flag = false;
Utils.debug = function (message, colour){
    if(typeof(document.body)!="object" || document.body==null)return;
    if(!colour) var colour = "#000000";
    var div;
    var a;
    var a_t;
    if(!Utils.debug_flag) return true;
    if(!eval(div = document.getElementById("message_div"))){
        try {
            div = document.createElement("<div id=\"message_div\" style=\"width:200px; overflow:auto;padding:5px;text-align:left;position:absolute;top:400px;left:0px;border:1px solid #8a8a8a; background-color:#fed6c6;\">");
        }catch(e){
            div = document.createElement('div');
            div.setAttribute('id', "message_div");
            div.setAttribute('style', "width:200px; overflow:auto;padding:5px;text-align:left;position:absolute;top:400px;left:0px;border:1px solid #8a8a8a; background-color:#fed6c6;");
        }
        try {
            a = document.createElement("<a href=\"javascript:Utils.debug(null);\">");
        }catch(e){
            a = document.createElement('a');
            a.setAttribute('href', "javascript:Utils.debug(null);");
        } 
        a_t = document.createTextNode("Clear");
        a.appendChild(a_t);
        div.appendChild(a);
        
        document.body.appendChild(div);
        //alert(div.toSource());
    }
    if(message == null){
        while(div.hasChildNodes()){
            div.removeChild(div.firstChild);
        }
        div.parentNode.removeChild(div);
    }else{
        try {
            var p = document.createElement("<p style=\"color:"+colour+";\">");
            
        }catch(e){
            var p = document.createElement('p');
            p.setAttribute('style', "color:"+colour+";");
        } 
        //var text = document.createTextNode(message);
        p.innerHTML = message;
        div.appendChild(p);
    }
};

Utils.setOpacity = function (element, value){    
    if (value == 1){
        Utils.setStyle(element, { opacity: 
            (/Gecko/.test(navigator.userAgent) && !/Konqueror|Safari|KHTML/.test(navigator.userAgent)) ? 
                0.999999 : null });
                if(/MSIE/.test(navigator.userAgent))  
                    Utils.setStyle(element, {filter: Utils.getStyle(element,'filter').replace(/alpha\([^\)]*\)/gi,'')});  
    } else {  
        if(value < 0.00001) value = 0;  
        Utils.setStyle(element, {opacity: value});
        if(/MSIE/.test(navigator.userAgent))  
            Utils.setStyle(element, 
            { filter: Utils.getStyle(element,'filter').replace(/alpha\([^\)]*\)/gi,'') +
              'alpha(opacity='+value*100+')' });  
    }   
};
Utils.setStyle = function (element, style) {
    for(k in style){
        element.style[k] = style[k];
    }
};
Utils.getStyle = function getStyle(element, name){
    return element.style[name];
};

Utils.newElement = function (element, attributes){
    var el;
    try{
        var att_string = "";
        if(attributes !=null){
            for(a in attributes){
				if(typeof(attributes[a]) != "function"){
							att_string += a+"=\""+attributes[a]+"\" ";
				}
            }
        }
        el = document.createElement("<"+element+" "+att_string+">");	
    }catch(e){
        el = document.createElement(element);
        if(attributes !=null){
            for(a in attributes){
				if(typeof(attributes[a]) != "function"){
							el.setAttribute(a, attributes[a]);
				}
            }
        }
    }
    return el;
};
Utils.findPos = function (obj) {
    
    var curleft = curtop = 0;
    
    if (obj.offsetParent) {
        curleft = obj.offsetLeft
        curtop = obj.offsetTop
        
        while (obj = obj.offsetParent) {
            curleft += obj.offsetLeft
            curtop += obj.offsetTop
        }
        
    }
    
    return [curleft,curtop];
};
Utils.getScrollLocation = function(){
	//QuirksMode http://www.jr.pl/www.quirksmode.org/viewport/compatibility.html:
	if (self.pageYOffset) // all except Explorer
	{
		x = self.pageXOffset;
		y = self.pageYOffset;
	}
	else if (document.documentElement && document.documentElement.scrollTop)
		// Explorer 6 Strict
	{
		x = document.documentElement.scrollLeft;
		y = document.documentElement.scrollTop;
	}
	else if (document.body) // all other Explorers
	{
		x = document.body.scrollLeft;
		y = document.body.scrollTop;
	}
	
	return {'y': y, 'x':  x};
};

Utils.showProperties = function(element, like){
	var properties = element.nodeName+":\n";
	var r =  RegExp('.*'+like+'.*', "i");
	for(i in element){
		if(r.test(i))properties += i+": "+element[i]+"\n";
	}
	alert(properties);
	
};

Utils.getWindowDimensions = function(scrollbarW, scrollbarH){
	if(!scrollbarW) var scrollbarW = true;
	if(!scrollbarH) var scrollbarH = false;
	
	
	if(window.innerWidth){
		winW = window.innerWidth;
	 	winH = window.innerHeight;
		if(scrollbarW) winW-=16;
		if(scrollbarH) winH-=16;
	 }
	 if(document.body.offsetWidth){
	  	winW = document.body.offsetWidth;
	  	winH = document.body.offsetHeight;
		if(scrollbarW) winW-=20;
		if(scrollbarH) winH-=20;
	 }
	 return {'width':winW, 'height':winH};
};

Utils.removeChildren = function(element){
    
    while(element.hasChildNodes()){
        element.removeChild(element.firstChild);
    }
};
Utils.getElementsByClassNameRecurse = function(baseElement, className, elementList){
    var children = baseElement.childNodes;
    
    for(var i=0; i < children.length; i++){
        var child = children[i];
        if(!child) continue;
        if(child.tagName == "#text") continue;
        if(child.hasChildNodes()){
            elementList = Utils.getElementsByClassNameRecurse(child, className, elementList);
        }
        if(child.className != null && child.className == className){
            elementList.push(child);
        }
    }
    return elementList;
};
Utils.getElementsByClassName = function(baseElement, className){
    var elementList = new Array();
    return Utils.getElementsByClassNameRecurse(baseElement, className, elementList);
};
/*
Utils.isInstance = function(obj, classType){
    if(obj==null || typeof(obj)!="object")return false;
    var constructorString = obj.constructor.toString();
    constructorString = constructorString.replace(/^function\s/, "");
    constructorString = constructorString.replace(/\([^)]*\)\s?\{(.|\n)*$/gm, "");
    constructorString = constructorString.replace(/\s/g, "");
    return (constructorString==classType);
};
*/
Object.prototype.isInstance = function(classType){
    var constructorString = this.constructor.toString();
    constructorString = constructorString.replace(/^function\s/, "");
    constructorString = constructorString.replace(/\([^)]*\)\s?\{(.|\n)*$/gm, "");
    constructorString = constructorString.replace(/\s/g, "");
    return (constructorString==classType);
};
Utils.propLength = function(object){
    var count = 0;
    for(i in object){
        if(typeof(this[i]) == "function") continue;
        count++;
    }
    return count;
};

Utils.isChildOf= function (parent, child){
    var isChild = false;
    
    while((child)&&(child.nodeName != "BODY")){
        if(child == parent){	
            isChild = true;
            break;
        }
        child = child.parentNode;
    }
    
    return isChild;
};
Utils.setElementText = function(element, text){
    var textElementFound = false;
    var i =0;
    while(!textElementFound && i<element.childNodes.length){
        if(element.childNodes[i].nodeName.toLowerCase() == "#text"){
            element.childNodes[i].nodeValue = text;
            textElementFound = true;
        }
        i++;
    }
	if(!textElementFound) element.appendChild(document.createTextNode(text));
    
};   

Utils.getElementText = function(element){
    var i =element.childNodes.length-1;
    var textString = null;
    while(textString == null && i>=0){
        if(element.childNodes[i].nodeName.toLowerCase() == "#text"){
            textString = element.childNodes[i].nodeValue;
        }
        i--;
    }
    return textString;
    
};

Utils.getMousePositionFromEvent = function(e){
    /*From QuirksBlog*/
    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
        + document.documentElement.scrollLeft;
        posy = e.clientY + document.body.scrollTop
        + document.documentElement.scrollTop;
    }
    return {'x':posx, 'y':posy};
};
