// editMedia.js for edit media, Only Photo and Video have edit page

var mediaList =  new Array();
var currentMediaIndex = -1;   
var numOfMedias = 0;       //numOfMedias   
var photoRotated = false;
var collectionName = "";
var edenCommands = new Array(4);
var imageIndex = 0;
var mediaName=""
var previousMediaName=""
var nextMediaName=""
var mediaType = "I"
var mediaTitle="";
var mediaDescription="";
var numberOfSlideshowPhotos = 0;
var currentAlbum = '';
var selectedMedia = null; //for slideshow, selected media on other than current pages

function setMediaType(type){
    mediaType = type;
}

function setMediaName(name) {
	mediaName = name;
}

function setPreviousMediaName(name) {
	previousMediaName = name;
}

function setNextMediaName(name) {
	nextMediaName = name;
}


function setMediaTitle(name) {
	mediaTitle = name;
}

function setedenCommands(){
  for (var i = 0; i < edenCommands.length; i++) {
    edenCommands[i] = new EdenCommand("com.casero.abode.media.image.ImageEditor", null, "rotate");
    edenCommands[i].addParam("mediaName", mediaName);
    edenCommands[i].addParam("rotation", i * 90);
  }
}

function setCollectionName(name){
    collectionName = name;
}

function setMediaList(mediaArray){
    mediaList = mediaArray;
    numOfMedias = mediaArray.length;
}

function submitForm(formName){
    var obj = document.getElementById(formName);
    obj.submit();
}

function callback_EditDescription(description){
    document.getElementById('displayDescription').innerHTML = description;
    var obj = document.getElementById('photoDescription');
    obj.style.display="block";
} 
   
function setCurrentMediaIndex(mediaIndex){
    currentMediaIndex = mediaIndex;
}

function getIndexOfMedia(mediaName) {
  for(i=0; i<mediaList.length; i++) {
    if (mediaList[i].name == mediaName) {
      return i;
    }
  }
  return -1;
}

function setVideoAsCover(text, mode, action){
		// hideVideoPlayer();
		showWarning(text,mode,action);
}

function ajaxSetCoverPhoto(condition) {
// 		showVideoPlayer();
    if(condition){
	    var url = "/media/setAlbumCover.jsp";
	    var pars = "collectionName=" + collectionName;
	    pars += "&mediaName=" + mediaName;
	    var myAjax = new Ajax.Updater('DynamicScriptSection', url, {method: 'post', parameters: pars, evalScripts: true});
    }
}

function showGuestPreviousMedia(userName) {

  if (currentMediaIndex > 0) {
        currentMediaIndex =  currentMediaIndex - 1;

        document.getElementById('showView_MediaTitle').innerHTML = mediaList[currentMediaIndex].title;
        if("I" == mediaType){
            loadGuestPhoto(currentMediaIndex, userName);        
        }else if("V" == mediaType){
            loadGuestVideo(currentMediaIndex, userName);        
        }
        
        document.getElementById('showView_MediaDescription').innerHTML = mediaList[currentMediaIndex].description;
        
        //ajaxGetDownloadLink();  
        if(document.getElementById('downloadLink') != null)document.getElementById('downloadLink').href = mediaList[currentMediaIndex].downloadsource;     
        
        //update viewfullsize link
        if(document.getElementById('viewfullsize') != null)updateViewFullSizeLink( mediaList[currentMediaIndex].source );     

        //update print link
        if(document.getElementById("printLink") != null)updateGuestPrintLink(userName, collectionName);          
  }
  
  if (currentMediaIndex == 0) {
    document.getElementById('buttonPrevious').disabled = true;
  }
  else {
    document.getElementById('buttonPrevious').disabled = false;
  }
  
  if (currentMediaIndex != numOfMedias - 1) {
    document.getElementById('buttonNext').disabled = false;
  }
}


