// mooShow 1.04
// (c)2006 Stuart Eaton - http://www.eatpixels.com
//
// Credit where credit is due: Inspiration from Lightbox (http://www.huddletogether.com/projects/lightbox2/)
// and Couloir (http://www.couloir.org/js_slideshow/) and of course moo.fx (http://moofx.mad4milk.net/
// moo.fx and prototype are covered by their own respective license terms.

// --------------------------------------------------------------------------------------------------------------

var loadingImage = "/mooshow/loading.gif";		// loading image

// --------------------------------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------------------------------

var mooShows = null;

// get current photo id from URL
var thisURL = document.location.href;
var splitURL = thisURL.split("#");
var photoId = splitURL[1]-1;
if (photoId<0) photoId=0;


// if no photoId supplied then set default
var photoId = (!photoId)? 0 : photoId;


	// -----------------------------------------------------------------------------------
	
	function start() {
		Element.hide("warningJavascript");
		
		/*var i; 
		var imagenes = new Array("/mooshow/prev.gif","/mooshow/next.gif","/mooshow/loading.gif"); 
		var lista_imagenes = new Array(); 
		
		for(i in imagenes){ 
		     lista_imagenes[i] = new Image(); 
		     lista_imagenes[i].src = imagenes[i]; 
		}*/
		
		
		var hideMenu = new fx.Width("title" , {duration: 50, onComplete: function()
	  	{
	    		hideMenu.hide();
	    		var widthContent = new fx.Width("content", {duration:100, onComplete: function()
	    		{
	    			// find any mooshows on the page
				find_mooshows();
				Element.showBlock("Navigation");
			} });
			width=Element.getWidth("content");
			widthContent.custom(width,width+172);
	  	} });
	  	hideMenu.toggle();	  	
		
	}
	
	// -----------------------------------------------------------------------------------
	
	function find_mooshows() {
		shows = document.getElementsByClassName("mooshow"); // find divs using the 'mooshow' class
		if(shows.length == 0) shows = showsIE; // If can't find shows list get it from html page (hack for IE5.5)
		
		mooShows = new Object();
		for ( var i = 0; i < shows.length; i++ ) {
			showName = shows[i].id; // get shownames
			if(showName==null) showName = shows[i]; // if we are getting show names from the html page (hack for IE5.5)			
			mooShows[ showName ] = new mooshow(showName, photoId); // create new mooshow objects
			//alert(mooShows[showName].photoArray);
			if (photoId>(mooShows[showName].photoArray.length-2)) {
				photoId=mooShows[showName].photoArray.length-1;
				create_mooshow(showName, i, photoId); // create the shows htmls
				mooShows[showName].jumptoImage(photoId);
			} else {
				create_mooshow(showName, i, photoId); // create the shows htmls
			}
		}
	}
	
	// -----------------------------------------------------------------------------------
	
	function create_mooshow( showName, shownumber, photoId ) {
		
		eval($(showName).innerHTML); //get slideshow settings
		photoArray = eval(showName); // set up image array based on id
		
	  	
		shownumber++; // add 1 to shownumber so it starts on 1 not 0
		
		if(this.dropShadow=='yes'){
			this.outerContainerClass = 'mooshow_outerContainer dropShadowBorder';
		} else {
			this.outerContainerClass = 'mooshow_outerContainer';
		}
		
		if (lang=="es") {
			tipText = "<strong>Ayuda:</strong> Haz click en la izquierda o en la derecha de la imagen para navegar por las fotos.";
		} else {
			tipText = "<strong>Tip:</strong> Click on the left or the right of the picture to browse in the slideshow.";
		}
		
		widthTopNav=(photoArray[photoId][1]/2)-20;		
		widthExtras = 16 + parseInt(photoArray[photoId][1]);
		var mooShow_html = ''+
		'<div id=\''+showName+'_outerContainer\' class=\''+this.outerContainerClass+'\' style=\'padding:'+this.border+'px; \'> \n'+
		'	<div id=\''+showName+'_tip\' style=\'width:'+widthExtras+'px;\' class=\'mooshow_tip\'>'+tipText+'</div>\n'+
		'	<div id=\''+showName+'_contentContainer\' class=\'mooshow_contentContainer\' > \n'+
		'		<img src=\''+photoArray[photoId][0]+'\' class=\'mooshow_image\' width=\''+photoArray[photoId][1]+'\' height=\''+photoArray[photoId][2]+'\' id=\''+showName+'_image\' /> \n'+
		'		<img src=\''+loadingImage+'\' id=\''+showName+'_loading\' class=\'mooshow_loading\' /> \n'+
		'		<div id=\''+showName+'_copyright\' class=\'mooshow_copyright\'></div> \n'+
		'		<div id=\''+showName+'_overlayNav\' class=\'mooshow_overlayNav\' style=\'height:'+photoArray[photoId][2]+'px; width:'+(parseInt(photoArray[photoId][1]))+'px;\'> \n'+
		'			<a href=\'#\' id=\''+showName+'_prevLink\' class=\'mooshow_prevLink\' onClick=\'mooShows[&#39;'+showName+'&#39;].prevImage();\' ></a> \n'+
		'			<a href=\'#\' id=\''+showName+'_nextLink\' class=\'mooshow_nextLink\' onClick=\'mooShows[&#39;'+showName+'&#39;].nextImage();\' ></a> \n'+
		'		</div> \n'+
		'		<div id=\''+showName+'_IPTC\' class=\'mooshow_IPTC\' style=\'width:'+photoArray[photoId][1]+'px;\'></div> \n'+
		//'		<a href=\'#'+shownumber+'\' ><img src=\'/mooshow/info.gif\' id=\''+showName+'_IPTCbutton\' class=\'mooshow_IPTCbutton\' onClick=\'mooShows[&#39;'+showName+'&#39;].updateIPTCinfoToggle();\' /></a> \n'+
		'	</div> \n'+
		'	<div id=\''+showName+'_extras\' class=\'mooshow_extras\' style=\'width:'+widthExtras+'px;\'> \n'+
		'		<div id=\''+showName+'_captions\' class=\'mooshow_captions\' style=\'padding-top:'+this.border+'px;padding-left:-'+this.border+'px;width:'+widthTopNav+'px;\'><span class=\'albumTitle\'>'+photoAlbum+'</span> / '+photoArray[photoId][3]+'&nbsp;</div> \n'+
		'		<div id=\''+showName+'_topNav\' class=\'mooshow_topNav\' style=\'padding-top:'+this.border+'px;width:'+widthTopNav+'px;\'></div> \n'+
		'		<div id=\''+showName+'_temp\' class=\'mooshow_temp\'></div> \n'+
		'	</div> \n'+
		'</div> \n'+
		'<script></script>';

		Element.setInnerHTML(showName, mooShow_html);
		
		// display or hide various options
		if(this.topNav == 'no') {Element.hide(showName+'_topNav');} else { mooShows[showName].updateTopNav(showName); }
		if(this.overlayNav == 'no') { Element.hide(showName+'_overlayNav'); } else { mooShows[showName].updateOverlayNav(showName); }
		if(this.captions == 'no') Element.hide(showName+'_captions');
		if(this.copyright == 'yes') Element.setInnerHTML(showName+'_copyright', photoArray[photoId][6]);
		//if(this.IPTCinfo == 'no') Element.hide(showName+'_IPTCbutton');
		Element.show(showName);
	}
	

