
var moz = (typeof document.implementation != 'undefined') && (typeof document.implementation.createDocument != 'undefined');
var ie = (typeof window.ActiveXObject != 'undefined');

// get current photo id from URL
var thisURL = document.location.href;
var splitURL = thisURL.split("#");
var photoId = splitURL[1] - 1;
// if no photoId supplied then set default
var photoId = (!photoId)? 0 : photoId;
var photoId = (photoId < 0)? 0 : photoId;

// CSS border size x 2
//var borderSize = 0;

// Photo directory for this gallery
//var photoDir = "photos/01/";
var photoDir = "startimages/";


var popupBox = 'PopupContainer';
var albumIndex = 'AlbumIndexContainer';
var albumSpecial = 'SpecialContainer';
var albumIndexText = 'AlbumIndex';
var albumSpecialIndexText = 'AlbumFeaturesIndex';
var albumPage = 'AlbumPageContainer';
var titleContainer = 'TitleContainer';
var titlePhoto = 'TitlePhoto';
var photoBox = 'PhotoContainer';
var specialPhotoBox = 'SpecialPhotoContainer';
var specialBox = 'SpecialContainer';
var mainBox = 'MainContainer';
var photo = 'Photo';
var specialPhoto = 'SpecialPhoto';
var sectionTitle = 'SectionTitle';
var caption = 'Caption';
var prevLink = 'PrevLink';
var nextLink = 'NextLink';
var photoCover = 'PhotoCover';
var specialCounter = 'AlbumFeatureCounter';
var specialCaption = 'AlbumFeatureCaption';
var videoLinks = 'LinksPanel';
var videoLinksText = 'LinksText';
var videoPanel = 'VideoPanel';
var coverPanelHeight = 470;

// Define each photo's name, height, width, and caption
var photoArray = new Array();
// Number of photos in this gallery
var photoNum = photoArray.length;

var nextImageToLoad = new Image();
var adjoiningImagesToLoad = new Array();

var specialPhotoArray = new Array();

var videoArray = new Array();

var xmlDoc;

var titleImage;
var titleImageWidth;
var titleImageHeight;

var slideShowMode = 'no';
var slideShowTime = 6000;

var specialFeatures;

/* XML File Handling */

function importXML(file) {
	if (moz) {
		xmlDoc = document.implementation.createDocument("", "", null);
		xmlDoc.async = false;
		xmlDoc.onload = readXML;
	} 
	else if (ie) {
		xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
		xmlDoc.async = false;
		while(xmlDoc.readyState != 4) {};
	}
	//alert('reading file');
	xmlDoc.load(file);
	if (ie) {
		readXML();
	}
}

// FLICKR: New method added to read XML from Flickr-based Generator
function importXMLfromURL(file) {
	var req;
	if(window.XMLHttpRequest) {
		req = new XMLHttpRequest();
	}
	else {
		//For IE6 and older
		req=new ActiveXObject("Microsoft.XMLHTTP");
	}
	if(req == null) {
		alert("AJAX not supported");
	}
	req.open('GET', file, false);
	req.send(null);
	if(req.status == 200) {
		xmlDoc = req.responseXML;
		readXML();
	}
	else
		alert("Something Went Wrong");
}
// ENDFLICKR


function readXML() {
	var album = xmlDoc.getElementsByTagName("album");
	var noOfAlbumTags = xmlDoc.getElementsByTagName("album").length;
	var photolist = album[0].getElementsByTagName("photos");
	var photos = photolist[0].getElementsByTagName("photo");
	photoDir = album[0].getAttribute("folder") + "/";
	// FLICKR: Empty directory if pictures are coming from flickr
	if(photoDir == "flickr/")
		photoDir = "";
	// ENDFLICKR


	titleImage = album[0].getAttribute("titlephoto");
	titleImageWidth = album[0].getAttribute("width");
	titleImageHeight = album[0].getAttribute("height");
	
	photoArray = new Array();
	videoArray = new Array();
	for(i=0; i<photos.length; i++) {
		photoArray[i] = new Array(photos[i].getAttribute("src"), photos[i].getAttribute("width"), photos[i].getAttribute("height"), photos[i].firstChild.nodeValue, photos[i].getAttribute("sectionHeader"), photos[i].getAttribute("indextext"), photos[i].getAttribute("break"));

		var video = photos[i].getElementsByTagName("videolink");
		if(video[0] != null) {
			videoArray[i] = new Array(video[0].getAttribute("src"), video[0].getAttribute("txt"));
		}
	}
	photoNum = photoArray.length;
	
	specialFeatures = album[0].getElementsByTagName("specials");

}