function showPreviousMedia() {
  if (currentMediaIndex > 0) {
        currentMediaIndex =  currentMediaIndex - 1;
        
        document.getElementById('showView_MediaTitle').innerHTML = mediaList[currentMediaIndex].title;
        if (mediaList[currentMediaIndex].description == ""){
					document.getElementById("showView_MediaDescription").innerHTML="Click to edit description.";
				} else {
        	document.getElementById('showView_MediaDescription').innerHTML = mediaList[currentMediaIndex].description;
				}
        ajaxGetRelatedAlbums();
        ajaxGetDownloadLink();         
        
		titleAjax.leaveEditMode();
		descriptionAjax.leaveEditMode();

        if("I" == mediaType){
            loadPhoto(currentMediaIndex);        
        }else if("V" == mediaType){
            loadVideo(currentMediaIndex);        
        }
        
        //update viewfullsize link
        if(document.getElementById('viewfullsize') != null)updateViewFullSizeLink();
  }
  
  if (currentMediaIndex == 0) {
    document.getElementById('buttonPrevious').disabled = true;
  }
  else {
    document.getElementById('buttonPrevious').disabled = false;
  }
  
  if (currentMediaIndex != numOfMedias - 1) {
    document.getElementById('buttonNext').disabled=false;
  }
}


function ajaxGetRelatedAlbums(){
    var url = "/media/ajaxGetRelatedAlbums.jsp";
    var pars = "collectionName=" + collectionName;
    pars += "&mediaName=" + mediaList[currentMediaIndex].name;
    var myAjax = new Ajax.Updater('DynamicScriptSection', url, {method: 'post', parameters: pars, evalScripts: true});
}   

function ajaxGetDownloadLink(){
    var url = "/media/ajaxGetDownloadLink.jsp";
    var pars = "collectionName=" + collectionName;
    pars += "&mediaName=" + mediaName;
    var myAjax = new Ajax.Updater('DynamicScriptSection', url, {method: 'post', parameters: pars, evalScripts: true});
} 

function displayRelatedAlbums(albums){
    var temp = "<ul>";
    for(i=0; i<albums.length; i++){
        //if(albums[i].name != "shoebox")temp += '<li><a href="/photo/collectionDetails.jsp?collectionName=' + '"' + albums[i].name + '"' + '>' + albums[i].title +  '</a></li>';  
        if(albums[i].name != "Shoebox")temp += "<li><a href='javascript:showAlbum(\"" + albums[i].name + '")' + "'>" + albums[i].title +  '</a></li>';          
    }
    temp += "</ul>";
    document.getElementById('showView_relatedAlbums').innerHTML = temp; 
}

function showAlbum(name){
    window.location.href = getMediaPath() + "collectionDetails.jsp?collectionName=" + name;
}

function getMediaPath(){
    var mediaPath = "/photo/";
    if("V" == mediaType)mediaPath = "/video/";
    if("M" == mediaType)mediaPath = "/document/";
    if("A" == mediaType)mediaPath = "/music/";    
    return mediaPath;
}


function showGuestNextMedia(userName, collectionName) {
  if (currentMediaIndex < (numOfMedias-1) ) {
        currentMediaIndex =  currentMediaIndex + 1;

		document.getElementById('showView_MediaTitle').innerHTML = mediaList[currentMediaIndex].title;        
        if("I" == mediaType){
            loadGuestPhoto(currentMediaIndex, userName);        
        }else if("V" == mediaType){
            loadGuestVideo(currentMediaIndex, userName);        
        }
        
        document.getElementById('showView_MediaDescription').innerHTML = mediaList[currentMediaIndex].description;
        
        //ajaxGetDownloadLink();  
        if(document.getElementById('downloadLink') != null)document.getElementById('downloadLink').href = mediaList[currentMediaIndex].downloadsource;     
        
        //update viewfullsize link
        if(document.getElementById('viewfullsize') != null)updateViewFullSizeLink( mediaList[currentMediaIndex].source );     

        //update print link
        if(document.getElementById("printLink") != null)updateGuestPrintLink(userName, collectionName);  
  }
  if (currentMediaIndex != 0) {
    document.getElementById('buttonPrevious').disabled = false;
  }
  else {
    document.getElementById('buttonPrevious').disabled = true;
  }
  
  if (currentMediaIndex == numOfMedias - 1) {
    document.getElementById('buttonNext').disabled = true;
  }
}

