/**
 * This javascript controls the UI
 * for the current page.
 *
 * $Id: $
 *
 * Copyright 2005-2006 Francois Helly <fhelly@bebop-design.net>
 *
 * See the enclosed file COPYING for license information (GPL). If you did not
 * receive this file, see http://www.fsf.org/copyleft/gpl.html.
 *
 * @version $Revision: 54 $
 * @package Rayge
 */

/* An array to store all the popups objects in */
var popups      = new Array();
/* An array to store all the toggler objects in */
var togglers    = new Array();
/* An array to store all the fixer objects in */
var fixers      = new Array();
/* An array to store all the cookie objects in */
var cookies     = new Array();
/* An array to store all the menu objects in */
var menus       = new Array();
/* An array to store all the rolllover objects in */
var rollovers   = new Array();
/* An array to store all the plugin objects in */
var plugins     = new Array();

/**
 * Activate the popup UI?
 * Determines if the popup user interface is allowed or not
 *
 * @return boolean  Default to false.
 */
function pisallowed()
{
    var isallowed      = new Boolean(false);
    if (typeof(cookies['pallow']) != 'undefined') {
        var userallow = new Boolean(cookies['pallow'].get(''));
        var support = new Boolean(document.getElementById && document.getElementsByTagName);
        isallowed = (userallow == true && support == true);
    }
    return isallowed;
}
/**
 * Activate/Deactivate the popup UI and reload the page
 * for updating functionalities attached to each.
 *
 * @param string allow    The flag defining the status of the popup UI
 * @param integer silent  Use silent mode? If set to 1,
 *                        the user will not be noticed
 */
function pallow(allow, silent)
{
    var msg = ui.I18N.popups.unsupported;
    if (typeof(silent) == 'undefined') {
        silent = 0;
    }
    if (typeof(cookies['pallow']) != 'undefined') {
        if (typeof(allow) != 'undefined') {
            cookies['pallow'].set(allow);
            msg = ui.I18N.popups.activated;

        } else {
            cookies['pallow'].set('');
            msg = ui.I18N.popups.deactivated;
        }
    }
    if (!silent) {
        alert(msg);
        wreload();
    }
}
/**
 * Close one popup or all the popups
 *
 * @param string name  If given, the popup to close
 *                     If undefined, all the opened
 *                     page popups will be closed
 */
function pclose(name)
{
    if (typeof(name) == 'undefined' &&
        typeof(window.name) != 'undefined') {
        name = window.name;
    }
    for (var pname in popups) {
        if (typeof(name) != 'undefined' &&
            (pname != name || pname != 'main')) {
            popups[pname].pclose();
        }
    }
}
function pforce()
{
    if (typeof(cookies['pallow']) != 'undefined') {
        cookies['pallow'].set(1);
    }
}
/**
 * If required, sets the current window name.
 *
 * @param string name  The name to set.
 *
 * @see uinit()
 */
function winit(name, rename)
{
    if (typeof(rename) == undefined) {
        rename = new Boolean(false);
    }
    if (window.name == '' ||
        (window.name != name && rename)) {
        window.name = name;
    }
}
/**
 * The window manager:
 * controls any window location.
 *
 * @param string name                The window name.
 * @param mixed string|array params  Any parameter the window might need.
 * @param integer relative           Compare relative or absolute location,
 *                                   or focus on opened popup.
 *                                   Default to 0: compare absolute location
 *                                   If set to -1, only focus on popup
 *                                   window, if opened.
 * @see uinit()
 * @see Popups()
 */
function wman(name, params, relative)
{
    /* Close the popup */
    if (typeof(popups[name]) != 'undefined' && !params) {
        popups[name].pclose();
        return true;
    }
    var allow  = pisallowed();
    var top    = 20;
    var left   = 20;
    var width  = 300;
    var height = 500;

    /* Use relative url or absolute ? */
    if (typeof(relative) == 'undefined') {
        relative = new Boolean(true);
    }

    switch (name)
    {
    case 'help':
    case 'dialog':
        relative = new Boolean(false);
        break;
    }
    if (allow == false) {
        window.location.href = params;
    } else {
        if (typeof(popups[name]) == 'undefined') {
            popups[name] = new Popups(name, top, left, width, height, 'toolbar=no,location=no,status=yes,scrollbars=yes,resizable=yes');
        }
        popups[name].popen(params, relative);
    }
    return true;
}
/**
 * Use this for debugging purposes.
 * add this line somewhere
 * window.onerror = handle_error;
 *
 * @param string msg  The message to display
 */