/* Album Index Handling */

function createIndex() {
		var indexText = '<table id=\"IndexTable\"><tr><td id=\"IndexTableCell\"><ul><h2>Journal</h2>';
		for(i=0; i<photoArray.length; i++) {
			var sectionTitle = photoArray[i][4] ;
			var isBreak = photoArray[i][6] ;
			if(sectionTitle == '' || sectionTitle == null) {
			}
			else {
				indexText = indexText + "<li><a class=\"sectionlink\" href=\"#\" onClick=\"showAlbumPhoto(" + i + "); return false;\">" + sectionTitle + "</a></li>";

				//if(i > 0) {
				//	indexText = indexText + "</ul><br>";
				//}
				if(isBreak=='yes') {
					indexText = indexText + "</ul></td><td id=\"IndexTableCell\"><h2>&nbsp;</h2><ul>"
				}
				//indexText = indexText + "<h2>" + sectionTitle + "</h2><ul>";
			}
			//var pageDesc = photoArray[i][5] ;
			//indexText = indexText + "<li><a href=\"#\" onClick=\"showAlbumPhoto(" + i + "); return false;\">" + pageDesc + "</a></li>";
		}
		indexText = indexText + "</ul></td></tr></table>";
		Element.setInnerHTML(albumIndexText, indexText);
		
		//Check if special features exist
		
		Element.setInnerHTML(albumSpecialIndexText, '');
		if(specialFeatures[0] == null) {
			return;
		}
		var specials = specialFeatures[0].getElementsByTagName("special");
		if(specials.length > 0) {
			createSpecialIndex();
		}
		else {
			var movies = specialFeatures[0].getElementsByTagName("movie");
			if(movies.length > 0) {
				createSpecialIndex();
			}
		}

	
}

function hideIndexAction(){
		// Get current width, subtracting CSS border size
		var hCoverCur = Element.getHeight(albumIndex);

		// Get new height, subtracting CSS border size
		var hCoverNew = 2;

		// Scalars based on change from old to new
		var yScale = (hCoverNew / hCoverCur) * 100;
		
		new Effect.Scale(albumIndex, yScale, {scaleX: false, duration: 0.3});
		  
	}


function showIndexAction(){
		//showElementVisibility(photoCover);
		// Get current width, subtracting CSS border size
		var hCoverCur = Element.getHeight(albumIndex);

		// Get new width, subtracting CSS border size
		var hCoverNew = coverPanelHeight + 0;
		
		// Scalars based on change from old to new
		var yScale = (hCoverNew / hCoverCur) * 100;
		
		new Effect.Scale(albumIndex, yScale, {scaleX: false, duration: 0.3, beforeStart: function() {stopSlideshowAction()}});
		  
	}

/* Special Feature Handling */

