var $j=jQuery.noConflict();

$j(document).ready(function(){
	//Global Variables
	var iFrameWidth = 482;
	var iFrameHeight = 297;
	var linkage = $j('#iFrameHere').attr("href");
	var title = $j('#iFrameHere').attr("title");
	var iFrameActive = false;
	var windowWidth;
	var windowHeight;
	
	$j('#iFrameHere').click( function(){
		figurePage();

		//Close controller
		iFrameActive = true;

		//Add Overlay
		$j('body').css('position', 'relative');
		//$j('body').append('<div id="lbOverlay" style="visibility: visible; opacity: 1; top: 0px;" />', function(){
		if( window.gecko ) {
			$j('body').append('<div id="lbOverlay" class="geckoShadow" style="visibility: visible; opacity: 1; top: 0px;" />');
		} else {
			$j('body').append('<div id="lbOverlay" class="nonGeckoShadow" style="visibility: visible; top: 0px; filter:alpha(opacity=50);" />');
		}
		//});
		
		//Add iFrame
		$j('body').append('<div id="theIFrame"><h2>' + title + '</h2><img src="/custom/citybankonline2/image/close.png" alt="Close" id="closeLB" /><iframe id="theIFrameID" src ="' + linkage + '" width="100%" height="100%"><p>Your browser does not support iframes.</p></iframe></div>');
		changeCss();
		
		//Close Controllers
		$j('#lbOverlay').click(closeItAll);
		$j("#closeLB").click(closeItAll);
		
		return false;
	});

	//OMG you  hit the esc key
	document.onkeydown = function(e){
		if (e == null) { // ie
			keycode = event.keyCode;
		} else { // mozilla
			keycode = e.which;
		}
		if(keycode == 27){
			closeItAll();
		}
	};
	
	//User resizes window, reposition iframe
	$j(window).bind("resize", function(){
		figurePage();
	    changeCss();  
	});  
	
	//Get current width and height of the window
	function figurePage(){
		windowWidth = $j(window).width();
		windowHeight = $j(window).height();
	}
	
	//reposition the iFrame and change the background height
	function changeCss(){
		if(iFrameActive){
			$j('#lbOverlay').css({
				"height" : windowHeight + "px"
			});

			$j('#theIFrame').css({
				"position" : "absolute",
				"left" : windowWidth / 2 - iFrameWidth / 2,
				"top" : windowHeight /2 - iFrameHeight / 2
			});
		}
	};
	
	//Remove the iFrame from the DOM and remove the click handlers
	function closeItAll(){
		if(iFrameActive){
			$j('#lbOverlay, #theIFrame').remove();
			iFrameActive = false;
		}
		$j('#lbOverlay').unbind("click");
		$j("#closeLB").unbind("click");
	}
});