/* ============================================================================
A support function, which loads JS/CSS, which are required for using overlays.

Requires:
	//www.ibm.com/common/js/ibmcommon.js
	
Part of:
	//www.ibm.com/itsolutions/uk/js/itmcommon.js

Sample usage:

1.) Adding overlay.css/js dynamically and using hard-coded overlay DIVs:
  - include this JS file to your page if you need to use standard overlays,
	which are hard-coded in the page (i.e. they are not dynamically created)
  - it is recommended to add class "ibm-access" to all hand-coded overlay
	DIVs, as this will make sure that no overlay DIV is not displayed briefly
	before "overlay.css" is loaded

2.) Adding overlay.css/js before creating overlays dynamically by Javascript
  - add the following function call at the start of your Javascript code
	which uses overlays:
		itmCommonOverlayInit();
  - make sure that this JS file is included before your Javascript code
  - adding "ibm-access" to overlay DIVs is not required when they are being
	created dynamically by Javascript

3.) Creating an overlay DIV
  - you can use the following code to prepare an overlay DIV for your JS code:
  		itmCommonOverlayCreate('myOverlay','390px','Close');
		- 'myOverlay' is the ID of the overlay DIV (must be unique)
		- '390px' is the type of the overlay (alternatives: 570px, 750px)
		- 'Close' is the text label for the 'Close' button in the overlay
  - please note that a dynamically created overlay DIV will destroy itself
	once it is closed
============================================================================ */


// checks if overlay JS/CSS files are loaded, loads them if they are not
//	(can be called on DOM Document ready or during runtime)
function itmCommonOverlayInit()
{

	// check if overlay.css is present, load it if it is not
	if(!jQuery("head>link[rel*=style][href*=overlay.css]").length)
	{
		jQuery("head").append("<link href=\"//www.ibm.com/common/v16/css/overlay.css\" media=\"screen,projection\" rel=\"stylesheet\" title=\"www\" type=\"text/css\" />");

		// remove "ibm-access" class from all hard-coded overlays, which rely
		//	on this function (the class is added in order to prevent
		//	the overlay content to be displayed before "overlay.css" loads
		jQuery("div.ibm-common-overlay").removeClass("ibm-access");
	}

	// check if overlay.js is present, load it if it is not
	if(!jQuery("script[type*=javascript][src*=overlay.js]").length)
		jQuery.getScript("//www.ibm.com/common/js/overlay.js");
}

// creates a new DIV and adds all overlay-required childnodes into it
function itmCommonOverlayCreate(overlayId,type,closeLabel)
{
	// check if a DIV with the same ID is not already created; if it is,
	//	clean it's contents are remove any ibm-alternate classes
	//	- note that the overlay cannot be destroyed due to IE6 bug
	if(jQuery("div#"+overlayId).length)
	{
		jQuery("div#"+overlayId).empty();
		if(jQuery("div#"+overlayId).hasClass("ibm-overlay-alt"))
			jQuery("div#"+overlayId).removeClass("ibm-overlay-alt");
		if(jQuery("div#"+overlayId).hasClass("ibm-overlay-alt-two"))
			jQuery("div#"+overlayId).removeClass("ibm-overlay-alt-two");
	}


	// create the main DIV
	else jQuery("div#ibm-content-main")
		.append("<div id=\""+overlayId+"\" class=\"ibm-common-overlay\"></div>");

	// add alternate classes for larger DIVs (if needed)
	if(type!=="390px")
	{
		if(type==="570px")
			jQuery("div#"+overlayId).addClass("ibm-overlay-alt");
		else if(type==="750px")
			jQuery("div#"+overlayId).addClass("ibm-overlay-alt-two");
	}

	// create the head
	jQuery("div#"+overlayId).append("<div class=\"ibm-head\"></div>");
	jQuery("div#"+overlayId+">div.ibm-head").append("<p><a href=\"#close\"></a></p>");
	jQuery("div#"+overlayId+">div.ibm-head>p>a")
		.addClass("ibm-common-overlay-close")
		.text(closeLabel+" [x]")
		.bind("click",{overlayIdToHide:overlayId},function(e)
		{
			e.preventDefault();
			itmCommonOverlayDestroy(jQuery("div#"+e.data.overlayIdToHide).attr("id"));
		});
	
	// create an empty DIV for body
	jQuery("div#"+overlayId)
		.append("<div class=\"ibm-body\"></div>");
	jQuery("div#"+overlayId+">div.ibm-body")
		.append("<div class=\"ibm-main\"><a></a></div>");
	jQuery("div#"+overlayId+">div.ibm-body>a")
		.addClass("ibm-access")
		.text("<!-- Accessibility anchor -->");
	
	// create the footer
	jQuery("div#"+overlayId).append("<div class=\"ibm-footer\"></div>");
}

// closes the overlay DIV and destroys it
function itmCommonOverlayDestroy(overlayId)
{
	// call native overlay "close" function (it solves one major IE6 bug)
	//ibmCommon.Overlays.close(jQuery("div#"+overlayId));
	jQuery("div#"+overlayId).queue(function()
	{
		ibmCommon.Overlays.close(jQuery(this));
		jQuery(this).dequeue();
	});
	
	// queue destruction of overlay DIV after native "close" function
	//	finishes
	jQuery("div#"+overlayId).queue(function()
	{
		jQuery(this).remove();
		jQuery(this).dequeue();
	});
}


// checks if there are any overlays in the page - if yes, runs the init fnc.
jQuery(document).ready(function()
{
	// if(jQuery("div.ibm-common-overlay").length)
		itmCommonOverlayInit();
});