// This script file has image.js as a prerequisite
/**
**/

function SlideShowMgr(albumElmtName, previewElmtName, selectorElmtName) {
    this.albumElmtName = albumElmtName;
    this.previewElmtName = previewElmtName;
    this.selectorElmtName = selectorElmtName;
    this.initialize();
}

//addEventListener
//attachEvent

SlideShowMgr.prototype = {
    slideShowDelay: 5,
    initialize: function() {
	this.albumElmt = undefined;
	this.albumWindow = undefined;
	this.previewElmt = undefined;
	this.previewWindow = undefined;
	this.selectorElmt = undefined;
	this.selectorWindow = undefined;
	this.nextSlideNumber = 0;
	this.movieId = "myFlv";
	this.slideShow = undefined;
	this.getAlbumElement();
	this.getAlbumWindow();
	this.getPreviewElement();
	this.getPreviewWindow();
	this.getSelectorElement();
	this.getSelectorWindow();
	this.albumWindow.mySlideShow = this;
	this.previewWindow.mySlideShow = this;
	this.selectorWindow.mySlideShow = this;
    },
    elementIsIframe: function(elmt) {
	return ((undefined != elmt.tagName) && (elmt.tagName.toUpperCase() == "IFRAME"));
    },
    elementIsFrame: function(elmt) {
	return ((undefined != elmt.tagName) && (elmt.tagName.toUpperCase() == "FRAME"));
    },
    getSSElement: function(elmtName, w) {
	var elmt = w.document.getElementById(elmtName);
	if ((undefined == elmt) && (undefined != w.frames)) {
	    elmt = w.frames[elmtName];
	}
	if (undefined == elmt) {
	    if ((undefined != w.parent) && (w != parent)) {
		elmt = this.getSSElement(elmtName, w.parent);
	    }
	}
	if (undefined != elmt) {
	    if (this.elementIsIframe(elmt) || this.elementIsFrame(elmt)) {
		if (undefined != elmt.contentDocument) {
		    elmt = elmt.contentDocument;
		} else if (undefined != elmt.contentWindow) {
		    elmt = elmt.contentWindow.document;
		}
	    } else if (undefined != elmt.document) {
		elmt = elmt.document;
	    }
	}
	return elmt;
    },
    getSSWindowNoElmt: function(elmtName, w) {
	var elmt = w.document.getElementById(elmtName);
	if ((undefined == elmt) && (undefined != w.frames)) {
	    elmt = w.frames[elmtName];
	}
	if (undefined == elmt) {
	    if ((undefined != w.parent) && (w != parent)) {
		elmt = this.getSSWindowNoElmt(elmtName, w.parent);
	    }
	}
	if (undefined != elmt) {
	    if (this.elementIsIframe(elmt)) {
		elmt = elmt.contentWindow;
	    } else if (this.elementIsFrame(elmt)) {
		elmt = elmt.window;
	    } else if ((undefined != elmt.ownerDocument) &&
		       (undefined != elmt.ownerDocument.defaultView) ) {
		elmt = elmt.ownerDocument.defaultView;
	    }
	}
	return elmt;
    },
    getSSWindow: function(elmtName, w) {
	var win = this.getSSElement(elmtName, w);
	if (undefined != win) {
	    win = (undefined != win.contentWindow) ? win.contentWindow : win.ownerDocument.defaultView;
	}
	return win;
    },
    getAlbumElement: function() {
	if (undefined == this.albumElmt) {
	    this.albumElmt = this.getSSElement(this.albumElmtName, window);
	    if (undefined == this.albumElmt) {
		this.albumElmt = document;
	    }
	}
	return this.albumElmt;
    },
    getAlbumWindow: function() {
	if (undefined == this.albumWindow) {
	    this.albumWindow = this.getSSWindow(this.albumElmtName, window);
	    if (undefined == this.albumWindow) {
		this.albumWindow = window;
	    }
	}
	return this.albumWindow;
    },
    getPreviewElement: function() {
	if (undefined == this.previewElmt) {
	    this.previewElmt = this.getSSElement(this.previewElmtName, window);
	}
	return this.previewElmt;
    },
    getPreviewWindow: function() {
	if (undefined == this.previewWindow) {
	    this.previewWindow = this.getSSWindowNoElmt(this.previewElmtName, window);
	    if (undefined == this.previewWindow) {
		this.previewWindow = window;
	    }
	}
	return this.previewWindow;
    },
    getSelectorElement: function() {
	if (undefined == this.selectorElmt) {
	    this.selectorElmt = this.getSSElement(this.selectorElmtName, window);
	}
	return this.selectorElmt;
    },
    getSelectorWindow: function() {
	if (undefined == this.selectorWindow) {
	    this.selectorWindow = this.getSSWindowNoElmt(this.selectorElmtName, window);
	}
	return this.selectorWindow;
    },
    getThumbIndexFromImgId: function(thumbImgId) {
	return parseInt(thumbImgId.slice(3));
    },
    getYScrollOffset: function(theWindow) {
	var iebody = (theWindow.document.compatMode && theWindow.document.compatMode != "BackCompat") ?
	theWindow.document.documentElement : theWindow.document.body;
	return (theWindow.document.all) ? iebody.scrollTop : theWindow.pageYOffset;
    },
    scrollToCurrentPhoto: function() {
	var myss = window.mySlideShow;
	var topThumb = myss.selectorElmt.getElementsByTagName("IMG")[myss.nextSlideNumber];
	var newPos = topThumb.offsetTop + topThumb.parentNode.offsetTop + 5;
	var currentOffset = myss.getYScrollOffset(myss.selectorWindow);
	var fastInc = (newPos - currentOffset) / 5;
	var slowInc = (fastInc > 0) ? 2 : -2;
	
	if (Math.abs(newPos - currentOffset) > slowInc) {
	    myss.selectorWindow.window.scrollTo(0, currentOffset + ((Math.abs(fastInc) > Math.abs(slowInc)) ? fastInc : slowInc));
	    if (myss.getYScrollOffset(myss.selectorWindow) != currentOffset) {
		myss.selectorWindow.scrollNextPos = myss.selectorWindow.setTimeout(myss.scrollToCurrentPhoto, 40);
	    } else {
		myss.nextSlideNumber++;
	    }  
	} else {
	    myss.selectorWindow.window.scrollTo(0, newPos);
	    myss.nextSlideNumber++;
	}
    },
    setImageLoading: function(strImageURI) {
	if (arguments.length < 2) {
	    this.markImageLoadTime();
	    this.previewWindow.defaultStatus = "Done";
	} else {
	    this.lastImageLoadStart = new Date();
	    this.imageLoadTime = 0;
	    this.previewWindow.defaultStatus = "Loading image " + strImageURI + "...";
	}
    },
    getDownloadAnchor: function(base, url) {
	var dlAnchor = this.previewWindow.document.getElementById("downloadAnchor");
	return (undefined != dlAnchor) ? dlAnchor.dlref : "";
    },
    setDownloadAnchor: function(base, url) {
	var dlAnchor = this.previewWindow.document.getElementById("downloadAnchor");
	if (undefined != dlAnchor) {
	    dlAnchor.dlref = base + url;
	    dlAnchor.href = base + url;
	}
	/*
	  else
	  dlAnchor.href = "javascript:downloadImage('"+base+"','"+url+"');";
	*/
    },
    showImageInFrameAfterPreload: function(baseURL, strImageURL, strDownloadURL, caption ) {
	var loadEndDate = new Date();
	var imgObj = this.previewWindow.document.getElementById("theImage");
  
	if (this.previewElmt.getElementsByTagName("A").length > 0) {
	    var previewDoc = this.previewWindow.document;
	    this.setDownloadAnchor(baseURL, strDownloadURL);
	    if (caption == "") caption = "No caption";
	    previewDoc.getElementById("imgCaption").innerHTML = caption;
	    fadeImageIn(imgObj, 12, 8, baseURL + strImageURL);
	}
	this.imageLoadTime = this.lastImageLoadStart - loadEndDate;
    },
    loadNextImageCB: function(objEvent) {
	this.showImageInFrameAfterPreload(this.loadingImage.baseURL,
				     this.loadingImage.previewURI,
				     this.loadingImage.downloadURI,
				     this.loadingImage.imgCaption);
	this.setImageLoading(this.loadingImage.previewURI);
    },
    renderPreviewImage: function( baseURL, strImageURL, strDownloadURL, thumbImgId, caption ) {
	// Loads the image in the background - only replace the current image
	// after the new image has been preloaded.
	this.setImageLoading(strImageURL);

	this.loadingImage = new Image();
	this.loadingImage.onload = loadNextImageCB;
	this.loadingImage.onerror = loadNextImageCB;
	this.loadingImage.onabort = loadNextImageCB;
	
	this.loadingImage.previewURI = strImageURL;
	this.loadingImage.baseURL = baseURL;
	this.loadingImage.downloadURI = strDownloadURL;
	this.loadingImage.imgCaption = caption;

	this.lastImageLoadStart = new Date();
	this.imageLoadTime = 0;
	this.loadingImage.src = baseURL + strImageURL;
	
	if (undefined != thumbImgId ) {
	    topThumb = this.selectorElmt.getElementById(thumbImgId);
	    this.nextSlideNumber = this.getThumbIndexFromImgId(thumbImgId);
	}
	this.selectorWindow.setTimeout(this.scrollToCurrentPhoto,0);
    },
    finishFlash: function() {
	var myss = window.mySlideShow;
	var videoDiv = myss.previewWindow.document.getElementById("videoDiv");
	var imageDiv = myss.previewWindow.document.getElementById("imageDiv");
	imageDiv.style.display="block";
	videoDiv.style.display="none";
	delete myss.flashDuration;
	continueSlideShow();
    },
    renderFlashVideo: function ( baseURL, strFlashURL, strVideoDownloadURL, thumbImgId, caption, duration ) {
	var previewDoc = this.previewWindow.document;
	var videoDiv = previewDoc.getElementById("videoDiv");
	var imageDiv = previewDoc.getElementById("imageDiv");
	
	this.stopSlideShow();
	this.flashDuration = duration;
	videoDiv.style.display="block";
	imageDiv.style.display="none";
	this.setDownloadAnchor(baseURL, strVideoDownloadURL);
	this.previewWindow.showVideo(videoDiv, baseURL + strFlashURL, this.movieId);
	this.nextSlide = this.selectorWindow.setTimeout(this.finishFlash, (this.flashDuration+1)*1000);
	
	if (undefined != thumbImgId ) {
	    topThumb = this.selectorElmt.getElementById(thumbImgId);
	    this.nextSlideNumber = this.getThumbIndexFromImgId(thumbImgId);
	}
	this.selectorWindow.setTimeout(this.scrollToCurrentPhoto,0);
    },
    loadNegatives: function(baseURL, negativeURL) {
	var selFrame = (undefined == this.selectorWindow.frameElement) ? this.selectorWindow : this.selectorWindow.frameElement;
	selFrame.loadSlideShow = function() {
	    window.parent.mySlideShow.loadSlideShow();
	}
	if (selFrame.addEventListener) {
	    selFrame.addEventListener("load", selFrame.loadSlideShow, true);
	} else if (selFrame.attachEvent) {
	    selFrame.attachEvent("onload", selFrame.loadSlideShow, true);
	} else {
	    selFrame.onload = selFrame.loadSlideShow;
	}
	
	var ssUrl = (undefined == baseURL) ? "about:blank" : baseURL + negativeURL;
	if (this.selectorWindow.navigate) {
	    this.selectorWindow.navigate(ssUrl);
	}
	else this.selectorWindow.location = ssUrl;
    },
    markImageLoadTime: function() {
	if (!(this.lastImageLoadStart)) {
	} else {
	    var loadedDate = new Date();
	    this.imageLoadTime = loadedDate.valueOf() - this.lastImageLoadStart.valueOf();
	}
    }, 
    checkAndCancelVideo: function() {
	if (this.flashDuration != undefined) {
	    this.previewWindow.stopFlash(this.movieId);
	    this.selectorWindow.clearTimeout(this.nextSlide);
	    this.nextSlide = 0;
	    delete this.flashDuration;
	}
    },
    showNextSlide: function() {
	if (this.nextSlideNumber >= this.slideShow.length) {
	    this.nextSlideNumber = 0;
	}
	this.checkAndCancelVideo();
	this.slideShow[this.nextSlideNumber]();
	this.previewWindow.focus();
    },
    showPreviousSlide: function() {
	if (this.nextSlideNumber > 1) {
	    this.nextSlideNumber -= 2;
	}
	else {
	    this.nextSlideNumber = this.slideShow.length - 2 + this.nextSlideNumber;
	}
	this.checkAndCancelVideo();
	this.slideShow[this.nextSlideNumber]();
	this.previewWindow.focus();
    },
    runNextSlide: function() {
	var myss = window.mySlideShow;
	var timeoutValue = myss.slideShowDelay;
	if (!myss.lastImageLoadStart ||
	    (myss.imageLoadTime && (myss.imageLoadTime > 0) )
	    ) {
	    // Add to the timeout to ensure the photo is visible for at least
	    //   slideShowDelay seconds.
	    var timeNow = new Date();
	    var waitSpan = 0;
	    if (myss.lastImageLoadStart) {
		waitSpan = (myss.slideShowDelay * 1000) - ((timeNow.valueOf() - myss.lastImageLoadStart.valueOf()) - myss.imageLoadTime);
	    }
	    if (waitSpan > 0) timeoutValue = waitSpan / 1000;
	    else myss.showNextSlide();
	}
	if (undefined == myss.flashDuration) {
	    // Suppress movement to next slide until flash signals completion
	    if (myss.nextSlideNumber > -1){
		myss.nextSlide = myss.selectorWindow.setTimeout(myss.runNextSlide, timeoutValue*1000);
	    } else {
		if (myss.nextSlide) delete myss.nextSlide;
	    }
	}
    }, 
    refreshSelector: function() {
	var img = document.getElementById("theImage");
	var pfOnload = this.selectorWindow.onload;
	this.selectorWindow.location.reload();
	this.slideShow[this.nextSlideNumber]();
	this.selectorWindow.focus();
    },
    showEditForm: function() {
	this.stopSlideShow();
	var previewDoc = this.previewWindow.document;
	var editDivObj = previewDoc.getElementById("editDiv");
	var editFormObj = previewDoc.getElementById("editForm");
	var captionObj = previewDoc.getElementById("imgCaption");
	editFormObj.caption = captionObj.firstChild.nodeValue;
	editDivObj.style.display = "block";
	editDivObj.style.zIndex = 99;
	/*
	*/
    },
    submitEditForm: function() {
	var previewDoc = this.previewWindow.document;
	var editDivObj = previewDoc.getElementById("editDiv");
	var editFormObj = previewDoc.getElementById("editForm");
	var imgName = this.getDownloadAnchor();
	var request = new Image();
	var rotdeg = editFormObj.rotdeg.value;
	var caption = editFormObj.caption.value;
	var paramStr = "";
	if (rotdeg != "0") {
	    paramStr = "&rotdeg=" + encodeURIComponent(rotdeg);
	}
	if (caption != "") {
	    paramStr = paramStr + "&caption=" + encodeURIComponent(caption);
	}
	request.src = "/photos/cgi-bin/editImage.pl?img=" + encodeURIComponent(imgName) + paramStr;
	this.refreshSelector();
	this.cancelEditForm(false);
    },
    cancelEditForm: function(continueShow) {
	var previewDoc = this.previewWindow.document;
	var editDivObj = previewDoc.getElementById("editDiv");
	var editFormObj = previewDoc.getElementById("editForm");
	editDivObj.style.display = "none";
	editDivObj.style.zIndex = 0;
	editFormObj.reset();
	if ((undefined == continueShow) || continueShow) {
	    this.continueSlideShow();
	}
    },
    downloadImage: function(x,y){
	myTempWindow = window.open(x+y,"","left=10000,screenX=10000");
	myTempWindow.document.execCommand("SaveAs",null,y);
	myTempWindow.close();
    },
    playSoundtrack: function() {
	var soundCtrl = this.albumElmt.getElementById('soundtrack');
	if(soundCtrl && (undefined != soundCtrl.CanPlay)) {
	    if(soundCtrl.CanPlay()) { soundCtrl.doPlay(); }
	} else if (soundCtrl && soundCtrl.controls) {
	    soundCtrl.controls.play();
	}
    },
    pauseSoundtrack: function() {
	var soundCtrl = this.albumElmt.getElementById('soundtrack');
	if(soundCtrl && (undefined != soundCtrl.CanPause)) {
	    if(soundCtrl.CanPause()) { soundCtrl.doPause(); }
	} else if (soundCtrl && soundCtrl.controls) {
	    soundCtrl.controls.pause();
	}
    }, 
    changeSlideControl: function(slideFunction) {
	var changeAnchor = null;
	var allAnchors = null;
	
	/*ACCESSIBILITY DEFECT*/
	allAnchors = this.previewElmt.getElementsByTagName("A");
	
	for (var i = 0; i < allAnchors.length; i++) {
	    if(allAnchors[i].name == "showController") {
		changeAnchor = allAnchors[i];
		break;
	    }
	}
	
	if (null != changeAnchor) {
	    var newImage = "";
	    var newLabel = "";
	    try {
		switch(slideFunction) {
		case "stopSlideShow":
		    newImage = "/images/sspause.gif";
		    newLabel = "Pause Show";
		    if (undefined == this.flashDuration) {
			this.playSoundtrack();
		    }
		    break;
		case "continueSlideShow":
		    newImage = "/images/ssplay.gif";
		    newLabel = "Play Show";
		    if (undefined == this.flashDuration) {
			this.pauseSoundtrack();
		    }
		    break;
		default:
		    break;
		}
	    }
	    catch(ex) {
		alert(ex);
	    }
	    changeAnchor.innerHTML = "<img src='" + newImage + "' alt='"+newLabel+"'>";
	    changeAnchor.href = "javascript:parent." + slideFunction + "();";
	}
    },
    stopSlideShow: function() {
	if (this.flashDuration != undefined) {
	    this.previewWindow.pauseFlash(this.movieId);
	}
	if (this.nextSlide) {
	    this.selectorWindow.clearTimeout(this.nextSlide);
	    this.nextSlide = 0;
	}
	this.changeSlideControl("continueSlideShow");
    },
    continueSlideShow: function() {
	if (this.flashDuration != undefined) {
	    this.previewWindow.playFlash(this.movieId);
	    this.nextSlide = this.selectorWindow.setTimeout(this.finishFlash, (this.flashDuration+1)*1000);
	} else  if (!this.nextSlide) {
	    this.nextSlide = this.selectorWindow.setTimeout(this.runNextSlide, 0);
	}
	this.changeSlideControl("stopSlideShow");
    },
    loadSlideShow: function() {
	var hasArgs = (0 <= top.document.URL.indexOf("?"));
	var myArgs = hasArgs ? top.document.URL.split("?",2)[1] : "";
	
	/*ACCESSIBILITY DEFECT*/
	try {
	    this.stopSlideShow();
	} catch (ex) {
	    alert(ex);
	}
	
	// Here we should reset the photoselector content by args
	this.selectorElmt = undefined;
	this.getSelectorElement();
	var allImages = this.selectorElmt.getElementsByTagName("IMG");
	
	if ( 0 < allImages.length) {
	    this.slideShow = new Array();
	    for (var i = 0; i < allImages.length; i++) {
		this.slideShow[i] = allImages[i].onclick;
		allImages[i].id="img" + i;
		allImages[i].photoNo= i;
	    }
	    // Try to force binding of new id attributes...
	    //   possible getElementsByTagName side effect
	    allImages = this.selectorElmt.getElementsByTagName("IMG");
	    this.nextSlideNumber = 0;
	    this.lastImageLoadStart = 0;
	    this.imageLoadTime = 0;
	    
	    this.nextSlide = this.selectorWindow.setTimeout(this.runNextSlide, 0);
	    this.changeSlideControl("stopSlideShow");


	    this.selectorElmt.showFlashInFrame = function( baseURL, strFlashURL, strVideoDownloadURL, strFrame, thumbImgId, caption, duration) {
		window.parent.mySlideShow.renderFlashVideo( baseURL, strFlashURL, strVideoDownloadURL, thumbImgId, caption, duration);
	    }
	    this.selectorElmt.showImageInFrame = function( baseURL, strImageURL, strDownloadURL, strFrame, thumbImgId, caption ) {
		window.parent.mySlideShow.renderPreviewImage( baseURL, strImageURL, strDownloadURL, thumbImgId, caption );
	    }
	}
    }
};