function createSpecialIndex() {
		var indexText = '';
		var sep = 0;
		
		//Deal with Specials
		var specials = specialFeatures[0].getElementsByTagName("special");

		for(i=0; i<specials.length; i++) {
			if(i>0) {
				indexText = indexText + "<span id=\"AlbumFeaturesSeparator\">///</span>";
			}
			var specialTitle = specials[i].getAttribute("title") ;
			
			indexText = indexText + "<a href=\"#\" onClick=\"showSpecialAlbumPhoto(" + i + "); return false;\">" + specialTitle + "</a>";
			
			sep++;
		}
		
		//Deal with Movies
		var movies = specialFeatures[0].getElementsByTagName("movie");
		
		for(i=0; i<movies.length; i++) {
			if(sep>0) {
				indexText = indexText + "<span id=\"AlbumFeaturesSeparator\">///</span>";
			}
			var movieTitle = movies[i].getAttribute("title") ;
			var movieURL = movies[i].getAttribute("url") ;
			indexText = indexText + "<a href=\"#\" onClick=\"showMovie('" + movieURL + "'); return false;\">" + movieTitle + "</a>";
			
			sep++;
		}
		
		
		Element.setInnerHTML(albumSpecialIndexText, indexText);

		showElementVisibility(albumSpecialIndexText);
}

function hideSpecialAlbumAction(){
		// Get current width, subtracting CSS border size
		var hCoverCur = Element.getHeight(albumSpecial);

		// Get new height, subtracting CSS border size
		var hCoverNew = 2;

		// Scalars based on change from old to new
		var yScale = (hCoverNew / hCoverCur) * 100;
		
		new Effect.Scale(albumSpecial, yScale, {scaleX: false, duration: 0.2});

	}


function showSpecialAction(){
		//showElementVisibility(photoCover);
		// Get current width, subtracting CSS border size
		var hCoverCur = Element.getHeight(albumIndex);

		// Get new width, subtracting CSS border size
		var hCoverNew = coverPanelHeight + 0;
		
		// Scalars based on change from old to new
		var yScale = (hCoverNew / hCoverCur) * 100;
		
		new Effect.Scale(albumIndex, yScale, {scaleX: false, duration: 0.3, beforeStart: function() {stopSlideshowAction()}});
		  
	}

/* Photo and Caption Handling */

function changePhotoAction1() {

		//Fade out photo and caption
		
		var effectDuration = 0.3;
		new Effect.Parallel(
		  [ new Effect.Opacity(photo, {duration: effectDuration, from:1.0, to:0.0}),
		    new Effect.Opacity(caption, {duration: effectDuration, from:1.0, to:0.0}),
		    new Effect.Opacity(sectionTitle, {duration: effectDuration, from:1.0, to:0.0}) ], 
		  {queue: 'front', afterFinish: function() {swapPhotoAction()}}
		);
}

function changePhotoAction2() {

		//Fade in photo and caption
		var effectDuration = 0.3;
		
		//alert('called:' + nextImageToLoad.complete);
		
		/*
		if(!nextImageToLoad.complete) {
			//alert('error:loading called out before complete');
			setTimeout(changePhotoAction2(), 2000);
			return;
		}
		*/


		Element.setSrc(photo, nextImageToLoad.src);

		/*
		new Effect.Parallel(
		  [ new Effect.Opacity(photo, {duration: effectDuration, from:0.1, to:1.0}),
		    new Effect.Opacity(caption, {duration: effectDuration, from:0.1, to:1.0}),
		    new Effect.Opacity(sectionTitle, {duration: effectDuration, from:0.1, to:1.0}) ], 
		  {queue: 'front', beforeStart: function() {swapPhotoAction()}}
		);
		*/
		new Effect.Parallel(
		  [ new Effect.Opacity(photo, {duration: effectDuration, from:0.1, to:1.0}),
		    new Effect.Opacity(caption, {duration: effectDuration, from:0.1, to:1.0}),
		    new Effect.Opacity(sectionTitle, {duration: effectDuration, from:0.1, to:1.0}) ]
		);
}


