var waitforresult = 0;
var popupStatus = 0;

//Popup code found from:
//http://yensdesign.com/2008/09/how-to-create-a-stunning-and-smooth-popup-using-jquery/

//loading popup with jQuery magic!
function loadPopup(){
	//loads popup only if it is disabled
	if(popupStatus==0){
		$("#backgroundPopup").css({"opacity": "0.7"});

	$('#backgroundPopup').hide().addClass("backgroundPopupUnhide")
		.css('opacity', "0.7")
		.click(function() { $(document).trigger('close.facebox') })
		.fadeIn("slow")

	$("#popupContact").fadeIn("slow");
	popupStatus = 1;
	}
}

//disabling popup with jQuery magic!
function disablePopup(){
	//disables popup only if it is enabled
	if(popupStatus==1){
		$('#backgroundPopup').fadeOut("slow", function(){
			$("#backgroundPopup").removeClass("backgroundPopupUnhide")
			$("#backgroundPopup").addClass("backgroundPopupHide")
		})

		$("#popupContact").fadeOut("slow");
		popupStatus = 0;
	}

}

var curwindowWidth;
var curwindowHeight;

//Based on document at http://www.howtocreate.co.uk/tutorials/javascript/browserwindow
function getRealSize() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
	curwindowWidth = myWidth;
	curwindowHeight = myHeight;
}

//centering popup
function centerPopup(){
	getRealSize();
	//request data for centering
	//var windowWidth = document.documentElement.clientWidth;
	//var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#popupContact").height();
	var popupWidth = $("#popupContact").width();
	//centering
	$("#popupContact").css({
		"position": "absolute",
		"top": curwindowHeight/2-popupHeight/2,
		"left": curwindowWidth/2-popupWidth/2
	});
	//only need force for IE6

	if (jQuery.browser.msie)
	{
		var bver = parseInt(jQuery.browser.version);
		//if(bver <= 6)
			$("#backgroundPopup").css({"height": curwindowHeight});
	}
}

$(document).ready(function(){

	//CLOSING POPUP
	//Click the x event!
	$("#popupContactClose").click(function(){
		disablePopup();
	});
	//Click out event!
	$("#backgroundPopup").click(function(){
		disablePopup();
	});
	//Press Escape event!
	$(document).keypress(function(e){
	if(e.keyCode==27 && popupStatus==1){
		disablePopup();
	}
	});

});

//Other stuff from this point on!

function get_result_list() {
	var p = {};
	p['text'] = $('#searchname').val();

	if(p['text'] == "")
	{
		$('#searchtext').html("&nbsp;");
		return;
	}
	waitforresult = 0;
	$('#searchtext').html("Searching...");
	//$('#content').load('/search/result',p,function(str)
	//{
	//	$('#searchtext').html("&nbsp;");
	//});

	$.ajax({
		type: "POST",
		url: "/search/result",
		dataType: "xml",
		data: p,
		success: parseXml,
		error: parseXml
	});
}

function ajaxFailed()
{
	$('#searchtext').html("Search could not be performed.");
}

function parseXml(xml)
{
	$('#popupArea').html("");

	centerPopup();
	loadPopup();

	var output = "";

	output += "<table width=600 align=center><tr><td colspan=3><center><img src='/images/layout/searchresults.png'></td></tr>";

	output += "<tr><td width=200 valign=top>";

	$(xml).find("monsters").each(function()
  	{
  		output += "<center><b>Monsters (" + $(this).attr("count") + ")</b></center><br />";
  		$(this).find("monster").each(function()
  		{
			output += "<a href='" + $(this).find("url").text() + "'>";
  			output += $(this).find("name").text();
  			output += "</a><br />";
  		});
  	});

	output += "</td><td width=200 valign=top>";

	$(xml).find("items").each(function()
  	{
		output += "<center><b>Items (" + $(this).attr("count") + ")</b></center><br />";
  		$(this).find("item").each(function()
  		{
			output += $(this).find("overlib").text();
			output += "<a href='";
			output += $(this).find("url").text();
			output += "'><img src='";
			output += $(this).find("image").text();
			output += "' border=0 align=absmiddle /></a> ";
			output += "<a href='";
			output += $(this).find("url").text();
			output += "'>";
			output += $(this).find("name").text();
			output += "</a></span><br />\n";

  			preloading($(this).find("collection").text());
  		});

	});

	output += "</td><td width=200 valign=top><center><b>Maps (0)</b></center></td></tr></table>";

	$("#popupArea").append(output);

	$('#searchtext').html("&nbsp;");
}

function search()
{
	if(waitforresult == 0)
	{
		waitforresult = setTimeout("get_result_list();",600);
		$('#searchtext').html("Waiting...");
	}
	else
	{
		clearTimeout(waitforresult);
		waitforresult = setTimeout("get_result_list();",600);
		$('#searchtext').html("Waiting...");
	}
}

var searchcleared = 0;
function clearsearch()
{
	if(searchcleared == 0)
	{
		$('#searchname').text("");
		searchcleared = 1;
	}
}

var preimages = new Array();
function preloading(image)
{
	var y = preimages.length;

	for (x=0; x<preloading.arguments.length; x++)
	{
		preimages[y+x] = new Image();
		preimages[y+x].src = preloading.arguments[x];
	}
}

