var AD_ROTATE_MIN = undefined===window.HUGO_ALFA ? 25 : 2; // sec
var AD_ROTATE_MAX = undefined===window.HUGO_ALFA ? 35 : 2; // sec
var AD_ROTATE_STOP = 1;
var TIMER_FREQ = 10; // Hz

var AwHttp = {};
var AwPost;
var AwPostNode = null;
var AwPostLife = 0;
var AwRotateAd = (AD_ROTATE_MIN + AD_ROTATE_MAX) / 2 * TIMER_FREQ;
var AwAdsShown = 1;

var AwIsNS4 = document.layers;
var AwIsIE4 = document.all;
var AwIsNN6 = document.getElementById && !document.all;


/****************************************************************************
* awHttpNew()
****************************************************************************/

function awHttpNew()
{
	var req;

/*@cc_on @*/
/*@if (@_jscript_version >= 5)
	try 
	{
		req = new ActiveXObject("Msxml2.XMLHTTP");
	} 
	catch (e) 
	{
		try
		{
			req = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch (e)
		{
		}
	}
@end @*/

	if(!req && typeof XMLHttpRequest!='undefined')
		req = new XMLHttpRequest();

	if(!req)
	{
		// error: cannot create the request
		return false;
	}

	AwHttp.Request = req;

	req.onreadystatechange = function ()
	{
		var req = AwHttp.Request;

		// only if req shows "loaded"
		if (req.readyState == 4)
		{
			// only if "OK"
			if (req.status == 200)
			{
				try
				{
					AwHttp.Resp = req.responseText;
				}
				catch(e)
				{
					AwHttp.Resp = e;
				}

				AwHttp.OnDone(AwHttp);
			} 
			else
			{
				AwHttp.Resp = "There was a problem retrieving the HTTP data:\n" + req.statusText;
			}
		}
	}

	return req;
}


/****************************************************************************
* awHttpSend()
****************************************************************************/

function awHttpSend (fnc, url, ondone)
{
	var a;

	// split URL into host and URI part
	if(url.substr(0,7) == "http://")
		url = url.substr(7);

	a = url.match(/^([^\/]+)(\/.*)$/);
	if(!a || a.length<3)
		return false;

	AwHttp.Host = a[1];
	AwHttp.Uri = a[2];

	var req = awHttpNew();
	if(!req)
	{
		return false;
	}

	req.open(fnc, "http://" + AwHttp.Host + AwHttp.Uri);
//	req.setrequestheader("Man", "POST " + SVC + "/ListRsrc HTTP/1.1")
	req.setRequestHeader("Host", AwHttp.Host);
	req.setRequestHeader("Content-Type", "text/plain");
	AwHttp.OnDone = ondone;
	req.send("");
}


/****************************************************************************
* awCalcOffs()
****************************************************************************/

function awCalcOffs (node)
{
	var x=0; y=0;

	do
	{
		x += node.offsetLeft;
		y += node.offsetTop;
		node = node.offsetParent;
	}
	while(node && typeof node.offsetLeft != "undefined");

	return {left:x,top:y};
}


/****************************************************************************
* awParentByTag()
****************************************************************************/

function awParentByTag (node,tag)
{
	do
	{
		node = node.parentNode;
	}
	while(node && node.tagName != tag);

	return node;
}


/****************************************************************************
* awHideNode()
****************************************************************************/

function awHideNode (id)
{
	try
	{
		if (AwIsNS4)
			document[id].visibility = "hide"
		else if (AwIsIE4)
			document.all[id].style.visibility = "hidden"
		else if (AwIsNN6)
			document.getElementById(id).style.visibility = "hidden"
	}
	catch(e)
	{
	}
}


/****************************************************************************
* awShowNode()
****************************************************************************/

function awShowNode (id)
{
	try
	{
		if (AwIsNS4)
			document[id].visibility = "show"
		else if (AwIsIE4)
			document.all[id].style.visibility = "visible";
		else if (AwIsNN6)
			document.getElementById(id).style.visibility = "visible";
	}
	catch(e)
	{
	}
}


/****************************************************************************
* awTrimText()
****************************************************************************/

String.prototype.trim = function()
{
	return this.replace(/^\s+|^&nbsp;+|\s+$|&nbsp;+$/g, '');
}

/****************************************************************************
* awGetPostText()
****************************************************************************/

function awGetPostText (text)
{
	var row = awParentByTag(AwPostNode,"TR");

	if(!row)
	{
		AwPost.style.visibility = "hidden";
		return;
	}

	var out = "<table width='100%' cellspacing='4px' cellpadding='4px'>";
	out += "<tr><td><b>Predmet</b></td><td style='width:100%'>" + AwPostNode.innerHTML.trim() + "</td></tr>";
	out += "<tr><td><b>Mesto</b></td><td>" + row.cells[1].innerHTML.trim() + "</td></tr>";
	out += "<tr><td colspan='2'>" + text + "</td></tr>";
	out += "</table>";

	return out;
}


/****************************************************************************
* awShowPost()
****************************************************************************/

