var clientIdPrefix = '';
var selectAlbumFirst = '';
var selectPhotosFirst = '';

// Add Album
function ShowAddAlbumDialog() {
    // get the dialog	
    eXpress.Web.UI.Website.UiService.GetAddAlbumDialog(ShowAddAlbumDialogComplete);
}

function ShowAddAlbumDialogComplete(result) {
    CreateOverlayDialog(result, true, 'dialogSmall addAlbum');
}

function AddAlbum(inputId, RequiredAlbumNameId) {
    var input = $get(inputId);

    if (input != null && input.value != '') {
        eXpress.Web.UI.Website.UiService.CreateAlbum(input.value, AddAlbumComplete);
    }
    else {
        $get(RequiredAlbumNameId).style.display = "";
    }
}

function AddAlbumComplete(result) {
    if (result.Message.length > 0) {
        alert(result);
    }
    else {
        CloseDialog();
        // Goto album.aspx
        window.location = "album.aspx?albumid=" + result.AlbumId;
    }
}

// Delete Album
function ShowDeleteAlbumDialog() {
    // get the dialog
    if (selectedFolders.length == 1) {
        var albumId = selectedFolders[0];
        eXpress.Web.UI.Website.UiService.GetDeleteAlbumDialog(albumId, ShowDeleteAlbumDialogComplete);
    }
    else {
        alert(selectAlbumFirst);
    }
}

function ShowDeleteAlbumDialogComplete(result) {
    CreateOverlayDialog(result, true, 'dialogSmall');
}

function DeleteAlbum(albumId) {
    if (albumId != null) {
        eXpress.Web.UI.Website.UiService.DeleteFolder(albumId, DeleteAlbumComplete);
    }
}

function DeleteAlbumComplete(result) {
    if (result.length > 0) {
        alert(result);
    }
    else {
        selectedFolders = [];
        CloseDialog();
        RefreshAlbum();
    }
}

// Upload pictures
function UploadPictures() {
    if (selectedFolders.length == 1) {
        var albumId = selectedFolders[0];
        window.location = "../upload/advancedupload.aspx?albumid=" + albumId;
    }
    else {
        window.location = "../upload/default.aspx";
    }
}

// Open ShareAlbumPage
function OpenShareAlbumPage() {
    if (selectedFolders.length == 1) {
        window.location = "sharealbum.aspx?albumid=" + selectedFolders[0];
    }
    else {
        alert(selectAlbumFirst);
    }
}

// Sharing: Add More Friends Dialog
function AddMoreFriends(clientIdPrefix) {
    var Friends = new Array();

    var i = 0;
    var j = 0;
    for (i = 1; i <= 4; i++) {
        var FirstName = $get(clientIdPrefix + 'FirstName' + i.toString());
        var LastName = $get(clientIdPrefix + 'LastName' + i.toString());
        var Email = $get(clientIdPrefix + 'Email' + i.toString());

        if (Email.value != '') {
            var Friend = new eXpress.Web.UI.Website.ContactFunctionResult();
            Friend.FirstName = FirstName.value;
            Friend.LastName = LastName.value;
            Friend.Email = Email.value;
            Friends[j] = Friend;
            j++;
        }
    }

    if (Friends.length > 0) {
        eXpress.Web.UI.Website.UiService.AddMoreFriends(Friends, OnAddMoreFriendsComplete);
    }
    else {
        lalert("Mypages.Contacts.Save.RequiredFields");
    }
}

function OnAddMoreFriendsComplete(result) {
    if (result.length > 0) {
    	alert(result);
    	
    	var button = $get('AddMoreFriendsButton');
    	if (button) {
    		EnableExpressButton(button);
    	}
    }
    else {
        RefreshContactsTable()
    }
}

// Sharing: InvitationMailFriends
function SaveInvitationMailFriends(clientIdPrefixDialog) {
    var subject = $get(clientIdPrefixDialog + 'InputSubject');
    var message = $get(clientIdPrefixDialog + 'InputMessage');

    if (subject && message) {
        $get(clientIdPrefix + 'HiddenFieldSubject').value = htmlEncode(subject.value);
        $get(clientIdPrefix + 'HiddenFieldMessage').value = htmlEncode(message.value);

        // Do postback and load InvitationMailFriendsConfirmationDialog 
        GetInvitationMailFriendsConfirmationDialog();
    }
}

