// JavaScript Document

//thickbox replacement

$(document).ready( function() {
	  $(".jqmClose").hover( function() {
							  $(this).css({ backgroundPosition: "0px -30px" });
							  },
							  function() {
								  $(this).css({ backgroundPosition: "0 0" });
					  });
});

var popupLoaded = new Array();
popupLoaded[0] = false;
popupLoaded[1] = false;
popupLoaded[2] = false;
popupLoaded[3] = false;
popupLoaded[4] = false;
popupLoaded[5] = false;
popupLoaded[6] = false;
popupLoaded[7] = false;
popupLoaded[8] = false;

var closeModal = function(hash)
{
	var $modalWindow = $(hash.w);

	//$('#jqmContent').attr('src', '');
	$modalWindow.fadeOut("slow", function()
	{
		hash.o.remove();
		
		//refresh parent
		if (hash.refreshAfterClose === 'true')
		{
			window.location.href = document.location.href;
		}
	});
};

var openInFrame = function(hash)
{
	var curIdx = hash.c.popupIdx;
	var $trigger = $(hash.t);
	var $modalWindow = $(hash.w);
	
	var $modalContainer = $( 'iframe', $modalWindow );

	var myUrl = $trigger.attr('href');

	var myTitle = $trigger.attr('title');
	var newWidth = 0, newHeight = 0, newLeft = 0, newTop = 0;
	
	$modalContainer.html('').attr('src', myUrl);
	// Safari, force reload of the iframe:
	//$modalContainer[0].contentWindow.location.reload(false);
	
	$('#jqmTitleText').text(myTitle);
	myUrl = (myUrl.lastIndexOf("#") > -1) ? myUrl.slice(0, myUrl.lastIndexOf("#")) : myUrl;

	var queryString = (myUrl.indexOf("?") > -1) ? myUrl.substr(myUrl.indexOf("?") + 1) : null;

	if (queryString != null && typeof queryString != 'undefined')
	{
		var queryVarsArray = queryString.split("&");
		for (var i = 0; i < queryVarsArray.length; i++)
		{
			if (unescape(queryVarsArray[i].split("=")[0]) == 'width')
			{
				var newWidth = queryVarsArray[i].split("=")[1];
			}
			if (escape(unescape(queryVarsArray[i].split("=")[0])) == 'height')
			{
				var newHeight = queryVarsArray[i].split("=")[1];
			}
			if (escape(unescape(queryVarsArray[i].split("=")[0])) == 'jqmRefresh')
			{
				// if true, launches a "refresh parent window" order after the modal is closed.
				hash.refreshAfterClose = queryVarsArray[i].split("=")[1]
			} else
			{

				hash.refreshAfterClose = false;
			}
		}
		
		if (newHeight != 0)
		{
			if (newHeight.indexOf('%') > -1)
			{

				newHeight = Math.floor(parseInt($(window).height()) * (parseInt(newHeight) / 100));

			}
			var newTop = Math.floor(parseInt($(window).height() - newHeight) / 2);
		}
		else
		{
			newHeight = $modalWindow.height();
		}
		if (newWidth != 0)
		{
			if (newWidth.indexOf('%') > -1)
			{
				newWidth = Math.floor(parseInt($(window).width() / 100) * parseInt(newWidth));
			}
			var newLeft = Math.floor(parseInt($(window).width() / 2) - parseInt(newWidth) / 2);

		}
		else
		{
			newWidth = $modalWindow.width();
		}
		  
	  /*alert("width: " + newWidth + " height: " + newHeight + 
			" top: " + newTop + " left: " + newLeft + 
			"\nwindowWidth: " + $(window).width() + 
			"\nwindowHeight: " + $(window).height() );*/
		
		//var halfTop = Math.floor($(window).height() / 2);
		//var halfLeft = Math.floor($(window).width() / 2);
		
		//alert($modalContainer.attr('src')); // DEBUG

		if(!popupLoaded[curIdx]) { // first time loading this popup
				$modalWindow.css({
					/*width: 0,
					height: 0,
					top: halfTop,
					left: halfLeft,*/
					top: newTop,
					left: newLeft,
					width: newWidth,
					height: newHeight,
					opacity: 0.0
				}).jqmShow().animate({
							width: newWidth,
							height: newHeight,
							/*top: newTop,
							left: newLeft,*/
							opacity: 1.0
						}, 200, function() {
									popupLoaded[curIdx] = true;
									$(".jqmClose:not('.inactive')").fadeIn("fast");
									  // handle IE ClearType animation bug:
									  if(jQuery.browser.msie) {
										  if(parseInt(jQuery.browser.version) < 8) {
															this.style.removeAttribute('filter');
										  }
									  }
								  });
		}
		else { // popup has loaded before (this avoids flash of iframe content) 
			$modalWindow.jqmShow().fadeIn("slow", function() {
													if(jQuery.browser.msie) {
														  if(parseInt(jQuery.browser.version) < 8) {
																			this.style.removeAttribute('filter');
														  }
													}
										});
			
			$(".jqmClose:not('.inactive')").fadeIn("fast");
		} // end else (popup has loaded before)
	}
	else
	{
		// don't do animations
		$modalWindow.jqmShow();
		$(".jqmClose").fadeIn("slow");
	}
}