function showNextMedia() {
  if (currentMediaIndex < (numOfMedias-1) ) {
        currentMediaIndex =  currentMediaIndex + 1;

        document.getElementById('showView_MediaTitle').innerHTML = mediaList[currentMediaIndex].title;
				if (mediaList[currentMediaIndex].description == ""){
					document.getElementById("showView_MediaDescription").innerHTML="Click to edit description.";
				} else {
        	       document.getElementById('showView_MediaDescription').innerHTML = mediaList[currentMediaIndex].description;
				}
        ajaxGetRelatedAlbums();        
        ajaxGetDownloadLink();        
        
		titleAjax.leaveEditMode();
		descriptionAjax.leaveEditMode();
        
        if("I" == mediaType){
            loadPhoto(currentMediaIndex);        
        }else if("V" == mediaType){
            loadVideo(currentMediaIndex);        
        }

        //update viewfullsize link
        if(document.getElementById('viewfullsize') != null)updateViewFullSizeLink( mediaList[currentMediaIndex].source );
        
        //update print link
	    if(document.getElementById("printLink") != null)updatePrintLink();        
  }
  if (currentMediaIndex != 0) {
    document.getElementById('buttonPrevious').disabled = false;
  }
  else {
    document.getElementById('buttonPrevious').disabled = true;
  }
  
  if (currentMediaIndex == numOfMedias - 1) {
    document.getElementById('buttonNext').disabled = true;
  }
}

function updateGuestPrintLink(userName, collectionName){
    var referer = encodeURIComponent(window.location);
    var params = "/photo/guestMultiplePrint.jsp?v1=&mediaName=" + mediaList[currentMediaIndex].name + "&referer=" + referer;
    params +=  "&userName=" + userName;    
    params +=  "&collectionName=" + collectionName; 
    document.getElementById('printLink').href = params;    
}

function updatePrintLink(){
    var referer = encodeURIComponent(window.location);
    document.getElementById('printLink').href = "/photo/multiplePrint.jsp?v1=&mediaName=" + mediaList[currentMediaIndex].name + "&referer=" + referer;; 
}

function updateViewFullSizeLink(src){
    //var viewfullsizeLink = "/service/com.casero.abode.media.image.PhotoController?v=getImage&mediaName=" + mediaList[currentMediaIndex].name ;
    document.getElementById('viewfullsize').href = src;  
}

function printAlbum(){
    var checkedCount = 0;
    var params = "";
    
    if (!document.collectionForm.collectionName) {
        //caseroAlert('There are no ' + mediaType + 's to print.');
        showWarning(printAlbumWarning1, 0, "");        
        return;
    }
    
    if (document.collectionForm.collectionName.length != undefined) {
        for (var i = 0; i < document.collectionForm.collectionName.length; i++) {
            if (document.collectionForm.collectionName[i].checked == true) {
                checkedCount++;
                params += "&collectionName=" + document.collectionForm.collectionName[i].value;
            }
        }
    }
    // there is only one mediaName element in the form
    else if (document.collectionForm.collectionName.checked == true) {
        checkedCount++;
        params += "&collectionName=" + document.collectionForm.collectionName.value;
    }else if(document.getElementById('_collectionName') != null && document.getElementById('_collectionName').type == "hidden" && document.getElementById('_collectionName').value != null) {
        /*print album in collectionDetails page*/
        params += "&collectionName=" + document.getElementById('_collectionName').value;        
    }    

    if ((checkedCount != 0) || (params != "")) {        
	    var referer = encodeURIComponent(window.location);
	    window.location = "/photo/multiplePrint.jsp?v=printAlbum&" + params + "&referer=" + referer;   
    }
    else {
        showWarning(printAlbumWarning2, 0, "");
    }   
}

function editPhotoPrint(name){
    var mediaName = name;
    if(mediaName == null || mediaName == ""){
        showWarning(editPhotoPrintWarningPart1 + mediaType + editPhotoPrintWarningPart2, 0, "");     
        return;
    }           
    //var referer = encodeURIComponent(window.location);
    var referer = encodeURIComponent("/photo/editMedia.jsp?mediaName="+mediaName+"&collectionName="+collectionName+"&mediaType=I");
    //v1 is necessary but no use, see request.getQueryString()
    window.location = "/photo/multiplePrint.jsp?v1=&mediaName=" + mediaName + "&referer=" + referer;
}

