/*
 * BASED ON:
 * Thickbox 3.1 - One Box To Rule Them All.
 * By Cody Lindley (http://www.codylindley.com)
 * Copyright (c) 2007 cody lindley
 * Licensed under the MIT License: http://www.opensource.org/licenses/mit-license.php
 *
 * ########## AJAX POPUP BOX ##########
 *
 *
*/

var aPopUpLoaderPath = WEB_ROOT + "/img/loadingAnimation.gif";

/*!!!!!!!!!!!!!!!!! edit below this line at your own risk !!!!!!!!!!!!!!!!!!!!!!!*/

//on page load call tb_init
$(document).ready(function(){
	imgLoader = new Image();// preload image
	imgLoader.src = aPopUpLoaderPath;
});

// LEGACY!

function thickBox(id, width, height, html_str, modal)
{
	aPopUp(id, width, height, html_str, modal);
}//thickBox

function thickBoxClose(id)
{
	aPopUpClose(id);
}//thickBoxClose

function aPopUp(id, width, height, html_str)
{
	var tbWinID = id;

	try
	{
		var winOverlayID = null;

		if(document.getElementById("PP_overlay") === null)
		{
			$("body").append("<div id='PP_overlay'></div>");
			$(document).data('windows', new Array());
		}//if !overlay
		else
		{
			winOverlayID =  tbWinID + '_overlay';
			$("body").append("<div id='" + winOverlayID + "' class='PP_secOverlay'></div>");
		}//else

		$("body").append("<div id='" + tbWinID + "' class='PP_window'></div>");

		if(aPopUpDetectMacXFF())$("#PP_overlay").addClass("PP_overlayMacFFBGHack");//use png overlay so hide flash
			else $("#PP_overlay").addClass("PP_overlayBG");//use background and opacity

		$("body").append("<div id='PP_load'><img src='" + imgLoader.src + "' /></div>");//add loader to the page
		$('#PP_load').show();//show loader

		var win_z = Number($("#PP_overlay").css('z-index')) + ($(document).data('windows').length * 2);

		$('#' + tbWinID).css("z-index", win_z);
		if(winOverlayID != null)
		{
			$('#' + winOverlayID).css("z-index", (win_z - 1));
			$('#' + winOverlayID).click(function(ev){aPopUpClose(tbWinID)});
		}//if window overlay (= second window)

		PP_WIDTH = (width * 1) || 630; //defaults to 630 if no paramaters were added to URL
		PP_HEIGHT = (height * 1) || 440; //defaults to 440 if no paramaters were added to URL

		ajaxContentW = PP_WIDTH;
		ajaxContentH = PP_HEIGHT;

		var contentID = tbWinID + '_Content';

		if($('#' + tbWinID).css("display") != "block")
		{
			$("#PP_overlay").unbind();
			$('#' + tbWinID).append("<div id='" + contentID + "' class='PP_ajaxContent' style='width:" + ajaxContentW + "px;height:" + ajaxContentH + "px;'></div>");
		}//if new
		else
		{
			$("#" + contentID)[0].style.width = ajaxContentW +"px";
			$("#" + contentID)[0].style.height = ajaxContentH +"px";
			$("#" + contentID)[0].scrollTop = 0;
		}//else change

		$("#" + contentID).append(html_str);

		aPopUpPosition(tbWinID);

		$("#PP_load").remove();
		$('#' + tbWinID).css({display:"block"});

		$(document).data('windows').push(tbWinID);
		$(document).data('active', tbWinID);

		$("#PP_overlay").click(function(ev){aPopUpClose()});
	}//try it out
	catch(e)
	{
		alert(e);
	}//catch e
}//tb_showDirect

function aPopUpClose(id)
{
	if(id == undefined)id = $(document).data('active');

	if($(document).data('windows').length == 1)
	{
		aPopUpCloseClear(id);
	}//if last one
	else
	{
		aPopUpCloseRemoveWin(id);
	}//else

	return false;
}//aPopUpClose

function aPopUpCloseClear(id)
{
 	$("#PP_imageOff").unbind("click");
	$("#PP_closeWindowButton").unbind("click");

	$('#' + id).fadeOut("fast", function(){$('#' + id + ',#PP_overlay,#PP_HideSelect').trigger("unload").unbind().remove();});
	$("#PP_load").remove();

	if(typeof document.body.style.maxHeight == "undefined")
	{
		$("body","html").css({height: "auto", width: "auto"});
		$("html").css("overflow","");
	}//if ie6
}//aPopUpCloseClear

function aPopUpCloseRemoveWin(id)
{
	var wins = $(document).data('windows');
	var tw = wins.length;
	var i;

	for(var w = 0; w < tw; w++)
	{
		if(wins[w] == id)
		{
			i = w;
			break;
		}//if dound
	}//for w

	if($(document).data('active') == id)
	{
		var act = $(document).data('windows')[i - 1];
		$(document).data('active', act);
	}//if active
	$(document).data('windows').splice(i, 1);

	$('#' + id).fadeOut("fast", function(){$('#' + id + ', #' + id + '_overlay').trigger("unload").unbind().remove();});
}//aPopUpCloseRemoveWin

function aPopUpPosition(id)
{
	$("#" + id).css({marginLeft: '-' + parseInt((PP_WIDTH / 2),10) + 'px', width: PP_WIDTH + 'px'});
	if(!(jQuery.browser.msie && jQuery.browser.version < 7))$("#" + id).css({marginTop: '-' + parseInt((PP_HEIGHT / 2),10) + 'px'});
}//aPopUpPosition

function aPopUpDetectMacXFF()
{
  var userAgent = navigator.userAgent.toLowerCase();
  if(userAgent.indexOf('mac') != -1 && userAgent.indexOf('firefox')!=-1)return true;
}//aPopUpDetectMacXFF
