var is_list = '';
var is_countdown = 0;
var mainImage = 'gallery_main';
var thumbContainer = 'gallery_thumbs';
var headlineContainer = 'gallery_headline';
var captionContainer = 'gallery_caption';
var textContainer = 'gallery_text';
var creditContainer = 'gallery_credit';
var imageNumber = 'gallery_num';
var galleryTotal = 'gallery_total';
var previousButtonTop = 'gallery_previous_top';
var nextButtonTop = 'gallery_next_top';
var previousButtonBottom = 'gallery_previous_bottom';
var nextButtonBottom = 'gallery_next_bottom';
var tagContainer = 'gallery_tags';
var first=true;
/* end of container names */
/* global stuff */
var numPhotos;
var galleryNext;
var galleryPrev;
// init
var thumbsLoaded = 0;
var galleryArray;
var currentPhoto;
/* ** */
try {jQuery(document).ready(function(){
	if(typeof window.nymag == 'undefined') window.nymag = {};
	window.nymag_config = function nymag_config(s) {
		s.charSet="ISO-8859-1";
		s.currencyCode="USD";
		s.trackDownloadLinks=true;
		s.trackExternalLinks=true;
		s.trackInlineStats=true;
		s.linkDownloadFileTypes="exe,zip,wav,mp3,mov,mpg,avi,wmv,doc,pdf,xls";
		s.linkInternalFilters="javascript:,nymag.com,#";
		s.linkLeaveQueryString=false;
		s.linkTrackVars="None";
		s.linkTrackEvents="None";

		s.visitorNamespace="newyorkmagazine";
		s.dc=112;
		s.trackingServer="stats.nymag.com";
		s.trackingServerSecure="sstats.nymag.com";
		return s;
	}

	window.nymag_blogSlideTags = function(photo) {
		if((photo!=0) && !photo) return '';
		if(isNaN(parseInt(photo))) return '';
		photos = window.galleryData.photos;
		if(photos.length <= photo) return '';
		this_photo = photos[photo];
		tags = this_photo.tags;
		names = new Array();
		for(tag in tags) {
			names.push(";" + tags[tag].tagName);
		}
		return(names.join());
	}

	window.nymag_pageViewSlide = function(slide){

		nymag_setMetadata({
			'content.subtype' : 'Blog Slideshow',
			   'content.tags' : nymag_blogSlideTags(slide)
		});

		if(0>window.location.href.search('#')){
			nymag_setMetadata({'content.tags' : nymag_blogSlideTags(0)});
			nymag_pageView();
			window.nymag['pageViewRequestedBlog'] = true;
			return;
		}
		if(typeof window.nymag['pageViewRequestedBlog'] == "undefined"){
			nymag_setMetadata({'content.tags' : nymag_blogSlideTags(slide)});
			window.nymag['pageViewRequestedBlog'] = false;
			return;
		}
		nymag_pageView();
		window.nymag['pageViewRequestedBlog'] = true;
	};

});} catch (e) {}

