﻿// JScript File

function SetFormAddButton()
{
    var submitbutton = document.getElementById('egalSubmitButtonId');
    submitbutton.value = LocEarthGallery.Add;
    submitbutton.onclick = function(){ AddAlbum();  };
}
function SetFormUpdateButton()
{
    var submitbutton = document.getElementById('egalSubmitButtonId');
    submitbutton.value = LocEarthGallery.Update; 
    submitbutton.onclick = function(){ UpdateAlbum();  };
}
function SetFormCancelButton()
{
    var cancelbutton = document.getElementById('egalCancelButtonId');
    cancelbutton.value = LocEarthGallery.Cancel;
    cancelbutton.onclick = function(){ HideAddForm();  };
}
function GoPhoto(url, id)
{
    var res = earthControls.earthGallery.ServiceFacade.PhotoTrack(id);
    if(res.error != null)
        alert(res.error.Message);
    else
        window.location = url;
}  
function GoAlbum(url, id)
{
    var res = earthControls.earthGallery.ServiceFacade.TrackAlbum(id);
    if(res.error != null)
        alert(res.error.Message);
    else
        window.location = url;
}    
function SavePhotoTitle(id, ezyEditId)
{
    var res = earthControls.earthGallery.ServiceFacade.SavePhotoTitle(id, ezyEdit.GetValue(ezyEditId));
    if(res.error != null)
        alert(res.error.Message);
    else
        ezyEdit.ShowChanges(ezyEditId);
}
function SavePhotoDescription(id, ezyEditId)
{
    var desc = ezyEdit.GetValue(ezyEditId);
    var res = earthControls.earthGallery.ServiceFacade.SavePhotoDescription(id, desc);
    if(res.error != null)
        alert(res.error.Message);
    else
        ezyEdit.ShowChanges(ezyEditId);
}
function SaveAlbumTitle(id, ezyEditId)
{
    var res = earthControls.earthGallery.ServiceFacade.SaveAlbumTitle(id, ezyEdit.GetValue(ezyEditId));
    if(res.error != null)
        alert(res.error.Message);
    else
        ezyEdit.ShowChanges(ezyEditId);
}
function SaveAlbumDescription(id, ezyEditId)
{
    var desc = ezyEdit.GetValue(ezyEditId);
    var res = earthControls.earthGallery.ServiceFacade.SaveAlbumDescription(id, desc);
    if(res.error != null)
        alert(res.error.Message);
    else
        ezyEdit.ShowChanges(ezyEditId);
}

var _id = '';
function DeletePhoto(id, photoFilename)
{
    _id = id;
    var arr = photoFilename.split('.');
    var thumbnail = arr[0] + "_lsq." + arr[1];
    var img = String.format("<img src='{0}'/>", earthGallery_PhotoFolderUrl + "/" + thumbnail);
    var html = String.format("<table><tr><td>{0}</td><td valign='top'>{1}",img, LocEarthGallery.PhotoDeleteMsg);
    ShowPrompt(LocEarthGallery.Warning, html, function(){ OnDelete(id, photoFilename);});
}
function OnDelete(id, photoFilename)
{
    HidePrompt();
    ShowProgress('Deleting photo now ...');
    earthControls.earthGallery.ServiceFacade.DeletePhoto(id, photoFilename, DeletePhoto_callback);
}
function DeletePhoto_callback(res)
{       
    if(res.error != null)
        alert(res.error.Message);
    else
    {
        var qs = new Querystring().get("album");
        if(qs != null)
            window.location = earthGallery_AlbumPageUrl + '?album=' + qs;
        else
            window.location = earthGallery_PhotoListPageUrl;
    }
}
// photo privacy ------------------------------------
function ShowPrivacyEzyPick(id, ezyPickId)
{
    var ep = new earth.EzyPick(); 
    ep.ShowEzyPick(ezyPickId, function(){SavePhotoPrivacy(id, ezyPickId);}, function(){}); 
}
function SavePhotoPrivacy(id, ezyPickId)
{
    var privacySetting = ezyPick.GetValue(ezyPickId);
    if(privacySetting == 1) privacySetting = true;
    else privacySetting = false;
    earthControls.earthGallery.ServiceFacade.UpdatePhotoPrivacy(id, ezyPickId, privacySetting, SavePhotoPrivacy_callback);
}
function SavePhotoPrivacy_callback(res)
{
    if(res.error != null)
        alert(res.error.Message);
    else
    {
        var arr = res.value;
        var eleId = arr[0];
        var text = (arr[1] == 'True') ? LocEarthGallery.Public : LocEarthGallery.Private;
        document.getElementById(eleId).innerHTML = text;
    }
}

function DisplayLoading(id)
{
    document.getElementById(id).style.position = 'absolute';
    document.getElementById(id).innerHTML = 'Loading...';
}
function UnloadLoading(id)
{
    document.getElementById(id).style.position = 'static';
}
// Add to album ------------------------------------
function ShowAddToAlbumEzyPick(id, ezyPickLayerId)
{
    DisplayLoading(ezyPickLayerId);
    earthControls.earthGallery.ServiceFacade.GetAlbumList(id, ezyPickLayerId, ShowAddToAlbumEzyPick_callback);
}
function ShowAddToAlbumEzyPick_callback(res)
{
    if(res.error != null)
        alert(res.error.Message);
    else
    {
        var ezyPickReturnObj = res.value;
        UnloadLoading(ezyPickReturnObj.LayerId);        
        document.getElementById(ezyPickReturnObj.LayerId).innerHTML = ezyPickReturnObj.Html;
        var ep = new earth.EzyPick(); 
        ep.ShowEzyPick(ezyPickReturnObj.Id, 
                function(){SaveAddToAlbum(ezyPickReturnObj.Id, ezyPickReturnObj.LayerId);}, 
                function(){}); 
    } 
}
function SaveAddToAlbum(id, layerid)
{
    var selection = ezyPick.GetValue(id);            
    var res = earthControls.earthGallery.ServiceFacade.AddAlbumToPhoto(id, selection);
    if(res.error != null)
        alert(res.error.Message);
    else
        document.getElementById('earthGalleryPhotoError_' + id).innerHTML = LocEarthGallery.AddPhotoToAlbum;
}
function ToggleRelatedAlbum(id)
{
    var ele = document.getElementById(id); 
    ele.style.display=( (ele.style.display=='none') ? '' : 'none');
}
function OnDirectionClick(albumId, photoId)
{
    earthControls.earthGallery.ServiceFacade.GetPreviousNextHtml(albumId, photoId, OnDirectionClick_callback);
}
function OnDirectionClick_callback(res)
{
    if(res.error != null)
        alert(res.error.Message);
    else
        document.getElementById('related_' + collectionId).innerHTML = res.value;
}