// -----------------------------------------------------------------------------------
// -----------------------------------------------------------------------------------



var mooshow = Class.create();
mooshow.prototype = {
	
	

	// -----------------------------------------------------------------------------------

	initialize: function(showName, counter) {
		this.id = showName;
		this.busy = 0;
		this.counter = counter;
		this.photoArray = eval(this.id);
		this.numberOfImages = this.photoArray.length -2;
		//if (this.counter>this.numberOfImages) this.counter=this.numberOfImages;
		this.visitedPictures = new Array(this.numberOfImages);
		for(x=0;x<this.numberOfImages;x++)this.visitedPictures[x] = "0";
		this.visitedPictures[this.counter]="1";
		this.IPTCinfoStatus = 0;
		eval($(showName).innerHTML); //get slideshow settings (set inside the mooshow div)
	},
	
	// -----------------------------------------------------------------------------------

	nextImage: function() {
		if(this.busy<1){
			this.busy=1;
			if(this.counter < this.numberOfImages) {
				this.counter ++;
			} else {
				this.counter = 0;
			}
			this.loadImage();
			this.visitedPictures[this.counter]="1";
		}
	},
	
	// -----------------------------------------------------------------------------------
	
	prevImage: function() {
		if(this.busy<1){
			this.busy=1;
			if(this.counter > 0) {
				this.counter --;
			} else {
				this.counter = this.numberOfImages;
			}
			this.loadImage();
			this.visitedPictures[this.counter]="1";		
		}
	},
	
	// -----------------------------------------------------------------------------------
	
	jumptoImage: function(counter) {
		if(this.busy<1){
			this.busy=1;
			this.counter = counter-1;
			this.loadImage();
			this.visitedPictures[this.counter]="1";		
		}
	},
	
	// -----------------------------------------------------------------------------------
	
	switchContent: function(newArray) {
		if(this.busy<1){
			this.busy=1;
			this.photoArray = eval(newArray);
			this.numberOfImages = this.photoArray.length -2;
			this.counter = 0;
			this.loadImage();		
		}
	},
	
	// -----------------------------------------------------------------------------------
	
	updateIPTCinfo: function() {
			//get showname
			showName = this.id;
			// 
			
			var langInfo = new Array(
				new Array ('Title','Titulo'),
				new Array ('Aperture','Apertura'),
				new Array ('Shutter speed','Velocidad'),
				new Array ('Focal length','Longitud focal'),
				new Array ('Flash','Flash'),
				new Array ('License','Licencia'),
				new Array ('Link to this image','Enlace a esta imagen')
			);
			
			if (lang=="en") i=0;
			else i=1;
			
			this.iptcHTML = ''+
			'<div class=\'mooshow_IPTC_left\'><ul>'+
			'<li><strong>'+langInfo[0][i]+': </strong>'+this.photoArray[this.counter][3]+'</li>'+
			'<li><strong>'+langInfo[1][i]+': </strong>'+this.photoArray[this.counter][6]+'</li>'+
			'<li><strong>'+langInfo[2][i]+': </strong>'+this.photoArray[this.counter][4]+'</li>'+
			'<li><strong>'+langInfo[3][i]+': </strong>'+this.photoArray[this.counter][5]+'</li>'+			
			'<li><strong>'+langInfo[4][i]+': </strong>'+this.photoArray[this.counter][7]+'</li>'+
			'<li><strong>'+langInfo[5][i]+': </strong><a href=\'http://creativecommons.org/licenses/by-nc-sa/2.5/\' title=\'Find out more about this license\'>Creative Commons by-nc-sa</a></li>'+
			'<li><strong>'+langInfo[6][i]+': </strong><a href=\''+document.location.href+'\'>'+document.location.href+'</a></li>'+
			'</ul></div>';
			/*'<div class=\'mooshow_IPTC_right\'><br/><br/>'+
			'	'+this.photoArray[this.counter][3]+'<br/><br/>'+ //title
			'	'+this.photoArray[this.counter][6]+'<br/>'+ // Aperture			
			'	'+this.photoArray[this.counter][4]+'<br/>'+ // shutter speed
			'	'+this.photoArray[this.counter][5]+'<br/>'+ //focal length
			'	'+this.photoArray[this.counter][7]+'<br/><br/>'+ // flash
			'	<a href=\'http://creativecommons.org/licenses/by-nc-sa/2.5/\' title=\'Find out more about this license\'>Creative Commons by-nc-sa</a></br>'+
			'	<a href=\''+document.location.href+'\'>'+document.location.href+'</a><br/><br/>'+ //img src
			'</div>';*/
			/*'<div class=\'mooshow_IPTC_left\'><br/><br/>'+
			'	Title <br/><br/>'+
			'	Aperture <br/>'+
			'	Shutter Speed <br/>'+
			'	Focal Length <br/>'+			
			'	Flash <br/><br/>'+
			'	License <br/>'+
			'	Link to this image <br/><br/>'+
			'</div>'+
			'<div class=\'mooshow_IPTC_right\'><br/><br/>'+
			'	'+this.photoArray[this.counter][3]+'<br/><br/>'+ //title
			'	'+this.photoArray[this.counter][6]+'<br/>'+ // Aperture			
			'	'+this.photoArray[this.counter][4]+'<br/>'+ // shutter speed
			'	'+this.photoArray[this.counter][5]+'<br/>'+ //focal length
			'	'+this.photoArray[this.counter][7]+'<br/><br/>'+ // flash
			'	<a href=\'http://creativecommons.org/licenses/by-nc-sa/2.5/\' title=\'Find out more about this license\'>Creative Commons by-nc-sa</a></br>'+
			'	<a href=\''+document.location.href+'\'>'+document.location.href+'</a><br/><br/>'+ //img src
			'</div>';*/

			Element.setInnerHTML(''+showName+'_IPTC', this.iptcHTML);
	},
	
	// -----------------------------------------------------------------------------------
	
	updateIPTCinfoToggle: function() {
		if(this.IPTCinfoStatus==1){
			Element.hide(''+showName+'_IPTC');
			this.IPTCinfoStatus=0;
		} else {
			Element.show(''+showName+'_IPTC');
			this.IPTCinfoStatus=1;
			this.updateIPTCinfo();
		}
	},
	
	// -----------------------------------------------------------------------------------
	
	updateTopNav: function(showName) {
		//Element.setWidth(showName+'_topNav', this.photoArray[this.counter][1]);
		Element.setInnerHTML(showName+'_topNav', '');
		this.topNavContent = $(showName+'_topNav').innerHTML;
		
		this.topNavContent = (this.counter+1) + ' / ' + (this.photoArray.length-1); //+ ' <img src=\'mooshow/blank.gif\' width=\'10\' height=\'1\' />';
		/*
		for ( var i = 1; i < this.photoArray.length; i++ ) {
			
			if(i==this.counter+1){this.topNavContent = this.topNavContent + i;} else {
			this.topNavContent = this.topNavContent + ' <a href=\'#'+i+'\' onClick=\'mooShows[&#39;'+showName+'&#39;].jumptoImage('+i+');\'>'+i+'</a>';}
			if(i<this.photoArray.length-1){this.topNavContent = this.topNavContent + ' | '}
		}*/
		
		this.topNavContent = this.topNavContent + ' | <a title=\'Show/Hide more information about this photograph\' href=\'#'+(this.counter+1)+'\' onClick=\'mooShows[&#39;'+showName+'&#39;].updateIPTCinfoToggle();\' />Info</a>';
		document.getElementById(showName+'_topNav').innerHTML = this.topNavContent;
	},
	
	updateOverlayNav: function(showName) {
		
		//For the next/prev links		
		Element.setInnerHTML(showName+'_overlayNav', '');
		this.overlayNavContent = $(showName+'_overlayNav').innerHTML;
		
		nextCounter = (this.counter+1)+1;
		prevCounter = (this.counter+1)-1;
		
		if ((this.counter+1)== 1) {
			prevCounter = (this.photoArray.length-1);
		}
		
		if ((this.counter+1)== (this.photoArray.length-1)) {
			nextCounter = 1;
		}
		
		this.overlayNavContent = '<a href=\'#'+prevCounter+'\' id=\''+showName+'_prevLink\' class=\'mooshow_prevLink\' onClick=\'mooShows[&#39;'+showName+'&#39;].prevImage();\' ></a> \n';
		this.overlayNavContent = this.overlayNavContent + '<a href=\'#'+nextCounter+'\' id=\''+showName+'_nextLink\' class=\'mooshow_nextLink\' onClick=\'mooShows[&#39;'+showName+'&#39;].nextImage();\' ></a> \n';
		
		document.getElementById(showName+'_overlayNav').innerHTML = this.overlayNavContent;	
	},
	
	// -----------------------------------------------------------------------------------

	loadImage: function() {
		//get showname
		showName = this.id;

		// update top navigation
		if(this.topNav=='yes') { Element.hide(showName+'_topNav'); this.updateTopNav(showName); }
		// show laoding animation
		Element.show(showName+'_loading');
		// hide IPTC info
		Element.hide(showName+'_IPTC');		
		//Element.hide(showName+'_IPTCbutton');		
		// overlay navigation
		if(this.overlayNav == 'yes') { Element.hide(showName+'_overlayNav'); this.updateOverlayNav(showName); }
		Element.setOpacity(showName+'_image',0);
		//if (this.visitedPictures[this.counter]!="1") {
			// resize containers to new size
		
		//}
		// preload in new image
		newImgPreloader = new Image();
		// if image is preloaded
		newImgPreloader.onload=function(){
			// when loaded
			// hide current photo
			Element.setSrc(showName+'_image','/mooshow/blank.gif');
			
			// hide laoding animation
			Element.hide(showName+'_loading');
			// set captions to blank space
			Element.setInnerHTML(showName+'_captions', '&nbsp;');
			Element.hide('Navigation');
			Element.hide(showName+'_tip');
			// set copyright to blank
			Element.setInnerHTML(showName+'_copyright', '');
			// get new sizes
			newHeight = newImgPreloader.height;
			newWidth = newImgPreloader.width;
			// resize containers to new size
			this.resizeTopNavWidth = new fx.Width(showName+'_topNav', {duration: mooShows[showName].speed});
			this.resizeCaptionWidth = new fx.Width(showName+'_captions', {duration: mooShows[showName].speed});
			this.resizeExtrasWidth = new fx.Width(showName+'_extras', {duration: mooShows[showName].speed});
			this.resizeOuterContainerHeight = new fx.Height(showName+'_image', {duration: mooShows[showName].speed});
			this.resizeOuterContainerWidth = new fx.Width(showName+'_image', {duration: mooShows[showName].speed, onComplete: function() {				
				
		
				this.resizeTopNavWidthK = new fx.Width(showName+'_topNav', {duration: mooShows[showName].speed});
				this.resizeCaptionWidthK = new fx.Width(showName+'_captions', {duration: mooShows[showName].speed});
				this.resizeExtrasWidthK = new fx.Width(showName+'_extras', {duration: mooShows[showName].speed});
				this.resizeOuterContainerHeightK = new fx.Height(showName+'_image', {duration: mooShows[showName].speed});
				this.resizeOuterContainerWidthK = new fx.Width(showName+'_image', {duration: mooShows[showName].speed, onComplete: function() {
				
					// set up next image
					Element.setSrc(showName+'_image',newImgPreloader.src);
					// reposition overlay nav
					Element.setHeight(showName+'_overlayNav',(newImgPreloader.height));
					Element.setWidth(showName+'_overlayNav',(newImgPreloader.width));
					//show captions
					if(mooShows[showName].captions=='yes') Element.setInnerHTML(showName+'_captions', '<span class=\'albumTitle\'>'+photoAlbum+'</span> / '+mooShows[showName].photoArray[mooShows[showName].counter][3] + '&nbsp;');
					// copyright
					if(mooShows[showName].copyright == 'yes') Element.setInnerHTML(showName+'_copyright', mooShows[showName].photoArray[mooShows[showName].counter][6]);
					// topNav
					if(mooShows[showName].topNav=='yes')  Element.show(showName+'_topNav');
					Element.showBlock('Navigation');
					// new moo.fx 'fader'
					this.fader = new fx.Opacity(showName+'_image', {duration: mooShows[showName].fadeSpeed, onComplete:function() { 
						mooShows[showName].busy = 0;
						// overlay navigation
						if(mooShows[showName].overlayNav == 'yes') Element.show(showName+'_overlayNav');
						//IPTC panel height
						//if(mooShows[showName].IPTCinfo == 'yes') Element.show(showName+'_IPTCbutton');
						Element.setWidth(showName+'_IPTC', newImgPreloader.width);
						if(mooShows[showName].IPTCinfoStatus==1){mooShows[showName].IPTCinfoStatus=0; mooShows[showName].updateIPTCinfoToggle();}
						}});
					// call fader fx
					this.fader.hide(); this.fader.toggle();
				}});
				// get current sizes
				oldHeight = Element.getHeight(showName+'_image');
				oldWidth = Element.getWidth(showName+'_image');
				
				// call moo.fx and when done switchImage()
				this.resizeTopNavWidthK.custom(oldWidth,newWidth/2);
				this.resizeCaptionWidthK.custom(oldWidth,newWidth/2);
				this.resizeExtrasWidthK.custom(oldWidth,newWidth+16);
				this.resizeOuterContainerHeightK.custom(oldHeight,newHeight);
				this.resizeOuterContainerWidthK.custom(oldWidth,newWidth);				
				
			}});
			
			// get current sizes
			oldHeight = Element.getHeight(showName+'_image');
			oldWidth = Element.getWidth(showName+'_image');
			// get new sizes
			newHeight2 = 200;
			newWidth2 = 300;
			// call moo.fx and when done switchImage()
			this.resizeTopNavWidth.custom(oldWidth,newWidth2/2);
			this.resizeCaptionWidth.custom(oldWidth,newWidth2/2);
			this.resizeExtrasWidth.custom(oldWidth,newWidth2);
			this.resizeOuterContainerHeight.custom(oldHeight,newHeight2);
			this.resizeOuterContainerWidth.custom(oldWidth,newWidth2);
		};
		
		newImgPreloader.src = this.photoArray[this.counter][0]; // preloader src
	}
	
	// -----------------------------------------------------------------------------------

}