function downloadImage(base, url) {
    mySlideShow.downloadImage();
}

function loadNextImageCB() {
    mySlideShow.loadNextImageCB();
}

function loadNegatives(target, baseURL, negativeURL) {
    mySlideShow.loadNegatives(baseURL, negativeURL);
}

function previewImageLoad() {
    mySlideShow.markImageLoadTime();
}

function showNextSlide() {
    mySlideShow.showNextSlide();
}

function showPreviousSlide() {
    mySlideShow.showPreviousSlide();
}

function stopSlideShow() {
    mySlideShow.stopSlideShow();
}

function continueSlideShow() {
    mySlideShow.continueSlideShow();
}

function showEditForm() {
    mySlideShow.showEditForm();
}

function submitEditForm() {
    mySlideShow.submitEditForm();
}

function cancelEditForm() {
    mySlideShow.cancelEditForm();
}

function editCaption() {
    mySlideShow.editCaption();
}

function rotateImg() {
    mySlideShow.rotateImg();
}

function initializeMySlideShow(albumsLabel, previewLabel, negativesLabel, startAlbum, menutree) {
    if (undefined == mySlideShow) {
	if (window != window.parent) mySlideShow = window.parent.mySlideShow;
	if (undefined != window.opener) {
	    try {
		mySlideShow = window.opener.mySlideShow;
	    } catch(ex) {
		window.status = ex;
	    }
	}
	if (undefined == mySlideShow) {
	    mySlideShow = new SlideShowMgr(albumsLabel, previewLabel,
					   negativesLabel);
	}
	if (undefined != menutree) {
	    try {
		addNavigationToTree(menutree, startAlbum, true);
	    } catch(ex) {
		alert(ex);
	    }
	}
    }
}

var mySlideShow = undefined;