function printCurrentGuestPhoto(mediaName, userName, collectionName, customerName){
    var referer = encodeURIComponent(window.location);
    var params = "/photo/guestMultiplePrint.jsp?v1=&mediaName=" + mediaName + "&referer=" + referer;
    params +=  "&userName=" + userName;   
    params +=  "&customerName=" + customerName; 
    params +=  "&user=" + userName;    
    params +=  "&collectionName=" + collectionName; 
    window.location = params;
}

function gotoPrintPage(userName){
    mediaType="photo";
    var params = "";

    //Make sure some selection was made
    var checkedCount = 0;
    if (!document.mediaForm.mediaName) {
        //caseroAlert('There are no ' + mediaType + 's to print.');
        showWarning(gotoPrintPageWarning1Part1 + mediaType + gotoPrintPageWarning1Part2, 0, "");        
        return;
    }
    if (document.mediaForm.mediaName.length != undefined) {
        for (var i = 0; i < document.mediaForm.mediaName.length; i++) {
            if (document.mediaForm.mediaName[i].checked == true) {
                checkedCount++;
                params += "&mediaName=" + document.mediaForm.mediaName[i].value;
            }
        }
    }
    // there is only one mediaName element in the form
    else if (document.mediaForm.mediaName.checked == true) {
        checkedCount++;
        params += "&mediaName=" + document.mediaForm.mediaName.value;
    }

    if ((checkedCount != 0) || (params != "")) {
        
        var referer = encodeURIComponent(window.location);
        var obj = document.mediaForm.collectionName;
        var pars = "";
        if(obj != null)pars = "&collectionName=" + obj.value;
        
        if(userName != null){
            //it's guest
             params +=  "&userName=" + userName;        
             window.location = "/photo/guestMultiplePrint.jsp?v1=" + pars + params + "&referer=" + referer;                     
        }else{
            //it's user
            window.location = "/photo/multiplePrint.jsp?v1=" + pars + params + "&referer=" + referer;        
        }
    }
    else {
        showWarning(gotoPrintPageWarning2Part1 + mediaType + gotoPrintPageWarning2Part2, 0, "");
    }    
}

function selectPrintOption(params, isGuest){
    if(document.getElementById('localPrinter').checked){
        if(isGuest != null && isGuest == true){
            window.location.href = "/photo/guestMultiplePrint.jsp?v2=" + params;        
        }else{
            window.location.href = "/photo/multiplePrint.jsp?v2=" + params;        
        }
    }else if(document.getElementById('networkPrinter').checked){
        var url = "/photo/printPhotos.jsp?v2=" + params;
        window.open(url); 
    }else{
        showWarning(selectPrintOptionWarning, 0, "");  
    }
}
    
function printImage() {
    ajaxSave();
    var imageSrc = "/service/com.casero.abode.media.image.PhotoController?v=getLargeImage&mediaName=" + mediaList[currentMediaIndex].name;    
    prnWnd = window.open(imageSrc, "print", "status=no,toolbar=no,scrollbars=no,resizable=no,menubar=no");
    prnWnd.onLoad = preload_complete();
}

function preload_complete() {
   prnWnd.print();
}

function rotateCW() {
		showWarning('The photo is being rotated.<br />Please wait a few seconds while the photo is being processed.', 0, false);
    imageIndex = (imageIndex + 1) % edenCommands.length;    
    photoRotated = true;
    ajaxSave();
}

function rotateAndUpdate(mName, aName){
		rotateCW();
		ajax_get_photo_details(mName, aName);
}

function call_back_photo_refresh(){
    /*if collectionName is null, is not  album photo*/
    var editPage = "editMedia.jsp?mediaName=";
    var listPage = "collectionDetails.jsp?collectionName=";

    if(collectionName == null || collectionName == ""){
        editPage = "editSharedMedia.jsp?mediaName=";
        listPage = "index.jsp?collectionName=";
    }
    if (mediaList[currentMediaIndex + 1] == null) {
        if (currentMediaIndex > 0) {
            // reload the page with the previous one as selected photo
            window.location.href =  editPage + previousMediaName + "&collectionName=" + collectionName;        
        }else {
            // back to thumbnail page
            window.location.href = listPage + collectionName;
        }
    } else {
        // reload the page with the next one as selected photo
        window.location.href = editPage + nextMediaName + "&collectionName=" + collectionName;
    }
}

function ajax_onFailure(request) {
  // refresh the current page
  window.location.href = window.location;
}