function galleryInit() {
	galleryArray = galleryData.photos
	numPhotos = galleryArray.length;
	currentPhoto = (r = document.location.href.match(/#photo=(\d+)/))?r[1]:0;
	currentPhoto = parseInt(currentPhoto);
	if (is_countdown && currentPhoto > 0) {
		//currentPhoto = Math.abs(numPhotos-currentPhoto)+1;
	}
	
	if (document.getElementById(thumbContainer) && currentPhoto <= 1) {
		writeThumbs();
	}
	
	if (is_list) {
		slideshowClass = 'slideshow-list-single';
		lastslideClass = 'slideshow-list-lastslide';
	} else {
		slideshowClass = 'slideshow-single';
		lastslideClass = 'slideshow-lastslide';
	}
	if (currentPhoto > 0) {
		writePhotos(currentPhoto);
	} else if (document.location.href.match(/#end-slideshow/)) {
		endSlideshow();
	}
}

function writePhotos(num) {
	launchSlideshow();
	window.nymag.dcads.reloadAds();
	if (document.getElementById(galleryTotal)) document.getElementById(galleryTotal).innerHTML = numPhotos;
	if ((typeof num).toLowerCase() == 'object' || num == undefined || num == '') {
		num = (currentPhoto>numPhotos)?numPhotos:currentPhoto;
		num = (num < 1)?1:num;
	}
	num = parseInt(num);
	photoTags = false;
	if(!isNaN(num)) photoTags = num - 1;
	if(typeof nymag_pageViewSlide == "function") nymag_pageViewSlide(photoTags);
	if (num > numPhotos) {
		endSlideshow();
	} else {
		galleryNext = num+1;
		galleryPrev = num-1;
		if (document.getElementById(imageNumber)) {
			document.getElementById(imageNumber).innerHTML = num;
		}
		if (is_list) {
			if (document.getElementById(imageNumber).className.match(/no-\d+/)) {
				if (is_countdown) {
					document.getElementById(imageNumber).className = document.getElementById(imageNumber).className.replace(/no-\d+/, 'no-' + Math.abs(numPhotos-(num-1)));
					} else {
						document.getElementById(imageNumber).className = document.getElementById(imageNumber).className.replace(/no-\d+/, 'no-' + num);
					}
			} else {
				if (is_countdown) {
					document.getElementById(imageNumber).className += " no-" + Math.abs(numPhotos-(num-1));
				} else {
					document.getElementById(imageNumber).className += " no-" + num;
				}
			}
		}
		num--; // subtract one to get the array index
		// populate data
		document.getElementById(mainImage).innerHTML = '';
		if (!galleryArray[num].type || galleryArray[num].type == 'image') {
			i = document.createElement('img');
			i.src = galleryArray[num].photo;
			document.getElementById(mainImage).appendChild(i);
			document.getElementById(previousButtonBottom).style.display = 'block';
			document.getElementById(nextButtonBottom).style.display = 'block';
		} else {
			div = document.createElement('div');
			div.id="tempDIV";
			document.getElementById(mainImage).appendChild(div);
			swfobject.embedSWF(galleryArray[num].photo, "tempDIV", 425, 344, "8.0.0");
			document.getElementById(previousButtonBottom).style.display = 'none';
			document.getElementById(nextButtonBottom).style.display = 'none';
		}
		document.getElementById(headlineContainer).innerHTML = galleryArray[num].title;
		document.getElementById(captionContainer).innerHTML = galleryArray[num].caption;
		document.getElementById(creditContainer).innerHTML = galleryArray[num].credit;
		
		
		// populate next/prev
		document.getElementById(nextButtonTop).onclick = function() {
			if(0<=document.getElementById(nextButtonTop).parentNode.className.search('inactive')) return;
			writePhotos(galleryNext);
		}
		document.getElementById(previousButtonTop).onclick = function() {
			if(galleryPrev <= 0) return; 
			writePhotos(galleryPrev);
		}
		document.getElementById(nextButtonBottom).onclick = function() {
			writePhotos(galleryNext);
		}
		document.getElementById(previousButtonBottom).onclick = function() {
			if(galleryPrev <= 0) return; 
			writePhotos(galleryPrev);
		}
		setTimeout(function() {
			if (parseInt(galleryNext) <= numPhotos) {
				document.getElementById(nextButtonTop).innerHTML = galleryArray[parseInt(galleryNext)-1].title;
				document.getElementById(nextButtonBottom).innerHTML= galleryArray[parseInt(galleryNext)-1].title;
				if (is_countdown) {
					document.getElementById(nextButtonTop).href = "#photo=" + (parseInt(Math.abs(numPhotos-(parseInt(galleryNext)))+1));
					document.getElementById(nextButtonBottom).href = "#photo=" + (parseInt(Math.abs(numPhotos-(parseInt(galleryNext)))+1));
				} else {
					document.getElementById(nextButtonTop).href = "#photo=" + parseInt(galleryNext);
					document.getElementById(nextButtonBottom).href = "#photo=" + parseInt(galleryNext);
				}				
			} else {
				document.getElementById(nextButtonTop).innerHTML = '';
				document.getElementById(nextButtonBottom).innerHTML= '';
				document.getElementById(nextButtonTop).href = "#end-slideshow";
				document.getElementById(nextButtonBottom).href = "#end-slideshow";
			}
			document.getElementById(nextButtonTop).parentNode.className = document.getElementById(nextButtonTop).parentNode.className.replace(/inactive/g, '');
			
			if (parseInt(galleryPrev) > 0) {
				document.getElementById(previousButtonTop).innerHTML = galleryArray[parseInt(galleryPrev)-1].title;
				document.getElementById(previousButtonBottom).innerHTML = galleryArray[parseInt(galleryPrev)-1].title;
				if (is_countdown) {
					document.getElementById(previousButtonTop).href = "#photo=" + (parseInt(Math.abs(numPhotos-(parseInt(galleryPrev)))+1));
					document.getElementById(previousButtonBottom).href = "#photo=" + (parseInt(Math.abs(numPhotos-(parseInt(galleryPrev)))+1));
				} else {
					document.getElementById(previousButtonTop).href = "#photo=" + parseInt(galleryPrev);
					document.getElementById(previousButtonBottom).href = "#photo=" + parseInt(galleryPrev);
				}
				document.getElementById(previousButtonTop).parentNode.className = document.getElementById(previousButtonTop).parentNode.className.replace(/inactive/g, '');
				document.getElementById(previousButtonBottom).parentNode.className = document.getElementById(previousButtonBottom).parentNode.className.replace(/inactive/g, '');
			
			} else {
				document.getElementById(previousButtonTop).parentNode.className += " inactive";
				document.getElementById(previousButtonBottom).parentNode.className += " inactive";
			}
		}, 100);
	}
}

function writeThumbs(num) {
	if (thumbsLoaded) return 0;
	galleryArray = galleryData.photos
	thumbs = document.getElementById(thumbContainer);
	thumbsLoaded = 1;
	for (x=0;x<galleryArray.length;x++) {
		var img = document.createElement('img');
		thumbs.appendChild(img);
		img.src = galleryArray[x].thumb;
		img.className = 'gallery_thumbnail';
		img.setAttribute('num', x+1);
		img.setAttribute('id',thumbContainer + '_' + (x+1));
		img.onclick = function() {
			r = new RegExp(thumbContainer + '_(\\d+)');
			writePhotos(parseInt(this.id.match(r)[1]));
		}
		if (x == num) {
			img.className += ' selected';
		}
	}
}

function launchSlideshow() {
	if (currentPhoto == 0) currentPhoto = 1;
	if (document.getElementById('blog').className.match(new RegExp(lastslideClass))) {
		document.getElementById('blog').className = document.getElementById('blog').className.replace(new RegExp(lastslideClass), ' ' + slideshowClass);
	} else {
		if (!document.getElementById('blog').className.match(new RegExp(slideshowClass))) document.getElementById('blog').className += ' ' + slideshowClass;
	}
	document.getElementById('entry_content').style.display = 'none';
	document.getElementById('gallery_finish').style.display = 'none';
	document.getElementById('gallery_content').style.display = 'block';
	document.getElementById('gallery_main').style.display = 'block';
	document.getElementById('nav_top').style.display = 'block';
	document.getElementById('nav_bottom').style.display = 'block';
	document.getElementById('kicker').style.visibility = 'visible';
	if (is_list) {
		document.getElementById('gallery_num').style.display = 'block';
	} else {
		document.getElementById('gallery_num').style.display = 'inline';
	}
	
}

function hideSlideshow() {
	if (document.getElementById('blog').className.match(new RegExp(lastslideClass))) {
		document.getElementById('blog').className = document.getElementById('blog').className.replace(new RegExp(lastslideClass), '');
	} else {
		document.getElementById('blog').className = document.getElementById('blog').className.replace(new RegExp(' ' + slideshowClass), '');
	}
	document.getElementById('entry_content').style.display = 'block';
	document.getElementById('gallery_content').style.display = 'none';
}


function endSlideshow() {
	if (document.getElementById('blog').className.match(new RegExp(slideshowClass))) {
		document.getElementById('blog').className = document.getElementById('blog').className.replace(new RegExp(slideshowClass), lastslideClass);
	} else {
		document.getElementById('blog').className += " " + lastslideClass;
	}
	
	document.getElementById('entry_content').style.display = 'none';
	document.getElementById('gallery_finish').style.display = 'block';
	document.getElementById('gallery_content').style.display = 'block';
	document.getElementById('gallery_main').style.display = 'none';
	document.getElementById('nav_top').style.display = 'block';
	document.getElementById('nav_bottom').style.display = 'none';
	document.getElementById('kicker').style.visibility = 'hidden';
	document.getElementById('gallery_num').style.display = 'none';

	document.getElementById(previousButtonTop).parentNode.className = document.getElementById(previousButtonTop).parentNode.className.replace(/inactive/g, '');	
	document.getElementById(nextButtonTop).parentNode.className += " inactive";
	document.getElementById(previousButtonTop).onclick = function() {
		writePhotos(numPhotos);
	}
	if (is_countdown) {
		document.getElementById(previousButtonTop).href = "#photo=" + (parseInt(Math.abs(numPhotos-(parseInt(numPhotos)))+1));
	} else {
		document.getElementById(previousButtonTop).href = "#photo=" + parseInt(numPhotos);
	}

	currentPhoto = 0;
}

if (window.attachEvent) {
	window.attachEvent('onload', galleryInit);
} else {
	window.addEventListener('load', galleryInit, true);
}