function awShowPost (text)
{
	var x = '';

//	x += "<div style='width:1px; height:1px; border:0; padding:0; background:white; overflow:hidden'>";
//	x += "<img src='http://ad2.billboard.sk/please/showit/"+SITE_BB_CODE+"/1/1/1/?typkodu=img&dummy="+Math.random()+"' width='468' height='60' style='border:0px none' alt='reklama' />";
//	x += "</div>";

	AwPost.innerHTML = awGetPostText(text) + x;
	awShowNode("awPopup");
}


/****************************************************************************
* awAttachEvent()
* This is OnMouseOver event for all <A> elements with 'href='viewtopic.php?'.
****************************************************************************/

function awAttachEvent (node,eventName,eventCode,flag)
{
	if(node.addEventListener)
	{
		node.addEventListener(eventName, eventCode, flag);
	}
	else if(node.attachEvent)
	{
		node.attachEvent("on"+eventName, eventCode);
	}
	else
	{
		node["on"+eventName] = eventCode;
	}
}


/****************************************************************************
* awPostMouseOver()
* This is OnMouseOver event for all <A> elements with 'href='viewtopic.php?'.
****************************************************************************/

function awPostMouseOver (ev)
{
	var ofs;

	AwPostNode = ev.target ? ev.target : ev.srcElement;

	if(!AwPost)
	{
		AwPost = document.createElement("div");
		AwPost.id = "awPopup";
		AwPost.style.display = "inline";
		AwPost.style.position = "absolute";
		AwPost.style.backgroundColor = "#ffd";
		AwPost.style.border = "solid #ccc 1px";
		AwPost.style.font = "normal 12px tahoma,arial,helvetica";
		AwPost.style.padding = "8px";
		AwPost.style.width = "360px";
		AwPost.style.minHeight = "40px";
		awAttachEvent(AwPost, "mouseover", function() { AwPostLife=300; }, false);
		awAttachEvent(AwPost, "mouseout", function() { AwPostLife=10; }, false);
		document.body.appendChild(AwPost);
	}


	AwPostLife = 300;
	if(AwPostNode.tagName == "B")
		AwPostNode = AwPostNode.parentNode;

	ofs = awCalcOffs(AwPostNode);
	AwPost.style.left = ""+(ofs.left+40)+"px";
	AwPost.style.top = ""+(ofs.top+30)+"px";

/*	if(AwPostNode.PostText)
		awShowPost(AwPostNode.PostText);
	else
	{*/
		var url = document.URL.replace(/\/\w+\.php\??.*|\/\??$/, "/getpost.php?fnc=post1&max=400&tid=" + AwPostNode.topicId);
		awShowPost("loading ...");
		awHttpSend("GET", url, function(h) { eval("var p="+h.Resp); /*AwPostNode.PostText=h.Resp;*/ awShowPost(p.msg); });
//	}
}


/****************************************************************************
* awTimer()
* This routine is executed with frequency TIMER_FREQ.
****************************************************************************/

function awTimer ()
{
	// limit number of rotated ads on one page
	if(AwAdsShown < AD_ROTATE_STOP && --AwRotateAd <= 1)
	{
		var node = document.getElementById("mainBanner");
		node.innerHTML = "";
		if(AwRotateAd <= 0)
		{
			node.innerHTML = "<a href='http://ad2.billboard.sk/please/redirect/"+SITE_BB_CODE+"/1/1/1/' target='_blank'><img src='http://ad2.billboard.sk/please/showit/"+SITE_BB_CODE+"/1/1/1/?typkodu=img&dummy="+Math.random()+"' width='468' height='60' style='border:0px none' alt='reklama' /></a>";
			AwRotateAd = (Math.round(Math.random() * (AD_ROTATE_MIN+AD_ROTATE_MAX)/2) + AD_ROTATE_MIN) * TIMER_FREQ;
			AwAdsShown ++;
		}
	}

	if(AwPostLife <= 0)
		return;

	if(--AwPostLife <= 0)
	{
		awHideNode("awPopup");
		AwPostLife = 0;
	}
}


/****************************************************************************
* awInstallPreview()
* This routine walks all the nodes and installs preview code.
****************************************************************************/

function awInstallPreview (node) 
{
	var count = 0;
	var anchors = document.getElementsByTagName("a")

	for(var i=anchors.length; i--; )
	{
		var anchor = anchors[i];

		if(anchor.href.indexOf('viewtopic.php?id=') > 0)
		{
			for(var parent=anchor.parentNode; parent; parent=parent.parentNode)
			{
				if(parent == node)
				{
					anchor.topicId = anchor.href.match(/\?id=(\d+)/)[1];
					awAttachEvent(anchor, "mouseover", awPostMouseOver, false);
					awAttachEvent(anchor, "mouseout", function() { AwPostLife=10; }, false);
					count ++;
					break;
				}
			}
		}
	}

	setInterval("awTimer()", 1000/TIMER_FREQ);
}


/****************************************************************************
* main code
****************************************************************************/

awInstallPreview(document.getElementById("vf"));
