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"
		}
	]

};
BrowserDetect.init();
var b = BrowserDetect.browser;
var v = BrowserDetect.version;
var o = BrowserDetect.OS;
// Fix CSS Error Opera & Chrome
if(b == "Opera" || b == "Chrome" || b == "Safari") {
	document.getElementById("intro").style.marginBottom  = "37px";
} else if(b == "Firefox" && o == "Mac") {
	document.getElementById("intro").style.marginBottom  = "32px";
	document.getElementById("intro").style.marginTop  = "45px";
}

// Fancybox for old browser (FF < 3, IE < 8, Opera < 10, Safari < 4)
function IncludeFancybox() {
	document.write('<script type="text\/javascript" src="\/js\/colorbox\/jquery.colorbox.js"><\/script>');
	document.write('<link rel="stylesheet" type="text\/css" href="\/js\/colorbox\/colorbox.css" media="screen" \/>');
	//Write in browser languages
	var userLang = (navigator.language) ? navigator.language : navigator.userLanguage; 
	var lang = userLang.substring(0, 2);
	if(lang == "it") {
		document.getElementById("titleAttention").innerHTML = "Il tuo browser non &egrave; completamente supportato da questo sito!";
		document.getElementById("txtAttention").innerHTML = "Devi aggiornare il tuo browser all'ultima versione disponbile presso il tuo sviluppatore per poter approfittare di tutte le funzioni di questo sito.";
		document.getElementById("upgradeLink").innerHTML = "Voglio aggiornare il mio browser";
		document.getElementById("continueLink").innerHTML = "Capisco, ma voglio continuare su questo sito";
	} else {
		document.getElementById("titleAttention").innerHTML = "Your browser is not fully supported by this site!";
		document.getElementById("txtAttention").innerHTML = "You need to upgrade your browser from your developer to enjoy this site.";
		document.getElementById("upgradeLink").innerHTML = "I want to upgrade my browser";
		document.getElementById("continueLink").innerHTML = "I understand, but I want continue in your site";
	}
}

function setCookieAlert() {
	document.cookie = 'luiisgroupAlert=true; path=/'
}
function readCookie(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 c.substring(nameEQ.length,c.length);
	}
	return null;
}
//alert(readCookie("luiisgroupAlert"));
if(!readCookie("luiisgroupAlert")) {
	if(b == "Firefox" && v < 3.00) {
		IncludeFancybox();
		$(window).load( function() {
			$("#alert").colorbox({
						open					: true,
						opacity					: "0.8",
						overlayClose			: false,
						inline					: true,
						transition				: "fade",
						speed					: 400,
						href					:'#alert'
			});
		});
		$('a#upgradeLink').attr("href", "http://www.mozilla-europe.org/firefox/");
		$('a#upgradeLink').click(function() {
			closeBox();
		});
	}
	if(b == "Explorer" && v < 8.00) {
		IncludeFancybox();
		$(window).load( function() {
			$("#alert").colorbox({
						open					: true,
						opacity					: "0.8",
						overlayClose			: false,
						inline					: true,
						transition				: "fade",
						speed					: 400,
						href					:'#alert'
			});
		});
		$('a#upgradeLink').attr("href", "http://www.microsoft.com/windows/internet-explorer/worldwide-sites.aspx");
		$('a#upgradeLink').click(function() {
			closeBox();
		});

	}
	if(b == "Opera" && v < 10.00) {
		IncludeFancybox();
		$(window).load( function() {
			$("#alert").colorbox({
						open					: true,
						opacity					: "0.8",
						overlayClose			: false,
						inline					: true,
						transition				: "fade",
						speed					: 400,
						href					:'#alert'
			});
		});
		$('a#upgradeLink').attr("href", "http://www.opera.com/browser/download/");
		$('a#upgradeLink').click(function() {
			closeBox();
		});
	}
	if(b == "Safari" && v < 3.00) {
		IncludeFancybox();
		$(window).load( function() {
			$("#alert").colorbox({
						open					: true,
						opacity					: "0.8",
						overlayClose			: false,
						inline					: true,
						transition				: "fade",
						speed					: 400,
						href					:'#alert'
			});
		});
		$('a#upgradeLink').attr("href", "http://www.apple.com/safari/download/");
		$('a#upgradeLink').click(function() {
			closeBox();
		});
	}
}
function closeBox() {
	$(document).ready( function() {
    	$('#cboxClose').trigger('click');
	});
	document.getElementById("colorbox").style.visibility  = "hidden";
	setCookieAlert();
}
