/*=============================================================================

			 	 TITLE:		NetMediaOne - Lightbox Widget
		  MODIFIED:		2008.07.09
		 AUTHOR(S): 	Graham Wheeler - NetMediaOne - www.netmediaone.com
		  REQUIRES:		jQuery 1.2
					INFO:		This script is derived from Cody Lindley's excellent "Thickbox"
									v3.1 script, and has been heavily modified as follows:
									-Widget now only provides functionality for images
									-Code structure and naming convention optimized for NMO
										library integration
									-Components are now created on window load instead of every
										time the image is changed, allowing for smooth transitions
										between images.

=============================================================================*/

NMO.Lightbox = {
	
	// Any elements found with this selector string will launch the viewer when clicked
	triggerElements: "a.ViewerLink, .ViewerLinkCollection a",
	imageRoot: NMO.rootPath + "design/",
	
	init: function() {
		
		if (typeof document.body.style.maxHeight === "undefined") {//if IE 6
			$("body","html").css({height: "100%", width: "100%"});
			$("html").css("overflow","hidden");
			if (document.getElementById("TB_HideSelect") === null) {//iframe to hide select elements in ie6
				$("body").append("<iframe id='TB_HideSelect'></iframe>\n");
			}
		}else{//all others
			if(document.getElementById("TB_overlay") === null){
				$("body").append("<div id='TB_overlay'></div>\n");
			}
		}

		$("body").append("<div id='TB_window'>\n<div id='TB_imgWrapper'><a href='' id='TB_ImageOff' title='Close'><img id='TB_Image' src='"+this.imageRoot+"blank.gif' alt='' width='640' height='480'></a></div>\n<div id='TB_caption'></div>\n<div id='TB_controls'>\n<span id='TB_prev'></span>&nbsp;&nbsp;<a href='#' id='TB_closeWindowButton' title='Close'><img src='"+this.imageRoot+"nmo_lightbox_close.gif' alt='Close Window'></a>&nbsp;&nbsp;<span id='TB_next'></span></div>\n</div>\n");
		$("#TB_closeWindowButton, #TB_overlay, #TB_ImageOff").click(NMO.Lightbox.hideWindow);
		
		if(navigator.userAgent.toLowerCase().indexOf('mac') != -1 && navigator.userAgent.toLowerCase().indexOf('firefox')!=-1){
			$("#TB_overlay").addClass("TB_overlayMacFFBGHack");//use png overlay so hide flash
		}else{
			$("#TB_overlay").addClass("TB_overlayBG");//use background and opacity
		}
		$("body").append("<div id='TB_load'><img src='"+this.imageRoot+"nmo_lightbox_loading.gif' alt='LOADING...'></div>\n");//add loader to the page
		$(NMO.Lightbox.triggerElements).click(function(){
			var t = this.title || this.name || null;
			var a = this.href || this.alt;
			var g = this.rel || false;
			NMO.Lightbox.showWindow();
			NMO.Lightbox.setImage(t,a,g);
			this.blur();
			return false;
		});
		
	},
	
	showWindow: function() {
		NMO.Lightbox.centerWindow(670,550);
		$('#TB_load,#TB_window, #TB_overlay').fadeIn("normal");//show loader
	},
	
	hideWindow: function() {
		$("#TB_load,#TB_window,#TB_overlay").fadeOut("normal");
		$("#TB_ImageOff").html("<img id='TB_Image' src='"+ this.imageRoot + "blank.gif' alt='' width='640' height='480'>");
		return false;
	},
	
	centerWindow: function(windowWidth, windowHeight) {
	$("#TB_window").css({marginLeft: '-' + parseInt((windowWidth / 2),10) + 'px', width: windowWidth + 'px'});
		if ( !(jQuery.browser.msie && jQuery.browser.version < 7)) { // take away IE6
			$("#TB_window").css({marginTop: '-' + parseInt((windowHeight / 2),10) + 'px'});
		}
	},
	
	getPageSize: function() {
		var de = document.documentElement;
		var w = window.innerWidth || self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
		var h = window.innerHeight || self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight;
		arrayPageSize = [w,h];
		return arrayPageSize;
	},
	
	setImage: function(caption, url, imageGroup) {
		
		try {
			$("#TB_load").show();
			$("#TB_imgWrapper").fadeTo("fast",0.01);
			if(caption===null){caption="";}

			var urlString = /\.jpg$|\.jpeg$|\.png$|\.gif$|\.bmp$/;
			var urlType = url.toLowerCase().match(urlString);
			
			TB_PrevCaption = "";
			TB_PrevURL = "";
			TB_PrevHTML = "";
			TB_NextCaption = "";
			TB_NextURL = "";
			TB_NextHTML = "";
			TB_imageCount = "";
			TB_FoundURL = false;
			
			if(imageGroup){
				TB_TempArray = $("a[rel="+imageGroup+"]").get();
				if ( TB_TempArray.length > 1 ) {
							
					TB_PrevHTML = "<img src='"+ this.imageRoot + "nmo_lightbox_prev_off.gif' alt='Previous Image'>";
					TB_NextHTML = "<img src='"+ this.imageRoot + "nmo_lightbox_next_off.gif' alt='Next Image'>";

					for (TB_Counter = 0; ((TB_Counter < TB_TempArray.length) && (TB_NextURL === "")); TB_Counter++) {
						if (!(TB_TempArray[TB_Counter].href == url)) {						
							if (TB_FoundURL) {
								TB_NextCaption = TB_TempArray[TB_Counter].title;
								TB_NextURL = TB_TempArray[TB_Counter].href;
								TB_NextHTML = "<a href='#'><img src='"+ this.imageRoot + "nmo_lightbox_next.gif' alt='Next Image'></a>";
							} else {
								TB_PrevCaption = TB_TempArray[TB_Counter].title;
								TB_PrevURL = TB_TempArray[TB_Counter].href;
								TB_PrevHTML = "<a href='#'><img src='"+ this.imageRoot + "nmo_lightbox_prev.gif' alt='Previous Image'></a>";
							}
						} else {
							TB_FoundURL = true;
							TB_imageCount = "Image " + (TB_Counter + 1) +" of "+ (TB_TempArray.length);											
						}
					}
				}
			}

			imgPreloader = new Image();
			imgPreloader.onload = function(){		
				imgPreloader.onload = null;
					
				// Resizing large images - orginal by Christian Montoya edited by me.
				var pagesize = NMO.Lightbox.getPageSize();
				var x = pagesize[0] - 150;
				var y = pagesize[1] - 150;
				var imageWidth = imgPreloader.width;
				var imageHeight = imgPreloader.height;
				if (imageWidth > x) {
					imageHeight = imageHeight * (x / imageWidth); 
					imageWidth = x; 
					if (imageHeight > y) { 
						imageWidth = imageWidth * (y / imageHeight); 
						imageHeight = y; 
					}
				} else if (imageHeight > y) { 
					imageWidth = imageWidth * (y / imageHeight); 
					imageHeight = y; 
					if (imageWidth > x) { 
						imageHeight = imageHeight * (x / imageWidth); 
						imageWidth = x;
					}
				}
				// End Resizing
				
				var TB_WIDTH = imageWidth + 30;
				var TB_HEIGHT = imageHeight + 60;
	
				$("#TB_prev").html(TB_PrevHTML).unbind("click");
				$("#TB_next").html(TB_NextHTML).unbind("click");
				
				if (!(TB_PrevURL === "")) {
					function goPrev() {
						if($(document).unbind("click",goPrev)){$(document).unbind("click",goPrev);}
						NMO.Lightbox.setImage(TB_PrevCaption, TB_PrevURL, imageGroup);
						return false;	
					}
					$("#TB_prev").click(goPrev);
				}
				
				if (!(TB_NextURL === "")) {		
					function goNext() {
						if($(document).unbind("click",goNext)){$(document).unbind("click",goNext);}
						NMO.Lightbox.setImage(TB_NextCaption, TB_NextURL, imageGroup);				
						return false;	
					}
					$("#TB_next").click(goNext);			
				}
				
				NMO.Lightbox.centerWindow(TB_WIDTH, TB_HEIGHT);
				$("#TB_load").hide();
				$("#TB_ImageOff").html("<img src='"+url+"' alt='"+caption+"' width='"+imageWidth+"' height='"+imageHeight+"' id='TB_Image'>");
				$("#TB_caption").html(caption);
				$("#TB_imgWrapper").fadeTo("fast",1.0);
				//$("#TB_window").css({display:"block"}); //for safari using css instead of show
			};
			imgPreloader.src = url;
			
		} catch(e) {
			//nothing here
		}
		
	}
	
};

$( function() { NMO.Lightbox.init(); } ); 