function handle_error(msg)
{
    window.alert(msg);
    return true;
}
/**
 * Encodes a string in unicode UTF-8.
 *
 * @params string str  The string to encode
 */
function cssAdd(css, media)
{
    if (!document.getElementsByTagName && !document.createElement &&
        !document.createAttribute) {
        var link = '<link hrel="stylesheet" href="'+css+'"';
        if (typeof(media) != 'undefined') {
            link += 'media="'+media+'"';
        }
        document.write(link+' />');
        return;
    }
    var link = document.createElement('LINK');
    /* todo: check defeat NS6 */
    link.rel = "stylesheet'";
    if (typeof(media) != 'undefined') {
        link.media = media;
    }
    link.href = css;
    document.getElementsByTagName('head')[0].appendChild(link);
}
/**
 * Encodes a string in unicode UTF-8.
 *
 * @params string str  The string to encode
 */
function utf8_encode(str)
{
    var c, s;
    var enc = '';
    var i = 0;
    while(i<str.length) {
        c= str.charCodeAt(i++);
        /* handle UTF-16 surrogates */
        if (c>=0xDC00 && c<0xE000) { continue; }
        if (c>=0xD800 && c<0xDC00) {
            if (i>=str.length) { continue; }
            s= str.charCodeAt(i++);
            if (s<0xDC00 || c>=0xDE00) { continue; }
            c= ((c-0xD800)<<10)+(s-0xDC00)+0x10000;
        }
        /* output value */
        if (c<0x80) {
            enc += String.fromCharCode(c);
        } else if (c<0x800) {
            enc += String.fromCharCode(0xC0+(c>>6),0x80+(c&0x3F));
        } else if (c<0x10000) {
            enc += String.fromCharCode(0xE0+(c>>12),0x80+(c>>6&0x3F),0x80+(c&0x3F));
        } else {
            enc += String.fromCharCode(0xF0+(c>>18),0x80+(c>>12&0x3F),0x80+(c>>6&0x3F),0x80+(c&0x3F));
        }
    }
    return enc;
}
/**
 * Returns the hexadecimal value of a character
 *
 * @param string n  The character
 */
function toHex(n)
{
    var hexchars = '0123456789ABCDEF';
    return hexchars.charAt(n>>4)+hexchars.charAt(n & 0xF);
}
/**
 * Encode a string to be safely used as an url
 *
 * @param string str  The string to encode
 */
function urlencode(str)
{
    if (str == '') {
        return str;
    }
    var str = str.replace(/%20/g, '+');
    if (typeof(encodeURI) == 'function') {
        return encodeURI(str);
    } else {
        /* escape() in IE5 and NS4 doesn't support utf8 */
        var okURIchars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-';
        var s = utf8_encode(str);
        var enc = '';
        for (var i= 0; i<s.length; i++) {
            if (okURIchars.indexOf(s.charAt(i))==-1) {
                enc += '%'+toHex(s.charCodeAt(i));
            } else {
                enc += s.charAt(i);
            }
        }
        return enc;
    }
}
/**
 * Decode an URL
 *
 * @param string str  The url to decode
 */
function urldecode(str)
{
    var str = str.replace(/[+]/g, ' ');
    if (typeof(decodeURI) == 'function') {
        return decodeURI(str);
    } else {
        return unescape(str);
    }
}
/**
 * Reloads the current window and,
 * if required, removes the location.hash
 *
 * @param boolean trunk  Defines if the location hash
 *                       will be removed or not,
 *                       default to false.
 */
function wreload(trunk)
{
    var nohash = new Boolean(false);
    if (typeof(trunk) != 'undefined') {
        nohash = trunk;
    }
    var url = document.location.href;
    if (url.lastIndexOf('#') != -1 && nohash) {
        url = url.substring(0, url.lastIndexOf('#'));
    }
    document.location.href = url;

}

