/**
 * Core library functions.
 *
 * @author Christian Hansen <christian@resource-it.dk>
 * @author Nikolai Gjerløff <nikolai@resource-it.dk>
 * @version 1.1
 * @package Resource it Modules
 * @copyright Resource it ApS - this script cannot be used without prior permission
 **/

var resExt = {

    config:{},

    /**
     * propstr
     * Output an object as a string for debugging
     **/
    propstr:function(obj) {
        var str = '';
        for(i in obj) str += i+'; ';
        return(str);
    },

    /**
     * isIE
     * Checks wether the browser is Internet Explorer or not. If arguments[0] is given the check is 
     * performed with respect to the version number given
     **/
    isIE:function() {
        if(navigator.userAgent.indexOf('MSIE') != -1) {
            if(arguments.length > 0 && parseInt(navigator.appVersion) > arguments[0]) return(0);        
            return(1);
        }
        return(0);
    },
    
    /**
     * addEventHandler
     * Add an event listener to any HTMLElement object o
     * @param object o              - the object
     * @param string eventname      - eventname without on-prefix - e.g. click, mouseover, mouseover
     * @param string functionname   - function name
     **/
    addEventHandler:function(obj, eventname, functionname) {
        try {
            obj.addEventListener(eventname,functionname, false);
        } catch(e) {
            obj.attachEvent('on' + eventname,functionname);
        }
    },

    /**
     * getElementPosition
     * Gets an elements absolute position according to the upper top left corner.
     * @param object o              - the object in question
     **/
    getElementPosition:function(obj) {

        var pos = new Object()
        pos.x = pos.y = 0;
        if (obj.offsetParent) {
            pos.x = obj.offsetLeft
            pos.y = obj.offsetTop
            while (obj = obj.offsetParent) {
                pos.x += obj.offsetLeft
                pos.y += obj.offsetTop
            }//while
        }//if

        return pos;
    },


    getMouseCoord:function(e) {
        var pos = new Object();
            pos.x = 0;
            pos.y = 0;

        if (!e) var e = window.event;

        if (e.pageX || e.pageY) {
            pos.x = e.pageX;
            pos.y = e.pageY;
        }

        else if (e.clientX || e.clientY)    {
            pos.x = e.clientX + document.body.scrollLeft
                + document.documentElement.scrollLeft;
            pos.y = e.clientY + document.body.scrollTop
                + document.documentElement.scrollTop;
        }//else if
        return pos;
    },


    getMousePos:function(obj,e) {
        var pos = new Object();
        var ep = resExt.getElementPosition(obj);
        var X = resExt.getMouseCoord(e).x;
        var Y = resExt.getMouseCoord(e).y;
        pos.x = (X - ep.x);
        pos.y = (Y - ep.y);
        return pos;
    },


    gt:function(str) {
        try { 
            if ( __LANG[str] !== undefined ) return __LANG[str];
            else return str; 
        } catch(e) { return str; }
    },

    getQuery:function() {
        var args = new Object();
        var query = location.search.substring(1);
        var pairs = query.split("&");
        for(var c=0;c<pairs.length;c++) {
            var pair = pairs[c].split("=");
            args[pair[0]] = unescape(pair[1]);                
        }//for
        return args;            
    },//getQuery

    image:function(arr) {
        if ( arr.src !== undefined && resExt.theme.paths.gfx !== undefined) {
            var img = new Image();
            if ( /\.png$/i.test(arr.src) && resExt.isIE() ) {
                img.src = resExt.theme.paths.gfx + '1x1_transparent.gif';
                img.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + arr.src + "', sizingMethod='scale');";
            } else img.src = arr.src;
    
            if ( arr.width !== undefined ) {
                img.width = arr.width;
                img.style.width = arr.width + "px";
            }//if
            
            if ( arr.height !== undefined ) {
                img.height = arr.height;
                img.style.height = arr.height + "px";
            }//if

            for ( var style in {position:1,top:1,left:1,zIndex:1} ) {
                if ( arr[style] !== undefined ) img.style[style] = arr[style];
            }//for

            return img;

        } else {
            return new Image();
        }
    },//image

    load:function(arr) {

        if ( arr === undefined ) return false;
        if ( arr.script === undefined ) return false;

        //get loaded scripts
        var scripts = document.getElementsByTagName("script");
        var included = {};
        for ( var c = 0; c < scripts.length; c++ ) {
            included[scripts[c].src] = true;
            if ( /lib\.resext\.js$/.test( scripts[c].src ) && resExt.config.scriptpath === undefined )
                resExt.config.scriptpath = scripts[c].src.replace(/lib\.resext\.js$/,"");
        }//for

        //if the path is not defined, define it.
        if ( arr.path === undefined ) var script = resExt.config.scriptpath + arr.script;
        else if ( !/^http:\/{2}/.test(arr.path) ) var script = resExt.config.scriptpath + arr.path + arr.script; 
        else var script = arr.path + arr.script;

        //if the script has been included before and inclusion should be forced
        if ( included[script] !== undefined && arr.force === undefined ) return true;

        // include the script
        var com = document.createElement("script");
            com.setAttribute("type","text/javascript");
            com.setAttribute("src", script);
        document.getElementsByTagName("head")[0].appendChild(com); 

        return true;

    },//load

    
    windowcs:function() {

    },//centerscreen


    /**
     * Opens a new window centeret according to the opener
     * Same syntax as window.open
     **/
    windowcp:function( url, name, features ) {

        if ( features === undefined ) {
            var features = {width:100,height:100}; 
        } else {
            var tmp = features.split(/,/);
            var features = {};
            for ( var c = 0; c < tmp.length; c++ ) {
                var pair = tmp[c].split(/=/);
                features[pair[0]] = pair[1];
            }//for
        }//else

        if( resExt.isIE() ) {
            features["top"] = ( self.screenTop + ( document.body.clientHeight/2) - ( features["height"] / 2 ) ) -20;
            features["left"] = self.screenLeft + ( document.body.clientWidth/2) - ( features["width"] / 2 );
        } else {
            features["top"] = ( window.screenY + ( window.outerHeight / 2 ) - ( features["height"] / 2 ) );
            features["left"] = window.screenX + ( window.outerWidth / 2 ) - ( features["width"] / 2 );
        }//else

        var properties = [];
        for ( var key in features ) properties[properties.length] = key + "=" + features[key];

        return window.open( url, name, properties.join(",") );

    }//centerparent

}//resExt
