/*function getFlash(movie) 
{
	if(window.document[movie])
		return window.document[movie]; 
	if(navigator.appName.indexOf("Microsoft Internet") ==- 1) 
	{
		if(document.embeds && document.embeds[movie])
			return document.embeds[movie]; 
	}
	else 
	{
		return document.getElementById(movie); 
	}
}*/
function getFlash(movie) 
{ 
	if(navigator.appName.indexOf("Microsoft Internet") ==- 1) 
	{
		if(window.document[movie])
			return window.document[movie];
		if(document.embeds && document.embeds[movie])
			return document.embeds[movie]; 
	}
	else 
	{
		if(window.document[movie+'_o'])
			return window.document[movie+'_o'];
		return document.getElementById(movie+'_o'); 
	}
}

var maxWidth = 763;
var maxHeight = 579;
var wFact = maxWidth/100;
var hFact = maxHeight/100;

var skip_wait = false;
function skipWait()
{
	skip_wait = true;
}

function initGame(url, delay)
{
	$('#flash_container').flash({ 
		src: url,
		width: 1,
		height: 1,
		id: 'flash_obj',
		name: 'flash_obj'
	},
	{
		version: 8
	});
	skip_wait = false;
	
	var timecounter = 0;
	
	$('#percent').everyTime(100, function(){
		var percent = 0;
		var flash_percent = getFlash('flash_obj').PercentLoaded();
		if (delay == 0)
			percent = flash_percent;
		else
			percent =Math.min(flash_percent, Math. round(((timecounter/10)/delay)*100));
			
		$('#percent').text(percent);
		
		if (percent >= 100 || skip_wait)
		{
			$('#percent').stopTime();
			$('#ad_container').css('display', 'none');
			$('#game_container').css('overflow', '');
			$('#game_container').css('width', '');
			$('#game_container').css('height', '');
			getFlash('flash_obj').width = (maxWidth);
			getFlash('flash_obj').height = (maxHeight);
		}
		else
			timecounter++;
	});
	
	$('#slider').bind('slide', function(event, ui) {
		if (getFlash('flash_obj').PercentLoaded()<100)
			return; 
		getFlash('flash_obj').width = (ui.value*wFact);
		getFlash('flash_obj').height = (ui.value*hFact);
	});
}

function isIE6()
{
	return ($('.ie6').length > 0);
}
		
/* 	creates a popup following the mouse while it is hovering over 'elem'
	'elem' can point to one or multiple elements (e.g. .my_class) 
	includes a fix for ie6 selects, isIE6 function required! */
jQuery.fn.trackingPopup = function (popupElement)
{
	var popup = null;
	var trackedElement = null;

	// create the popup element if it doesn't exist already
	if ($('#'+popupElement).length == 0)
	{
		$("body").append("<div id='"+popupElement+"'><div id='"+popupElement+"_inner' class='center_inner'><div><p id='"+popupElement+"_text'>a</p></div></div></div>");
		if (isIE6())
		{
			$("body").append('<iframe id="ie6iframe" src="javascript:\'\';" marginwidth="0" marginheight="0" align="bottom" scrolling="no" frameborder="0" style="position:absolute; display:none; filter:alpha(opacity=0);" ></iframe>');
		}
		$('#'+popupElement).css({cursor: 'pointer', 'z-index': '1000'})/*.click(
			function()
			{
				if (trackedElement.attr("href"))
					window.location = trackedElement.attr("href");
			}
		)*/;
	}
	
	popup = $('#'+popupElement);
	
	this.mouseenter(
		function(e)
		{
			// keep a reference to the tracked element, we need it for bounds checking and possibly linking
			if (isIE6())
			{
				$('#ie6iframe').css({
					width: popup.width(),
					height: popup.height(),
					top: e.pageY + "px",
					left: (e.pageX-20) + "px"
				});
				$('#ie6iframe').show();
			}
			
			trackedElement = $(this);
			popup.css({
				top: e.pageY + "px",
				left: (e.pageX-20) + "px"
			});
			if (trackedElement.attr("alt"))
				popup.find('#'+popupElement+'_text').html(trackedElement.attr("alt"));
			else
				popup.find('#'+popupElement+'_text').html(trackedElement.attr("title"));
			popup.fadeIn("fast");
			
			popup.click(function () {
				if (trackedElement.attr('href'))
					document.location = trackedElement.attr('href');
				else if (trackedElement.parent().attr('href'))
					document.location = trackedElement.parent().attr('href');
			});
			
			$(document).mousemove(
				function mouseMovedHandler(e) 
				{
					var pos = trackedElement.offset();
					//var relativeX = e.pageX - trackedElement[0].offsetLeft;
    				//var relativeY = e.pageY - trackedElement[0].offsetTop;
			
					// if the mouse is outside of the elements bounds we can hide it and remove the pointer, otherwise update the popup position
					if (e.pageX < pos.left || e.pageY < pos.top || e.pageX > pos.left + trackedElement.width() || e.pageY > pos.top + trackedElement.height())
					//if (relativeX < 0 || relativeY < 0 || relativeX > trackedElement.width() || relativeY > trackedElement.height())
					{
						if (isIE6())
							$('#ie6iframe').hide();
						popup.stop(true, true);
						popup.hide();
						$(document).unbind('mousemove', arguments.callee);
						$('#'+popupElement).unbind('click');
					}
					else	
					{
						if (isIE6())
							$('#ie6iframe').css({
								top: e.pageY + "px",
								left: (e.pageX-20) + "px"
							});
						popup.css({
							top: e.pageY + "px",
							left: (e.pageX-20) + "px"
						});
					}
				}
			);
		}
	);
}