function htmlEncode(s) {
    var str = new String(s);
    str = str.replace(/&/g, "&amp;");
    str = str.replace(/</g, "&lt;");
    str = str.replace(/>/g, "&gt;");
    str = str.replace(/"/g, "&quot;");
    return str;
}

// Sharing: Selections
function ShareSelectionChanged(clientIdPrefix) {
    var radioButtonDoShare = $get(clientIdPrefix + 'RadioButtonDoShare');
    var OlShare = $get(clientIdPrefix + 'OlShare')
    var TextboxPublicSharing = $get(clientIdPrefix + 'TextboxPublicSharing')
    var ExpressPanelSharingPrivate = $get(clientIdPrefix + 'ExpressPanelSharingPrivate')
    var CheckboxSharePublic = $get(clientIdPrefix + 'CheckboxSharePublic')
    var CheckboxSharePrivate = $get(clientIdPrefix + 'CheckboxSharePrivate')
    var ButtonContinue = $get(clientIdPrefix + 'ButtonContinue')

    if (radioButtonDoShare) {
        if ((this.event != null) && (this.event.srcElement.id == radioButtonDoShare.id) && (this.event.srcElement.checked)) {
            CheckboxSharePrivate.checked = true;
        }

        if (radioButtonDoShare.checked) {
            OlShare.style.display = '';
            if (CheckboxSharePublic.checked) {
                TextboxPublicSharing.style.display = '';
            }
            else {
                TextboxPublicSharing.style.display = 'none';
            }
            if (CheckboxSharePrivate.checked) {
                ExpressPanelSharingPrivate.style.display = '';
            }
            else {
                ExpressPanelSharingPrivate.style.display = 'none';
            }
        }
        else {
            CheckboxSharePrivate.checked = false;
            CheckboxSharePublic.checked = false;
            OlShare.style.display = 'none';
            TextboxPublicSharing.style.display = 'none';
            ExpressPanelSharingPrivate.style.display = 'none';
        }
    }
}

// Sharing: Private: Selection of friends
function addFriends(clientIdPrefix) {
    var notSharedTable = $get(clientIdPrefix + "notSharedContactsTable")
    var sharedTable = $get(clientIdPrefix + "sharedContactsTable")
    var NotSharedCheckBoxArray = getElementsByClassName(notSharedTable, "input", "cb");
    var i = 0;
    for (i = 0; i < NotSharedCheckBoxArray.length; i++) {
        if (NotSharedCheckBoxArray[i].checked) {
            sharedTable.appendChild($get(clientIdPrefix + "contactsTR_" + NotSharedCheckBoxArray[i].getAttribute("contactId")));
            $get(clientIdPrefix + "checkBoxHelp_" + NotSharedCheckBoxArray[i].getAttribute("contactId")).checked = true;
            // uncheck moved checkboxes
            NotSharedCheckBoxArray[i].checked = false;
        }
    }
}

function removeFriends(clientIdPrefix) {
    var notSharedTable = $get(clientIdPrefix + "notSharedContactsTable")
    var sharedTable = $get(clientIdPrefix + "sharedContactsTable")
    var sharedCheckBoxArray = getElementsByClassName(sharedTable, "input", "cb");
    var i = 0;
    for (i = 0; i < sharedCheckBoxArray.length; i++) {
        if (sharedCheckBoxArray[i].checked) {
        	notSharedTable.appendChild($get(clientIdPrefix + "contactsTR_" + sharedCheckBoxArray[i].getAttribute("contactId")));
        	$get(clientIdPrefix + "checkBoxHelp_" + sharedCheckBoxArray[i].getAttribute("contactId")).checked = false;
            // uncheck moved checkboxes
            sharedCheckBoxArray[i].checked = false;
        }
    }
}

// RenameAlbum
function ShowRenameAlbumDialog() {
    // get the dialog
    if (selectedFolders.length == 1) {
        var albumId = selectedFolders[0];
        eXpress.Web.UI.Website.UiService.GetRenameAlbumDialog(albumId, ShowRenameAlbumDialogComplete);
    }
    else {
        alert(selectAlbumFirst);
    }
}

function ShowRenameAlbumDialogComplete(result) {
    CreateOverlayDialog(result, true, 'dialogSmall editName');
}

function RenameAlbum(albumId) {
    if (albumId != null) {
        var newAlbumName = $get('RenameAlbumInput').value;
        if ((newAlbumName) && (newAlbumName.length > 0)) {
            eXpress.Web.UI.Website.UiService.RenameAlbum(albumId, newAlbumName, RenameAlbumComplete);
        }
    }
}

function RenameAlbumComplete(result) {
    if (result.length > 0) {
        alert(result);
    }
    else {
        selectedFolders = [];
        CloseDialog();
        RefreshAlbum();
    }
}

var onclickEventRotateButton = '';
var onclickEventLeftRotateButton = '';
function ImageRotate(rotateLeft) {
    if (selectedImages.length > 0) {

        // Disable rotate buttons to prevent multiple calls per sec.
        var rotateButton = $get(clientIdPrefix + 'linkRotate');
        var rotateLeftButton = $get(clientIdPrefix + 'linkRotateLeft');
        onclickEventRotateButton = rotateButton.getAttribute("onclick");
        onclickEventLeftRotateButton = rotateLeftButton.getAttribute("onclick");
        rotateButton.setAttribute("onclick", "");
        rotateLeftButton.setAttribute("onclick", "");

        ShowLoadingDialog();

        var obj = new eXpress.Web.UI.Website.ImageTransformationFunctionResult();
        obj.Transformation = new eXpress.ImageLib.TransformationProperties();
        obj.PhotoIds = selectedImages;
        obj.AppendToExistingRotation = true;
        obj.SaveAsCopy = false;

        if (rotateLeft) {
            obj.Transformation.Rotation = 90;
        }
        else {
            obj.Transformation.Rotation = 270;
        }

        obj.Transformation.WhiteBorderWidth = -1
        obj.Transformation.Proportion = -1

        if (obj.PhotoIds.length > 100) {
            // slice the array and show a custom confirm box that only the first 100 images will be rotated
            obj.PhotoIds = obj.PhotoIds.slice(0, 100);
            confirm("PhotoDetail.ImageTransformApply.TooMuchImages", null, function() { ShowLoadingDialog(); eXpress.Web.UI.Website.UiService.ApplyImageTransformation(obj, OnImageTransformApplyComplete); }, function() { CloseDialog(); });
        }
        else {
            eXpress.Web.UI.Website.UiService.ApplyImageTransformation(obj, OnImageTransformApplyComplete);
        }
    }
    else {
        alert(selectPhotosFirst);
    }
}

function OnImageTransformApplyComplete(result) {
    // Enable rotate buttons again.
    if (onclickEventRotateButton) {
        $get(clientIdPrefix + 'linkRotate').setAttribute("onclick", onclickEventRotateButton);
    }
    if (onclickEventLeftRotateButton) {
        $get(clientIdPrefix + 'linkRotateLeft').setAttribute("onclick", onclickEventLeftRotateButton);
    }
    if (result) {
        if (result.Result) {
            RefreshAlbum();
        }
        else {
            alert(result.Message);
        }
    }
}

// Delete Photos
var dialogdelete = false;
function ShowDeletePhotosDialog(detaildialog) {
    dialogdelete = detaildialog;
    if (dialogdelete) {
        eXpress.Web.UI.Website.UiService.GetDeleteFilesDialog(1, ShowDeletePhotosDialogComplete);
    }
    else {
        // get the dialog
        if (selectedImages.length > 0) {
            eXpress.Web.UI.Website.UiService.GetDeleteFilesDialog(selectedImages.length, ShowDeletePhotosDialogComplete);
        }
        else {
            alert(selectPhotosFirst);
        }
    }
}

function ShowDeletePhotosDialogComplete(result) {
    CreateOverlayDialog(result, true, 'dialogSmall');
}

function DeletePhotos() {
    if (dialogdelete) {
        var arr = new Array();
        arr[0] = currentImageId;
        eXpress.Web.UI.Website.UiService.DeleteImages(arr, DeletePhotosComplete);
    }
    else {
        if (selectedImages.length > 0) {
            eXpress.Web.UI.Website.UiService.DeleteImages(selectedImages, DeletePhotosComplete);
        }
        else {
            alert(selectPhotosFirst);
        }
    }
}

function DeletePhotosComplete(result) {
    if (result) {
        selectedImages = [];
        UpdateSelectedImagesCounter();
        CloseDialog();
        RefreshAlbum();
    }
}

// Move Photos
function ShowMovePhotosDialog() {
    // get the dialog
    if (selectedImages.length > 0) {
        eXpress.Web.UI.Website.UiService.GetMovePhotosDialog(ShowMovePhotosDialogComplete);
    }
    else {
        alert(selectPhotosFirst);
    }
}

function ShowMovePhotosDialogComplete(result) {
    CreateOverlayDialog(result, true, 'dialogMedium moveSelected');
}

function MovePhotos(clientIdPrefix, AlbumNameRequired) {
    if (selectedImages.length > 0) {
        var RadioNew = $get(clientIdPrefix + "RadioButtonNew");
        var AlbumName = '';
        var AlbumId = '';
        if ((RadioNew != null) && (RadioNew.checked)) {
            AlbumName = $get(clientIdPrefix + "InputNewAlbum").value;
            if (AlbumName.trim() == "") {
                // show AlbumNameRequired message
                $get(AlbumNameRequired).style.display = "";
                return;
            }
        }
        else {
            AlbumId = $get(clientIdPrefix + "DropDownListAlbums").value;
        }

        eXpress.Web.UI.Website.UiService.MovePhotos(selectedImages, AlbumName, AlbumId, MovePhotosComplete);
    }
    else {
        alert(selectPhotosFirst);
    }
}

function MovePhotosComplete(result) {
    if (result) {
        selectedImages = [];
        UpdateSelectedImagesCounter();
        CloseDialog();
        RefreshAlbum();
    }
}

// Sign Me Out
function ShowSignMeOutDialog() {
    // get the dialog
    if (selectedFolders.length == 1) {
        var albumId = selectedFolders[0];
        eXpress.Web.UI.Website.UiService.GetSignMeOutDialog(albumId, ShowSignMeOutDialogComplete);
    }
    else {
        alert(selectAlbumFirst);
    }
}

function ShowSignMeOutDialogComplete(result) {
    CreateOverlayDialog(result, true, 'dialogSmall');
}

function RemoveAlbumFromFriend(albumId) {
    if (albumId != null) {
        eXpress.Web.UI.Website.UiService.RemoveAlbumFromFriend(albumId, RemoveAlbumFromFriendComplete);
    }
}

function RemoveAlbumFromFriendComplete(result) {
    if (result.length > 0) {
        alert(result);
    }
    else {
        selectedFolders = [];
        RefreshAlbum();
        CloseDialog();
    }
   }