function swapPhotoAction() {
		// Set source of new image
		Element.setSrc(photo, "c.gif");
		
		nextImageToLoad = new Image();
		nextImageToLoad.onload = function() {changePhotoAction2()};
		nextImageToLoad.src = photoDir + photoArray[photoId][0];

		Element.setWidth(photoBox, photoArray[photoId][1]);
		Element.setWidth(photo, photoArray[photoId][1]);
		
		//next photo
		nxtPhotoId = photoId;
		prvPhotoId = photoId;
		(nxtPhotoId == (photoArray.length - 1)) ? nxtPhotoId = 0 : nxtPhotoId = photoId+1;
		// Figure out which photo is previous
		(prvPhotoId == 0) ? prvPhotoId = photoArray.length - 1 : prvPhotoId = photoId-1;
		
		adjoiningImagesToLoad[0] = new Image();
		adjoiningImagesToLoad[0].src = photoDir + photoArray[nxtPhotoId][0];
		adjoiningImagesToLoad[1] = new Image();
		adjoiningImagesToLoad[1].src = photoDir + photoArray[prvPhotoId][0];
		

		setPhotoCaption();
		//Element.hide('SectionTitleContainer');
		//hideCoverAction();
}


function setPhotoCaption() {
		// Add caption from gallery array
		var capText = photoArray[photoId][3] ;
		Element.setInnerHTML(caption, capText);
		var capTitle = photoArray[photoId][4] ;
		if(capTitle == '' || capTitle == null) {
		  Element.hide(sectionTitle);
		}
		else {
		  Element.setInnerHTML(sectionTitle, capTitle);
		  Element.show(sectionTitle);
		}

		// Add Movie Link
		if(videoArray[photoId] != null) {
			var videoText = videoArray[photoId][1] ;
			var videoSrc = videoArray[photoId][0] ;
			if(videoText == '' || videoText == null) {
			  Element.hide(videoLinks);
			}
			else {
			  //alert(videoText);
			  var videoTextLink = "<a href=\"#\" onclick=\"showVideo('" + videoSrc + "');return false;\">" + videoText + "</a>";
			  Element.setInnerHTML(videoLinksText, videoTextLink);
			  Element.show(videoLinks);
			}
		}
		else {
			Element.hide(videoLinks);
		}
	}


/* Special Photo and Caption Handling */

function changeSpecialPhotoAction1() {

		//Fade out photo and caption
		
		var effectDuration = 0.3;
		new Effect.Parallel(
		  [ new Effect.Opacity(specialPhoto, {duration: effectDuration, from:1.0, to:0.0}),
		    new Effect.Opacity(caption, {duration: effectDuration, from:1.0, to:0.0}),
		    new Effect.Opacity(sectionTitle, {duration: effectDuration, from:1.0, to:0.0}) ], 
		  {queue: 'front', afterFinish: function() {swapSpecialPhotoAction()}}
		);
}

function changeSpecialPhotoAction2() {

		//Fade in photo and caption
		var effectDuration = 0.3;
		
		Element.setSrc(specialPhoto, nextImageToLoad.src);

		new Effect.Parallel(
		  [ new Effect.Opacity(specialPhoto, {duration: effectDuration, from:0.1, to:1.0}),
		    new Effect.Opacity(caption, {duration: effectDuration, from:0.1, to:1.0}),
		    new Effect.Opacity(sectionTitle, {duration: effectDuration, from:0.1, to:1.0}) ]
		);
}


function swapSpecialPhotoAction() {
		// Set source of new image
		Element.setSrc(specialPhoto, "c.gif");
		
		nextImageToLoad = new Image();
		nextImageToLoad.onload = function() {changeSpecialPhotoAction2()};
		nextImageToLoad.src = photoDir + specialPhotoArray[photoId][0];

		Element.setWidth(specialPhoto, specialPhotoArray[photoId][1]);

		Element.setInnerHTML(specialCounter, "[" + (photoId+1) + "/" + specialPhotoArray.length + "]");
		Element.setInnerHTML(specialCaption, specialPhotoArray[photoId][4]);
		
		//next photo
		nxtPhotoId = photoId;
		prvPhotoId = photoId;
		(nxtPhotoId == (specialPhotoArray.length - 1)) ? nxtPhotoId = 0 : nxtPhotoId = photoId+1;
		// Figure out which photo is previous
		(prvPhotoId == 0) ? prvPhotoId = specialPhotoArray.length - 1 : prvPhotoId = photoId-1;
		
		adjoiningImagesToLoad[0] = new Image();
		adjoiningImagesToLoad[0].src = photoDir + specialPhotoArray[nxtPhotoId][0];
		adjoiningImagesToLoad[1] = new Image();
		adjoiningImagesToLoad[1].src = photoDir + specialPhotoArray[prvPhotoId][0];
		

		setPhotoCaption();
		//Element.hide('SectionTitleContainer');
		//hideCoverAction();
}

























