if (Drupal.jsEnabled) {
   Lightbox.fileLoadingImage = '/homes/sites/all/themes/custom/localleaders/images/ajax-loading-cancel.gif'; //='/homes'+Lightbox.fileLoadingImage;
   Lightbox.fileBottomNavCloseImage = '/homes'+Lightbox.fileBottomNavCloseImage;
  
   Lightbox.initialize = function() {
		Lightbox.updateImageList();
		
		// Attribute galleryimg="false" hides IE image toolbar.
		$("body").append('<div id="overlay"></div> \
			<div id="lightbox"> \
				<div id="outerImageContainer"> \
				  <div id="lightwindow_title_bar"> \
    	            <div id="tbar-nav"> \
    	              <a id="tbar-prev" href="#">&lt;prev</a>&nbsp;&nbsp;&nbsp;<span id="numberDisplay"></span>&nbsp;&nbsp;&nbsp;<a id="tbar-next" href="#">next&gt;</a> \
    	            </div> \
   	                <div id="lightwindow_title_bar_inner"> \
        	          <span id="lightwindow_title_bar_title"></span>\
            	    </div> \
                    <div id="closebox"><a id="lightwindow_title_bar_close_link">[x] close</a></div> \
              	  </div> \
				  <div id="imageContainer"> \
					  <img id="lightboxImage" galleryimg="false" /> \
					  <div style="" id="hoverNav"> \
						<a href="#" id="prevLink"></a> \
						<a href="#" id="nextLink"></a> \
					  </div> \
					  <div id="loading"> \
						<a href="#" id="loadingLink"> \
							<img src="'+ Lightbox.fileLoadingImage +'" /> \
						</a> \
					  </div> \
				  </div> \
				</div> \
				<div id="imageDataContainer"> \
					<div id="imageData"> \
						<div id="imageDetails"> \
						    <div id="ownerInfoContainer" ></div> \
							<span id="caption"></span> \
						</div> \
					</div> \
				</div> \
			</div>');
		$('#overlay').click(function(){ Lightbox.end(); return false; });
		$('#loadingLink').click(function(){ Lightbox.end(); return false; });
		$('#lightwindow_title_bar_close_link').click(function(){ Lightbox.end(); return false; });
		$('#outerImageContainer').width(250).height(250);

		// Add padding for navigation links. 18/09/2007 sun
		$('#prevLink').css({ paddingTop: Lightbox.borderSize, paddingLeft: Lightbox.borderSize });
		$('#nextLink').css({ paddingTop: Lightbox.borderSize, paddingRight: Lightbox.borderSize });
		
		// Setup onclick handlers for previous and next buttons ONCE.
		// Lightbox wacks out if we reset those in updateNav(). 13/09/2007 sun
		$('#prevLink, #tbar-prev').click(function() {
		        $('#tbar-prev, #tbar-next, #closebox').css( { visibility: 'hidden'} );
				Lightbox.changeImage(Lightbox.activeImage - 1); return false;
		});
		$('#nextLink, #tbar-next').click(function() {
		        $('#tbar-prev, #tbar-next, #closebox, #numberDisplay').css( { visibility: 'hidden'} );
				Lightbox.changeImage(Lightbox.activeImage + 1); return false;
		});
		$('#tbar-prev, #tbar-next').css( { visibility: 'hidden' } );
		if( $.browser.safari ) {
		   $('#overlay, #lightbox').fadeOut(100);
	    } else {
		  $('#overlay, #lightbox').hide();
		}
	};
	
	 function get_encoded_owner_link( str )
	 {
	   pos = str.indexOf( "--/" );
	   if( pos > 0 ) {
	       haveUrl = true;
	   } else {
	      pos = str.indexOf( "--http:" );
	      if( pos > 0 ) {
	          haveUrl = true;
		   } else {
	           pos = str.indexOf( "--]" );
	           haveUrl = false;
	       }
       }
       if( pos < 0 ) { return ""; } // no encoded info
       
       company_name = str.substring( str.lastIndexOf(' [') + 2, pos );
       if( haveUrl ) {
           url = str.substring( pos+2, str.lastIndexOf(']') );
           retval = '<a href="'+url+'">'+company_name+'</a>';
       } else {
           retval = company_name;
       }
       return retval;
    }

    function strip_encoded_owner_link( str )
    {
       return str.substring( 0, str.lastIndexOf( ' [' ) );
    }
	



    if( $.browser.safari ) { /* fix a safari specific bug */
	   	Lightbox.changeImage = function(imageNum) {	
			// update global var
			Lightbox.activeImage = imageNum;
	
			// hide elements during transition
			$('#loading').show();
			$('#lightboxImage, #hoverNav, #imageDataContainer, #numberDisplay').hide();
			
			imgPreloader = new Image();
			
			// once image is preloaded, resize image container
			imgPreloader.onload=function(){
	
				$('#lightboxImage').attr('src', Lightbox.imageArray[Lightbox.activeImage][0]);
				//Lightbox.resizeImageContainer(this.width, this.height);
				Lightbox.resizeImageContainer(imgPreloader.width, imgPreloader.height);
				
				// clear onLoad, IE behaves irratically with animated gifs otherwise 
				if($.browser.msie) imgPreloader.onload=function(){};
			};
			imgPreloader.src = Lightbox.imageArray[Lightbox.activeImage][0];
		};
	};
	
		//
	//	updateDetails()
	//	Display caption, image number, and bottom nav.
	//
	Lightbox.updateDetails = function() {
		// if caption is not null
		if(Lightbox.imageArray[Lightbox.activeImage][1]){
		    captiontxt = Lightbox.imageArray[Lightbox.activeImage][1];
		    if( captiontxt && captiontxt.length > 0 )
		    {
			    ownerlink = get_encoded_owner_link( captiontxt );
			    if( ownerlink.length > 0 )
			    {
			        captiontxt = strip_encoded_owner_link( captiontxt );
			        $('#ownerInfoContainer').html( 'by '+ownerlink);
			    } else {
			        $('#ownerInfoContainer').html('');
			    }
				$('#caption').html(captiontxt).show();
			}
		}
		
		// if image is part of set display 'Image x of x' 
		if(Lightbox.imageArray.length > 1){
			$('#numberDisplay').html("Image " +eval(parseInt(Lightbox.activeImage) + 1) + " of " + Lightbox.imageArray.length).css({visibility: 'visible'}).show();
		}

		$('#closebox').css( {visibility: 'visible'} ).show();

		$("#imageDataContainer").slideDown(Lightbox.resizeSpeed, function() {
			// Usability optimization: Display image navigation first.
			$('#hoverNav').show();				
			Lightbox.updateNav();
//			$('#tbar-prev, #tbar-next').show();
			// if not first image in set, display prev image button
		if(Lightbox.activeImage != 0) {
			$('#tbar-prev').css( { visibility : 'visible' } );
		}
		else {
			$('#tbar-prev').css( { visibility : 'hidden' } );
		}
		// if not last image in set, display next image button
		if(Lightbox.activeImage != (Lightbox.imageArray.length - 1)) {
			$('#tbar-next').css( { visibility : 'visible' } );
		}
		else {
			$('#tbar-next').css( { visibility : 'hidden' } );
		}

			// update overlay size and update nav
			var arrayPageSize = Lightbox.getPageSize();
			$('#overlay').height(arrayPageSize[1]);

			Lightbox.preloadNeighborImages();
			Lightbox.enableKeyboardNav();
		});
	};
	
	//
	//	end()
	//
	Lightbox.end = function() {
		// Try to prevent multiple fadeouts on double-click.
		// $('#overlay, #lightbox, #loadingLink').unbind('click');
		
		Lightbox.disableKeyboardNav();
		$('#tbar-prev, #tbar-next').css( { visibility: 'hidden' } );
		$('#lightbox, #lightboxImage').hide();
		$("#overlay").fadeOut(Lightbox.overlaySpeed);
		$("select, object, embed").show();
	};
	
	
}

