Hint = {
	server: document.location.protocol + '//' + 'forums.linux-index.org/cgi-bin/hints.pl',
	mainWindow: null,
	title: '',
	content: '',
	isMoved: false,
	beginMove: false,
	offsetX: 0,
	offsetY: 0,
	default_refresh: null,
	more_refresh: null,
	top: '',
	width: '',
	right: '',
	opacity: parseFloat('0'),
	desired_opacity: parseFloat('0'),
	fadeIn_time: null,
	fadeIn_timeout: 0,
	fadeOut_time: null,
	fadeOut_timeout: 0,
	fade_step: 0.05,
	c_name: 'Li_',
	c_path: '/',
	c_domain: '.linux-index.org',
	c_expires: 'Wed, 11-Mar-2015 00:00:01 UTC',
	c_secure: '',

	getWindowStyle: function()
	{
		var windowStyle = 'position: fixed;' +
					( (Hint.top) ? "top:" + Hint.top + ";" : "") + 
					( (Hint.right) ? "right:" + Hint.right + ";" : "") + 
					( (Hint.bottom) ? "bottom:" + Hint.bottom  + ";" : "") + 
					( (Hint.left) ? "left:" + Hint.left + ";" : "") + 
					( (Hint.width) ? "width:" + Hint.width + ";" : "") + 
					( (Hint.opacity) ? "opacity:"+ Hint.opacity.toString() + ";" + "-moz-opacity: " + Hint.opacity.toString() + ";" : "");
		return windowStyle;
	},

	getTitleStyle: function()
	{
		var titleStyle = 'background-image: url("http://www.linux-index.org/images/rightbar_title_bg.gif");';
		titleStyle += 'height: 16px;';
		titleStyle += 'padding: 2px 0px 0px 10px;';
		titleStyle += 'background-repeat: repeat-x;';
		titleStyle += 'font-family: Arial, Helvetica, sans-serif;';
		titleStyle += 'font-size: 11px;';
		titleStyle += 'color: #000000;';
		titleStyle += 'font-weight: bold;';
		titleStyle += 'text-align: left;';
		titleStyle += 'border-left: 1px solid #4b6868;';
		titleStyle += 'cursor: move;';
		return titleStyle;
	},

	getContentStyle: function()
	{
		var contentStyle = 'border: 1px dashed #4b6868;';
		contentStyle += 'margin-top: 1px;';
		contentStyle += 'background-color: #fff;';
		contentStyle += 'font-family: Arial, Helvetica, sans-serif;';
		contentStyle += 'font-size: 11px;';
		contentStyle += 'text-align: justify;';
		contentStyle += 'padding: 2px 13px 2px 10px;';
		contentStyle += 'cursor: default;';
		return contentStyle;
	},

	onMouseDown: function(event)
	{
		Hint.beginMove = true;

		Hint.offsetY = event.clientY - parseInt(Hint.mainWindow.style.top);
		Hint.offsetX = event.clientX - parseInt(Hint.mainWindow.style.left);

		document.addEventListener("mouseup", Hint.onMouseUp, true);
		document.addEventListener("mousemove", Hint.onMouseMove, true);
	},

	onMouseUp: function()
	{
		Hint.beginMove = false;
		document.removeEventListener("mouseup", Hint.onMouseUp, true);
		document.removeEventListener("mousemove", Hint.onMouseMove, true);
	},

	onMouseMove: function(event)
	{
		if(Hint.beginMove)
		{
			var oldX = parseInt(Hint.mainWindow.style.left);
			var oldY = parseInt(Hint.mainWindow.style.top);

			Hint.mainWindow.style.top = eval(event.clientY - Hint.offsetY).toString() + 'px';
			Hint.mainWindow.style.left = eval(event.clientX - Hint.offsetX).toString() + 'px';

			if(((oldX - parseInt(Hint.mainWindow.style.left)) != 0) || (oldY - parseInt(Hint.mainWindow.style.top)) != 0)
				Hint.isMoved = true;

	            window.getSelection().removeAllRanges();
		}

	},

	createWindow: function()
	{

		Hint.mainWindow = document.createElement('div');
		var mainTitle = document.createElement('div');
		var mainContent = document.createElement('div');

		Hint.mainWindow.setAttribute('style', Hint.getWindowStyle());
		mainTitle.setAttribute('style', Hint.getTitleStyle());
		mainContent.setAttribute('style', Hint.getContentStyle());
		mainContent.setAttribute('id', 'tip');

		mainTitle.setAttribute('onMouseDown', 'Hint.onMouseDown(event);');

		mainTitle.innerHTML = Hint.title;
		mainContent.innerHTML = Hint.content;

		var map = document.createElement('map');
		map.setAttribute('name', 'more');
		map.setAttribute('id', 'more');
		var area = document.createElement('area');
		area.setAttribute('shape', 'rect');
		area.setAttribute('coords', '8,1,58,17');
		area.setAttribute('href', 'javascript:Hint.show();');
		area.setAttribute('alt', 'Покажи ми още');
		area.setAttribute('title', 'Покажи ми още');
		document.body.appendChild(map);
		map.appendChild(area);

		var image = document.createElement('img');
		image.setAttribute('src', 'http://www.linux-index.org/images/rightbar_title_more.gif');
		image.setAttribute('style', 'border: none; position: absolute; right: 0px; top: 0px;');
		image.setAttribute('title', 'Покажи ми още');
		image.setAttribute('usemap', '#more');

		Hint.mainWindow.appendChild(mainTitle);
		mainTitle.appendChild(image);

		var image = document.createElement('img');
		image.setAttribute('src', 'http://www.linux-index.org/images/true.gif');
		image.setAttribute('style', 'border: none;');

		var a = document.createElement('a');
		a.setAttribute('href', 'javascript:Hint.hide();');
		a.setAttribute('style', 'position: absolute; right: 0px; top: 19px;');
		a.setAttribute('title', 'Затвори');

		Hint.mainWindow.appendChild(a);
		a.appendChild(image);
		
		Hint.mainWindow.appendChild(mainContent);
		document.body.appendChild(Hint.mainWindow);
//		document.captureEvents(Event.MOUSEMOVE);
		document.addEventListener("mousemove", Hint.onMouseMove, true);
		if (Hint.mainWindow.clientHeight < parseInt(Hint.mainWindow.style.top))
			Hint.mainWindow.style.top = eval(parseInt(Hint.mainWindow.style.top) - Hint.mainWindow.clientHeight).toString() + 'px';
		Hint.fadeIn();
	},

	fadeIn: function()
	{
		Hint.opacity += Hint.fade_step;
		Hint.opacity = Math.round(Hint.opacity*100)/100;
		Hint.mainWindow.style.opacity = Hint.opacity,toString();
		Hint.fadeIn_time = setTimeout('Hint.fadeIn()', Hint.fadeIn_timeout);
		if (Hint.mainWindow.style.opacity == Hint.desired_opacity.toString())
			clearTimeout(Hint.fadeIn_time);
	},

	fadeOut: function()
	{
		Hint.opacity -= Hint.fade_step;
		Hint.opacity = Math.round(Hint.opacity*100)/100;
		Hint.mainWindow.style.opacity = Hint.opacity.toString();
		Hint.fadeOut_time = setTimeout('Hint.fadeOut()', Hint.fadeOut_timeout);
		if (Hint.mainWindow.style.opacity == '0')
			clearTimeout(Hint.fadeIn_time);
	},

	parceXML: function(xml)
	{
		var tip = xml.getElementsByTagName('tip');
		if(tip.length)
		{
			Hint.title = tip[0].getAttribute('title');
			Hint.content = tip[0].childNodes[0].nodeValue;
			if (Hint.mainWindow)
			{
				document.getElementById('tip').innerHTML = tip[0].childNodes[0].nodeValue;
				clearTimeout(Hint.default_refresh);
				clearTimeout(Hint.more_refresh);
				Hint.more_refresh = setTimeout('Hint.show()', 30000);
			}
			else
			{
				Hint.createWindow();
				Hint.default_refresh = setTimeout('Hint.show()', 30000);
			}
		}
	},

	getHint: function()
	{
		if(window.XMLHttpRequest)
		{
			var httpRequest = new XMLHttpRequest();
			httpRequest.onreadystatechange = function()
			{
				if(httpRequest.readyState == 4 && httpRequest.status == 200)
				{
					Hint.parceXML(httpRequest.responseXML);
				}
			}

			httpRequest.open('get', Hint.server, true);
			httpRequest.send(null);
		}
	},

	hide: function()
	{
		Hint.fadeOut();
//		document.body.removeChild(Hint.mainWindow);
		clearTimeout(Hint.default_refresh);
		clearTimeout(Hint.more_refresh);

		var str = Hint.getCookie(Hint.c_name + 'hints');
		var k = eval(parseInt(str.split(/x/)[0])+1).toString() + 'x';
		Hint.setCookie(Hint.c_name + 'hints', str.replace(/^(\d+)x/, k), Hint.c_expires, Hint.c_path, Hint.c_domain, Hint.c_secure);

//		Hint = null;
	},

	show: function()
	{
		if (document.getElementById('tip')) 
		{
			document.getElementById('tip').innerHTML = '<img src="http://www.linux-index.org/images/loading.gif" style="border: none; margin: 3px;" alt="Зареждане..." />';
		}
		else
		{
			Hint.width = '280px';
			Hint.opacity = parseFloat('0');
			Hint.desired_opacity = parseFloat('0.85');
			var padding = '10px';
			var scrollWidth = '15px';
			
			var tr = [function() {return padding},
					 function() {return ''},
					 function() {return ''},
					 function() {return eval(window.innerWidth - parseInt(padding) - parseInt(Hint.width) - parseInt(scrollWidth)).toString() + 'px'}];

			var br = [function() {return eval(window.innerHeight - parseInt(padding)).toString() + 'px'},
					 function() {return ''},
					 function() {return ''},
					 function() {return eval(window.innerWidth - parseInt(padding) - parseInt(Hint.width) - parseInt(scrollWidth)).toString() + 'px'}];

			var bl = [function() {return eval(window.innerHeight - parseInt(padding)).toString() + 'px'},
					 function() {return ''},
					 function() {return ''},
					 function() {return padding}];

			var position = [tr, br, bl];
			var num = Math.floor(Math.random()*3);
			Hint.top = position[num][0]();
			Hint.right = position[num][1]();
			Hint.bottom = position[num][2]();
			Hint.left = position[num][3]();

			Hint.fadeIn_timeout = parseInt((Hint.fade_step*2000)/Hint.desired_opacity);
			Hint.fadeOut_timeout = parseInt((Hint.fade_step*1000)/Hint.desired_opacity);
			
			if (Hint.checkCookie()) return;
		}
		Hint.getHint();
	},

	setCookie: function(name,value,expires,path,domain,secure) 
	{
	    var cookieString = name + "=" +escape(value) + 
      	 ( (expires) ? ";expires=" + expires : "") + 
	       ( (path) ? ";path=" + path : "") + 
	       ( (domain) ? ";domain=" + domain : "") + 
	       ( (secure) ? ";secure" : ""); 
	    document.cookie = cookieString; 
	},

	getCookie: function(name) 
	{
	   var start = document.cookie.indexOf(name+"="); 
	   var len = start+name.length+1; 
	   if ((!start) && (name != document.cookie.substring(0,name.length))) return ""; 
	   if (start == -1) return ""; 
	   var end = document.cookie.indexOf(";",len); 
	   if (end == -1) end = document.cookie.length; 
	   return unescape(document.cookie.substring(len,end)); 
	},

	checkCookie: function()
	{
		var cookie = Hint.getCookie(Hint.c_name + 'hints');
		var k = 1;
		var l = 1;
		var m = 0;
		var str = k.toString() + 'x' + l.toString() + 'x' + m.toString();


		if (!(cookie) || !(cookie.match(/x/)))
			Hint.setCookie(Hint.c_name + 'hints', str, Hint.c_expires, Hint.c_path, Hint.c_domain, Hint.c_secure);
		else
		{
			values = cookie.split(/x/);
			k = parseInt(values[0]);
			l = parseInt(values[1]);
			m = parseInt(values[2]);

			if (k >= 4)	m += 1;
			if (parseInt(m / k) >= 3)
			{
				k -= 1;
				m = 0;
			}
			l += 1;

			str = k.toString() + 'x' + l.toString() + 'x' + m.toString();

			Hint.setCookie(Hint.c_name + 'hints', str, Hint.c_expires, Hint.c_path, Hint.c_domain, Hint.c_secure);
		}

		if (k == 1) return false;
		if (k == 5) return true;
		if (l % k)
			return true;	
		else
			return false;
	}
}

