var Gallery = new Class({
  
  initialize: function() {
    this.amountOfClicks = 0;
    this.adShowInterval = 5;      // How many clicks before showing an ad
    this.adDomId = 'ad300x250b';  // The DOM ID of the interstitial ad
    this.showingAd = false;       // Are we showing an ad right now?
    this.adExists = false;
    this.imageIndex = 0;
    this.imageArray = Array();
    this.magicNumbers = Array();
    this.nextGallery = '';
    this.prevGallery = '';
    this.ads = [['topad', 728, 90],
                ['middle-ad', 300, 250],
                ['right-ad', 160, 600]];
    this.title = "";
    //
    this.displayInterstitialAds = true;
    this.adzone = 'photo_player';
  },
  
  addImage: function(id, src, mn, description, credit, title) {
    this.imageArray.push([id, src, mn, description, credit, title]);
  },
  
  switchImage: function(idx) {
    this.imageIndex = idx;
    var img = this.imageArray[idx];
    
    // Switch all DOM elements that need it
    $E('img', 'full_image').src = img[1];
    $('image_count').setHTML((idx + 1) + "/" + this.imageArray.length);
    //$('full_image_title').setHTML(img[5]);
    $('full_image_description').setHTML(img[3]);
    //$('full_image_credit').setHTML(img[4]);
      
    // Hide the ad if we're showing it
    if(this.showingAd) this.hideAd();

    // record omniture stat unless we're switching via deep-link
    if(this.justDeepLinked) {
      this.justDeepLinked = false;
      this.amountOfClicks--;
    } else {
      s.pageName = this.title + " - " + img[5] + " - " + img[0] + " page";
      var s_code=s.t();
    }
    
    // reset all .prev-galleries to just be 'prev'
    $$('.prev').setHTML('&laquo; Previous');
    $$('.next').setHTML('Next &raquo;');
    
    
    // If we're at the first image, we need to display the prev-gallery button
    if(this.imageIndex == 0) {
      $$('.prev').setHTML('&laquo; Previous Gallery');
    }
    // If we're at the last image, we need to display the next-gallery button
    if(this.imageIndex >= this.imageArray.length-1) {
      $$('.next').setHTML('Next Gallery &raquo;');
    }
    
    // popup external links in description field
    externalizeExternalLinks();
    
    // Deep linking
    document.location.href = document.location.href.split("#")[0] + "#" + (img[0]);
  },
  
  shouldDisplayAd: function() {
    if(this.displayInterstitialAds == false) return false;
    this.amountOfClicks++;
    return (this.amountOfClicks % this.adShowInterval) == 0;
  },

  
  // We display the ad by turning on the wrapper (black background) and then loading iframeAd.php with the appropriate values
  displayAd: function() {
    this.showingAd = true;
    $('interstitial_ad_wrapper').setStyle("display", "block");
    $('outer_image_wrapper').setStyle("z-index", "10005");
    $('image_count').setHTML("");
    $('full_image_description').setHTML("");
  //  var ad300HTML = '<div id="ad-top">advertisement</div><iframe src="'+adsurl+'iframeAd.php?adsize=300x250&adzone='+adzone+'&ord='+ord+'&site='+adsite+'&tile=1" width="300" height="250" id="ad300x250fb" frameborder="0" scrolling="no"></iframe>';
    var ad300HTML = '<div id="ad-top">advertisement</div><iframe src="http://www.lopeztonight.com/ads/300x250.php" width="300" height="250" id="ad300x250fb" frameborder="0" scrolling="no"></iframe>';

    $('ad300x250b').innerHTML = ad300HTML;
  },

  // We hide the ad by hiding the wrapper div
  hideAd: function() {
    $('interstitial_ad_wrapper').setStyle("display", "none");
    $('outer_image_wrapper').setStyle("z-index", "90");
    this.showingAd = false;
  },
  
  refreshAd: function() {
    var ad = document.getElementById("ad300x250f");
    if(ad) {
      ad.src=adsurl+'iframeAd.php?adsize=300x250&adzone='+adzone+'&ord='+ord+'&site='+adsite+'&tile=2&segQS='+segQS;
    }   
  },
  
  recordHit: function(id) {
    new Ajax('/record_image_hit.html?id=' + id).request();
  },
  
  next: function() {
    if(this.shouldDisplayAd()) {
      this.displayAd();
    } else {
      this.imageIndex += 1;
      // If we're on the last image, load the previous gallery
      if (this.imageIndex >= this.imageArray.length) {
        window.location = '/galleries/' + this.nextGallery;
        return true;
      }
      this.switchImage(this.imageIndex);
    }
  },
  
  previous: function() {
    if(this.shouldDisplayAd()) {
      this.displayAd();
    } else {
      this.imageIndex -= 1;
      // If we were already at the first image, load the previous gallery
      if (this.imageIndex < 0) {
        window.location = '/galleries/' + this.prevGallery;
        return true;
      }
      this.switchImage(this.imageIndex);
    }
  },

  getIndexByID: function(image_id) {
    for(var i = 0; i < this.imageArray.length; i++) {
      if(this.imageArray[i][0] == image_id) {
        return i;
      }
    }
    return 0;
  },

  gotoImageByID: function(image_id) {
    var idx = this.getIndexByID(image_id);
    if(this.imageIndex != idx) {
      if(this.shouldDisplayAd()) {
        this.displayAd();
      } else {
        this.switchImage(idx);
      }
    }
  },

  gotoImageByLocation: function() {
    if(id = document.location.href.split("#")[1]) {
      this.justDeepLinked = true;
      this.gotoImageByID(id);
    }
  }
  
});

