// google-chrome-frame-for-microsoft-ie-with-cancel.js
// writed by Andrej A Antonov <polymorphm@gmail.com>
// version 2010-02-17 03:21
// licensed by LGPL version 3

(function() {
	var cancelCookieName = 'google_chrome_frame_for_microsoft_ie_canceled';
	var cancelExpireSeconds = 60 * 60 * 24 * 7 * 2;
	var installCookieName = 'google_chrome_frame_for_microsoft_ie_installed';
	var installExpireSeconds = 60 * 20;
	
	var rltrimString = function(str, space, right) {
		var work = str;
		
		for(;;) {
			if(work.length != 0) {
				var curr;
				
				if(right) {
					curr = work.charAt(work.length - 1);
				} else {
					curr = work.charAt(0);
				}
				
				if(curr == space) {
					if(right) {
						work = work.substr(0, work.length - 1);
					} else {
						work = work.substr(1, work.length - 1);
					}
					
					continue;
				}
			}
			
			break;
		}
		
		return work;
	};
	
	var rtrimString = function(str, space) {
		return rltrimString(str, space, true);
	};
	
	var ltrimString = function(str, space) {
		return rltrimString(str, space, false);
	};
	
	var trimString = function(str, space) {
		return ltrimString(rtrimString(str, space), space);
	};
	
	var toUTCString = function(obj) {
		if(obj.toUTCString != null) {
			return obj.toUTCString(); // <-- 'Microsoft IE' возможно не поддерживает это
		} else if(obj.toGMTString != null) {
			return obj.toGMTString(); // <-- специально для 'Microsoft IE'
		} else {
			throw "toUTCString() not implemented";
		}
	};
	
	var getCookie = function(cookieName) {
		if(document.cookie.length != 0) {
			var cookiePairs = document.cookie.split(';');
			for(var i = 0; i < cookiePairs.length; ++i) {
				var cookiePair = cookiePairs[i];
				
				if(cookiePair.length != 0) {
					var cookiePairSplit = cookiePair.split('=');
					
					if(cookiePairSplit.length == 2) {
						var cookiePairName = trimString(cookiePairSplit[0], ' ');
						var cookiePairValue = trimString(unescape(cookiePairSplit[1]), ' ');
						
						if(cookiePairName == cookieName) {
							return cookiePairValue;
						}
					}
				}
			}
		}
		
		return '';
	};
	
	var setCookie = function(cookieName, value, expireSeconds) {
		if(expireSeconds != null) {
			var expireTime = new Date();
			
			expireTime.setTime(expireTime.getTime() + expireSeconds * 1000);
			document.cookie = 
				cookieName + '=' + escape(value) +
				';path=/;expires=' + toUTCString(expireTime);
		} else {
			document.cookie = cookieName + '=' + escape(value);
		}
	};
	
	var getCookieAndRefresh = function(cookieName, expireSeconds) {
		var value = getCookie(cookieName);
		
		if(value.length != 0) {
			setCookie(cookieName, value, expireSeconds);
		}
		
		return value;
	};
	
	var addEventListener = function(element, type, listener, useCapture) {
	// потомучто 'Microsoft IE' не поддерживает addEventListener()
		if(element.addEventListener != null) {
			element.addEventListener(type, listener, useCapture);
		} else if(element.attachEvent != null) {
			element.attachEvent('on' + type, listener);
		} else {
			throw "addEventListener() not implemented";
		}
	};
	
	var detectMicrosoftIE = function() {
	    // детектирование 'Microsoft IE' по характерным несоответствиям стандартам
		
		if( // детектирование версий: '6.0', '7.0', '8.0'
			window.addEventListener == null && // <-- нет важной функции
			window.attachEvent != null // <-- но есть нестандартная альтернатива
		) {
			return true;
		}
		
		// <ЗДЕСЬ> в будущем возможно будет детектирование других версий
		
		return false;
	};
	
	var makeClearBothDiv = function() {
		var clearBothDiv = document.createElement('div');
		clearBothDiv.style.clear = 'both';
		
		return clearBothDiv;
	};
	
	var makeGoogleChromeFrameNotify = function() {
		var makeButton = function(value, listener) {
			var button = document.createElement('input');
			
			button.type = 'button';
			button.value = value;
			button.style.cssFloat = 'right'; // <-- 'Microsoft IE' не поддерживает это
			button.style.styleFloat = 'right'; // <-- специально для 'Microsoft IE'
			addEventListener(button, 'click', listener, false);
			
			return button;
		};
		
		var install = makeButton('Установить', function(event) {
			setCookie(installCookieName, 'true', installExpireSeconds);
			window.location.assign(
				'http://www.google.com/chromeframe/eula.html');
		});
		
		var googleChromeFrame = document.createElement('span');
		googleChromeFrame.style.fontWeight = 'bold';
		googleChromeFrame.appendChild(
			document.createTextNode(
				'Chrome Frame'
			)
		);
		
		var learnMore = document.createElement('span');
		learnMore.style.cursor = 'pointer';
		learnMore.style.color = 'rgb(0,0,255)';
		learnMore.appendChild(
			document.createTextNode(
				'Узнать больше'
			)
		);
		addEventListener(learnMore, 'click', function(event) {
			window.location.assign(
				'http://code.google.com/intl/ru/chrome/chromeframe/');
		}, false);
			
		
		var text = document.createElement('div');
		text.style.padding = '5px';
		text.appendChild(
			document.createTextNode(
				'У Вас не установлен компонент '
			)
		);
		text.appendChild(googleChromeFrame);
		text.appendChild(
			document.createTextNode(
				', необходимый для корректной работы Вашего браузера ('
			)
		);
		text.appendChild(learnMore);
		text.appendChild(
			document.createTextNode(')')
		);
		
		var notify = document.createElement('div');
		
		var cancel = makeButton('Отключить это уведомление', function(event) {
			setCookie(cancelCookieName, 'true', cancelExpireSeconds);
			notify.parentNode.removeChild(notify);
		});
		
		notify.style.padding = '3px';
		notify.style.font = '12px "DejaVu Sans", "Sans", sans-serif';
		notify.style.border = '1px rgb(245,245,181) outset';
		notify.style.background = 'rgb(245,245,181)';
		notify.style.color = 'rgb(0,0,0)';
		notify.appendChild(cancel);
		notify.appendChild(install);
		notify.appendChild(text);
		notify.appendChild(makeClearBothDiv());
		
		return notify;
	};
	
	var showNotify = function(notify) {
	    document.body.style.margin = '0';
		if(document.body.firstChild != null) {
			document.body.insertBefore(notify, document.body.firstChild);
		} else {
			document.body.appendChild(notify);
		}
	};
	
	var main = function(event) {
		if(detectMicrosoftIE()) {
			if(getCookieAndRefresh(cancelCookieName, cancelExpireSeconds) != 'true' &&
				getCookieAndRefresh(installCookieName, installExpireSeconds) != 'true')
			{
				var notify = makeGoogleChromeFrameNotify();
				
				showNotify(notify);
			}
		}
	};
	
	addEventListener(window, 'load', main, false);
})();


