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);
		this.blur();
		return false;
	});	
});

//This function looks at the calling url and creates our overlay as required.
function uiBox_Show(parameters)
{
	//Extract our Qs Parameters to a name value collection.
	var uiLightBoxOverlay;
	var ui = uiBox_GetUiComponents(parameters.target);
	
	//In IE6 we have add a dodgy iframe hack 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].width();
	//var targetHeight = ui[UI_TARGET].height();
	var targetWidth		= uiBox_GetDimensionAbsolute(ui[UI_TARGET], CONST_UIBOX_WIDTH);
	var targetHeight	= uiBox_GetDimensionAbsolute(ui[UI_TARGET], CONST_UIBOX_HEIGHT);
	
	// 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");
		var overlay = ui[UI_TARGET].find(".ui-lightbox-overlay");
		
		iframe.css("width", String(targetWidth));
		iframe.css("height", String(targetHeight));
		
		overlay.css("width", String(targetWidth));
		overlay.css("height", String(targetHeight));
	}
	
	
	
    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	
	var containerTop = (parameters.top * 1) || ((targetHeight - containerHeight) / 2);
	var containerLeft = (parameters.left * 1) || ((targetWidth - containerWidth) / 2);
	
	//Show our pre-loader while we set up other content elements.
	uiBox_ToggleLoader(parameters);
	
	ui[UI_TARGET].append(ui[UI_CONTAINER]); 
	
	//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]); 
				
	    var containerCloned = ui[UI_TARGET].find(".ui-lightbox-container");
	    var containerContentCloned = containerCloned.find(".ui-lightbox-content");
	    $("#" + parameters.source).clone(true).appendTo(containerContentCloned).show();
		
        containerCloned.css({	left	: containerLeft  + "px",
								top		: containerTop + "px",
								height	: containerHeight + "px",
								width	: containerWidth + "px"});
		/*
		containerCloned.css({left	: targetLeft + 'px'});
		containerCloned.css({height : containerHeight + 'px'});
	    containerCloned.css({width	: containerWidth + 'px'});
		*/
        containerCloned.show();	       
   
		uiBox_ToggleLoader(null);
		
		/*Fire a load event on the light box window to run any custom events.*/
		containerCloned.trigger("load");
	}
}

//Get a friendly collection of ui components for the overlay.
function uiBox_GetDimensionAbsolute(targetJQueryObject, axis)
{
	var t = targetJQueryObject;
	if (axis == CONST_UIBOX_WIDTH) return t.width() + parseInt(t.css("paddingLeft")) + parseInt(t.css("paddingRight")) + parseInt(t.css("borderLeftWidth")) + parseInt(t.css("borderRightWidth"));
	else if (axis == CONST_UIBOX_HEIGHT) return t.height() + parseInt(t.css("paddingTop")) + parseInt(t.css("paddingBottom")) + parseInt(t.css("borderTopWidth")) + parseInt(t.css("borderBottomWidth"));
}



//Method used to hide the lightbox overlay.
function uiBox_Hide(parameters)
{
	var ui = uiBox_GetUiComponents(parameters.target[0]);    
    ui[UI_CONTAINER].hide();
    ui[UI_CONTENT].empty();
    $("body").append(ui[UI_CONTAINER]);
	if(ui[UI_CLOSE].length > 0) ui[UI_CLOSE].unbind().hide();
	ui[UI_TARGET].remove(".ui-lightbox-container");	
	$("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["container"].find(".ui-lightbox-content");
	ui[UI_LOADING]	= ui["container"].find(".ui-lightbox-loading");
	ui[UI_CLOSE]	= ui["container"].find(".ui-lightbox-close");
	ui[UI_TARGET]	= $((targetElement != null) ? ("#" + targetElement) : "body");	
	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();
	}
}