		var currentPage = getAnchorTag();

      function previousImage(totalImages){

          if( currentPage > 1 ){
              //If we're not on the first page, subtract one, to the previous page
              prevPage = currentPage - 1;
          }
          else{ //Use last images
              prevPage = totalImages;
          }
    
          refresh(photoData[prevPage][0], photoData[prevPage][2], photoData[prevPage][3], prevPage, totalImages , photoData[prevPage][4]);
      }

      function nextImage(totalImages){

          currentPage = parseInt(currentPage);

          //If on the last page, next should take us to the first image
          if(currentPage == totalImages){
              var nextPage = 1;
          }
          else{
              var nextPage = currentPage + 1;
          }

          refresh(photoData[nextPage][0], photoData[nextPage][1], photoData[nextPage][2], nextPage, totalImages, photoData[nextPage][3]);
				
      }

      // Refresh image, ads, and wallpaper
      function refresh(imageURL, imageTitle, imageDescription, index, numImages, largeURL){
    
          // Change Image, Title, Description
          $('#gallery_image').attr("src", imageURL);
          $('#enlarge').attr("href", largeURL);
			 $('#gallery_image').attr("alt", imageTitle);
			 $('#gallery_image').attr("title", imageTitle);
          $('#image-title').html(imageTitle);
          $('#image-description').html(imageDescription);
          
          h_tile = 0; // Reset h_tile
          h_ord = Math.random()*10000000000000000; // New h_ord
          
          // refresh ads
          CampaignManager.look();
/*
          reloadAd('leaderboard', h_ord);
          reloadAd('skyscraper', h_ord);
          reloadAd('medrec1', h_ord);
          reloadAd('medrec2', h_ord);
*/
          h_tile=0;
          $(adArray).each(function(index){
              switch(adArray[index].substring(0,1)){
                  case 'l':
                      w = 728;
                      h = 90;
                      adType ='leaderboard';
                      break;
                  case 'm':
                      w = 300;
                      h = 250;
                      adType ='medrec';
                      break;
                  case 's':
                      w = 160;
                      h = 600;
                      adType ='skyscraper';
                      break;
              }
              reloadAd(adArray[index], adType, h_ord, w, h);
          })
    
          // Change URL
          setAnchorTag(index);
          prevPage = currentPage;
          currentPage = index;
    
          // Image 1 of totalImage
          $('#curImage').html(currentPage + ' ');
          
      }

		/*
      // Method to reload ads
      function reloadAd(adSize, h_ord){
          h_tile+=1;
          frameId='frame-'+adSize;
          document.getElementById(frameId).src = 'http://www.0-60mag.com/js/ads/ad-tags.php?h_ord='+h_ord+'&size='+adSize+'&section='+section+'&post='+post+'&h_tile='+h_tile;
          return true;
      }
		*/
      function reloadAd(adSize,adType, h_ord, w, h){
          ////////////////////////
          // Method to reload ads
          ////////////////////////
          
      	h_tile+=1;
      	frameId='frame-'+adSize;

      	if(!document.getElementById(frameId)){
      			var adFrame = $("<iframe />")
      			var adSrc   = '/js/ads/ad-tags.php?h_ord='+h_ord+'&size='+adType+'&section='+section+'&post='+post+'&h_tile='+h_tile;
       			adFrame.attr("src",adSrc);
      			adFrame.attr("scrolling","no");
      			adFrame.attr("width",w);
      			adFrame.attr("height",h);
      			adFrame.attr("frameborder",0)
      			var adDiv = $("#"+adSize);
      			adDiv.empty();
      			adDiv.append(adFrame);
      	}else{
      		document.getElementById(frameId).src = '/js/ads/ad-tags.php?h_ord='+h_ord+'&size='+adType+'&section='+section+'&post='+post+'&h_tile='+h_tile;
      	}
      return true;
      }

      // Track refresh in Google Analytics
      function track(url){
          pageTracker._trackPageview(url + '#' + currentPage); 
      }

      function getAnchorTag(){
          var re = /#[0-9]+$/; 
          var match = re.exec(document.location.toString());
          anchor = 1;
          if (match != null) var anchor = match[0].substr(1);
          return anchor;
      }

      //Add anchor tag to URL:
      function setAnchorTag(anchorTag){
          var url = document.location.toString();
          //Remove any existing hash sign from URL (if present)
          var re = /#[0-9]+$/;
          url = url.replace(re, '');
          //Add anchor tag to URL:
          document.location = url + '#' + anchorTag;
      }

		
		$(function(){ 
			adArray = [];
	     	$(".ad-slot").each(function(){
	     		adArray.push($(this).attr("id"))
	     	})
	     	console.log(adArray);

          var numPhotos = $(photoData).size();
          //account for index 0
          numPhotos -=1;
			 var uri = document.location.pathname;
          var photoLinks = ''
          photoLinks +='<a id="prevLinkTop" class="back" onclick="previousImage(' + numPhotos + ');track(\''+uri+'\');return false;">\n';
          photoLinks +='<img id="prevImg" src="/images/btns/left-arrow.gif" alt="Previous" /></a>\n';
          photoLinks +='<span id="imageCount" class="inline-block"><span id="curImage">' + currentPage + ' </span>of ' + numPhotos + '</span>\n';
          photoLinks +='<a id="nextButtonTop" class="next" onclick="nextImage(' + numPhotos + ');track(\''+uri+'\');return false;">\n';
          photoLinks +='<img id="nextImg" src="/images/btns/right-arrow.gif" alt="next" /></a>\n';
          $('#prev-next').html(photoLinks);
   
          // Display Main Image
          anchorTag = getAnchorTag();
          if(anchorTag){
              currentPage = anchorTag;
              currentPhoto = photoData[anchorTag];
          }else{
              currentPhoto = photoData[1];
          }
                       
          //Display Photo, Caption, and Description
			  $('#main-image').html('<a id="mainImageLink" target="_blank" onclick="nextImage('+numPhotos+'); return false;"><img id="gallery_image" src="' + currentPhoto[0] + '"' + ' title="'+currentPhoto[1]+'"'+' alt="' + currentPhoto[1] + '"></a>');
	        $('#image-title').html(photoData[currentPage][1]);
	        $('#image-description').html(photoData[currentPage][2]);
			  $('#enlarge').attr("href", currentPhoto[3]);
			})	
