var imgTitle = '', imgDesc = '', _autoplay = false;
function fetchSection(section){
	thisCollection=[];
	for(var i=0;i<collection.img.length;i++){
		if(section==collection.img[i].section){
			thisCollection.push(collection.img[i]);
			//set array for slideshow
			var sImg = 'images/galleries/'+collection.img[i].section+'/'+collection.img[i].src+'_Small.jpg';
			gallery.list.push(sImg);
		}
	}
	gallery.loadFirst();
	//respondCollection(thisCollection);
}
function respondCollection(curImg){
	if(thisCollection.length>0){
		imgSrc = 'images/galleries/'+thisCollection[curImg].section+'/'+thisCollection[curImg].src+'_Large.jpg';
		imgTitle = thisCollection[curImg].title;
		imgDesc = thisCollection[curImg].description;
		loadPhoto(imgSrc);
		thisNum=curImg;
	}else{
		alert('No images for this collection yet.')
	}
}
function loadPhoto(imgSrc,fade){
	$('lightboxImg').innerHTML = '<img id="lightboxImgSrc" alt="" src=""/>';
	w=0;
	h=0;
	var	imgLoad = new Image();
	imgLoad.onload = function(){
		if(fade){
			var lightboxOut = new Fx.Style('lightbox','opacity',{duration:150,transition:Fx.Transitions.sineOut}).start(1,0);
			setTimeout(function(){
				$('lightboxImgSrc').src = imgLoad.src;
				$('lightboxTitle').innerHTML = imgTitle;
				$('lightboxDescription').innerHTML = imgDesc;
				w=imgLoad.width;h=imgLoad.height;
				imgLoader(w,h);
			},200);
		}else{
			$('lightboxImgSrc').src = imgLoad.src;
			$('lightboxTitle').innerHTML = imgTitle;
			$('lightboxDescription').innerHTML = imgDesc;
			w=imgLoad.width;h=imgLoad.height;
			imgLoader(w,h);
		}
	}
	imgLoad.src = imgSrc;
}
function imgLoader(w,h){
	imgLoad = '';
	//new Effect.Appear('s2',{duration:0.5,queue:'end'});
	scaleLightbox(w,h);
}
function prevImage(){
	l = thisCollection.length - 1;
	thisNum = thisNum-1;
	if(thisNum<0){thisNum=l;}
	imgSrc = 'images/galleries/' +thisCollection[thisNum].section+ '/' +thisCollection[thisNum].src+'_Large.jpg';
	imgTitle = thisCollection[thisNum].title;
	imgDesc = thisCollection[thisNum].description;
	setTimeout(function(){loadPhoto(imgSrc,true);},100);
}
function nextImage(){
	l = thisCollection.length - 1;
	thisNum = thisNum+1;
	if(thisNum>l){thisNum=0;}
	imgSrc = 'images/galleries/' +thisCollection[thisNum].section+ '/' +thisCollection[thisNum].src+'_Large.jpg';
	imgTitle = thisCollection[thisNum].title;
	imgDesc = thisCollection[thisNum].description;
	setTimeout(function(){loadPhoto(imgSrc,true);},100);
}
function scaleLightbox(w,h){
	$('lightboxImg').setStyle('width',w);
	$('lightboxImg').setStyle('height',h);
	$('lightbox').setStyle('width',w+20);
	centerLightbox();
	var lightboxIn = new Fx.Style('lightbox','opacity',{duration:250,transition:Fx.Transitions.sineIn}).start(0,1);
}
function centerLightbox(){
	lH = $('lightbox').offsetHeight;
	var wH = (window.getHeight()/2)-(lH/2)+window.getScrollTop();
	$('lightbox').setStyle('top',wH);
	$('lightbox').setStyle('left',(window.getWidth()/2)-($('lightbox').offsetWidth/2));
}
var gallery = {
	init:function(section){
		gallery.list = [];
		fetchSection(section);
	},
	loadFirst:function(){
		gallery.cur = 0;
		$('photoImg').innerHTML = '<img src="'+gallery.list[0]+'" alt="gallery image"/>';
		if($('photoBox')){
			$('photoBox').onclick = function onclick(event){
				gallery.lightbox(gallery.cur);
			};
		}else if($('photoBoxLeft')){
			$('photoBoxLeft').onclick = function onclick(event){
				gallery.lightbox(gallery.cur);
			};
		}else if($('photoBoxRight')){
			$('photoBoxRight').onclick = function onclick(event){
				gallery.lightbox(gallery.cur);
			};
		}
	},
	run:function(){
		_autoplay = true;
		gallery.autoplay = setInterval(function(){ 
			gallery.cur = gallery.cur + 1;
			if(gallery.cur>gallery.list.length-1){gallery.cur=0}
			var	imgLoad = new Image();
			var fadeOut = new Fx.Style('photoImg','opacity',{onComplete:function(){
				var	imgLoad = new Image();
				imgLoad.onload = function(){
					$('photoImg').innerHTML = '<img src="'+gallery.list[gallery.cur]+'" alt="gallery image"/>';
					setTimeout(function(){var fadeIn = new Fx.Style('photoImg','opacity').start(0,.99);},250);
				}
				imgLoad.src = gallery.list[gallery.cur];
			}}).start(1,0);
		},5000);
	},
	stop:function(){
		if(gallery.autoplay) clearInterval(gallery.autoplay);
	},
	lightbox:function(curImg){
		gallery.stop();
		var modalIn = new Fx.Style('modal','opacity',{duration:150,transition:Fx.Transitions.sineIn}).start(0,.8);
		setTimeout(function(){respondCollection(curImg);},100);
		$('modal').onclick = function onclick(event){
			gallery.lightboxKill();
		};
	},
	lightboxKill:function(){
		setTimeout(function(){var lightboxOut = new Fx.Style('lightbox','opacity',{duration:150,transition:Fx.Transitions.sineOut}).start(1,0);},100);
		setTimeout(function(){var modalOut = new Fx.Style('modal','opacity',{duration:250,transition:Fx.Transitions.sineOut,onComplete:function(){if(_autoplay){gallery.run()};$('lightboxDirection').setStyle('display','block');}}).start(.8,0);},150);
	}
}
function lightboxMap(map){
	var modalIn = new Fx.Style('modal','opacity',{duration:150,transition:Fx.Transitions.sineIn}).start(0,.8);
	scaleLightbox(650,450);
	$('lightboxDirection').setStyle('display','none');
	setTimeout(function(){
		var s = '';
		if(map=='mnh'){
			s = 'http://maps.google.com/maps?f=q&hl=en&geocode=&q=1120+Avenue+of+the+Americas,+new+york,+ny+10036&sll=40.757408,-73.984991&sspn=0.005112,0.009087&ie=UTF8&z=16&iwloc=addr&om=1'
		}else{
			s = 'http://maps.google.com/maps?f=q&hl=en&geocode=&q=791+East+132nd+Street,+bronx,+ny+10454&sll=37.0625,-95.677068&sspn=43.578243,74.443359&ie=UTF8&z=16&iwloc=addr&om=1'
		}
		$('lightboxImg').innerHTML = '<iframe id="lightwindow_iframe" width="100%" scrolling="auto" height="100%" frameborder="0" name="lightwindow_iframe" src="'+s+'" marginwidth="0" marginheight="0"></iframe>';
	},200);
	$('modal').onclick = function onclick(event){
		gallery.lightboxKill();
	};
}
window.addEvent('scroll',function(){
	var d = window.getScrollTop();
	$('modal').setStyle('top',d);
	centerLightbox();
});