// Browser type
var Browser = new Object();
var ua = navigator.userAgent.toLowerCase();
Browser.isMozilla = (typeof document.implementation != 'undefined')
                    && (typeof document.implementation.createDocument != 'undefined')
                    && (typeof HTMLDocument!='undefined');
Browser.isIE      = window.ActiveXObject ? true : false;
Browser.isIE7     = (Browser.isIE && ua.indexOf('msie 7') != -1) ? true : false;
Browser.isFirefox = (ua.indexOf("firefox") != -1);
Browser.isSafari  = (ua.indexOf("safari") != -1);
Browser.isOpera   = (typeof(window.opera) != 'undefined');

// Environment
var Environment = {};
Environment.isDev  = new RegExp("dev").test(window.location.host);
Environment.isLife = Environment.isDev ? false : true;

if (Environment.isLife) {
    self.onerror = function() {return true;}
}

if (!("console" in window) || !("firebug" in console)) {
    var names = ["log", "debug", "info", "warn", "error", "assert", "dir", "dirxml", "group",
                 "groupEnd", "time", "timeEnd", "count", "trace", "profile", "profileEnd"];
    window.console = {};
    for (var i = 0, length = names.length; i < length; ++i) {
        window.console[names[i]] = function() {};
    }
}

function dispatchException(e) {
    if (Environment.isDev) {
        if (Browser.isIE) {
            //alert(e.message);
        } else {
            console.error(e);
        }
    }
}

function dispatchException (e) {
}
if (typeof $ == "undefined") {
    var $ = function (id) {
        return document.getElementById(id);
    }
}

function elem(name, attrs, style, text) {
    var e = document.createElement(name);
    if (attrs) {
        for (key in attrs) {
            if (key == 'class') {
                e.className = attrs[key];
            } else if (key == 'id') {
                e.id = attrs[key];
            } else {
                e.setAttribute(key, attrs[key]);
            }
        }
    }
    if (style) {
        for (key in style) {
            e.style[key] = style[key];
        }
    }
    if (text) {
        e.appendChild(document.createTextNode(text));
    }
    return e;
}

var debugLine = 1;
function debug(message) {
    message = message || "";
    message = message ? (debugLine++) + ": " + message : message;
    var blockElement = elem("DIV", false, {
        height: "15px",
        background: "#EFEFEF",
        padding: "0 2px",
        margin: "2px",
        border: "1px solid white"
    });

    blockElement.appendChild(elem("DIV", false, {
        width: "120px",
        fontSize: "9px",
        styleFloat: "right",
        cssFloat: "right",
        border:"1px solid gray",
        background:"silver"
    }, new Date().toTimeString()));

    blockElement.appendChild(document.createTextNode(message));
    $('debug').appendChild(blockElement);
}

function getQueryVariable(variable) {
    var query = window.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];
        }
    }
    return false;
}

function wopen(wurl, h, w, name) {
    window.open(wurl, 'NN' + name, 'height=' + h + ',width=' + w + ',resizable=yes,scrollbars=yes,menubar=no,status=yes');
}

if (typeof (worker) == "undefined") {
    var worker = function() {};
}

Date.prototype.toGtmString = function() {
    var weekDays = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
    var months   = ["Jan", "Feb", "Mar", "Apr", "May", "Jun",
                    "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];

    var string = weekDays[this.getDay()] 
               + ", " + this.getDate()
               + " " + months[this.getMonth()]
               + " " + this.getFullYear();
    return string;
}

