/**
 * Copyright (c) 2001-2010 Laszlo Systems, Inc.
 * All Rights Reserved.
 */
function BrowserUtilities(){
    var me = this;
    //TODO: move locale discovery into here

    this.isff = navigator.userAgent.indexOf("Firefox") != -1;
    this.isie = navigator.userAgent.indexOf('MSIE') != -1;
    this.issafari = navigator.userAgent.indexOf("Safari") != -1;
    this.isopera = navigator.userAgent.indexOf("Opera") != -1;
    this.isnetscape = navigator.userAgent.indexOf("Netscape") != -1;

    this.iswin = navigator.appVersion.indexOf("Win") != -1;
    this.ismac = navigator.appVersion.indexOf("Mac") != -1;
    this.ismacppc = navigator.platform.indexOf("PPC") != -1;
    this.isunix = navigator.appVersion.indexOf("X11") != -1;
    this.islinux = navigator.appVersion.indexOf("Linux") != -1;

    //holds reference to the framescollection once it's been found
    //see getFrameByName
    this._framesColl = null;

    this._windowHasFocus = true;

    //set a cookie value
    this.setCookie = function (cookieName, cookieValue, nDays){
        var today = new Date();
        var expire = new Date();
        if (nDays == null || nDays == 0) 
            nDays = 1;
        expire.setTime(today.getTime() + 3600000 * 24 * nDays);
        document.cookie = cookieName + "=" + escape(cookieValue) +
        ";expires=" +
        expire.toGMTString() +
        ";path=/"; // Scopes the cookie to the same scope as cookies set by the server.
    }

    //read a cookie value
    this.readCookie = function(name){
        var nameEQ = name + "=";
        var ca = document.cookie.split(';');
        for (var i = 0; i < ca.length; i++) {
            var c = ca[i];
            while (c.charAt(0) == ' ') {
                c = c.substring(1, c.length);
            }
            if (c.indexOf(nameEQ) == 0) {
                return unescape(c.substring(nameEQ.length, c.length));
            }
        }
        return null;
    }

    // Check that cookies are enabled.
    this.cookiesEnabled = function() {
        var v = Math.floor(1000 * Math.random());
        this.setCookie('_ce', v);
        return v == this.readCookie('_ce');
    }

    //get the value for the given variable name off the query string
    this.getQueryVariable = function(variable){
        var query = window.top.location.search.substring(1);
        var vars = query.split("&");
        for (var i = 0; i < vars.length; i++) {
            var pair = vars[i].split("=");
            if (pair[0] == variable) {
                return pair[1];
            }
        }
    }

    //Sets the URL of the top level window
    this.setLocation = function(url){
        top.window.location.href = url;
    }

    //Get the URL of the top level window
    this.getLocation = function(){
        return top.window.location;
    }

    //Opens a URL with the provided target and options. A wrapper around 
    //window.open.
    this.openUrlWithOptions = function(url, target, options){
        var test = null;
        if (options !='null' && options != ''){//while the lzx function call pass the 'null' string
            test = window.open(url, target, options);
        } else {
            test = window.open(url, target);
        }
        if (test == null) {
            if (this.isie) {
                alert('Please disable your popup blocker to use this function.\r\rWarning: Internet Explorer may reload the application if you enable popups.  Be sure to save your work before doing so.');
            } else {
                alert('Please disable your popup blocker to use this function.');
            }
        }
    }

    //get references to the window objects of the hidden frames via the
    //frames collection. document.getElementById would just give reference
    //to the iframe object which doesn't support printing
    this.getFrameByName = function(frameName){
        if (!this['_framesColl']) {
            if (document.frames) { //Opera, Internet Explorer
                this._framesColl = document.frames;
            } else {
                this._framesColl = window.frames; //Firefox, Safari, Netscape
            }
        }
        if (this._framesColl) {
            return this._framesColl[frameName];
        }
        return null;
    }

    //Display a growl notification if the user's system supports it and the 
    //browser window is in the background.
    this.showDesktopNotification = function(title, message, iconUrl) {
        if (window['fluid'] && !me._windowHasFocus) {
            window.fluid.showGrowlNotification({
                title: title,
                description: message,
                icon: iconUrl
            });
        }
    }

    //Track if the browser window has focus or not.
    this._onBlur = function() {me._windowHasFocus = false;}
    this._onFocus = function() {me._windowHasFocus = true;}
    if (this.isie) {
        document.onfocusin = this._onFocus;
        document.onfocusout = this._onBlur;
    } else {
        window.onfocus = this._onFocus;
        window.onblur = this._onBlur;
    }
}

