/**
 * Namespace WB
 *
 * $Id$
 *
 * @author gERD Schaufelberger
 * @license PHP License
 * @package WB
 * @subpackage JavaScript
 */

/**
 * Namespace WB
 *
 * @class WB
 * @package WB
 * @subpackage JavaScript
 */
var WB          = {};

WB.lang         = 'en_GB';
WB.areas        = $H();
WB.cookies		= $H();
WB.root         = "/testingFIT";
WB.self         = "/testingFIT/index.php/";
WB.ajax         = "/testingFIT/ajax.php/";
WB.loader       = "/testingFIT/js.php/";
WB.loaded		= false;
WB.mouseCoords = [0,0];

/**
 * detect user agent
 */
WB.browser	= {
				ie: false,
				mozilla: false,
				opera: false
			};
if (new RegExp(/Opera.([\d]+\.[\d]+)/).exec(navigator.userAgent)) {
	WB.browser.opera = true;
} else {
	if (document.all) {
		WB.browser.ie = true;
	} else {
		WB.browser.mozilla = true;
	}
}

/**
 * fetch cookies
 */
document.cookie.split("; ").each(function(c) {
	c = c.split("=");
	WB.cookies.set(c[0], c[1]);
});


/**
 * init function
 *
 * @param string lang current language, if selected
 */
WB.init	= function(lang)
{
    if (typeof lang != "undefined") {
        WB.lang = lang;
    }

    Event.observe(document, 'mousemove', WB.observeMouse);

	WB.loaded	=	true;

};

/**
 * observe mouse position
 */
WB.observeMouse = function(e)
{
    WB.mouseCoords[0] = e.pointerX();
    WB.mouseCoords[1] = e.pointerY();
};

/**
 * access mouse position
 *
 * @return array
 */
WB.pointer = function()
{
    return WB.mouseCoords;
};

/**
 * find area
 *
 * Static function to find content area of current HTML node
 * @param string|node
 */
WB.find = function(el)
{
	el	=	$(el);

    var keys    =   WB.areas.keys();
    if (-1 != keys.indexOf(el.id)) {
        return el;
    }
    /*
    if (typeof WB.areas[el.id] != "undefined") {
        return el;
    }
    */

    if (!el.parentNode) {
        return false;
    }
    return $(WB.find(el.parentNode));
};

/**
 * make content area
 *
 * @param string area
 * @return WB.ContentArea
 */
WB.mkArea = function(area)
{
	console.debug("WB.mkArea is deprecated");
    WB.Class.load("WB.Ajax");
    WB.Class.load("WB.ContentArea");
    return new WB.ContentArea(area);
};

/**
 * Class loader
 *
 * Dynamic class loader provides some static functions to load
 * additional classes using AJAX requests
 *
 * @class WB.Class
 * @package WB
 */
WB.Class        = {};
WB.Class.loaded = {};

/**
 * load class
 *
 * Load class / JS file only once. Use list of loaded counters to avoid
 * duplicate loading of files
 *
 * @param string class name
 * @return true on success
 */
WB.Class.load = function(clazz)
{
    if ("undefined" != typeof WB.Class.loaded[clazz]) {
        ++WB.Class.loaded[clazz];
        return true;
    }

    var file = clazz.replace(/\./g, "/");
    try{
        WB.Class.include(file);
    }
    catch(e) {
        console.debug("failed to load class", clazz, e);
    }

    // remember load
    WB.Class.loaded[clazz]  =   1;
    return true;
};

/**
 * include file from server
 *
 * Load fiel synchronly and parse it. This should be avoided somehow,
 * e.g. by adding loaded modules to site.xml
 *
 * @param string
 */
WB.Class.include = function(file)
{
    file    =   WB.loader + file;

    // load javascript
    var req = Ajax.getTransport();
    req.open("get", file, false);

    req.send(null);
    if (!req.status || parseInt(req.status, 10) == 200) {
        eval(req.responseText);
    }
};


