
var UI_TARGET			= "target";
var UI_CONTAINER		= "container";
var UI_LOADING			= "loading";
var UI_CONTENT			= "content";
var UI_CLOSE			= "close";

var CONST_UIBOX_WIDTH	= "WIDTH";
var CONST_UIBOX_HEIGHT	= "HEIGHT";

	
$(document).ready(function()
{	
	$("a.ui-lightbox-link, area.ui-lightbox-link, input.ui-lightbox-link").click(function()
	{		
		var url = this.href || this.alt;
		var parameters = uiBox_GetQueryString(url);
			
		uiBox_Show(parameters, url);
		this.blur();
		return false;
	});	
});

//This function looks at the calling url and creates our overlay as required.
function uiBox_Show(parameters, url)
{
	//Extract our Qs Parameters to a name value collection.
	var uiLightBoxOverlay;
	var ui = uiBox_GetUiComponents(parameters.target);
	
	//In IE6 we have add a iframe fix to hide the overlayed content.
	if (	($.browser.msie) &&
			($.browser.version < 7) &&
			(ui[UI_TARGET].find("iframe.ui-lightbox-iframe-fix").length == 0))
	{
		ui[UI_TARGET].append("<iframe class=\"ui-lightbox-iframe-fix\"></iframe>");
	}
	
	//Set-up our overlay element to block out overlay element content.
	if(ui[UI_TARGET].find(".ui-lightbox-overlay").length == 0)
	{
		ui[UI_TARGET].append("<div class=\"ui-lightbox-overlay\"></div>");
		uiLightBoxOverlay = ui[UI_TARGET].find("div.ui-lightbox-overlay");
		//If we are not forcing modal functionality where the user is forced to close only from the lightbox itself.
		if(!parameters.modal) uiLightBoxOverlay.click(uiBox_Hide);
		if(uiBox_IsUserAgent("firefox", null, "mac")) uiLightBoxOverlay.addClass("ui-lightbox-overlay-png");
	}
	
	//Wire up our close button if we have one.
	if(ui[UI_CLOSE].length > 0) ui[UI_CLOSE].click(uiBox_Hide);
	
	//Get the target position values
    var pos				= ui[UI_TARGET].position();
    var targetTop		= ui[UI_TARGET].position().top;
	var targetLeft		= ui[UI_TARGET].position().left;
	var targetWidth		= ui[UI_TARGET].outerWidth();
	var targetHeight	= ui[UI_TARGET].outerHeight();
	var fullWidth		= targetWidth;
	var fullHeight		= targetHeight;
	
	if (ui[UI_TARGET].is("body") || ui[UI_TARGET].is(document))
	{
		fullWidth = $(document).width();
		fullHeight = $(document).height();
	}
	
	//alert("targetTop = " + targetTop + "\ntargetLeft = " + targetLeft + "\n" + "targetWidth = " + targetWidth + "\ntargetHeight = " + targetHeight);
		
	// Size overlay and i-frame-fix if ie.6 as has problems with css 100% height / width in certain situations
	if ($.browser.msie && $.browser.version < 7)
	{
		var iframe	= ui[UI_TARGET].find(".ui-lightbox-iframe-fix");		
		iframe.width(fullWidth);
		iframe.height(fullHeight);
	}
	
	// to get rid of the gap at the bottom overlay should always have width and height defined.
	var overlay = ui[UI_TARGET].find(".ui-lightbox-overlay");
	overlay.width(fullWidth);
	overlay.height(fullHeight);
	
    if (uiLightBoxOverlay) uiLightBoxOverlay.show();
    		
	//Set the co-ordinate properties of our lightbox container.
	var containerWidth = (parameters.width * 1) || 630; //defaults to 630 if no paramaters were added to URL
	var containerHeight = (parameters.height * 1) || 440; //defaults to 440 if no paramaters were added to URL	
	
	//we need to switch this to check if this to be displayed centred in the window, or centered in the viewable portion
	var containerTop = 0;
	var containerLeft = 0;
	
	//if the viewableCentre displayPosition parameter is passed in, then add the scrollTop param to centre within the viewpane not the container
	if(parameters.displayPosition == 'viewableCentre')
	{
		containerTop = (parameters.top * 1) || (($(window).height() - containerHeight) / 2) + $(window).scrollTop();
	}
	else
	{
		containerTop = (parameters.top * 1) || ((targetHeight - containerHeight) / 2);
	}
	if (parameters.left)
	{
		containerLeft = (parameters.left * 1) || ((targetWidth - containerWidth) / 2);
	}
	else
	{
		containerLeft = ((targetWidth - containerWidth) / 2);
	}
	
	//Show our pre-loader while we set up other content elements.
	uiBox_ToggleLoader(parameters);
	
	ui[UI_TARGET].append(ui[UI_CONTAINER]);
	
	var containerCloned;
	var containerContentCloned;
	
	//Check if we should load content from the current page or the url in an i-frame.
	if(!parameters.source)
	{
		uiLightBoxContent.append("<iframe frameborder=\"0\" src=\"" + url + "\" id=\"ui-lightbox-iframe\" name=\"ui-lightbox-iframe\" onload=\"uiBox_ToggleLoader()\"> </iframe>");
	}
	else
	{	
		//$("#" + parameters.source).clone().appendTo(uiLightBoxContent);		
		// ui[UI_CONTAINER].clone().appendTo(ui[UI_TARGET]); 
				
	    containerCloned = ui[UI_TARGET].find(".ui-lightbox-container");
	    containerContentCloned = containerCloned.find(".ui-lightbox-content");
		
	    $("#" + parameters.source).clone(true).appendTo(containerContentCloned).show(); 
	    
	    //If our cloned source fragment contains a target iframe with a class of "ui-lightbox-iframe-destination" descendent load the url to this frame.
		uiIFrameTarget = containerContentCloned.find("iframe.ui-lightbox-iframe-destination");
		if(uiIFrameTarget.length > 0)
		{
			uiIFrameTarget.attr("src", url);
		}
		
		//alert("containerLeft = " + containerLeft + "\ncontainerTop = " + containerTop + "\ncontainerHeight = " + containerHeight + "\ncontainerWidth = " + containerWidth);
		
		var cssSettings = {};
		if (!isNaN(containerLeft))
		{
			cssSettings.left = containerLeft + "px";
		}
		if (!isNaN(containerTop))
		{
			cssSettings.top = containerTop + "px";
		}
		if (!isNaN(containerHeight))
		{
			cssSettings.height = containerHeight + "px";
		}
		if (!isNaN(containerWidth))
		{
			cssSettings.width = containerWidth + "px";
		}
		
		containerCloned.css(cssSettings);
        
        containerCloned.show();
		
		uiBox_ToggleLoader(null);
		
		/* Fire a load event on the light box window to run any custom events. */
		containerCloned.trigger("load");
	}
	
	/* Fire a document level overlay loaded event */
	var lightboxLoadedEvent					= jQuery.Event("LIGHTBOX-LOAD-COMPLETE");
	lightboxLoadedEvent.targetContainerId	= parameters.target;
	lightboxLoadedEvent.jqLightboxContent	= containerContentCloned;
	
	$(document).trigger(lightboxLoadedEvent);
}


