/*
 * XTmotion's core JavaScript library. It contains set of variables, 
 * functions and objects which can be used accross different web applications.
 * All objects (variables, functions) in this library are prefixed with the xtmotion.core
 * objects to avoid possible conflict with other libraries from other parties.
 *
 * @version 0.4 2006-09-15
 *
 * Copyright (c) XTmotion
 * United Kingdom, Europe
 * All Rights Reserved.
 */

/**
 * These are XTmotion's unique namespace objects 
 */
if (typeof(xtmotion) == "undefined") {
    xtmotion = new Object();
}
xtmotion.core = new Object();

/**
 * The following adds transparent ability to specify what should be executed
 * when the page loads (the window.onload event); As multiple calls to window.onload
 * would effectively cancel all those that came before, having a unified way to attach
 * onload events is a nice thing. Now, if we want to have our function executed onload, 
 * we register it with the <code>window.addOnLoadListener()</code> method. Any number of 
 * functions can be registered from anywhere on the page and they all get executed when
 * the page load finishes.
 *
 * Taken from the "Ajax in Action", ©2006 by Manning Publications Co. All rights reserved.
 */
window.onloadListeners = new Array();
window.addOnLoadListener = function (listener) {
    window.onloadListeners[window.onloadListeners.length] = listener;
}
window.onload = function(){
    for (var i = 0; i < window.onloadListeners.length; i++) {
        var func = window.onloadListeners[i];
        func.call();
    }
}

/**
 * Browser-sniffing variables useful when program needs to know which browser 
 * it is executed in. As fake values can be sometimes given by the browser (i.e. Opera pretends
 * to be MSIE), these are not always reliable.
 */
xtmotion.core.AgntUsr = navigator.userAgent.toLowerCase();
xtmotion.core.DomYes  = document.getElementById ? 1 : 0;
xtmotion.core.NavYes  = xtmotion.core.AgntUsr.indexOf('mozilla') != -1 && xtmotion.core.AgntUsr.indexOf('compatible') == -1 ? 1 : 0;
xtmotion.core.ExpYes  = xtmotion.core.AgntUsr.indexOf('msie') != -1 ? 1 : 0;
xtmotion.core.OprYes  = xtmotion.core.AgntUsr.indexOf('opera') != -1 ? 1 : 0;
xtmotion.core.Opr6OrLess = window.opera && navigator.userAgent.search(/opera.[1-6]/i) != -1; 
xtmotion.core.DomNav  = xtmotion.core.DomYes && xtmotion.core.NavYes ? 1 : 0;
xtmotion.core.DomExp  = xtmotion.core.DomYes && xtmotion.core.ExpYes ? 1 : 0;
xtmotion.core.Nav4    = xtmotion.core.NavYes && !xtmotion.core.DomYes && document.layers ? 1 : 0;
xtmotion.core.Exp4    = xtmotion.core.ExpYes && !xtmotion.core.DomYes && document.all ? 1 : 0;
xtmotion.core.Exp7    = (xtmotion.core.ExpYes && window.XMLHttpRequest);
xtmotion.core.Linux   = xtmotion.core.AgntUsr.indexOf('linux') != -1 || xtmotion.core.AgntUsr.indexOf('x11') != -1 ? 1 : 0;

/**
 * Strips empty space(s) from the beginning and the end of the given string.
 *
 * @param string - str
 * @return string - trimmed string
 */
xtmotion.core.trim = function(str) {
    return str.replace(/^\s*|\s*$/g,"");
}


/**
 * Returns random integer within the given boundaries.
 *
 * @param int - start
 * @param int - end
 * @return int - randmom integer within 'start' and 'end' boundaries
 */
xtmotion.core.getRandomInteger = function(start, end) {
    return start + Math.floor(Math.random()*(end-start+1));
}


/**
 * Returns true if the key pressed is an integer.  
 */
xtmotion.core.justNumeric = function(ev,dot) {

    /* Get ASCII value of key that user pressed */
    var key = (xtmotion.core.ExpYes) ? ev.keyCode : ev.which;
    
    /* 
     * Was key that was pressed a numeric character (0-9) or a dot (.) or a minus (-) 
     * or enter?
     */
    return ((key > 47 && key < 58) || (key == 45) || (key == 46 && dot) 
            || key == 13  
            || key == 0);
}


/**
 * Opens a popup window with the given URL and without browser chrome.
 *
 * @param string - URL that should load within the popup windows
 * @param string - name of the popup window
 * @param int - width of the popup window
 * @param int - height of the popup window
 * @param bool - indicates whether the popup should be centered on screen
 * @return object - reference to the created popup window
 */
xtmotion.core.openWin = function(target, name, width, height, center) {
    if (xtmotion.core.NavYes && !xtmotion.core.Linux) {
        height += 27;
    }

    ypos = (screen.height - height) / 2;
    xpos = (screen.width - width) / 2;

    if (xtmotion.core.OprYes || (center && screen.height <= 600)) {
        ypos = 0;
        xpos = 0;
    }

    features = 'width='+width+',height='+ height+',top='+ypos+',left='+xpos+
                   ',scrollbars=yes,status=no,resizable=yes,outerWidth='+width+',outerHeight='+height+',screenY='+ypos+',screenX='+ xpos;
    return window.open(target, name, features);
}


/**
 * Returns the available visible size of the page body area.
 * It is very difficult to obtain width and height of the viewable
 * area within the browser window as browsers behave differently 
 * if the page is in standard compliant mode or not. Every use
 * should be carefully tested on specific pages!
 *
 * Found this function on the web, credits go to: 
 * TODO credit line
 *
 * @return array - two elements representing width and height.
 */
xtmotion.core.getPageBodySize = function() {
    var myWidth = 0;
    var 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 new Array(myWidth, myHeight);
}


/**
 * Returns the given string with newline characters replaced by html <br/> tags.
 *
 * @param int - start
 * @param int - end
 * @return int - randmom integer within 'start' and 'end' boundaries
 */
xtmotion.core.nl2br = function(str) {
    breakTag = "<br/>";
    return (str + '').replace(/([^>]?)\n/g, '$1'+ breakTag +'\n');
}
