(function($){
	$.fn.numoshow = function(options){
		var defaults = {
			thumbHldWth: 0, // width of thumb and info panel
			thumbNoPerRow: 3,
			InfoContainerHeight: 60,
			gallery: 'one',
			modal: '.5',
			overlayBgColor: '#000',
			overlayOpacity: 0.9,
			containerPadding: 20,
			galleryLink: '#gal-link',
			pageID: '#gallery-thumbs',
			imageArray: [],
			imageThumbArray: [],
			imageTextArray: []
			}, settings = $.extend({}, defaults, options);

			var jQueryMatchedObj = this;

			function init(){
				//idx = $(defaults.pageID+'.active').prevAll().length;
				idxa = $('.product-img-box .newmanGal.active').attr('rel');
				idx = idxa - 1;
				
					
				gal_url = $(defaults.galleryLink).attr('href');


				getData(gal_url, jQueryMatchedObj, idx);


				$('a.prev-numo, a.next-numo').live('click', function(c){
					var $activeThumb = $("#numoThumbContainer a.active");
					var total = $("#numoThumbContainer a").length;
					var index = $activeThumb.prevAll().length;
					$(this).attr('href') === '#prev' ?  index-- : index++;
					if (index === -1) { index = total - 1; }
					if (index === total) { index = 0 }
					$activeThumb.removeClass('active');
					$('#numoThumbContainer a:eq(' + index + ')').addClass('active');
					var newImage = $('#numoThumbContainer a.active').attr('href');
					// updateImageText(newImage);
					loadNewImage(newImage);
					c.preventDefault();
				});
				
				
				$('#numoThumbContainer a').live('click', function(c){
				if ($(this).attr('href') != $('div#target img').attr('src')) {
				loadNewImage($(this).attr('href'));
				updateImageText($(this).attr('href'));
				}
				c.preventDefault();
				});
	


				return false;
			}

			function getData(url, idx){
				var productTitle = "";
				/*defaults.imageArray.length = 0;
				defaults.imageThumbArray.length = 0;*/
				defaults.imageTextArray.length = 0;
				defaults.activeImage = idx;
				imageNum = 0;

				$.getJSON(
					url,
					function(data){
						$.each(data.imagedata, function(i,images){
							defaults.imageTextArray.push(new Array(images.labels, images.description, images.thumb, images.image));
						});
						setUp(idx);
					});
				}
				
				
				

				function setUp(){
					
					/* 
					
					if (typeof document.body.style.maxHeight === "undefined") { //if IE 6
						// $("body","html").css({height: "100%", width: "100%",position: "relative"});
					}

					var numoHtml =  '<div id="numoOverlay" class="loading"></div>'
					numoHtml +=  '<div id="numoContainer" style="display:none;"><a id="numoShowClose" href="#close"><span>Close</span></a><div id="numoInfoHeading"></div><div id="numoInfoContainer"></div>'
					numoHtml +=  '<div id="target"><a class="icn_s prev" href="#prev"><span id="prev-inside">Previous</span></a><a class="icn_s next" href="#next"><span id="next-inside">Next</span></a></div>'
					numoHtml +=  '<div id="numoThumbContainer"></div>';
					numoHtml +=  '</div>';

					$('body').append(numoHtml);
					$('#numoContainer').css({'position': 'fixed'});
					$('#target').css({'float': 'none'});
					$('#numoThumbContainer').css({'position': 'relative'});
					
					
					*/

					// handle ie6
					
					/* 
					
					if ($.browser.msie && $.browser.version <= 6.0) {
						var overlayHeight = $(document).height();
						var overlayWidth = $(window).width();
						$('#numoOverlay').css({'position': 'absolute', 'width': '100%', 'height': overlayHeight });
						$('#numoContainer').css({ 'position': 'absolute' });

						var $scrollingDiv = $("#numoContainer");

						$(window).scroll(function()
						{
							$scrollingDiv.stop().animate({"marginTop": ($(window).scrollTop()) + "px"}, "fast" );
						});
					} 
					
					*/
					
			if (typeof document.body.style.maxHeight === "undefined") { //if IE 6
				$("body","html").css({height: "100%", width: "100%",position: "relative"});
			}
			
			
			if ($.browser.msie && $.browser.version <= 6.0) {
				$('body').append('<div id="numoOverlay" class="loading"></div>');
				$('body').append('<div id="ieWrapper"><div id="numoContainer" style="display:none;"><a id="numoShowClose" href="#close"><span>Close</span></a><div id="numoInfoHeading"></div><div id="numoInfoContainer"></div><div id="target"><a class="icn_s prev-numo" href="#prev"><span id="prev-inside">Previous</span></a><a class="icn_s next-numo" href="#next"><span id="next-inside">Next</span></a></div><div id="numoThumbContainer"></div></div></div>').show();
            } else {
				$('body').append('<div id="numoOverlay" class="loading"></div>');
				$('body').append('<div id="numoContainer" style="display:none;"><a id="numoShowClose" href="#close"><span>Close</span></a><div id="numoInfoHeading"></div><div id="numoInfoContainer"></div><div id="target"><a class="icn_s prev-numo" href="#prev"><span id="prev-inside">Previous</span></a><a class="icn_s next-numo" href="#next"><span id="next-inside">Next</span></a></div><div id="numoThumbContainer"></div></div>').show();
						
				$('#numoContentWrapper').css({
					'position': 'fixed'
				});
					
				$('#numoContainer').css({
					'position': 'fixed',
					'margin-top': (-$('#numoContainer').width() / 2) + 'px',
					'margin-left': (-$('#numoContainer').height() / 2) + 'px'
				});
	
				$('#target').css({
					'float': 'none'
				});
	
				$('#numoThumbContainer').css({
					'position': 'relative'
				});
			
			}

            if ($.browser.msie && $.browser.version <= 6.0) {
                var overlayHeight = $(window).height();
                var overlayWidth = $(window).width();
                $('#numoOverlay, #ieWrapper').css({
                    'position': 'absolute',
                    'width': '100%',
                    'height': $(document).height(),
					'z-index': '900',
					'top':($(window).scrollTop()) + "px",
					'left':'0'
                });
				
				$('#ieWrapper').css({
                    'position': 'absolute',
                    'width': '100%',
					'background':'transparent',
                    'height': overlayHeight,
					'z-index': '900',
					'top':($(window).scrollTop()) + "px",
					'left':'0'
                });
							
				$('#numoContainer').css({
	                'position': 'relative',
					'top' : '10px',
					'left' : '10px',
					'margin-top' : '10px',
					'margin' : '0 auto'
	            });
				
            
			// alert(($(window).scrollTop()) + "px");
			
				var $scrollingDiv = $("#ieWrapper");
				var $scrollingWrapper = $("#numoOverlay");
	
				$(window).scroll(function()
				{
					$scrollingWrapper.stop().animate({"marginTop": ($(window).scrollTop()) + "px"}, "fast" );
					$scrollingDiv.stop().animate({"marginTop": ($(window).scrollTop()) + "px"}, "fast" );
				});
			
			} 
			
			
			
			

					$('#numoOverlay').fadeTo("slow", settings.overlayOpacity).show();

					var imageArraySize = defaults.imageTextArray.length;

					//loop through imageThumbArray and name some vars related to the thumbnail
					for (i = 0; i < imageArraySize; i++) {
						var thumbImageTxt = defaults.imageTextArray[i][0];
						var thumbImageDisc = defaults.imageTextArray[i][1];
						var thumbImageURL = defaults.imageTextArray[i][2];
						var thumbImageSRC = defaults.imageTextArray[i][3];

						if(imageArraySize > 1){
							makethumb(thumbImageTxt, thumbImageDisc, thumbImageURL, thumbImageSRC, thumbImageSRC);
						}


					};

					loadNewImage(defaults.imageTextArray[idx][3], imageArraySize);

					$('#numoThumbContainer a:eq(' + idx + ')').addClass('active');
					$('#numoInfoHeading').append('<p class="title">' + defaults.imageTextArray[idx][0] + '</p>');
					/*$('#numoInfoContainer').append('<p class="text">' + defaults.imageTextArray[idx][0] + '</p>');*/
				} 

				function makethumb(thumbImageTxt, thumbImageDisc, thumbImageURL, thumbImageSRC, clickedURL){
					var img = new Image();
					var numoThumbLink = document.createElement('a');

					$(numoThumbLink).attr("href", thumbImageSRC).attr("title", thumbImageTxt+' - '+thumbImageDisc).attr('class', 'loading');
					$('#thumbContainer').append(numoThumbLink);

					if ((i + 1) / defaults.thumbNoPerRow == 1)
					$(numoThumbLink).addClass('last');

					$('#numoThumbContainer').append(numoThumbLink);

					$(img).load(function(){
						$(this).css('display', 'none'); 
						$(numoThumbLink).removeClass('loading').append(this);
						$(this).fadeIn();
					}).error(function(){
						alert('error');
						}).attr('src', thumbImageURL).show();

						$(numoThumbLink).removeClass('loading');
					}

					// resize the container to match image width...
					function resizeContainer(containerWidth, maxImageHeight, imageHeight){
					
						if ($.browser.msie && $.browser.version <= 6.0) {
						 	$('#numoContainer').css({'margin-top':10 +'px'});
							$('#numoContainer').animate({ 'width': containerWidth + 'px', 'height': (imageHeight + 90) + 'px' }, 'slow', function() { });   
							
						} else {
							$('#numoContainer').css({'margin-top': ((-imageHeight / 2) - 60)   + 'px','margin-left': ((-containerWidth / 2) - 60) + 'px'});
							$('#numoContainer').animate({ 'width': containerWidth + 'px', 'height': (imageHeight + 90) + 'px' }, 'slow', function() { });   					
						}
						
						$('#numoOverlay').removeClass('loading'); 
						$('#numoContainer').fadeIn();
					}

					function openThumbLink(imgSrc){
						/*$('#target').children().fadeOut('fast', function(){
							loadNewImage(imgSrc); 
						});*/
						
						loadNewImage(imgSrc);
					}

					function loadNewImage(imgSrc, imageArraySize){

						//	alert(defaults.imageTextArray);
						$('#target').children('div').remove();
						$('#target').addClass('loading')


						// create the image holder
						var imageholder = $('<div id="targetImage" class="src_image"></div>').attr('class', 'loading');
						$('#target').append(imageholder);
						var currLoad = $("div#targetImage");

						// new image

						var img = new Image();
						$(img).load(function(){
							$(this).css('display', 'none');
							$(currLoad).removeClass('loading').append(this);

							var maxImageWidth = ($(window).width() - parseInt($('#numoContainer').css('padding-left'), 10) - parseInt($('#numoContainer').css('padding-right'), 10)) - 150;
							var maxImageHeight = (($(window).height() > window.innerHeight ? window.innerHeight : $(window).height()) - parseInt($('#numoContainer').css('padding-top'), 10) - parseInt($('#numoContainer').css('padding-bottom'), 10)) - 150;


							resize_image(img,maxImageWidth,maxImageHeight);						

							$('#target').animate({'height': img.height + 'px', 'width': img.width + 'px'}, 'fast', function() {
								// stuff to do after animation is complete
								$('#target').css({'overflow': 'hidden'});
								$(currLoad).css({'width': img.width + 'px', 'height': img.height + 'px', 'overflow': 'hidden'});

								if(imageArraySize > 1){
									
									$('.next-numo').css({ 'width': 40 + 'px', 'height': 40 + 'px', 'right': 5 + 'px', 'top': (img.height/2) + 'px' }).fadeIn('slow');
									$('.prev-numo').css({ 'width': 40 + 'px', 'height': 40 + 'px', 'left': 5 + 'px', 'top': (img.height/2) + 'px' }).fadeIn('slow');
									
								}
								
								
								
								
								$('#target img').fadeIn(2000);
							});

							updateImageText(imgSrc)
							defaults.imageArray.push(new Array(img.height));

							//	alert(img.height);

							//$(this).fadeIn('slow');
						}).error(function(){
							// NEED NEW ERROR MESSAGE
							alert('There has been a problem loading images. Please try again later');
							}).attr('src', imgSrc);
						}

						function resize_image(img, w, h) {
							if (img.width > w || img.height > h) {
								var ratio = img.width / img.height;
								if( img.height >= h ) {
									img.height = h;
									img.width = h * ratio;
								} else {
									img.width = w;
									img.height = ((w / ratio)) ;
								}
							}
							var containerWidth = img.width;
							var imageHeight = img.height;
							resizeContainer(containerWidth, imageHeight, imageHeight);
							return;
						}

						function updateImageText(imgSrc){
							$('#numoThumbContainer a').filter('.active').removeClass('active');
							$('#numoThumbContainer a').each(function(){
								if (imgSrc == $(this).attr("href")) {
									var imageText = $(this).attr("title");
									$('p.title').text(imageText).fadeTo("slow", 1);
									$(this).addClass('active')
								}
							});
						}

						function finish(){
							$('#numoInfoHeading').text('');
							$('#numoThumbContainer').children().remove();
							$('#numoInfoContainer').children().remove();
							$('#target').children().remove();
							$('#target').replaceWith('');
							$('#numoContainer').replaceWith('');
							
							
							if ($.browser.msie && $.browser.version <= 6.0) {
								$('#ieWrapper').remove();
							}
							
							
							$('#numoOverlay').fadeOut(function(slow){
								$('#numoOverlay').remove();
							});

							return null;
						}

						$('#numoShowClose, #numoOverlay').live('click', function(c){
							finish();
							c.preventDefault();
						});

						$('#numoThumbContainer a').live('click', function(c){
							c.preventDefault();
							if ($(this).attr('href') != $('div#target img').attr('src')) {
								openThumbLink($(this).attr('href'));
								updateImageText($(this).attr('href'));
							}
						});
						
						
					
						//Press Escape event!  
						$(document).keypress(function(e){  
							if(e.keyCode==27){  
								finish();  
							}
						});

						return this.unbind('click').click(init);

					}



					})(jQuery);

