/**
 * This javascript controls the UI for the current page.
 *
 * $Id: lib.js 872 2009-12-30 19:26:52Z fhelly $
 *
 * Copyright 2005-2009 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: 808 $
 * @package Rayge
 */
/* Dependencies */
var protocol = (('https:' == document.location.protocol) ? 'https://ssl.' : 'http://www.');
var load = {
    core : [ 'prototype', 'cookie', 'window', 'popup', 'element', 'browser' ],
    dyn : [ 'toggler', 'tips', 'chaser', 'rollovers', 'tbox', 'list','tabset' ],
    navigation : [ 'menu' ],
    external : [ protocol + 'google-analytics.com/ga.js' ]
};
/* The web path to the javascripts files to load */
if (typeof (path) == 'undefined') {
    var path = '/resources/js/';
}
/* An array to store all the popups objects in */
var popups = [];
/* An array to store all the cookie objects in */
var cookies = [];
var tboxes = [];
/* An array to store all the plugin objects in */
var plugins = [];

/**
 * Activate the popup UI? Determines if the popup user interface is allowed or
 * not
 *
 * @return boolean Default to false.
 */
var pisallowed = function() {
    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
 */
var pallow = function(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);
        window.reload();
    }
};
/**
 * 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
 */
var pclose = function(name) {
    if (typeof (name) == 'undefined' && typeof (window.name) != 'undefined') {
        name = window.name;
    }
    for ( var pname in popups) {

        if (typeof (name) != 'undefined' && typeof (popups[pname]) == 'object'
                && (pname != name || pname != 'main')) {
            popups[pname].pclose();
        }
    }
};
var pforce = function() {
    if (typeof (cookies['pallow']) != 'undefined') {
        cookies['pallow'].set(1);
    }
};
/**
 * Cookies initialization Add here any common cookie You can also initialize a
 * cookie locally
 *
 * @see Cookie()
 */
var cinit = function() {
    /* Set cookie for 2 days */
    cookies['pallow'] = new bbCookie('pallow', {
        life : 2
    });
};
/**
 * If required, sets the current window name.
 *
 * @param string
 *            name The name to set.
 *
 * @see uinit()
 */
var winit = function(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()
 */
var wman = function(n, u, r) {
    /* Close the popup */
    if (typeof (popups[n]) != 'undefined' && !u) {
        popups[n].pclose();
        return true;
    }
    var allow = pisallowed();
    var top = 20;
    var left = 20;
    var width = 300;
    var height = 500;
    var options = 'toolbar=no,location=no,status=yes,scrollbars=yes,resizable=yes';

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

    switch (n) {
    case 'new':
        window.open(u);
        return true;
    case 'help':
    case 'dialog':
    case 'history':
    case 'viewer':
        r = new Boolean(false);
        break;
    }

    if (allow == false) {
        if(n == 'viewer') {
            window.open(u);
        } else {
            window.location.href = u;
        }
    } else {
        if (typeof (popups[n]) == 'undefined') {
            if (typeof (ui.settings.popups[n]) != 'undefined') {
                top = ui.settings.popups[n].top;
                left = ui.settings.popups[n].left;
                width = ui.settings.popups[n].width;
                height = ui.settings.popups[n].height;
                options = ui.settings.popups[n].options;
            }
            popups[n] = new bbPopup(n, top, left, width, height, options);
        }
        popups[n].popen(u, r);
    }
    return true;
};
/**
 * UI initialization, any effect/functionality required on each page.
 *
 * @see Popups()
 */
var uinit = function() {
    popups['main'] = new bbPopup('main', 0, 0, 800, 600,
            'toolbar=yes,location=yes,status=yes,scrollbars=yes,resizable=yes');
    if (typeof (bbCompat) != 'undefined') {
        var compat = new bbCompat( {
            pngFix : {
                objects : $$('input.cancel', 'input.submit', 'input.reset')
            }
        });
    }
    if (!!(window.attachEvent && !window.opera)) {
        if (navigator.appVersion.indexOf('MSIE 5') > -1
                || navigator.appVersion.indexOf('MSIE 6') > -1) {
            bbLoader.addCss('/resources/themes/css/cefael/css/library/ie6.css',
                    'screen');
        }
    }
};
var minit = function() {

    var m = $('head').down('ul.util', 0);

    if (m == null) {
        return false;
    }
    /* Popups enabling */
    var text = 'deactivate';
    if (typeof (ui.I18N.popups) != 'undefined') {
        text = ui.I18N.popups.deactivation;
    }
    var allow = pisallowed();

    var value = '';
    var id = 'nopopups';

    if (allow == false) {
        text = 'activate';
        if (typeof (ui.I18N.popups) != 'undefined') {
            text = ui.I18N.popups.activation;
        }
        id = 'popups';
        value = 1;
    }
    var param = {
        id : id,
        title : text,
        position : -1,
        href : 'javascript:pallow(' + value + ');',
        className : 'icon'
    };
    if (window.name != 'main') {
        param.href = undefined;
        m.addMenuItem(m.id, param);
    } else {
        m.addMenuItem(m.id, param);
    }
};
var tinit = function() {

    if (typeof (bbToggler) != 'undefined') {
        var otitle = 'show';
        var ctitle = 'hide';
        if (typeof (ui.I18N.toggler) != 'undefined') {
            otitle = ui.I18N.toggler.display;
            ctitle = ui.I18N.toggler.hide;
        }
        $('main').addToggler('head', {
            id : 'thm',
            classname : 'reducer',
            otitle : otitle,
            ctitle : ctitle,
            usecookie : 'cr'
        });
   }
};
var spinner = function() {
    tboxes['spinner'] = new bbTBox('spinner');
    tbload('spinner', null);
};
var tbload = function(i, s) {
    if (typeof (tboxes[i]) != 'undefined' && typeof (s) != 'undefined') {
        var o = {
            onComplete : function() {
                tboxes[i].display();
            },
            onFailure : function() {
                tboxes[i].remove();
                alert('A problem occured');
            }
        };
        tboxes[i].load(s, o);
    }
};
var tabset = function () {
    $$('ul.tabset', 'ul.admin').each(function(t){t.addTabset({resize: true, col: 6, classname: 'tabset'});});
}
var ga = function() {
    /*
    try {
        var pageTracker = _gat._getTracker('UA-8580289-1');
        pageTracker._trackPageview();
    } catch (err) {
    }
    */
};
if (typeof (bbLoader) != 'undefined') {
    for ( var module in load) {
        bbLoader.load(load[module], module, path);
    }
    bbLoader.compat();
}
