(function($) {
	$.fn.LightWindow = function(p_options) {		
		m_startWidth = 10;		// Default For Loading Status
		m_startHeight = 10;	// Default For Loading Status

		m_endWidth = 400;		// Default For Loading Status
		m_endHeight = 150;		// Default For Loading Status
		
		m_imgLoad = null;
		m_interval = null;
		
		m_frameWidth = null;
		m_frameHeight = null;
		
		m_imgLoaded = false;
				
		setOptions(p_options);
		drawWindow();
	}
	
	function setOptions(p_options){
	
		m_options = new Object();
		m_options.mediaType = p_options.mediaType;
		m_options.src = p_options.src;
		
		switch(p_options.mediaType){
			case "img":
				if(p_options.slideShow){
					m_options.slideShow = true;
					m_options.id = p_options.id;
					m_options.totImgs = p_options.totImgs;			
				}
				break;
				
			case "iFrame":
				m_frameWidth = (p_options.width > 0) ? p_options.width : null;
				m_frameHeight = (p_options.height > 0) ? p_options.height : null;
				break;
		}
	}
	  
 	function drawWindow(){
		
		var browserWidth = ( jQuery.browser.msie ) ? document.documentElement.clientWidth : window.innerWidth;
		var browserHeight = ( jQuery.browser.msie ) ? document.documentElement.clientHeight : window.innerHeight;
		
		var windowOverlay = document.createElement("div");
			windowOverlay.id = "windowOverlay";
			
		var overlayHeight;
		
		if(jQuery.browser.msie){
			overlayHeight = (document.body.offsetHeight < document.documentElement.clientHeight) ? document.documentElement.clientHeight : document.body.offsetHeight;
		}else{
			overlayHeight = (document.height < document.documentElement.clientHeight) ? document.documentElement.clientHeight : document.height;
		}
			
		windowOverlay.style.height = overlayHeight + "px";

		// Add Slide Show Navigation
		if(m_options.slideShow){
			var controlsContainer = document.createElement("div");
				controlsContainer.id = "slideShowControls";
							
			document.body.appendChild(controlsContainer);
			
			var ctrlTable = "<table cellspacing=\"0\" cellpadding=\"0\" border=\"0\">"
			ctrlTable += "<tbody>";
			ctrlTable += "<tr>";
			ctrlTable += "<td  class=\"caption\">XXXXX</td>";
			ctrlTable += "<td class=\"controls\" valign=\"middle\">";
			ctrlTable += "<img class=\"btnPrev\" src=\"/img/lightwindow/slideShowNav/back.jpg\"/>";
			ctrlTable += "<span class=\"positionCount\">8/24</span>";
			ctrlTable += "<img class=\"btnNext\" src=\"/img/lightwindow/slideShowNav/next.jpg\"/>";
			ctrlTable += "</td>";
			ctrlTable += "<td class=\"close\"><img class=\"btnClose\" src=\"/img/lightwindow/slideShowNav/close.jpg\"/></td>";
			ctrlTable += "</tr></tbody></table>";
			
			$("#slideShowControls").html(ctrlTable);
			
			$("#slideShowControls table:first").attr("width", Math.max(805, browserWidth));
			$("#slideShowControls .caption").text($("h1:first").text());
			setImgPosCount();
			
			$("#slideShowControls img").each(		
				function(i, elm){
					imgItem = $(elm);

					switch($(imgItem).attr("class")){
						case "btnPrev":
							$(imgItem).click(function(p_evt){
								showPrevImg();
								p_evt.preventDefault();
							});
							break;
						case "btnNext":
							$(imgItem).click(function(p_evt){
								showNextImg();
								p_evt.preventDefault();
							});						
							break;
						case "btnClose":
							$(imgItem).click(function(p_evt){
								closeWindow();
								p_evt.preventDefault();
							});						
							break;
					}
				}
			)
		}

		var lightContainer = document.createElement("div");
			lightContainer.id = "lightContainer";
			
		var lightWrapper = document.createElement("div");
			lightWrapper.id = "lightWrapper";
			lightWrapper.style.padding = "5px"
			
		var lightWindow = document.createElement("div");
			lightWindow.id = "lightWindow";

		lightWrapper.appendChild(lightWindow);		
		lightContainer.appendChild(lightWrapper)		

		// Add Elements To DOM
		document.body.appendChild(windowOverlay);
		document.body.appendChild(lightContainer);
								
		// Position Container Element : Center According To Browser Width/Height	
		document.getElementById("lightContainer").style.left = (browserWidth / 2) + "px";
		document.getElementById("lightContainer").style.top = (browserHeight / 2) + "px";

		m_lightWindow = document.getElementById("lightWindow");
		m_lightWindow.style.left = ((m_startWidth / 2) * -1) + "px";	// Offset By Half
		m_lightWindow.style.top = ((m_startHeight / 2) * -1) + "px";
		
		// Position Container Element : Center According To Where It Will Finish
		document.getElementById("lightContainer").style.left = (browserWidth / 2) - (m_endWidth / 2) + "px";
		document.getElementById("lightContainer").style.top = (browserHeight / 2) - (m_endHeight / 2) + "px";
		
		// Position Window : Center The Middle Of LightWindow According To The Center Of Where It Will Finish
		m_lightWindow.style.left = Math.round((m_endWidth / 2) - (m_startWidth / 2)) + "px";
		
		document.getElementById("lightContainer").style.display = 'block';	
																
		m_interval = setInterval(animateLoadingWidth, 20);
	}
	
	function windowResize(){
	
		var browserWidth = ( jQuery.browser.msie ) ? document.documentElement.clientWidth : window.innerWidth;
		var browserHeight = ( jQuery.browser.msie ) ? document.documentElement.clientHeight : window.innerHeight;
	
		$("#slideShowControls table:first").attr("width", Math.max(805, browserWidth));

		$("#lightContainer").css("left", Math.max(0, (browserWidth - parseInt($("#lightWindow").css("width"))) / 2));
		$("#lightContainer").css("top", Math.max(60, (browserHeight - parseInt($("#lightWindow").css("height"))) / 2));
	}
	
	function loadImage(p_imgSrc){
		
		if(m_imgLoad != null && m_options.slideShow){
			// Image Was Loaded Through Slide Show
			// Remove View State From Previous Image
			$("#" + m_imgLoad.id.substring(3, m_imgLoad.id.length)).css("background", "none");
		}else{
			// First Time Loading Setup Resize
			$(window).resize(function(){
				windowResize();
			});
		}
		
		m_imgLoad = new Image();
		m_imgLoad.style.display = "none";
		m_imgLoad.isLoaded = false;
		m_imgLoad.isShown = false;
		m_imgLoad.onload = function(){
			this.isLoaded = true;
			setTimeout(imageLoaded, 10);
		}
		
		if(m_options.slideShow){
			// Slide Show - Scroll Images With Navigation 
			$("#" + m_options.id).css("background-color", "yellow");
			$(document).scrollTop($("#" + m_options.id).offset().top - 50);
			m_imgLoad.id = "img" + m_options.id;
		}
		
		m_imgLoad.src = m_options.src;
	}
		
	function imageLoaded(p_img){
	
		m_startWidth = m_endWidth;
		m_startHeight = m_endHeight;
	
		m_endWidth = m_imgLoad.width;
		m_endHeight = m_imgLoad.height;
		
		m_interval = setInterval(animateWidth, 40);
		//m_interval = setInterval(animateWidth, 1000);
	}
		
	function imgFadeOut(){
	
		// Check To See If Image Is Loaded
		if(!m_imgLoad.isLoaded){
			$("#lightWindow img:first").replaceWith("<img src=\"/img/lightWindow/ajax-loading.gif\" \>");
		}
	}
	
	function imgFadeIn(){
	
		// Check To See If Image Is Loaded
		m_imgLoad.isShown = true;
	}
	
	function showPrevImg(){
	
		if(m_imgLoad.isShown){
			var imgId = (m_options.id == 1) ? m_options.totImgs : parseInt(m_options.id) - 1;
			setOptions({mediaType:"img", slideShow:true, totImgs:m_options.totImgs, src:getImgFilePath(imgId), id:imgId});
			setImgPosCount(imgId);

			loadImage();

			$("#lightWindow img:first").fadeOut("fast", imgFadeOut);
		}
	}
	
	function showNextImg(){
	
		if(m_imgLoad.isShown){
			var imgId = (m_options.id == m_options.totImgs) ? 1 : parseInt(m_options.id) + 1;
			setOptions({mediaType:"img", slideShow:true, totImgs:m_options.totImgs, src:getImgFilePath(imgId), id:imgId});
			setImgPosCount(imgId);

			loadImage();

			$("#lightWindow img:first").fadeOut("fast", imgFadeOut);
		}
	}
	
	function setImgPosCount(p_imgId){
	
		p_imgID = (arguments.length == 0) ? m_options.id : p_imgId;
		$("#slideShowControls .positionCount").text(p_imgID + " / " + m_options.totImgs);
	}
	
	function displayImage(){
	
		$("#lightWindow").empty();
		$("#lightWindow").append(m_imgLoad);
		$("#lightWindow img:first").fadeIn("fast", imgFadeIn);
		
		displayNavBar(m_imgLoad);
	}
	
	function loadIFrame(){
	
		if(jQuery.browser.msie){
			//
			//	Another Reason Why IE Sucks
			//	onload event from an iFrame element does not fire when assigned through js using DOM
			//	Solution/Hack: assign an onload event to the iframe "this.onloadHack", through DOM assign a function value to "onLoadHack" to call to a appropriate scoped function.
			//
			document.getElementById("lightWindow").innerHTML = '<iframe name="iFrameElement" id="iFrameElement" style="display:none;" width="100%" height="100%" frameBorder="0"></iframe>'
			
			$("#iFrameElement").load(function(){
				iFrameLoaded();
			});
			
			//document.getElementById("iFrameElement").onLoadHack = function(){ this.iFrameLoaded(); }.bind(this);
			document.getElementById("iFrameElement").src = m_options.src;
		}else{
			var iFrame = document.createElement("iframe");
				iFrame.src = this.m_options.src;
				iFrame.id = "iFrameElement"
				iFrame.style.scrolling = "yes";
				iFrame.onload = iFrameLoaded;
				
				iFrame.style.display = "none";
				iFrame.style.border = "none";
	
			document.getElementById("lightWindow").appendChild(iFrame);
		}
	}

	function iFrameLoaded(){
	
		m_startWidth = m_endWidth;
		m_startHeight = m_endHeight;
	
		var browserWidth = ( jQuery.browser.msie ) ? document.documentElement.clientWidth : window.innerWidth;
		var browserHeight = ( jQuery.browser.msie ) ? document.documentElement.clientHeight : window.innerHeight;
		
		// Open iFrame To Full Height Of Window Being Displayed		
		m_endWidth = (m_frameWidth != null) ? m_frameWidth : browserWidth - 50;
		m_endHeight = (m_frameHeight != null) ? m_frameHeight : browserHeight - 50;
		
		document.getElementById("iFrameElement").width = (m_endWidth - 0) + "px";
		document.getElementById("iFrameElement").height = (m_endHeight - 0) + "px";

		m_interval = setInterval(animateWidth, 40);
	}
	
	function displayIFrame(){
	
		var childNodes = document.getElementById("lightWindow").childNodes;
	
		for(var i = 0; i < childNodes.length; i++){
			if(childNodes[i].id != "iFrameElement"){
				document.getElementById("lightWindow").removeChild(childNodes[i]);
			}
		}
		
		document.getElementById("iFrameElement").style.display = "block";
		displayNavBar(document.getElementById("iFrameElement"));
	}
	
	function displayNavBar(p_element){

		if($("#lightClose").length == 0 && !m_options.slideShow){
			var divClose = document.createElement("div");
				divClose.id = "lightClose";

			var divCloseBody = document.createElement("div");
				divCloseBody.className = "body";

			var closeLink = document.createElement("a");
				closeLink.onclick = function(){
					closeWindow();
					return false;
				}

			var imgClose = document.createElement("img");
				imgClose.src = "/img/lightWindow/close.gif";
				imgClose.width = "93"
				imgClose.height = "27"

			closeLink.appendChild(imgClose);
			divCloseBody.appendChild(closeLink);
			
			divClose.appendChild(divCloseBody);	

			// Add 10 Pixels For "lightWrapper" Padding - 5px x 2 ( left:right)
			document.getElementById("lightContainer").style.width = (this.m_endWidth + 10) + "px"; 
			document.getElementById("lightContainer").insertBefore(divClose, document.getElementById("lightWrapper"));
		}
	}
	
	function getScrollXY() {
	
		var scrOfX = 0, scrOfY = 0;
		
		if(jQuery.browser.msie){
			scrOfY = document.documentElement.scrollTop;
			scrOfX = document.documentElement.scrollLeft;
		}
		else if(jQuery.browser.mozilla){
			scrOfY = window.pageYOffset;
			scrOfX = window.pageXOffset;
		}
		else{
			scrOfY = document.body.scrollTop;
			scrOfX = document.body.scrollLeft;
		}		
		return [ scrOfX, scrOfY ];
	}
	
	function animateWidth(){
	
		m_startWidth += 60;
				
		if(m_startWidth >= m_endWidth){		
			var browserWidth = ( jQuery.browser.msie ) ? document.getElementById("windowOverlay").clientWidth : document.getElementById("windowOverlay").offsetWidth;			
			document.getElementById("lightContainer").style.left = (Math.min((browserWidth - this.m_endWidth) / 2))  + "px";
			m_startWidth = m_endWidth;
		}else{
			document.getElementById("lightContainer").style.left = ($("#lightContainer").position().left  - Math.max(60 / 2) ) + "px";
		}
		
		m_startWidth = (m_startWidth >= m_endWidth) ? m_endWidth : m_startWidth;
				
		m_lightWindow.style.width = m_startWidth + "px";
		
		if(m_startWidth == m_endWidth){
			clearInterval(m_interval);
			m_interval = setInterval(animateHeight, 40);
		}
	}
	
	function animateHeight(){
	
		m_startHeight += 60;
		
		if(m_startHeight >= (m_endHeight - 60)){
			var browserHeight = ( jQuery.browser.msie ) ? document.documentElement.clientHeight : window.innerHeight;
			document.getElementById("lightContainer").style.top = Math.max(60, (Math.min((browserHeight - this.m_endHeight) / 2))) + "px";
			m_startHeight = m_endHeight;
		}else{
			document.getElementById("lightContainer").style.top = Math.max(60, (($("#lightContainer").position().top - getScrollXY()[1]) - (60 / 2) )) + "px";
		}
		
		m_lightWindow.style.height = m_startHeight + "px";
		
		if(m_startHeight == m_endHeight){
			clearInterval(m_interval);

			switch(m_options.mediaType){
				case "iFrame":
					displayIFrame();
					break;
				case "img":
					displayImage();
					break;
			}
		}
	}
	
	function animateLoadingWidth(){
				
		m_startWidth += 40;	
		m_startWidth = (m_startWidth >= m_endWidth) ? m_endWidth : m_startWidth;
		
		m_lightWindow.style.left = Math.round((m_endWidth / 2) - (m_startWidth / 2)) + "px";
		m_lightWindow.style.width = m_startWidth + "px";
		
		if(m_startWidth == m_endWidth){
			clearInterval(m_interval)
			m_interval = setInterval(animateLoadingHeight, 20);
		}
	}
	
	function animateLoadingHeight(){
	
		m_startHeight += 40;
		m_startHeight = (m_startHeight >= m_endHeight) ? m_endHeight : m_startHeight;
		
		m_lightWindow.style.top = Math.round((m_endHeight / 2) - (m_startHeight / 2)) + "px";
		m_lightWindow.style.height = m_startHeight + "px";
		
		if(m_startHeight == m_endHeight){
			clearInterval(m_interval)
			
			var loadingStatus = document.createElement("div")
				loadingStatus.style.padding = "10px";
				
			var loadingImg = document.createElement("img")
				loadingImg.src = "/img/lightWindow/ajax-loading.gif";
				loadingImg.style.marginRight = "5px";
				
				loadingStatus.appendChild(loadingImg)
				loadingStatus.appendChild(document.createTextNode("  Loading..."));
				
				document.getElementById("lightWindow").appendChild(loadingStatus)
			
			switch(m_options.mediaType){
				case "iFrame":
					setTimeout(loadIFrame, 500);
					break;
				case "img":
					setTimeout(loadImage, 500);
					break;
			}
		}
	}
	
	function closeWindow(){

		m_startWidth = 10;		// Default For Loading Status
		m_startHeight = 10;		// Default For Loading Status

		m_endWidth = 400;		// Default For Loading Status
		m_endHeight = 150;		// Default For Loading Status
				
		$("#windowOverlay").remove();
		$("#lightContainer").remove();
	
		if(m_options.slideShow){
			$("#slideShowControls").remove();
			$("#" + m_imgLoad.id.substring(3, m_imgLoad.id.length)).css("background", "none");
		}
		
		m_interval = null;
		m_imgLoad = null;		
		m_options = null;
	}
	
	function toInt(p_val){
		
		if(p_val.indexOf("px") != -1){
			return p_val.substring(0, p_val.length - 2);
		}else{
			return p_val;
		}
	}
	
	function getImgFilePath(p_val){
		return "img/" + $("#" + p_val).attr("src").substring(7, $("#" + p_val).attr("src").length)
	}
	
})(jQuery);

$(function() {
	var headID = document.getElementsByTagName("head")[0];         
	var cssNode = document.createElement('link');
		cssNode.type = 'text/css';
		cssNode.rel = 'stylesheet';
		cssNode.href = '/css/lightWindow/lightWindow.css';
		headID.appendChild(cssNode);
});