var g = new Gallery();

function flagComment(commentId, domId, url) {
  var commentAjax = new Ajax(url, {method: 'get', update: $(domId)});
  commentAjax.request();
}

function anyPopup(url,h,w){window.open(url,"TMZ","scrollbars=yes,location=no,directories=no,height="+h+",width="+w+",menubar=no");}

function validateComment() {
  var errors = "";
  var emailRegex = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
  var linkRegex = /(http:\/\/|https:\/\/|www\.|\.com|\.net|\.org|\.edu|\.gov|\.php|\.asp|\.htm|\.html)/;
  
  if( $('comment_name').getProperty('value') == "" || $('comment_name').getProperty('value') == "YOUR NAME" ) {
    errors += "You must provide a name.<br/>";
  }
  if( !emailRegex.test( $('comment_email').getProperty('value') ) ) {
    errors += "You must provide a valid email address.<br/>";
  }
  if( $('comment_body').getProperty('value') == "" || $('comment_body').getProperty('value') == "TYPE YOUR MESSAGE HERE" ) {
    errors += "You must enter a comment.<br/>";
  }
  if( linkRegex.test( $('comment_name').getProperty('value') ) || linkRegex.test( $('comment_body').getProperty('value') ) ) {
    errors += "Your name or comment cannot contain links.<br/>";
  }
  if(errors != "") {
    $('comment_errors').setHTML("<strong>There were some errors:</strong><br/>" + errors);
    return false;
  }
  return true;
}

function externalizeExternalLinks() {
  var domainRegex = /^(?:[^\/]+:\/\/)?(.*\.(\w*\.\w*(:\d*)?)+)/;
  var domain = document.location.href.match(domainRegex);
  if(domain) {
    domain = domain[2]
    $('full_image_description').getElements('a').each(function(el) {
      var link_domain = el.href.match(domainRegex)[2];
      if(link_domain != domain) {
        // external
        $(el).setProperty('target', '_blank');
      }
    });
  }
}
// leftover function from wb
function place300x250(zone,site,ord)
{
var ad300HTML = '<div id="ad-top">advertisement</div><iframe src="'+siteurl+'ads/300x250refresh.php?adzone='+zone+'&ord='+ord+'&site='+site+'&tile=2" width="300" height="250" id="ad300x250f" frameborder="0" scrolling="no"></iframe></div>';
var Ad300Div = document.getElementById("ad300x250");
  if( Ad300Div ) {
    if( typeof adSyncHTML != "undefined" ) {
      if( adSyncHTML != "" ) {
        ad300HTML = adSyncHTML;
      }
    }
    Ad300Div.innerHTML = ad300HTML;
  }
}