/* Title Bar Handling */

function drawTitleImage() {
		new Effect.Opacity(titlePhoto, {duration: 0.1, from:1.0, to:0.1, queue: 'front', afterFinish: function() {showTitleImage();createIndex();}});
}

function showTitleImage() {
		Element.setSrc(titlePhoto, titleImage);
		Element.setWidth(titlePhoto, titleImageWidth);
		Element.setHeight(titlePhoto, titleImageHeight);
		Element.setHeight(titleContainer, titleImageHeight);
		new Effect.Opacity(titlePhoto, {duration: 0.1, from:0.1, to:1.0, queue: 'front', afterFinish: function() {swapPhotoAction()}});
}


/* Action Functions called from Web page */	
function openPortfolio(portId) {
		// FLICKR: Check the file for url
		var fileStarts = portId.substr(0, 4);
		if(fileStarts=="http") {
			importXMLfromURL(portId);
		}
		// ENDFLICKR
		//Element.hide(albumPage);
		else {
			var fileName = portId + ".xml";
			photoId = 0;
			importXML(fileName);
			//drawTitleImage();
		}
		
		//Element.show(popupBox);
		new Effect.Opacity(popupBox, {duration: 0.1, from:1.0, to:0.1, beforeStart: function() {drawTitleImage()}, afterFinish: function() {openPortfolio2()}}); 
		//new Effect.Opacity(popupBox, {duration: 0.1, from:1.0, to:0.1, afterFinish: function() {showElementVisibility(popupBox)}}); 
		//showElementVisibility(popupBox);
		//new Effect.Opacity(popupBox, {duration: 0.7, from:0.1, to:1.0}); 

}

function openPortfolio2() {
		new Effect.Opacity(popupBox, {duration: 0.7, from:0.1, to:1.0, beforeStart: function() {showElementVisibility(popupBox)}}); 
}

function closePortfolio() {
		new Effect.Opacity(popupBox, {duration: 0.7, from:1.0, to:0.1, beforeStart: function() {stopSlideshowAction();showIndexAction()}, afterFinish: function() {hideElementVisibility(popupBox)}}); 
}
	
function showAlbumPhoto(photoToShow) {
		//Clear it out first
		Element.setSrc(photo, "c.gif");
		Element.setInnerHTML(caption, "");
		Element.setInnerHTML(sectionTitle, "");

		photoId = photoToShow;
		//Element.hide(albumIndex);
		showElementVisibility(mainBox);
		hideIndexAction();
		//hideSpecialAlbumAction();
		//Element.show(albumPage);
		////swapPhotoAction();
		changePhotoAction1();
}

function showSpecialAlbumPhoto(featureToShow) {
		specialPhotoArray = new Array();
		var specials = specialFeatures[0].getElementsByTagName("special");
		var photos = specials[featureToShow].getElementsByTagName("photo");


		for(i=0; i<photos.length; i++) {
			specialPhotoArray[i] = new Array(photos[i].getAttribute("src"), photos[i].getAttribute("width"), photos[i].getAttribute("height"), specials[featureToShow].getAttribute("folder"), photos[i].getAttribute("title"));
		}

		//Element.hide(albumIndex);
		showElementVisibility(specialBox);
		hideElementVisibility(mainBox);
		hideIndexAction();
		//hideSpecialAlbumAction();
		//Element.show(albumPage);
		////swapSpecialPhotoAction(0);
		changeSpecialPhotoAction1(0);
}


