﻿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";
	},
	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.vendor,
		subString: "Apple",
		identity: "Safari"
	}, {
		prop: window.opera,
		identity: "Opera"
	}, {
		string: navigator.userAgent,
		subString: "Flock",
		identity: "Flock"
	}, {
		string: navigator.userAgent,
		subString: "Firefox",
		identity: "Firefox"
	}, {
		string: navigator.userAgent,
		subString: "MSIE",
		identity: "IExplorer",
		versionSearch: "MSIE"
	}]
};
var BrowserCompatible = {
	check: function(){
		BrowserDetect.init();
		if ((this.useBlackList && this.unCompatibleBrowsers[BrowserDetect.browser] && BrowserDetect.version <= this.unCompatibleBrowsers[BrowserDetect.browser]) ||
		    (!this.useBlackList && (BrowserDetect.version < this.compatibleBrowsers[BrowserDetect.browser] || !this.compatibleBrowsers[BrowserDetect.browser]))) {
			if (!this.readCookie('browsercheck_dontShowAgain')) 
				this.showWarning();
		}
	},
	getStyle: function(el, styleProp){
		var x = el;
		if (x.currentStyle) 
			var y = x.currentStyle[styleProp];
		else 
			if (window.getComputedStyle) 
				var y = document.defaultView.getComputedStyle(x, null).getPropertyValue(styleProp);
		return y;
	},
	createCookie: function(name, value, days){
		if (days) {
			var date = new Date();
			date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
			var expires = ";expires=" + date.toGMTString();
		}
		else 
			var expires = "";
		document.cookie = name + "=" + value + expires + ";path=/";
	},
	
	readCookie: function(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;
	},
	
	eraseCookie: function(name){
		this.createCookie(name, "", -1);
	},
	showWarning: function(){
		if(!this.lang){
			this.lang=navigator.language || navigator.browserLanguage;
			if(!this.langTranslations[this.lang]) this.lang="en";
		}
		var bg = document.createElement("div");
		bg.id = "browsercheck_bg";
		bg.style["background"] = "#fff";
		bg.style["filter"] = "alpha(opacity=90)";
		bg.style["-moz-opacity"] = "0.90";
		bg.style["opacity"] = "0.9";
		bg.style["position"] = "fixed";
		if (BrowserDetect.browser == "IExplorer" && BrowserDetect.version < 7) 
			bg.style["position"] = "absolute";
		bg.style["z-index"] = "9998";
		bg.style["top"] = "0";
		bg.style["left"] = "0";
		bg.style["height"] = (screen.availHeight + 300) + "px";
		bg.style["width"] = (screen.availWidth + 300) + "px";
		
		var warning_html = "";
		if (this.allowCancel) 
			warning_html += '<a href="javascript:BrowserCompatible.cancel()" style="background:url('+this.images['cancel']+') no-repeat; height:15px; width:16px; position:absolute; right:5px; top:7px;" title="' + this.langTranslations[this.lang]['cancel'] + '"></a>';
		warning_html += '<div id="browsercheck_title" style="font-family:arial; font-size:24px; color:#000; margin:15px;">' + this.langTranslations[this.lang]['title'] + '</div>';
		warning_html += '<div id="browsercheck_description" style="font-family:arial; font-size:12px; color:#707070; margin:15px;">' + this.langTranslations[this.lang]['description'] + '</div>';
		warning_html += '<div id="browsercheck_recomendation" style="font-family:arial; font-size:12px; color:#707070; margin:15px;">' + this.langTranslations[this.lang]['recomendation'] + '</div>';
		for (var i = 0; i < this.offeredBrowsers.length; i++) {
			warning_html += '<a href="' + this.browsersList[this.offeredBrowsers[i]].link + '" title="' + this.langTranslations[this.lang][this.offeredBrowsers[i]] + '" style="height:60px; width:155px; display:block; float:left; margin:15px; text-decoration:none; background: url(' + this.browsersList[this.offeredBrowsers[i]].image + ') no-repeat;" target="_blank"> </a>';
			
		}
		if (this.allowToHide) 
			warning_html += '<div style="clear:both;font-family:arial; font-size:12px; color:#707070; padding:7px 15px;"><label><input type="checkbox" id="browsercheck_dontShowAgain" onclick="BrowserCompatible.dontShowAgain()" />' + this.langTranslations[this.lang]['dontShowAgain'] + '</label></div>';
		var warning = document.createElement("div");
		warning.id = "browsercheck_warning";
		warning.style["background"] = "url("+this.images['background']+") no-repeat";
		warning.style["padding"] = "2px";
		warning.style["width"] = "600px";
		warning.style["height"] = "400px";
		warning.style["position"] = "fixed";
		if (BrowserDetect.browser == "IExplorer" && BrowserDetect.version < 7) 
			warning.style["position"] = "absolute";
		warning.style["z-index"] = "9999";
		warning.style["top"] = ((window.innerHeight || document.body.parentNode.offsetHeight) - 400) / 2 + "px";
		warning.style["left"] = ((window.innerWidth || document.body.parentNode.offsetWidth) - 600) / 2 + "px";
		warning.innerHTML = warning_html;
		
		this.old_overflow_style = this.getStyle(document.body.parentNode, "overflow") || this.getStyle(document.body, "overflow");
		if (BrowserDetect.browser == "Opera" && this.old_overflow_style == "visible") 
			this.old_overflow_style = "auto";
		document.body.parentNode.style["overflow"] = "hidden";
		document.body.style["overflow"] = "hidden";
		
		document.body.appendChild(bg);
		document.body.appendChild(warning);
		
		if (document.addEventListener) {
			document.addEventListener('resize', this.warningPosition, false);
		}
		else {
			document.attachEvent('onresize', this.warningPosition);
		}
		
	},
	warningPosition: function(){
		var warning = document.getElementById('browsercheck_warning');
		warning.style["top"] = ((window.innerHeight || document.body.parentNode.offsetHeight) - 400) / 2 + "px";
		warning.style["left"] = ((window.innerWidth || document.body.parentNode.offsetWidth) - 600) / 2 + "px";
	},
	dontShowAgain: function(){
		var inpDontShowAgain = document.getElementById('browsercheck_dontShowAgain').checked;
		var dontShowAgain = this.readCookie('browsercheck_dontShowAgain');
		if (inpDontShowAgain) {
			this.createCookie('browsercheck_dontShowAgain', 'on', this.cookiesExpire);
		}
		else {
			this.eraseCookie('browsercheck_dontShowAgain');
		}
	},
	cancel: function(){
		var bg = document.getElementById('browsercheck_bg');
		var warning = document.getElementById('browsercheck_warning');
		bg.parentNode.removeChild(bg);
		warning.parentNode.removeChild(warning);
		document.body.parentNode.style["overflow"] = this.old_overflow_style;
		if (BrowserDetect.browser != "IExplorer") 
			document.body.style["overflow"] = this.old_overflow_style;
		document.onresize = this.resize_function;
	},
	old_overflow_style: "",
	resize_function: null,
	allowCancel: true,
	allowToHide: false,
	cookiesExpire: 1,
	images : {
		'background':"/images/bg.gif",
		'cancel':"/images/cancel.gif"
	},
	useBlackList: false,
	compatibleBrowsers: {
		"Opera": 9.25,
		"Firefox": 2,
		"IExplorer": 7,
		"Safari": 525.17,
		"Flock": 1.1,
		"Chrome": 1
	},		
	unCompatibleBrowsers: {
		"IExplorer": 6
	},
	offeredBrowsers: ["Chrome","Firefox", "Flock", "Safari", "IExplorer", "Opera"],
	browsersList: {
		"Chrome": {
			"image": "/images/chrome.gif",
			"link": "http://www.google.com/chrome/"
		},
		"Opera": {
			"image": "/images/opera.gif",
			"link": "http://www.opera.com/"
		},
		"Firefox": {
			"image": "/images/firefox.gif",
			"link": "http://th.www.mozilla.com/th/"
		},
		"IExplorer": {
			"image": "/images/iexplorer.gif",
			"link": "http://www.microsoft.com/windows/internet-explorer/default.aspx"
		},
		"Safari": {
			"image": "/images/safari.gif",
			"link": "http://www.apple.com/safari/"
		},
		"Flock": {
			"image": "/images/flock.gif",
			"link": "http://www.flock.com/"
		}
	},
	lang: "",
	langTranslations: {
		"th": {
			"title": "บราวเซอร์ ของคุณนั้นล้าสมัยแล้ว!",
			"description": "บราวเซอร์ ของคุณนั้นล้าสมัยแล้ว, ซึ่งหมายความว่ามันไม่ได้มีสิ่งที่จำเป็นสำหรับการทำงานกับเว็บไซต์หลายแห่งในปัจจุบัน เว็บไซต์สมัยใหม่ได้ถูกสร้างขึ้นเพื่อให้ความสะดวกและมีความสามารถที่ดีเพื่อคุณ และเพื่อให้มีการปรับปรุงที่ดีแก่เว็บไซต์ไปด้วยกัน บราวเซอร์ก็ต้องพัฒนาอยู่เสมอ ยิ่งไปกว่านั้น บราวเซอร์รุ่นเก่า ยังมีความบกพร่องมากมายเรื่องความปลอดภัย ซึ่งอาจจะทำให้เครื่องคอมพิวเตอร์ของคุณเป็นเป้าหมายของการโจรกรรมข้อมูลส่วนตัว ข้อมูลด้านการเงิน เป็นต้น ดังนั้น การอัพเกรดบราวเซอร์ของคุณนี้ ไม่เพียงแต่จะเพิ่มประสบการณ์การใช้งานที่เพิ่มขึ้น แต่มันยังเป็นการทำให้คุณมั่นใจยิ่งขึ้นว่า ข้อมูลส่วนตัวของคุณ ได้ถูกป้องกันไว้ในระดับหนึ่ง",
			"recomendation": "เราขอแนะนำให้คุณใช้เวอร์ชั่นล่าสุด จาก browser ต่อไปนี้ ซึ่งฟรีทั้งหมด ติดตั้งง่ายและจะไม่มีการเรียกเก็บเงินเพิ่มเติมแต่อย่างใด",
			"cancel": "ปิดหน้าต่างการเตือนนี้",
			"dontShowAgain": "ไม่ต้องแสดงข้อความนี้อีก",
			"Firefox": "ไฟร์ฟ็อก ได้รับการพิจารณาให้เป็นเว็บ บราวเซอร์ ตัวหนึ่งที่มีความสามารถปรับแต่ง รวมไปถึงการติดตั้งปลั๊กอิน และความสามารถอื่นๆเช่น การเปิดหน้าเว็บด้วยแท็บ",
			"Flock": "ฟล็อค เป็นบราวเซอร์ที่ถูกปรับแต่งสำหรับการใช้งาน Blog การอ่านข่าวผ่าน RSS และเว็บไซต์ Social Network ต่างๆ ซึ่งพัฒนาตัวโปรแกรมมาจาก ไฟร์ฟ็อก โดยคงไว้ซึ่งคุณประโยชน์หลักๆ",
			"IExplorer": "เป็นเว็บบราวเซอร์หลักที่มากับผู้ใช้ Windows",
			"Safari": "ซาฟารี ถูกพัฒนาโดยบริษัท Apple Inc. โดยเป็นบราวเซอร์ที่มากับผู้ใช้เครื่อง Macintosh",
			"Opera": "โอเปร่า เว็บบราวเซอร์และชุดโปรแกรมการใช้งานอินเตอร์เน็ต (Internet suite) พัฒนาโดย Opera Software สามารถทำงานที่เกี่ยวข้องกับอินเตอร์เน็ตได้ตั้งแต่เปิดเว็บไซต์ ส่งและรับอีเมล์ จัดการสมุดที่อยู่ IRC Chat, ดาวน์โหลดไฟล์ผ่าน Bit Torrent และอ่านข่าวผ่าน RSS Feed",
			"Chrome" : "กูเกิ้ลโครม มากับการออกแบบที่เรียบหรู แต่แฝงไว้ด้วยเทคโนโลยีชั้นสูง ทำให้การใช้งานเว็บเร็วขึ้น ปลอดภัยขึ้น และง่ายขึ้น"
		},
		
		"en": {
			"title": "Obsolete browser",
			"description": "Your browser is obsolete, which means it does not contain all of the necessary functions for the correct working of many current web sites. Modern web sites are created to be convenient and effective for you and, together with improvement of web sites themselves, browsers continue to improve. In addition, older browsers have many security glitches which can be maliciously abused to steal personal and financial information; therefore by upgrading your web browser you not only benefit from an enhanced web experience, but ensure that your private data is better protected.",
			"recomendation": "We recommend using the latest version of one of the following browsers. All are free, quick to install and won't cost you anything.",
			"cancel": "Close this warning",
			"dontShowAgain": "Don't show this warning again",
			"Firefox": "Firefox is considered by many to be the most advanced web browser available today it has an ability to adapt to individual usage through a large range of plug-ins and other popular features such as tabbed browsing that offers useful management of the browsing experience.",
			"Flock": "Flock is a web browser that is optimised for blogs, news aggregation, and social networking sites. It is built on Mozilla’s Firefox codebase and has many of the same benefits.",
			"IExplorer": "Internet Explorer has been the default web browser for Windows users for many years.",
			"Safari": "Safari is a web browser developed by Apple Inc. and included in Mac OS X and is also available for Windows.",
			"Opera": "Opera is a web browser and Internet suite developed by the Opera Software company. Opera handles common Internet-related tasks such as displaying web sites, sending and receiving e-mail messages, managing contacts, IRC online chatting, downloading files via BitTorrent, and reading web feeds.",
			"Chrome" : "Google Chrome is a browser that combines a minimal design with sophisticated technology to make the web faster, safer, and easier."
		}
	}
}
