var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{
			string: navigator.userAgent,
			subString: "Chrome",
			identity: "Chrome"
		},
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari",
			versionSearch: "Version"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			   string: navigator.userAgent,
			   subString: "iPhone",
			   identity: "iPhone/iPod"
	    },
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]
 
};
//////////////////////////////
//browser settings //////////
////////////////////////////
var unsupportedBrowsers = ["Chrome"];
var unsupportedBrowsersVersions = [{browser:"Explorer",version:6.0,ltFlag:true}];

function _checkBrowser(){
	
	BrowserDetect.init();
	var _brow = BrowserDetect.browser.toLowerCase();
	var _browV = BrowserDetect.version;
	
	if (jQuery.cookie('usb_msg_ignored' + _brow + _browV)) {
	    return true;
	}
	
	var failFlag = false;	
			
	jQuery.each(unsupportedBrowsers,function(i){
			if(this.toLowerCase() == _brow){
				failFlag = true;
				return _showUnsupportedBrowserWarning(BrowserDetect,false);
			}

    });
	
	if(!failFlag){		
		jQuery.each(unsupportedBrowsersVersions,function(i){
			if(this.browser.toLowerCase() == _brow){				
				if(this.ltFlag && _browV <= this.version){
						failFlag = true;
						return _showUnsupportedBrowserWarning(BrowserDetect,true);
					
				}else if(_browV == this.version){
						failFlag = true;
						return _showUnsupportedBrowserWarning(BrowserDetect,true);						
				}
			}
		});
	}
}

function _showUnsupportedBrowserWarning(b,v){

    var u_warning = document.createElement('div');
    u_warning.id = "u_warning_div";

    var u_warning_html = "<b>Your browser ( <b style='color:red;'>" + b.browser + ' ' + b.version + "</b> ) is not recommended for viewing the Village of Schaumburg website.";
    u_warning_html = u_warning_html + " Some areas of the website may not display correctly, or certain features and functions of the website may be unavailable.</b><br/><br/>";
    u_warning_html = u_warning_html + "The Village of Schaumburg website was designed & developed in 2009 to work with the latest <b>Microsoft Internet Explorer</b> and <b>Mozilla Firefox</b> web browsers. ";
    u_warning_html = u_warning_html + "Please click on any of the links below to download the latest browser for your computer...<br/>"
    u_warning_html = u_warning_html + "<center><table cellpadding='0' cellspacing='0' width='300'><tr><td align='center'><a style='text-decoration:none;' href='http://www.microsoft.com/windows/Internet-explorer/'><img src='/Style%20Library/Images/VOS/ie8_icon.gif' border='0' alt='Microsoft Internet Explorer'><br/>Internet Explorer</a></td>";
    u_warning_html = u_warning_html + "<td align='center'><a style='text-decoration:none;' href='http://www.firefox.com'><img src='/Style%20Library/Images/VOS/firefox_icon.gif' border='0' alt='Firefox'><br/>Mozilla Firefox</a></td></table></center>";

    $(u_warning).html('<table width="100%" cellspacing="5"><tr><td>' + u_warning_html + '</td><td align="right" valign="top"><img src="/Style%20Library/Images/VOS/close_icon.gif" onclick="_hideUnsupportedBrowserWarning(\'' + b.browser + '\', \'' + b.version + '\')" alt="Close Message" style="cursor:pointer;" /></td>');
    $(u_warning).css({
                    "border-bottom": "solid 1px #000",
                    "background": "#ffffe7",
                    "color": "#000000",
                    "font-family": "Arial",
                    "font-size": "11pt",
                    "text-align": "left",
                    "display": "none",
                    "position" :"absolute",
                    "top" : "0px",
                    "left" : "0px",
                    "z-index" : "2000"                 
                }); 
  
    document.body.insertBefore(u_warning, document.body.firstChild);

    setTimeout(function() {
        $(u_warning).slideDown("slow");       
    }, 400);
	
	return false;
}

function _hideUnsupportedBrowserWarning(b, v) {
    var u_warning = document.getElementById('u_warning_div');
    if (u_warning) {
        $(u_warning).slideUp("slow", function() {
            document.body.removeChild(u_warning);
        });
        jQuery.cookie('usb_msg_ignored' + b.toString().toLowerCase() + v, '1', { secure: false, path:"/"});
    }
}

//Cookie functions
jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value == null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};


