/**
 * 1.0 Original article and Script is from: http://think2loud.com/build-an-unsupported-browser-warning-with-jquery/
 * 1.1 Then the script was extended here: http://blog.team-noir.net/2009/06/fight-old-browsers-warning-with-jquery/
 * 1.2 And finally Fleshgrinder had a look at it and also minified it: http://www.nervenhammer.com/
 * 1.3 Google Chrome & new Safari detect added by www.team-noir.net
 * 1.4 Browser Version Update by www.team-noir.net
 * Extra modifications - Brian Power
 */

function badBrowser() {

	var userAgent = navigator.userAgent.toLowerCase();
	
	// Check for Microsoft Internet Explorer 8.0
	if ($.browser.msie && parseInt($.browser.version, 10) < 7) {
		return true;
	}
	// Check for Opera 10
	if ($.browser.opera && ($.browser.version *10) <= 100) {
		return true;
	}
	// Check for Mozilla Firefox 7.0
	if (/firefox[\/\s](\d+\.\d+)/.test(userAgent)) {
		var ffversion = Number(RegExp.$1);
		if (ffversion < 6) {
			return true;
		}
	}
	// Check for Safari < Version 5.0
	if (/safari[\/\s](\d+\.\d+)/.test(userAgent) && !/chrome[\/\s](\d+\.\d+)/.test(userAgent)) {
		var safari = userAgent.indexOf('version');
		if (safari > -1) {
			var snip1 = safari+8;
			var version = userAgent.substring(snip1, (snip1+1));
			if (version < 4) {
				return true;
			}
		}
	}
	// Check for Chrome < Version 14.0
	/*var chrome = userAgent.indexOf('chrome');
	if (chrome > -1) {
		var snip1 = chrome+7;
		var version = userAgent.substring(snip1, (snip1+1));
		if (version < 14) {
			return true;
			}
	}*/
	
    return false;
}

function getBadBrowser(c_name) {
	if (document.cookie.length > 0) {
		c_start = document.cookie.indexOf(c_name + "=");
		if (c_start != -1) {
			c_start = c_start +15;
			
			c_end   = document.cookie.indexOf(";",c_start + 4);
			if (c_end == -1) {
				c_end = document.cookie.length;
			}
			
			return unescape(document.cookie.substring(c_start,c_end));
		} 
	}
	return "";
}

function setBadBrowser(c_name,value,expiretime) {
	var now = new Date();
	var time = now.getTime();
	time += expiretime * 1000;
	now.setTime(time);
	document.cookie = c_name + "=" + escape(value) + ((expiretime === null) ? "" : ";expires=" + now.toGMTString());
}

if(badBrowser() && (getBadBrowser('browserWarning') != 'seen')) {
	$(function() {
		// Here you go with translating the content of the box
		$("<div id='browserWarning'>Fairway Market and Fairway Marketplace are best enjoyed using the latest version of Internet Explorer, Chrome or Firefox. We have detected that your browser is not one if these, and suggest that you upgrade in order to be able to fully experience our site and all of the fantastic things it has to offer.<br/><br/><span style='display:block;text-align:center;'>Click below to download the latest browser versions:<br/><a href='http://windows.microsoft.com/en-ie/internet-explorer/products/ie/home' target='_blank'>Internet Explorer</a> &nbsp;&nbsp;<a href='http://www.google.com/chrome' target='_blank'>Chrome</a> &nbsp;&nbsp;<a href='http://www.mozilla.com' target='_blank'>Firefox</a></span><br/><br/> <a href='#' id='warningClose' style='display:block;text-align:center;'>Close Window</a> </div>")
			// Delete the following to get full control over the style
			// of the warning box and style it through your stylesheet
			.css({
				'backgroundColor': '#FFF',
				'background-image': 'url(fairway/images/dialog_bg.jpg)',
				'background-repeat': 'no-repeat',
				'width': '100%',
				'text-align': 'justify',
				padding:'130px 30px 5px 30px'
			})
			// Last step is to prepend the just created DIV to the
			// opening <body>-Tag
			.dialog({ draggable: false,  width: '480', height: '320', modal: true, dialogClass: 'alert', resizable: false });
			$(".ui-dialog-titlebar").hide();
		// This generates the "close" link
		// You have the possibility to set up the closing speed of the box
		// just enter something different to the slow statement at slideUp
		$('#warningClose').click(function(){
			setBadBrowser('browserWarning','seen',3600);			
			$('#browserWarning').dialog('close');
			return false;
		});
	});
}

