// +------------------------------------------------------------------------+
// | Behaviour                                                              |
// +------------------------------------------------------------------------+
var Behaviour = {

    /**
     * Enter description here...
     *
     * @var Array
     */
    aRuleSet : new Array(),

    /**
     * Enter description here...
     *
     * @return void
     */
    register : function (_oRuleSet)
    {
        Behaviour.aRuleSet.push(_oRuleSet);
    },

    /**
     * Enter description here...
     *
     * @return void
     */
    apply : function ()
    {
        var iLength = Behaviour.aRuleSet.length;
        var iIndex  = 0;

        while (iLength--)
        {
            var oRuleSet = Behaviour.aRuleSet[iIndex++];

            for (var sSelector in oRuleSet)
                oRuleSet[sSelector]($j(sSelector));
        }
    }
};



// +------------------------------------------------------------------------+
// | Collection of miscellaneous useful functions                           |
// +------------------------------------------------------------------------+
var misc = {

    /**
     * Add to favorites
     *
     * @param string _sUrl
     * @param string _sTitle
     */
    add_favorite : function (_sUrl, _sTitle)
    {
        if (document.all)
            window.external.AddFavorite(_sUrl, _sTitle);

        else if (window.sidebar)
            window.sidebar.addPanel(_sTitle, _sUrl,"");

        else if (window.opera && window.print)
        {
            var eBm = document.createElement('a');
            eBm.setAttribute('rel'  , 'sidebar');
            eBm.setAttribute('href' , _sUrl);
            eBm.setAttribute('title', _sTitle);
            eBm.click();
        }
    }
};



// +------------------------------------------------------------------------+
// | Collection of useful functions to handle global vars (eg. cookies)     |
// +------------------------------------------------------------------------+
var vars = {

    /**
     * Set a cookie.
     *
     * @param  string  cookie name
     * @param  string  cookie value
     * @param  date    cookie expiring date
     * @param  string  path
     * @param  string  domain
     * @param  boolean secure
     * @return void
     */
    setCookie : function  (_sName, _sValue, _iE, _sP, _iH, _iS)
    {
        _sP = _sP ? _sP : '/'

        document.cookie = escape(_sName) + '=' + escape(_sValue)
                        + (_iE ? '; expires=' + _iE.toGMTString() : '')
                        + (_sP ? '; path='    + _sP               : '')
                        + (_iH ? '; host='    + _iH               : '')
                        + (_iS ? '; secure'                       : '');
    },

    /**
     * Return value of given cookie name.
     *
     * @param  string _sName       Cookie name
     * @return string sCookieValue Cookie value
     */
    getCookie : function (_sName)
    {
        var sCookieValue = '';
        var iPosName     = document.cookie.indexOf(escape(_sName) + '=');

        if (iPosName != -1)
        {
            var iPosVal = iPosName + (escape(_sName) + '=').length;
            var iEndPos = document.cookie.indexOf(';', iPosVal);

            sCookieValue = (iEndPos != -1)
                         ? unescape(document.cookie.substring(iPosVal, iEndPos))
                         : unescape(document.cookie.substring(iPosVal));
        }

        return sCookieValue;
    },

    /**
     * Clears given cookie.
     *
     * @param  string _sCookie Cookie name
     * @return void
     */
    clearCookie : function (_sCookie)
    {
        var oNow       = new Date();
        var iYesterday = new Date(oNow.getTime() - 1000 * 60 * 60 * 24);

        this.setCookie(_sCookie, '', iYesterday);
    }
};



// +------------------------------------------------------------------------+
// | Collection of useful functions concerning environment                  |
// +------------------------------------------------------------------------+
var env = {

    /**
     * Returns true if user agent is MSIE.
     *
     * @return boolean
     */
    isIE : function ()
    {
        return (GLOBALS.ie6 || GLOBALS.ie7)
    },

    /**
     * Returns true if user platform is mac.
     *
     * will not work with opera & internet explorer
     * @return boolean
     */
    isMac : function ()
    {
        return (
            !window.opera
         && this.agent('lower').indexOf("mac") != -1
        );
    },

    /**
     * Return true if user agent is apple safari.
     *
     * @return boolean
     */
    isSafari : function ()
    {
        return (
            !window.opera
         && (this.productSub() >= 20020000)
         && (this.vendor().indexOf("Apple Computer") != -1)
        );
    },

    /**
     * Returns user agent as string.
     *
     * @param  string _sCase Optional.
     * @return string
     */
    agent : function (_sCase)
    {
        var sUA = navigator.userAgent;

        if (!_sCase)
            return sUA;

        if (_sCase == 'lower')
            return sUA.toLowerCase();

        if (_sCase == 'upper')
            return sUA.toUpperCase();

        return sUA;
    },

    /**
     * Returns browser's vendor.
     *
     * @return string
     */
    vendor : function ()
    {
        return navigator.vendor;
    },

    /**
     * Returns naviagator productSub.
     *
     * @return string
     */
    productSub : function ()
    {
        return parseInt(navigator.productSub);
    },

    /**
     * Returns query string.
     *
     * @return string
     */
    queryString : function ()
    {
        var sUrl = window.location.href;

        if (!sUrl.match(/\?/))
            return '';

        return sUrl.substring(sUrl.indexOf('?')+1);
    },

    /**
     * Redirect to given URL.
     *
     * @param  string _sUrl Optional. URL
     * @return void
     */
    redirect : function (_sUrl)
    {
        if (!_sUrl)
        {
            window.location.reload();
            return;
        }

        window.location.href = _sUrl;
    },

    /**
     * Returns array with page width, height and window width, height
     * Core code from - quirksmode.org
     * Edit for Firefox by pHaez
     *
     * @return Array
     */
    getPageSize : function ()
    {
        var xScroll, yScroll;

        if (window.innerHeight && window.scrollMaxY) {
            xScroll = document.body.scrollWidth;
            yScroll = window.innerHeight + window.scrollMaxY;
        } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
            xScroll = document.body.scrollWidth;
            yScroll = document.body.scrollHeight;
        } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
            xScroll = document.body.offsetWidth;
            yScroll = document.body.offsetHeight;
        }

        var windowWidth, windowHeight;
        if (self.innerHeight) { // all except Explorer
            windowWidth = self.innerWidth;
            windowHeight = self.innerHeight;
        } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
            windowWidth = document.documentElement.clientWidth;
            windowHeight = document.documentElement.clientHeight;
        } else if (document.body) { // other Explorers
            windowWidth = document.body.clientWidth;
            windowHeight = document.body.clientHeight;
        }

        // for small pages with total height less then height of the viewport
        if(yScroll < windowHeight){
            pageHeight = windowHeight;
        } else {
            pageHeight = yScroll;
        }

        // for small pages with total width less then width of the viewport
        if(xScroll < windowWidth){
            pageWidth = windowWidth;
        } else {
            pageWidth = xScroll;
        }

        aSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight)
        return aSize;
    }
};