// --------------------------------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------------------------------







// -----------------------------------------------------------------------------------
//	Additional methods for Element by SU, Couloir
//	- further additions by Lokesh Dhakar (huddletogether.com)
//	- and Stuart Eaton (eatpixels.com)

Object.extend(Element, {
	getWidth: function(element) {
	   	element = $(element);
	   	return element.offsetWidth; 
	},
	
	getHeight: function(element) {
	   	element = $(element);
	   	return element.offsetHeight; 
	},
	

	setWidth: function(element,w) {
	   	element = $(element);
    	element.style.width = w +"px";
	},
	setHeight: function(element,h) {
   		element = $(element);
    	element.style.height = h +"px";
	},
	setTop: function(element,t) {
	   	element = $(element);
    	element.style.top = t +"px";
	},
	setSrc: function(element,src) {
    	element = $(element);
    	element.src = src; 
	},
	
	setAlt: function(element,alt) {
    	element = $(element);
    	element.alt = alt; 
	},
	
	setOpacity: function(element,opacity) {
    	element = $(element);
    	element.style.opacity = opacity; 
	},

	setHref: function(element,href) {
    	element = $(element);
    	element.href = href; 
	},
	setInnerHTML: function(element,content) {
		element = $(element);
		element.innerHTML = content;
	},

	hide: function(element) {
      	element = $(element);
      	element.style.display = 'none';
  	},

  	show: function(element) {
      	element = $(element);
      	element.style.display = 'inline';
  	},
  	
  	showBlock: function(element) {
      	element = $(element);
      	element.style.display = 'block';
  	}
});

// ---------------------------------------------------
// addLoadEvent()
// Adds event to window.onload without overwriting currently assigned onload functions.
// Function found at Simon Willison's weblog - http://simon.incutio.com/
//
function addLoadEvent(func)
{	
	var oldonload = window.onload;
	if (typeof window.onload != 'function'){
    	window.onload = func;
	} else {
		window.onload = function(){
		oldonload();
		func();
		}
	}

}

// ---------------------------------------------------

function run() {
	start();
}

addLoadEvent(run);	// run initMooshow onLoad

// --------------------------------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------------------------------