jQuery.preloadImages = function()
{
  for(var i = 0; i<arguments.length; i++)
  {
    jQuery("<img>").attr("src", arguments[i]);
  }
}

$(document).ready(function () {
    //$("p").text("The DOM is now loaded and can be manipulated.");
    $('.game_image').trackingPopup('game_desc_popup');
    
    $("body").append('<div id="quickSearchResults" style="display:none"></div>');
	searchbox = $('#searchbox');
	searchresults = $("#quickSearchResults");
	
	if (searchbox.length > 0)
	{
		var numres = 0;
		var lasttext = '';
		var lastkind = '';
		var quickSearchTimer = null;
		
		function searchfunc(data) {
			searchresults.empty();
			for (i=0; i<data.length; i++)
			{
				searchresults.append('<div onclick="location.href=\''+data[i].url+'\'" class="quickSearchEntry"><a href="'+data[i].url+'">'+data[i].name+'</a></div>');
			}
			numres = data.length;
			if (data.length > 0)
				searchresults.show();
			else
				searchresults.hide();
		}
		
		var divCss = {
				"left": searchbox.offset().left,
				"padding": 2,
				"position": "absolute",
				"top": searchbox.offset().top + searchbox.height() + 1,
				"border": "1px solid #7f9db9",
				"width": searchbox.width() - 3,
				"background": "white",
				"max-width": searchbox.width() - 3
				};
		searchresults.css(divCss);
		searchbox.attr("autocomplete","off"); 
		searchbox.keyup(function(event) {
			if (searchbox.val().length > 2)
			{
				//$("#quickSearchResults").fadeOut(200, initSearch);
				//searchresults.show();
				if (quickSearchTimer != null) // cancel the delayed event
					clearTimeout(quickSearchTimer);
				quickSearchTimer = setTimeout(function() { // delay the searching
					quickSearchTimer = null;
					//$("#quickSearchResults").fadeOut(200, initSearch);
					
					if (lasttext != searchbox.val() )
					{
						lasttext = searchbox.val();
						$.getJSON('/de/ajax/quicksearch/' + $.URLEncode(searchbox.val()), searchfunc);
					}
					
				} , 300);
			}
			else if (searchbox.val().length <= 2)
				searchresults.hide();
		});
		searchbox.blur(function(event) {
			setTimeout(function(){
			searchresults.hide();
			}, 200);
		});
		var searchCleared = false;
		searchbox.focus(function(event) {
			if (!searchCleared)
			{
				searchbox.css("color", "black");
				searchbox.val("");
				searchCleared = true;
			}
			if (searchbox.val().length > 2)
			{
				//$("#quickSearchResults").fadeOut(200, initSearch);
				if (lasttext != searchbox.val() )
				{
					lasttext = searchbox.val();
						$.getJSON('/de/ajax/quicksearch/' + $.URLEncode(searchbox.val()), searchfunc);
				}
				else if (numres > 0)
				{
					searchresults.show();
				}
			}
		});
    }
});