function nextPhotoAction(){
		// Figure out which photo is next
		(photoId == (photoArray.length - 1)) ? photoId = 0 : photoId++;

		changePhotoAction1();
		if(slideShowMode == 'yes') {
			var mytime=setTimeout('nextPhotoAction()',slideShowTime);
		}
	}

function prevPhotoAction(){
		stopSlideshowAction();
		// Figure out which photo is previous
		(photoId == 0) ? photoId = photoArray.length - 1 : photoId--;

		changePhotoAction1();
	}


function nextSpecialPhotoAction(){
		// Figure out which photo is next
		(photoId == (specialPhotoArray.length - 1)) ? photoId = 0 : photoId++;

		changeSpecialPhotoAction1();
		if(slideShowMode == 'yes') {
			var mytime=setTimeout('nextPhotoAction()',slideShowTime);
		}
	}

function prevSpecialPhotoAction(){
		stopSlideshowAction();
		// Figure out which photo is previous
		(photoId == 0) ? photoId = specialPhotoArray.length - 1 : photoId--;

		changeSpecialPhotoAction1();
	}




function showMovie(movieURL) {
	OpenWindow=window.open("", "moviewin", "height=400,width=400,toolbar=no,scrollbars=no,menubar=no");
	OpenWindow.document.write("<HTML>")
	OpenWindow.document.write("<TITLE>My Embedded Video</TITLE>")
	OpenWindow.document.write("<BODY BGCOLOR=000000>")
	OpenWindow.document.write("<EMBED SRC='" + movieURL + "' AUTOSTART=true></EMBED>")
	OpenWindow.document.write("</HTML>")
	OpenWindow.document.close()
	self.name="main"
}

function showVideo(movieURL) {
	var videoObjectText = "<object width=\"425\" height=\"350\"><param name=\"movie\" value=\"" + movieURL + "\"></param><embed src=\"" + movieURL + "\" type=\"application/x-shockwave-flash\" width=\"425\" height=\"350\"></embed></object><br><a href=\"#\" onclick=\"closeVideo(); return false;\">[ Close ]</a>";
	Element.setInnerHTML(videoPanel, videoObjectText);
	showElementVisibility(videoPanel);
}

function closeVideo() {
	hideElementVisibility(videoPanel);
}

/* Slideshow Methods */

function startSlideshowAction() {
	showAlbumPhoto(0);
	slideShowMode = 'yes';
	var mytime=setTimeout('nextPhotoAction()',slideShowTime);
}

function stopSlideshowAction() {
	slideShowMode = 'no';
}

/* Utility Methods */

function showElementVisibility(elemName)
{
	var myItem = document.getElementById(elemName);
	myItem.style.display = "block";
}

function hideElementVisibility(elemName)
{
	var myItem = document.getElementById(elemName);
	myItem.style.display = "none";
}

/*--------------------------------------------------------------------------*/

// Additional methods for Element added by SU, Couloir
Object.extend(Element, {
	getWidth: function(element) {
   		element = $(element);
   		return element.offsetWidth; 
	},
	setWidth: function(element,w) {
   		element = $(element);
    		element.style.width = w +"px";
	},
	setHeight: function(element,h) {
   		element = $(element);
    		element.style.height = h +"px";
	},
	setSrc: function(element,src) {
    		element = $(element);
    		element.src = src; 
	},
	setHref: function(element,href) {
    		element = $(element);
    		element.href = href; 
	},
	setInnerHTML: function(element,content) {
		element = $(element);
		element.innerHTML = content;
	}
});


// Establish CSS-driven events via Behaviour script
var myrules = {
	'#PrevLink' : function(element){
		element.onclick = function(){
			prevPhotoAction();
			return false;
		}
	},
	'#NextLink' : function(element){
		element.onclick = function(){
			nextPhotoAction();
			return false;
		}
	},
	a : function(element){
		element.onfocus = function(){
			this.blur();
		}
	}
};

// Add window.onload event to initialize
Behaviour.register(myrules);
Behaviour.addLoadEvent(init);
Behaviour.apply();
function init() {
	//Element.hide(popupBox);

	//openPortfolio('italy');
}
