var Gallery = new Class({
	
	initialize: function() {
		this.amountOfClicks = 0;
		this.adShowInterval = 4; 			// 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.title = "";
		this.adzone = 'photo_player';
		this.displayInterstitialAds = true;
	},
	
	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
		s.pageName = this.title + " - " + img[5];
		var s_code=s.t();
		
		// 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");
		$('image_count').setHTML("");
		$('full_image_title').setHTML("");
		$('full_image_description').setHTML("");
		$('full_image_credit').setHTML("");
		
		$('secondary-gallery-nav').setStyle('display', 'none');

		var ad300HTML = '<div id="ad-top">advertisement</div><iframe src="'+adsurl+'iframeAd.php?adsize=300x250&adzone='+this.adzone+'&segQS='+segQS+'&ord='+ord+'&site='+adsite+'&tile=1" 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");
		$('secondary-gallery-nav').setStyle('display', 'block');
		this.showingAd = false;
	},
	
	refreshAd: function() {
		var ad = document.getElementById("ad300x250f");
		if(ad) {
			ad.src=adsurl+'iframeAd.php?adsize=300x250&adzone='+this.adzone+'&ord='+ord+'&site='+adsite+'&tile=2';
		}		
	},
	
	recordHit: function(id) {
		new Ajax('/record_image_hit.html?id=' + id).request();
	},
	
	next: function() {
		if(this.shouldDisplayAd()) {
			this.displayAd();
		} else {
			this.imageIndex += 1;
			if (this.imageIndex >= this.imageArray.length)
				this.imageIndex = 0;
			this.switchImage(this.imageIndex);
		}
	},
	
	previous: function() {
		if(this.shouldDisplayAd()) {
			this.displayAd();
		} else {
			this.imageIndex -= 1;
			if (this.imageIndex <= -1)
				this.imageIndex = this.imageArray.length - 1;
			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;
	},
	
	// This is only called during page load to facilitate deep linking. Note that this is probably bad for SEO.
	gotoImage: function(idx) {
		if(this.imageIndex != 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]);
			
			if(this.showingAd) this.hideAd();
		}
	},
	
	thumbnailClick: function(image_id) {
		if(this.shouldDisplayAd()) {
			this.displayAd();
		} else {
			this.switchImage(this.getIndexByID(image_id));
		}
	},
	
	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(document.location.href.split("#")[1]) this.gotoImage(this.getIndexByID(document.location.href.split("#")[1]));
	}
	
});

var g = new Gallery();

var urlChangeListener = function(newLocation, historyData) {
	g.gotoImageByID(newLocation);
}

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;
}
