<!--

/**
 * @author Ferdy Perdaan
 * @copyright Copyright (c) 2010, WFIT
 * @version 1.0
 */

/** OVERALL METHODS ----------------------------------------------------------------------------------------------------------------------------- **/
function addLoadEvent(func) {
    var oldonload = window.onload;
    if(typeof(window.onload) == 'function'){
	window.onload = function(){
            oldonload();
            func();
        }
    }else{
        window.onload = func;
    }
}

function convertColor(color, type){
    if(typeof(color) == 'string' && color.indexOf('rgb') == 0){                                         //MICROSOFT RGB
        color = color.match(/[0-9]+/g);
        color = {r:parseInt(color[0]), g:parseInt(color[1]), b:parseInt(color[2])};
    }else if(typeof(color) == 'string' && color.indexOf('#') == 0){                                     //HEX
        var HEX = color.substr(1, color.length);
        color   = new Array();
        color.r = parseInt(HEX.substr(0, 2), 16);   
        color.r = parseInt(HEX.substr(2, 2), 16);   
        color.r = parseInt(HEX.substr(4, 2), 16);  
    }else if(typeof(color) != 'object'){                                                                 //UNKNOWN EXCLUDING RGB
        return 'INVALID';
    }

    if(isNaN(color.r) || typeof(color.r) != 'number')    color.r = 0;
    if(isNaN(color.g) || typeof(color.g) != 'number')    color.g = 0;
    if(isNaN(color.b) || typeof(color.b) != 'number')    color.b = 0;

    switch(type){
        case 'HEX':
            function toHex(code){
                if(code == 0 || isNaN(code))
                    return '00';
                code = Math.round(Math.min(Math.max(0, code)));
                return '0123456789ABCDEF'.charAt((code-code%16)/16) + '0123456789ABCDEF'.charAt(code%16);
            }
            return '#' + toHex(color.r) + toHex(color.g) + toHex(color.b);
            break;
        default:
            return color;
    }    
}

function getViewport(){
    var data = {};
    data.width   = window.innerWidth || document.documentElement.clientWidth || document.getElementsByTagName('body')[0].clientWidth;
    data.height  = window.innerHeight || document.documentElement.clientHeight || document.getElementsByTagName('body')[0].clientHeight;
    data.scrollX = window.pageXOffset || document.body.scrollTop || document.documentElement.scrollTop;
    data.scrollY = window.pageYOffset || document.body.scrollLeft || document.documentElement.scrollLeft;

    return data;
}

function callbackTimeout(instance, method){
	return function() {
		return method.apply(instance, arguments);
	}
}

/** IE METHOD **/
function getIEVersionNumber(){
    var ua = navigator.userAgent;
    var MSIEOffset = ua.indexOf('MSIE ');
    
    if(MSIEOffset == -1)
        return 0;
	else
        return parseFloat(ua.substring(MSIEOffset + 5, ua.indexOf(';', MSIEOffset)));
}

/** EXTENSION METHODS ----------------------------------------------------------------------------------------------------------------------------- **/
//OBJECT EXTENSION
Object.prototype.merge = function(object, full){
    if(object instanceof Object){
        for(var key in object){
            if(full !== false && this[key] && object[key] instanceof Object && typeof(object[key]) != 'function')
                this[key] = this[key].merge(object[key]);
            else
                this[key] = object[key];
        }
    }
    return this;
}

Object.prototype.getKey = function(value, strict){
    if(strict){
        for(var key in this){
            if(this[key] === value)
                return key;
        }
    }else{
        for(var key in this){
            if(this[key] == value)
                return key;
        }
    }
    return null;
}

Object.prototype.inObject = function(value, strict){
    return (this.getKey(value, strict) != null ? true : false);
}

//ELEMENT EXTENSION
Element = {
	addEvent : function(element, event, functionName){
	    if(element.addEventListener)
	        element.addEventListener(event, functionName, false);
	    else if(element.attachEvent)
	        element.attachEvent('on' + event, functionName);
	},
	
	removeEvent : function(element, event, functionName){
	    if(element.removeEventListener)
	        element.removeEventListener(event, functionName, false);
	    else if(element.detachEvent)
	        element.detachEvent('on' + event, functionName);
	},
	
	getStyle : function(element, attribute){
	    var style = 'undefined';
	    if(window.getComputedStyle && document.defaultView.getComputedStyle(element, null))
	        style = document.defaultView.getComputedStyle(element, null).getPropertyValue(attribute);
		else if(element.currentStyle)
	        style = element.currentStyle[attribute.replace(/\-(\w)/g, function(strMatch, part){ return part.toUpperCase(); })];
			
	    return style;
	},
	
	getPosition : function(element, absolute){
	    var top  = parseInt(element.offsetTop);
	    var left = parseInt(element.offsetLeft);
	
	    if(absolute){
	        while(element.offsetParent && element.offsetParent.tagName != 'HTML'){
	            element = element.offsetParent;
	            top  += parseInt(element.offsetTop);
	            left += parseInt(element.offsetLeft);
	        }
	    }
	
	    return {top:top, left:left};
	},
	
	getDimension : function(element){
	    return {width :parseInt((!isNaN(Element.getStyle(element, 'width')) ? Element.getStyle(element, 'width') : element.offsetWidth)),
	            height:parseInt((!isNaN(Element.getStyle(element, 'height')) ? Element.getStyle(element, 'height') : element.offsetHeight))};
	},
	
	getElementsByClassName : function(element, className){
		if(typeof(element.getElementsByClassName) == 'undefined'){
			var nodes = [];
			var match = new RegExp('\\b' + className + '\\b');
			var element = element.getElementsByTagName('*');
			for (var i = 0; i < element.length; i++){
				if(match.test(element[i].className)) 
					nodes.push(element[i]);
			}
			return nodes;
		}else{
			return element.getElementsByClassName(className);
		}
	},
	
	setOpacity : function(element, opacity){
        element.style.opacity    = opacity / 100;
        element.style.MozOpacity = opacity / 100;
        element.style.filter     = 'alpha(opacity=' + opacity + ')';
	}
}

//-->

