/*	
Bubble 0.82
Copyright (c) 2008 Sebastian Ohme
*/

var swfURL = "http://lp.trade-a-game.de/media/live/Bubble/bubble.swf";
var currentEAN;
var timeout = 800;
var timeoutId;
var bubbleWidth = 292;
var bubbleHeight = 192;
var swfWidth = 254;
var swfHeight = 154;
var bubble;
var bubbleId = "contextAdBubble";
var bubbleContent;
var bubbleContentId = "contextAdBubbleContent";
var classname = 'contextAd';

var window_onload;

if(typeof window.onload != 'function') 
{
	window.onload = initialize;
} 
else 
{
	window_onload = window.onload;
	window.onload = function()
	{
		window_onload();
		initialize();
	}
}

var cX = 0; var cY = 0; var rX = 0; var rY = 0;

function updateCursorPosition(e)
{
	cX = e.pageX; cY = e.pageY;
}

function updateCursorPositionDocAll()
{
	cX = event.clientX; cY = event.clientY;
}

if(document.all)
{
	document.onmousemove = updateCursorPositionDocAll;
}
else
{
	document.onmousemove = updateCursorPosition; 
}

function assignPosition(d)
{
	if(self.pageYOffset)
	{
		rX = self.pageXOffset;
		rY = self.pageYOffset;
	}
	else if(document.documentElement && document.documentElement.scrollTop)
	{
		rX = document.documentElement.scrollLeft;
		rY = document.documentElement.scrollTop;
	}
	else if(document.body)
	{
		rX = document.body.scrollLeft;
		rY = document.body.scrollTop;
	}
	if(document.all) 
	{
		cX += rX; 
		cY += rY;
	}
	
	d.style.left = (cX - 10) + "px";
	d.style.top = (cY + 10) + "px";
}

function getElementsByClass(searchClass, node, tag)
{
		var classElements = new Array();
		if (node == null) node = document;
		if (tag == null) tag = '*';
		var els = node.getElementsByTagName(tag);
		var elsLen = els.length;
		var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
		for (i = 0, j = 0; i < elsLen; i++) 
		{
			if (pattern.test(els[i].className)) 
			{
				classElements[j] = els[i];
				j++;
			}
		}
		return classElements;
}

function createDiv(id, html, append, width, height, left, top) 
{
	var div = document.createElement('div');
	div.setAttribute('id', id);

	if(width) div.style.width = width + "px";
	if(height) div.style.height = height + "px";
	if(left) div.style.left = left + "px";
	if(top) div.style.top = top + "px";
	
	if(html) div.innerHTML = html;
	
	if(append) document.body.appendChild(div);
	
	return div;
} 

function showBubble(ean)
{
	if(currentEAN != ean)
	{
		currentEAN = ean;

		var attributes = {};
		var flashvars = {};
		var params = {};
		
		attributes.styleclass = "contextAdSWF";
		
		flashvars.ean = ean;
		
		// If the following variables are undefined, this will cause an error!
		flashvars.advertiser = advertiser;
		flashvars.publisher = publisher;
		flashvars.textlink = textlink;
		
		params.play = "false";
		params.loop = "false";
		params.menu = "false";
		params.wmode = "transparent";
		params.allowscriptaccess = "always";
		
		swfobject.embedSWF(swfURL, bubbleContentId, swfWidth, swfHeight, "9.0.0", false, flashvars, params, attributes);
		
		bubbleContent = document.getElementById(bubbleContentId);
	}
	
	assignPosition(bubble); // Move bubble to the current cursor position. 
	//bubble.style.display = "block";
}

function hideBubble()
{
	//bubble.style.display = "none"; // This causes the SWF to reload every time. :(
	bubble.style.top = "-10000px"; 	 // So just move the div to the nirvana...
}

function initialize()
{
	bubble = createDiv(bubbleId, "", true, bubbleWidth, bubbleHeight);
	bubble.onmouseover = deactivateTimer;
	bubble.onmouseout = activateTimer;
	bubble.className = "contextAdBubble";
	bubble.appendChild(createDiv(bubbleContentId, "", false));
	hideBubble();

	//var tags = document.getElementsByClassName(classname); // Super fast, but not supported in Internet Explorer... :(
	var tags = getElementsByClass(classname);
	
	for(i = 0; i < tags.length; i++)
	{
		tags[i].onmouseout = activateTimer;
		tags[i].onmouseover = function()
		{
			deactivateTimer();
			showBubble(this.id);
		};
	}
}


function activateTimer()
{
	if(timeoutId) deactivateTimer();
	timeoutId = window.setTimeout("hideBubble()", timeout)
}

function deactivateTimer()
{
	window.clearTimeout(timeoutId);
}