function loadPhoto(mediaIndex) {
  //ajaxSave();
  for (var i = 0; i < edenCommands.length; i++) {
    edenCommands[i] = new EdenCommand("com.casero.abode.media.image.ImageEditor", null, "rotate");
    edenCommands[i].addParam("mediaName", mediaName);
    edenCommands[i].addParam("rotation", i * 90);
  }
  new Ajax.Updater('mainPhoto', getLargePhotoURL(mediaIndex), {method: 'post', onFailure: ajax_onFailure, callback: document.getElementById("mainPhoto").src=getLargePhotoURL(mediaIndex)});
}
  
function loadGuestPhoto(mediaIndex, userName){
    new Ajax.Updater('mainPhoto', getGuestLargePhotoURL(mediaIndex, userName), {method: 'post', onFailure: ajax_onFailure, callback: document.getElementById("mainPhoto").src=getGuestLargePhotoURL(mediaIndex, userName)});
}

function getGuestLargePhotoURL(mediaIndex, userName) {
  //return "/gemini/service/com.casero.abode.media.PublicMediaManager?v=getLargeImage&mediaName=" + mediaList[mediaIndex].name + "&user=" + userName;
  return mediaList[mediaIndex].source;
}

function getLargePhotoURL(mediaIndex) {
  //return "/service/com.casero.abode.media.image.PhotoController?v=getLargeImage&mediaName=" + mediaList[mediaIndex].name;
  return mediaList[mediaIndex].source;
}

function loadVideo(mediaIndex) { 
  document.getElementById("videoPlayer").src="/video/videoPlayer.jsp?mediaName=" + mediaName;
}

function loadGuestVideo(mediaIndex, userName) { 
  document.getElementById("videoPlayer").src="/video/videoPlayer.jsp?mediaName=" + mediaName + "&user=" + userName;
}
    
function ajaxSave() {
    if (photoRotated) {
      var cmd = new EdenCommand("com.casero.abode.media.image.ImageEditor", null, "save");
      cmd.addParam("mediaName", mediaName);
      cmd.addParam("rotation", imageIndex * 90);
      cmd.addParam("collectionName", collectionName );
      var myAjax = new Ajax.Updater('DynamicScriptSection', cmd.toString(), {method: 'post', evalScripts: true, asynchronous: false});
      photoRotated = false;
    }
}

function changeAllPhotoListView(){
    window.location.href = "/photo/index.jsp?viewas=" + document.mediaForm.viewas.value + "&queryOption=" +  document.mediaForm.queryOption.value;
}

function changeAlbumListView(){
    window.location.href = "/photo/collections.jsp?viewas=" + document.collectionForm.viewas.value ; //+ "&queryOption=" +  document.collectionForm.queryOption.value;
}

function ajaxEditDescription() {
    var url = "processDescriptionEdit.jsp";
    var pars = "collectionName=" + collectionName;
    pars += "&mediaName=" + mediaList[currentMediaIndex].name;
    pars += "&editDescription=" + document.getElementById('editDescription').value;
    var myAjax = new Ajax.Updater('DynamicScriptSection', url, {method: 'post', parameters: pars, evalScripts: true});
}

function ajaxEditSharing(mediaTypeChar){
    var url = "/media/processEditSharing.jsp";
    var pars = "collectionName=" + collectionName;
    pars += "&sharing=" + document.getElementById('sharing').value;
    if(mediaType != null)pars += "&mediaTypeChar=" + mediaTypeChar;
    var myAjax = new Ajax.Updater('DynamicScriptSection', url, {method: 'post', parameters: pars, evalScripts: true});
}

function setMediaTitle(photoIndex, newTitle){
      mediaTitle = newTitle;
}

function setMediaDescription(newDescription) {
  mediaDescription = newDescription;
}

function confirmDeletePhoto(){
   showWarning(confirmDeletePhotoWarning, 1, 'ajaxDeletePhoto');
}

function confirmDeleteVideo(){
   showWarning(confirmDeleteVideoWarning, 1, 'ajaxDeletePhoto');
}


