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
		s.pageName = this.title + " - " + img[5] + " - " + img[0] + " page";
		var s_code=s.t();
		
		// reset all .prev-galleries to just be 'prev'
		$$('.prev-gallery').each(function(el) {
			el.className = 'prev';
		});
		// reset all .next-galleries to just be 'next'
		$$('.next-gallery').each(function(el) {
			el.className = 'next';
		});
		
		// If we're at the first image, we need to display the prev-gallery button
		if(this.imageIndex == 0) {
			$$('.prev').each(function(el) {
				el.className = 'prev-gallery';
			});
		}
		// If we're at the last image, we need to display the next-gallery button
		if(this.imageIndex >= this.imageArray.length-1) {
			$$('.next').each(function(el) {
				el.className = 'next-gallery';
			});
		}
		
		// popup external links in description field
		externalizeExternalLinks();
	},
	
	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_title').setHTML("");
		$('full_image_description').setHTML("");
		$('full_image_credit').setHTML("");
		var ad300HTML = '<div id="ad-top">advertisement</div><iframe src="http://apps.telepixtv.com/ads/300x250interstitial.php?ord='+tpixAdConfig.ord+'&site='+adsite+'&adzone='+this.adzone+'" frameborder="0" width="300" height="250" scrolling="no" marginwidth="0" marginheight="0" hspace="0" vspace="0"></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", "9000");
		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);
		}
	}
	
});

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)[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');
		}
	});
}