//Method used to hide the lightbox overlay.
function uiBox_Hide()
{
	var ui = uiBox_GetUiComponents(null);    
    
    ui[UI_CONTAINER].hide();
    ui[UI_CONTENT].empty();
    $("body").append(ui[UI_CONTAINER]);
	if(ui[UI_CLOSE].length > 0) ui[UI_CLOSE].unbind().hide();
	$("iframe.ui-lightbox-iframe-fix, div.ui-lightbox-overlay").remove();
	return false;
}

//Get a friendly collection of ui components for the overlay.
function uiBox_GetUiComponents(targetElement)
{
	var ui = new Object();
	ui[UI_CONTAINER]		= $(".ui-lightbox-container");
	ui[UI_CONTENT]			= ui[UI_CONTAINER].find(".ui-lightbox-content");
	ui[UI_LOADING]			= ui[UI_CONTAINER].find(".ui-lightbox-loading");
	ui[UI_CLOSE]			= ui[UI_CONTAINER].find(".ui-lightbox-close");
	
	var uiTarget = null;
	if (targetElement)
	{
		uiTarget = $("#" + targetElement);
	}
	
	if (uiTarget === null || uiTarget.length < 1)
	{
		uiTarget = $("body");
	}
	
	ui[UI_TARGET] = uiTarget;
	
	return ui;
}

//Parse the QueryString parameters from the passed in url to a name value pair.
function uiBox_GetQueryString(url)
{ 
	var args = new Object(); 
	var query = url.substring(url.indexOf("?") + 1);
	var pairs = query.split("&"); 
	for(var i = 0; i < pairs.length; i++)
	{ 
		var pos = pairs[i].indexOf("="); 
		if (pos == -1) continue; 
		var argname = pairs[i].substring(0,pos); 
		var value = pairs[i].substring(pos+1); 
		args[argname] = unescape(value); 
	} 
	return args; 
}

//Detect the browser, version and os of the user agent.
function uiBox_IsUserAgent(browser, version, os)
{
  var userAgent = navigator.userAgent.toLowerCase();
  var isUserAgent = false;
  if(browser != null) isUserAgent = (userAgent.indexOf(browser) != -1);
  if(version != null) isUserAgent = (userAgent.indexOf(version) != -1);
  if(os != null) isUserAgent = (userAgent.indexOf(os) != -1);
  return isUserAgent;
}

//Utility function to show and hide our preloader and content.
function uiBox_ToggleLoader(parameters)
{
    if(parameters)
		var ui = uiBox_GetUiComponents(parameters.target);
	else
		var ui = uiBox_GetUiComponents();
	
	if (ui[UI_LOADING].is(":hidden"))
	{
		ui[UI_CONTENT].hide();
		if(ui[UI_CLOSE].length > 0) ui[UI_CLOSE].hide();
		ui[UI_LOADING].show();
	}
	else
	{
		ui[UI_LOADING].hide();
		ui[UI_CONTENT].show();
		if(ui[UI_CLOSE].length > 0) ui[UI_CLOSE].show();
	}
}


function uiBox_Trigger(url)
{
	var parameters = uiBox_GetQueryString(url);
	
	uiBox_Show(parameters,url);
}