function ajaxDeletePhoto(){
    var cmd = new EdenCommand("com.casero.abode.media.MediaManager", "../media/ajaxDeleteMedia.jsp", "removeMediaItem");
    cmd.addParam('collectionName', collectionName);
    cmd.addParam("mediaName", mediaName);
    new Ajax.Updater('DynamicScriptSection', cmd.toString(), {method: 'post', onFailure: ajax_onFailure, evalScripts: true});
}

function confirmUnlinkPhoto(){
   showWarning(confirmUnlinkPhotoWarning, 1, 'ajaxUnlinkPhoto');
   
}

/* Depends on /base/js/browserdetect.js */
function confirmUnlinkVideo(){
	 hideVideoPlayer();
	 showWarning(confirmUnlinkVideoWarning, 1, 'ajaxUnlinkPhoto');
}

/* Depends on /base/js/browserdetect.js */
function ajaxUnlinkPhoto(condition){
    if (condition == true) {
	  document.mediaForm.action="/media/editMedia.action?removeItem&previousIndex="+currentMediaIndex+"&collectionName="+collectionName+"&mediaName="+mediaName+"&mediaType="+mediaType;
	  document.mediaForm.submit();
    }
}

function disableButtons() {
    document.getElementById('rotateCW_btn').disabled = true;
}

function enableButtons() {
    document.getElementById('rotateCW_btn').disabled = false;
}

function launchFile(user, file){
    var cmd = new EdenCommand("com.casero.abode.storage.FileController", "", "launchFile");
    cmd.addParam('user', user);
    cmd.addParam("file", file);
    window.location.href = cmd.toString();
}

function launchBackupFile(mediaName){
    var cmd = new EdenCommand("com.casero.abode.media.MediaManager", "", "launchFile");
    cmd.addParam('mediaName', mediaName);
    window.location.href = cmd.toString();
}

function downloadFile(user, file){
    var cmd = new EdenCommand("com.casero.abode.storage.FileController", "", "downloadFile");
    cmd.addParam('user', user);
    cmd.addParam("file", file);
    window.location.href = cmd.toString();
}

function guestSlideShow(userName, collectionName){
    if (collectionName == null || collectionName == "" ) {
        showWarning('There are no photos to slideshow.', 0, "");        
        return;
    }    
      
    var bgURL = '/photo/getSlideshowXML.jsp?v=slideShow'; 
    
    bgURL += "&collectionName=" + collectionName;
    bgURL += "&user=" + userName; 
    bgURL += "&userName=" + userName;        
    
    var w = screen.width-100;
    var h = screen.height-100;
    window.open(bgURL, 'Slideshow', 'width='+w+', height='+h+', menubar=no, toolbar=no, scrollbars=no, status=no, resizable=no');
}

function currentAlbumSlideShow(){
    var checkedCount = 0;
    var params = "";
    
    if (!document.mediaForm.mediaName) {
        showWarning('There are no photos to slideshow.', 0, "");        
        return;
    }    
    if (document.mediaForm.mediaName.length != undefined) {
        for (var i = 0; i < document.mediaForm.mediaName.length; i++) {
            if (document.mediaForm.mediaName[i].checked == true) {
                checkedCount++;
                params += "&mediaName=" + document.mediaForm.mediaName[i].value;
            }
        }
    }
    else if (document.mediaForm.mediaName.checked == true) {
        // there is only one mediaName element in the form
        checkedCount++;
        params += "&mediaName=" + document.mediaForm.mediaName.value;
    }
    
    /* if no media selected, slideshow current album */
    if(checkedCount == 0){
	    if( document.getElementById('collectionName') != null ) {
	        /**/
	        checkedCount++;
	        params += "&collectionName=" + document.getElementById('collectionName').value;        
	    }         
    }

    var bgURL = '/photo/getSlideshowXML.jsp?v=slideShow'; 
    
    if(checkedCount == 0){
        showWarning("no photo selected for slideshow", 0, "");
        return;
    }  
    
    w = screen.width-100;
    h = screen.height-100;
    window.open(bgURL + params, 'Slideshow', 'width='+w+', height='+h+', menubar=no, toolbar=no, scrollbars=no, status=no, resizable=no');
}

