DAP.Util.HeaderDetection = function() {
	this.setUserAgent(navigator.userAgent);
    this.getCssClass();
};

DAP.Util.HeaderDetection.prototype = {

    chrome : false,
    opera : false,
    firefox : false,
    firefox2 : false,
    firefox3 : false,
    firefox3_5 : false,
    ie : false,
    ie6 : false,
    ie7 : false,
    ie8 : false,
    mozilla : false,
    safari : false,
    safari3 : false,
    safari4 : false,
    windows : false,
    mac : false,
    linux : false,

    getCssClass : function() {
        if (document.getElementById && document.createTextNode) {
            var n = document.body;
            if (!n || n == undefined || n == null || n.length == 0) {
                setTimeout(function() {
                    var o = DAP.get('Util.HeaderDetection');
                    o.getCssClass();
                }, 250);
                return;
            }
        } else {
            DAP.log("Browser doesn't support document.getElementById()");
            return;
        }

        this.addClass('javascript');
        
        if (this.windows) {
            this.addClass('windows');
        } else if (this.mac) {
            this.addClass('mac');
        } else if (this.linux) {
            this.addClass('linux');
        }

        if (this.ie) {
            this.addClass('ie');
            if (this.ie8) {
                this.addClass('ie8');
            } else if (this.ie7) {
                this.addClass('ie7');
            } else if (this.ie6) {
                this.addClass('ie6');
            }
        } else if (this.firefox) {
            this.addClass('firefox');
            if (this.firefox3_5) {
                this.addClass('firefox3-5');
            } else if (this.firefox3) {
                this.addClass('firefox3');
            } else if (this.firefox2) {
                this.addClass('firefox2');
            }
        } else if (this.safari) {
            this.addClass('safari');
            if (this.safari4) {
                this.addClass('safari4');
            } else if (this.safari3) {
                this.addClass('safari3');
            }
        } else if (this.chrome) {
            this.addClass('chrome');
        } else if (this.opera) {
            this.addClass('opera');
        } else if (this.mozilla) {
            this.addClass('mozilla');
        }
    },

    isChrome : function() {
        return this.chrome;
    },

    isOpera : function() {
        return this.opera;
    },

    isFirefox : function() {
        return this.firefox;
    },

    isFirefox2 : function() {
        return this.firefox2;
    },

    isFirefox3 : function() {
        return this.firefox3;
    },

    isFirefox3_5 : function() {
        return this.firefox3_5;
    },

    isIe : function() {
        return this.ie;
    },

    isIe6 : function() {
        return this.ie6;
    },

    isIe7 : function() {
        return this.ie7;
    },

    isIe8 : function() {
        return this.ie8;
    },

    isMozilla : function() {
        return this.mozilla;
    },

    isSafari : function() {
        return this.safari;
    },

    isSafari3 : function() {
        return this.safari3;
    },

    isSafari4 : function() {
        return this.safari4;
    },

    isWindows : function() {
        return this.windows;
    },

    isMac : function() {
        return this.mac;
    },

    isLinux : function() {
        return this.linux;
    },

    setUserAgent : function(ua) {
        this.userAgent = ua;
        var u = ua.toLowerCase()

        var t = '';
        var v = '';

        if (u.indexOf("msie") != -1){
            this.ie = true;
            t = u.substring(u.indexOf('msie'), u.length);
            v = t.substring(4, t.indexOf(';'));
            v = this.trim(v);

            if (v.charAt(0) == '6') {
                this.ie6 = true;
            } else if (v.charAt(0) == '7') {
                this.ie7 = true;
            } else if (v.charAt(0) == '8') {
                this.ie8 = true;
            }
        } else if (u.indexOf("firefox") != -1) {
            this.firefox = true;
            t = u.substring(u.indexOf('firefox'));
            v = t.substring(8);
            v = this.trim(v);

            if (v.charAt(0) == '2') {
                this.firefox2 = true;
            } else if (v.charAt(0) == '3') {
                if (v.match("^" + '3.5') == '3.5') {
                    this.firefox3_5 = true;
                } else {
                    this.firefox3 = true;
                }
            }
        } else if (u.indexOf("safari") != -1) {
            this.safari = true;
            t = u.substring(u.indexOf('version/'));
            v = t.substring(8);
            v = this.trim(v);

            if (v.charAt(0) == '3') {
                this.safari3 = true;
            } else if (v.charAt(0) == '4') {
                this.safari4 = true;
            }
        } else if (u.indexOf("chrome") != -1) {
            this.chrome = true;
        } else if (u.indexOf("opera") != -1) {
            this.opera = true;
        } else if (u.indexOf("mozilla") != -1) {
            this.mozilla = true;
        }

        if (u.indexOf("windows") != -1) {
            this.windows = true;
        } else if (u.indexOf("macintosh") != -1) {
            this.mac = true;
        } else if (u.indexOf("linux") != -1) {
            this.linux = true;
        }
    },

    addClass : function(c) {
        if(document.getElementById && document.createTextNode) {
            var n = document.body;
            if(n) {
                n.className += n.className ? ' ' + c : c;
            }
        }
    },

    trim : function(str) {
        return (str.replace(/^[\s\xA0]+/, "").replace(/[\s\xA0]+$/, ""));
    }

};

DAP.register('Util.HeaderDetection', new DAP.Util.HeaderDetection());
