﻿var Utilities = {
    isDefined : function( t_object )
    {
        return typeof t_object != "undefined";
    }
};
var $defined = Utilities.isDefined;

Utilities.Window = Object.extend(Utilities, {
    getSize : function()
    {
        var t_calcWidth, t_calcHeight;
        if (window.innerWidth) {
			t_calcWidth = window.innerWidth;
			t_calcHeight = window.innerHeight;
		} else {
			t_calcWidth = document.compatMode == 'CSS1Compat' ? document.documentElement.clientWidth : document.body.clientWidth;
			t_calcHeight = document.compatMode == 'CSS1Compat' ? document.documentElement.clientHeight : document.body.clientHeight;
		}
		return {
		    width: t_calcWidth,
		    height : t_calcHeight
		}
    }
});

Utilities.Page = Object.extend(Utilities, {
    _pageLoaded : false,
    _arrToInvoke : [],
    _attemptInvoke : function( t_func )
    {
        if( !Utilities.Page._pageLoaded )
        {
            Utilities.Page._arrToInvoke.push(t_func);
        }
        else
        {
            t_func();
        }    
    },
    
    _invokePending : function()
    {
        Utilities.Page._pageLoaded = true;    
        Utilities.Page._arrToInvoke.each(
            function(t_func) {t_func();}
        );
        Utilities.Page._arrToInvoke.clear();
    }
});
var $attemptInvoke = Utilities.Page._attemptInvoke;
var $invokePending = Utilities.Page._invokePending;

Utilities.DOM =  Object.extend(Utilities, {
    create : function(t_elementName, t_text, t_childNodes, t_parent)
    {    
        return $(document.createElement(t_elementName)).$add(t_childNodes).$text(t_text).$addTo(t_parent);    	
    },
    
    _addAttribute : function(t_element, t_name, t_value)
    {
        if( t_name == "style" )
        { 
            try
            {
                var t_tokens = t_value.split(";").compact();
                
                var t_hash = t_tokens.inject({}, function(t_params, t_pairString)
                    {
                        var t_pair = t_pairString.split(':');
                        t_params[t_pair[0]] = t_pair[1];
                        return t_params;
                    });
                
                Element.setStyle(
                    t_element, 
                    t_hash);
            }
            catch(e)
            {
                alert("Exception adding attribute style: " + e.message);
            }
        }
        else if( t_name == "class" )
        {
            t_element.className = t_value;
        }
        else
        {
            t_element.setAttribute(t_name,t_value);
        }
        return t_element;
    },
    
    _addConditionalAttribute : function( t_element, t_name, t_value, t_condition )
    {
        return t_condition ? t_element.$attr(t_name,t_value) : t_element;
    },
    
    _appendChild : function(t_element)
    {
        t_element.appendChild($dom.apply(this, $A(arguments).slice(1)));
        return t_element;
    },
    
    _setText : function( t_element, t_text )
    {
        if(typeof t_text != "undefined" && t_text != null)
	    {
            var t_node = document.createTextNode(t_text);
            t_element.appendChild(t_node);
        }
        return t_element;
    },
    
    _setParent : function( t_element, t_parent )
    {
        if( typeof t_parent != "undefined" && t_parent != null )
        {
            t_parent.appendChild(t_element);
        }
        return t_element;
    },
    
    _addChild : function( t_element, t_childNodes )
    {
        if(typeof t_childNodes != "undefined" && t_childNodes != null)
	    {	    
	        try
	        {
                if( typeof t_childNodes.length != "undefined" )
	            {
	                var i, t_length = t_childNodes.length;
		            for(var i=0; i < t_length; i++)
		            {
			            t_element.appendChild(t_childNodes[i]);
		            }
	            }
	            else
	            {
		            t_element.appendChild(t_childNodes);
	            }        
            }
		    catch(e)
		    {
			    throw "Invalid child node " + t_childNodes[i] + ". " + e.message;
		    }
        }
        
        return t_element;
    }
});

/*
Elements are only extended in IE6 if they are processed through the $-method
*/
Element.addMethods({
        $attr : Utilities.DOM._addAttribute,
        $condAttr : Utilities.DOM._addConditionalAttribute,
        $dom : Utilities.DOM._appendChild,
        $text : Utilities.DOM._setText,
        $addTo : Utilities.DOM._setParent,
        $add : Utilities.DOM._addChild
    });

var $dom = Utilities.DOM.create;


String.prototype.trim	=	new Function("return this.replace(/^\\s+|\\s+$/g,'')");
Array.prototype.sum = function(){
	for(var i=0,sum=0;i<this.length;sum+=this[i++]);
	return sum;
}

 