function slideShowMedia(userName, entireAlbum, zformName) {
    var whichitem = 0;
    var selecteditem = 0;
    var mediaNames = '';
    var collectionNames = '';
    var elements = null;
    var havePhotos = true;
    var bgURL = '/photo/getSlideshowXML.jsp';

   (zformName) ? zForm = zformName : zForm = "mediaForm";
    
    //If there are media items on the page those will be used. If there 
    //are collections on the page those will be used instead.
    if (document.forms[zForm].mediaName != null) {
        elements = document.forms[zForm].mediaName;
    } 
    else if (document.forms[zForm].collectionName != null) {
    	elements = document.forms[zForm].collectionName;
        havePhotos = false;
    }

	//If we have either media items or collections
    if (elements != null) {    
        if (elements.length != null) {        
            while (whichitem < elements.length) { 
            
            	//If  the element is checked           
                if (elements[whichitem].checked) {
                    var nodeName = elements[whichitem].name;
                    
                    //If we have a media item add it to the paramaters
                    if (nodeName == 'mediaName') {
                        if (mediaNames != '') {
                        	mediaNames = mediaNames + '&';
                        }
                        
                        mediaNames = mediaNames + 'mediaName=' + elements[whichitem].value;
                        selecteditem++;
                    } 
                    else if (nodeName == 'collectionName') {
                    
                        if (collectionNames != '') {
                        	collectionNames = collectionNames + '&';
                        }
                        
                        collectionNames = collectionNames + 'collectionName=' + elements[whichitem].value;
                        
                        /* assure there photos in slideshow*/
                        if(elements[whichitem].accessKey != "") {
                        	havePhotos = true;                        
                        }
                        
                        selecteditem++;
                    }
                }
                
                whichitem++;
            }
        } 
        else {
            //it's for guest
            havePhotos = true;
            currentAlbum = document.forms[zForm].collectionName.value;
            if (elements.checked) {
                var nodeName = elements.name;
                if (nodeName == 'mediaName') {
                    if (mediaNames != '') mediaNames = mediaNames + '&';
                    mediaNames = mediaNames + 'mediaName=' + elements.value;
                    selecteditem++;
                } else if (nodeName == 'collectionName') {
                    if (collectionNames != '') collectionNames = collectionNames + '&';
                    collectionNames = collectionNames + 'collectionName=' + elements.value;
                    selecteditem++;
                }
            }
        }
    }

    if (selectedMedia != null) {
        for (i = 0; i< selectedMedia.length; i++)
        {
            if (mediaNames != '') {
            	mediaNames = mediaNames + '&';
            }
            
            if (mediaNames.indexOf(selectedMedia[i]) == -1) {
            	mediaNames = mediaNames + 'mediaName=' + selectedMedia[i];
            }
        }
    }        
    
    if ((selecteditem == 1) && (mediaNames != '')) {
        var pars = 'user=' + userName + '&' + mediaNames;
    } 
    else if ((mediaNames != '') || (collectionNames != '')) {
        if ((collectionNames != '') && (mediaNames != '')) collectionNames = '&' + collectionNames;
        var pars = 'user=' + userName + '&' + mediaNames + collectionNames;
    } 
    else {
        var pars = 'user=' + userName + '&';
        if (currentAlbum != "") {
            pars = 'collectionName=' + currentAlbum;
        }           
    }
    
    if(havePhotos){
			w = screen.width-100;
			h = screen.height-100;
			window.open(bgURL + '?' + 'user=' + userName + '&' + pars, 'Slideshow', 'width='+w+', height='+h+', menubar=no, toolbar=no, scrollbars=no, status=no, resizable=no');
    }
    else {
    	showWarning('No photos have been selected to slideshow.', 0, "");
    }
}

function slideShowMedia_Callback() {
    bgURL='slideshowV2.swf?xmlLocation=/photo/getSlideshowXML.jsp%3Fv=readFromSession';
		window.open(bgURL, 'Slideshow', 'width='+w+', height='+h+', menubar=no, toolbar=no, scrollbars=no, status=no, resizable=no');
		
}

function slideShowOnePhoto_Callback(sURL) {
		window.open(sURL, 'Slideshow', 'width='+w+', height='+h+', menubar=no, toolbar=no, scrollbars=no, status=no, resizable=no');
}

function slideShowMedia_Callback2(count) {
    if (count > 0) {
			bgURL='slideshowV2.swf?xmlLocation=/photo/getSlideshowXML.jsp%3Fv=readFromSession';
			window.open(bgURL, 'Slideshow', 'width='+w+', height='+h+', menubar=no, toolbar=no, scrollbars=no, status=no, resizable=no');
    }
}

