
/*
 *	CUSTOMISED VERSION OF 'Thickbox' :
 *		Adjusted to remove redundant 'unused' elements of code as ONLY implimented in one way
 *
 *		ORIGINAL VERSION :
 *			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
*/
		  
	/*
	var tb_pathToImage = "images/loadingAnimation.gif";
	var tb_pathToImage = "/assets/web/images/loadingAnimation.gif";
	*/
	var tb_pathToImage = "/assets/web/images/loadingIcon.gif";
	
	/*
	 *************************************************
	 * DOCUMENT READY
	 *
	 *	- on page load trigger call to tb_init()
	 *************************************************
	 */
	 
		$(document).ready(function(){   
			tb_init('a.thickbox, area.thickbox, input.thickbox');//pass where to apply thickbox
			
			imgLoader = new Image();// preload image
			imgLoader.src = tb_pathToImage;
		});



	/*
	 *************************************************
	 * INITILISATION
	 *
	 *	- Add thickbox functionality to any href & area elements that have a class of .thickbox
	 *************************************************
	 */
	 
		function tb_init(domChunk){
			$(domChunk).click(function(){
			var t = this.title || this.name || null;
			var a = this.href || this.alt;
			var g = this.rel || false;
			tb_show(t,a,g);
			this.blur();
			return false;
			});
		}



	/*
	 *************************************************
	 * DISPLAY EVENT HANDLER
	 *************************************************
	 */
	 
		function tb_show(caption, url, imageGroup) {//function called when the user clicks on a thickbox link

			try {
				
				/*
				 * SET UP CORE html assets according to browser [ TB_overlay, TB_window & TB_HideSelect (where required) ] :
				 */
					if (typeof document.body.style.maxHeight === "undefined") {//if IE 6
						$("body","html").css({height: "100%", width: "100%"});
						$("html").css("overflow","hidden");
						//$("body").css("overflow","hidden");	//This fix causes page to jump to top!!!
						if (document.getElementById("TB_HideSelect") === null) {//iframe to hide select elements in ie6
							$("body").append("<iframe id='TB_HideSelect'></iframe><div id='TB_overlay'></div><div id='TB_window'></div>");
							$("#TB_overlay").click(tb_remove);
						}
					}else{//all others
						if(document.getElementById("TB_overlay") === null){
							$("body").append("<div id='TB_overlay'></div><div id='TB_window'></div>");
							$("#TB_overlay").click(tb_remove);
						}
					}
				
				
				/*
				 * SET UP 'TB_overlay' styles
				 */
					if(tb_detectMacXFF()){
						$("#TB_overlay").addClass("TB_overlayMacFFBGHack");//use png overlay so hide flash
					}else{
						$("#TB_overlay").addClass("TB_overlayBG");//use background and opacity
					}
				
				
				/*
				 * DISPLAY LOADING GRAPHIC
				 */
					$("body").append("<div id='TB_load'><img src='"+imgLoader.src+"' /></div>");//add loader to the page
					$('#TB_load').show();//show loader


				/*
				 * SETUP & GENERATE ACTUAL OVERLAY html :
				 */
					
					var queryString = url.replace(/^[^\?]+\??/,'');
					var params = tb_parseQuery( queryString );
					
					var borderSize		= 20;
					var totalBorderSize = borderSize * 2;
					
					//alert("= = = = = " + params['height']);
					//alert("= = = = = " + params['width']);
					
					var actualContentW = (params['width']*1)  || 400;	//defaults to 400 if no paramaters were added to URL
					var actualContentH = (params['height']*1) || 300;   //defaults to 300 if no paramaters were added to URL 
					
					//alert("- - - - - " + actualContentH)
					
					if (actualContentH < 290) actualContentH = 290;		//min content height = 290 >> ensures gradient renders correctly
					
					/*
					 * #TB_window SIZE settings (assigned during 'tb_position()')
					 */
					TB_WIDTH	= actualContentW + totalBorderSize;
					TB_HEIGHT	= actualContentH + totalBorderSize;
					
					ajaxContentW = TB_WIDTH;
					ajaxContentH = TB_HEIGHT;
					
					
					if($("#TB_window").css("display") != "block")
					{
						/*	
						 * CORE html construct
						 */
							/*	
							 * IF IN-LINE
							 */
							if(url.indexOf('TB_iframe') == -1)
							{
								$("#TB_window").append(
														"<div id='TB_closeWindow'><a href='#' id='TB_closeWindowButton'>Close</a></div>" +
														
														"<div class='innerWindow innerWindowGradient'>" +
															"<div class='TL'></div>" +
															"<div class='TR'></div>" +
															"<div class='BL'></div>" +
															"<div class='BR'></div>" +
															"<div class='T'></div>"  +
															"<div class='L'></div>"  +
															"<div class='B'></div>"  +
															"<div class='R'></div>"  +
															"<div id='TB_actualContent'>" +
																"" +
															"</div>" +
														
														"</div>"
													  );
							}
							
							/*	
							 * ELSE I-FRAME
							 */
							else
							{
								/*
								 * EXTRACT url from link
								 */
								urlNoQuery = url.split('TB_');
								
								$("#TB_window").append(
													"<div id='TB_closeWindow'><a href='#' id='TB_closeWindowButton'>Close</a></div>" +
													
													"<div class='innerWindow innerWindowGradient'>" +
														"<div class='TL'></div>" +
														"<div class='TR'></div>" +
														"<div class='BL'></div>" +
														"<div class='BR'></div>" +
														"<div class='T'></div>"  +
														"<div class='L'></div>"  +
														"<div class='B'></div>"  +
														"<div class='R'></div>"  +
														"<div id='TB_actualContent' class='TB_actualContent_iFrame'>" +
															"<iframe id='TB_iFrame' name='TB_actualContent" + Math.round(Math.random()*1000) + "' align='left' frameborder='0' marginheight='0' marginwidth='0'" + "src='" + urlNoQuery[0] + "' onload='tb_showIframe()' scrolling='auto' allowtransparency='true'></iframe>" +
														"</div>" +
													"</div>"
												  );
								
								/*
								 * SET IFRAME WIDTH & HEIGHT
								 */
								$("#TB_window > div.innerWindow > #TB_actualContent > #TB_iFrame").css({width: actualContentW + 'px', height: actualContentH + 'px'});
												  
							}
											  
						
						/*	
						 * SET 'CONTENT CONTAINER' WIDTH & HEIGHT
						 */	
							$("#TB_window > div.innerWindow > #TB_actualContent").css({width: actualContentW + 'px', height: actualContentH + 'px'});
						
						
						/*	
						 * ADJUST SIZINGS OF CONTAINER WINDOW IF 'CONTENT CONTAINER' has any PADDING to accommodate
						 */				  
							var pT = parseInt($("#TB_actualContent").css("paddingTop"));
							var pB = parseInt($("#TB_actualContent").css("paddingBottom"));
							var pL = parseInt($("#TB_actualContent").css("paddingLeft"));
							var pR = parseInt($("#TB_actualContent").css("paddingRight"));
							
							//alert("pT : " + pT + " // pB : " + pB + " // pL : " + pL + " // pR : " + pR);
							
							if (pT != undefined)
							{
								actualContentH	+= pT;
								TB_HEIGHT		+= pT;
							}
							if (pB != undefined)
							{
								actualContentH	+= pB;
								TB_HEIGHT		+= pB;
							}
							if (pR != undefined)
							{
								actualContentW	+= pR;
								TB_WIDTH		+= pR;
							}
							if (pL != undefined)
							{
								actualContentW	+= pL;
								TB_WIDTH		+= pL;
							}
						
						
						/*	
						 * SET 'innerWindow' dimensions
						 */
							$("#TB_window > div.innerWindow").css({width: actualContentW + 'px', height: actualContentH + 'px'});
					
					}
					
					
					
					//////////////////////////////////////////////////////////////////////////////////////////////////
					// Add close button handler method
					//////////////////////////////////////////////////////////////////////////////////////////////////
					$("#TB_closeWindowButton").click(tb_remove);
					
					
					//////////////////////////////////////////////////////////////////////////////////////////////////
					// IF inline : add content to generated framework
					//////////////////////////////////////////////////////////////////////////////////////////////////
					if(url.indexOf('TB_inline') != -1)
					{	
						$("#TB_actualContent").append($('#' + params['inlineId']).children());
						
						$("#TB_window").unload(function () {
							$('#' + params['inlineId']).append( $("#TB_actualContent").children() ); // MOVE CHILDREN BACK INTO ORIGINAL LOCATION
						});
						
						tb_position();
						$("#TB_load").remove();
						$("#TB_window").css({display:"block"}); 
					}
					
					//////////////////////////////////////////////////////////////////////////////////////////////////
					// ELSE IF iframe : content already added via iframe url property, instead add safari fix if required
					//////////////////////////////////////////////////////////////////////////////////////////////////
					else if(url.indexOf('TB_iframe') != -1)
					{
						tb_position();
						if($.browser.safari){//safari needs help because it will not fire iframe onload
							$("#TB_load").remove();
							$("#TB_window").css({display:"block"});
						}
					}
					
					
				
				
				/*
				 **********************************************************************************************
				 * ALWAYS WANT TO DO FOLLOWING - LEAVE IN BUT NEVER SET 'modal=true' in link href
				 * - Adds escape key close
				 **********************************************************************************************
				 */
					if(!params['modal']){
						document.onkeyup = function(e){ 	
							if (e == null) { // ie
								keycode = event.keyCode;
							} else { // mozilla
								keycode = e.which;
							}
							if(keycode == 27){ // close
								tb_remove();
							}	
						};
					}
					
				/*
				 **********************************************************************************************
				 **********************************************************************************************
				 */
				
				
			} catch(e) {
				//nothing here
			}
		}


	/*
	 *************************************************
	 * UTILITY METHODS
	 *************************************************
	 */
		
		function tb_showIframe()
		{
			$("#TB_load").remove();
			$("#TB_window").css({display:"block"});
		}
		
		function tb_remove() {
 			$("#TB_imageOff").unbind("click");
			$("#TB_closeWindowButton").unbind("click");
			
			/*
			 * FADE OUT WIN : ie has problems with transparent png files so only animate for firefox
			 *
			 */
			 //if (tb_detectFF())
			 //{
			 //	$("#TB_window").fadeOut("fast",function(){$('#TB_window,#TB_overlay,#TB_HideSelect').trigger("unload").unbind().remove();});
			 //}
			 //else 
			 //{
				$('#TB_window,#TB_overlay,#TB_HideSelect').trigger("unload").unbind().remove();
			 //}
			
			
			
			$("#TB_load").remove();
			
			if (typeof document.body.style.maxHeight == "undefined") {//if IE 6
				$("body","html").css({height: "auto", width: "auto"});
				$("html").css("overflow","");
			}
			document.onkeydown = "";
			document.onkeyup = "";
			return false;
		}

		function tb_position() 
		{
			$("#TB_window").css({marginLeft: '-' + parseInt((TB_WIDTH / 2),10) + 'px', width: TB_WIDTH + 'px', height: TB_HEIGHT + 'px'});
			
			//IF !ie.6
			if ( !(jQuery.browser.msie && jQuery.browser.version < 7)) {
				$("#TB_window").css({marginTop: '-' + parseInt((TB_HEIGHT / 2) ,10) + 'px'});
			}
		}

		function tb_parseQuery ( query ) {
		   var Params = {};
		   if ( ! query ) {return Params;}// return empty object
		   var Pairs = query.split(/[;&]/);
		   for ( var i = 0; i < Pairs.length; i++ ) {
			  var KeyVal = Pairs[i].split('=');
			  if ( ! KeyVal || KeyVal.length != 2 ) {continue;}
			  var key = unescape( KeyVal[0] );
			  var val = unescape( KeyVal[1] );
			  val = val.replace(/\+/g, ' ');
			  Params[key] = val;
		   }
		   return Params;
		}

		function tb_detectMacXFF() {
		  var userAgent = navigator.userAgent.toLowerCase();
		  if (userAgent.indexOf('mac') != -1 && userAgent.indexOf('firefox')!=-1) {
			return true;
		  }
		}
		
		function tb_detectFF() {
		  var userAgent = navigator.userAgent.toLowerCase();
		  if (userAgent.indexOf('firefox')!=-1) {
			return true;
		  }
		}