function showConfirmSetCover(){

}

function hideVideoPlayer(){
	/*
	if(document.getElementById("videoPlayer")){
		document.getElementById("videoPlayer").style.visibility="hidden";
	}
	*/
}

function showVideoPlayer(){
	/*
	
	if(document.getElementById("videoPlayer")){
		document.getElementById("videoPlayer").style.visibility="visible";
	}
	 
	*/
}

function ajax_get_photo_details(mediaName, albumTitle, collectionName){  
	var url = "/photo/photoDetails.jsp";
    var params = "mediaName="+mediaName + "&albumTitle=" + albumTitle + "&collectionName=" + collectionName;
	var myAjax = new Ajax.Updater('editPhoto', url, {method: 'post', parameters: params, evalScripts: true});   
}

function ajax_get_video_details(mediaName){  
	var url = "/video/videoDetail.jsp";
    var params = "mediaName="+mediaName;
	var myAjax = new Ajax.Updater('editVideo', url, {method: 'post', parameters: params, evalScripts: true});   
}

function showNextPhoto() {
  if (currentMediaIndex < (numOfMedias-1) ) {
        currentMediaIndex =  currentMediaIndex + 1;
		ajax_get_photo_details(mediaList[currentMediaIndex].name);
		
	    setCurrentMediaIndex(currentMediaIndex);
		
        document.getElementById('showView_MediaTitle').innerHTML = mediaList[currentMediaIndex].title;
        
		titleAjax.leaveEditMode();
  }
  if (currentMediaIndex != 0) {
    document.getElementById('buttonPrevious').disabled = false;
  }
  else {
    document.getElementById('buttonPrevious').disabled = true;
  }
  
  if (currentMediaIndex == numOfMedias - 1) {
    document.getElementById('buttonNext').disabled = true;
  }
}

function showPreviousPhoto() {
  if (currentMediaIndex > 0) {
        currentMediaIndex =  currentMediaIndex - 1;
        ajax_get_photo_details(mediaList[currentMediaIndex].name);
        
        setCurrentMediaIndex(currentMediaIndex);
        	    
        document.getElementById('showView_MediaTitle').innerHTML = mediaList[currentMediaIndex].title;

		titleAjax.leaveEditMode();
  }
  
  if (currentMediaIndex == 0) {
    document.getElementById('buttonPrevious').disabled = true;
  }
  else {
    document.getElementById('buttonPrevious').disabled = false;
  }
  
  if (currentMediaIndex != numOfMedias - 1) {
    document.getElementById('buttonNext').disabled=false;
  }
}


function showNextVideo() {
  if (currentMediaIndex < (numOfMedias-1) ) {
        currentMediaIndex =  currentMediaIndex + 1;
		ajax_get_video_details(mediaList[currentMediaIndex].name);
		
	    setCurrentMediaIndex(currentMediaIndex);
		
        document.getElementById('showView_MediaTitle').innerHTML = mediaList[currentMediaIndex].title;
        
		titleAjax.leaveEditMode();
  }
  if (currentMediaIndex != 0) {
    document.getElementById('buttonPrevious').disabled = false;
  }
  else {
    document.getElementById('buttonPrevious').disabled = true;
  }
  
  if (currentMediaIndex == numOfMedias - 1) {
    document.getElementById('buttonNext').disabled = true;
  }
}

function showPreviousVideo() {
  if (currentMediaIndex > 0) {
        currentMediaIndex =  currentMediaIndex - 1;
        ajax_get_video_details(mediaList[currentMediaIndex].name);
        
        setCurrentMediaIndex(currentMediaIndex);
        	    
        document.getElementById('showView_MediaTitle').innerHTML = mediaList[currentMediaIndex].title;

		titleAjax.leaveEditMode();
  }
  
  if (currentMediaIndex == 0) {
    document.getElementById('buttonPrevious').disabled = true;
  }
  else {
    document.getElementById('buttonPrevious').disabled = false;
  }
  
  if (currentMediaIndex != numOfMedias - 1) {
    document.getElementById('buttonNext').disabled=false;
  }
}

function getDownloadPhotoLink(link){
    	document.location.href= link;
}