﻿// JScript File
var activelbox='';
var lboxstatus='show';
var addtolboxbuttonid='';
var currentimageid='';

function initializeCurrentLightbox(clboxid,lStatus)
{
    var albox = dId('currentLightBoxes');
    if(albox)
    {
        if(clboxid == '')
        {
            if(albox.options.length > 0)
                albox.selectedIndex = 0;
        }
            
        updateCurrentLightbox(albox);
        GetLightBoxImagesHtml();
        setLboxStatus(lStatus);
    }
}

function togglelbox()
{
    if(lboxstatus != 'hide')
    {
        setLboxStatus('hide');
    }
    else
    {
        setLboxStatus('show');
    }   
}

function setLboxStatus(lStatus)
{
    var lboxBasket = dId('fixedBasket');
    var lboxStatusCmd = dId('cmdLboxStatusToggle');
    
    if(lStatus == 'hide')
    {
        lboxBasket.style.height = '59px';
        lboxstatus = lStatus
        lboxStatusCmd.src = 'siteImages/showLightboxButton.jpg';
    }
    else
    {
        lboxBasket.style.height = '195px';
        lboxstatus = lStatus
        lboxStatusCmd.src = 'siteImages/hideLightboxButton.jpg';
    }
}

function updateCurrentLightbox(lbox)
{
    if(lbox.selectedIndex >= 0)
        activelbox = lbox.options[lbox.selectedIndex].value;
    else
        activelbox = '';
       
    var faction = document.getElementById("ActionsFrame");
    faction.src = 'doActions.aspx?action=currentlbox&lboxid=' + activelbox;    
}

function AddImageToLightBox(imageid)
{
    if(activelbox == '')
    {
        alert('No active lightbox. Please create a new lightbox and try again.');
        return;
    }
    
    var imgTag = dId('image' + imageid.toString());
    
    if(imgTag)
    {
        addtolboxbuttonid = imgTag.value;
    }
    
    currentimageid = imageid;    
    ActionImages.AIMedia.LightboxService.AddImageToLightBox(activelbox, imageid, SucceededCallbackGeneric, FailedCallbackGeneric);
}

function DeleteImageFromLightBox(imageid)
{
    if(activelbox == '')
    {
        alert('No active lightbox. Please create a new lightbox and try again.');
        return;
    }
    
    var imgTag = dId('image' + imageid.toString());
    
    if(imgTag)
    {
        addtolboxbuttonid = imgTag.value;
    }
    
    currentimageid = imageid;
    ActionImages.AIMedia.LightboxService.DeleteImageFromLightBox(activelbox, imageid, SucceededCallbackGeneric, FailedCallbackGeneric);
}

function GetLightBoxImagesHtml()
{
    ActionImages.AIMedia.LightboxService.GetLightBoxImagesHTML(activelbox, SucceededCallbackLightbox,FailedCallbackLightbox);
}

function clearLightbox()
{
    if(activelbox == '')
    {
        alert('No active lightbox to clear');
        return;
    }
    
    if(confirm('Are you sure want to clear all images from current lightbox?') == true)
    {
        ActionImages.AIMedia.LightboxService.ClearLightBox(activelbox, SucceededCallbackLboxClear, FailedCallbackGeneric);
        
    }
}

function addLightboxToDCart()
{
    if(activelbox == '')
    {
        alert('No active lightbox to add to Download Cart');
        return;
    }

    var faction = document.getElementById("ActionsFrame");
    faction.src = 'doActions.aspx?action=addlbox2dcart&lboxid=' + activelbox;
}

function deleteLightbox()
{
    if(activelbox == '')
    {
        alert('No active lightbox to delete');
        return;
    }
        
    if(confirm('Are you sure want to delete the current lightbox?') == true)
    {
        ActionImages.AIMedia.LightboxService.DeleteLightBox(activelbox, SucceededCallbackLboxDelete, FailedCallbackGeneric);
    }
}

function newLightbox()
{
    var activelboxForm = dId('activeLightBox');
    var newlbox = dId('newLightBox');
    var newlboxText = dId('txtNewLightBox');
    var renlbox = dId('renameLightBox');
    
    if(newlboxText)
        newlboxText.value = '';
        
    activelboxForm.style.visibility = 'hidden';
    renlbox.style.visibility = 'hidden';
    newlbox.style.visibility = 'visible';
    
    activelboxForm.style.display = 'none';
    renlbox.style.display = 'none';
    newlbox.style.display = 'block';    
}

function renameLightbox()
{
    if(activelbox == '')
    {
        alert('No active lightbox to rename');
        return;
    }
    
    var albox = dId('currentLightBoxes');
    var activelboxForm = dId('activeLightBox');
    var renlbox = dId('renameLightBox');
    var renlboxText = dId('txtRenameLightBox');
    var newlbox = dId('newLightBox');
    
    if(renlboxText)
        renlboxText.value = albox.options[albox.selectedIndex].text;
        
    activelboxForm.style.visibility = 'hidden';
    newlbox.style.visibility = 'hidden';
    renlbox.style.visibility = 'visible';
    
    activelboxForm.style.display = 'none';
    newlbox.style.display = 'none';
    renlbox.style.display = 'block';    
}

function createNewLightBox()
{
    var newlboxText = dId('txtNewLightBox');
    var cUser = dId('txtLboxUserID');

    if(newlboxText.value == '')
    {
        alert('Please enter a name for the new LightBox');
        return;
    }
    
    ActionImages.AIMedia.LightboxService.AddNewLightBox(cUser.value,newlboxText.value, SucceededCallbackLboxNew, FailedCallbackGeneric);
}

function cancelNewLightBox()
{
    var activelboxForm = dId('activeLightBox');
    var newlbox = dId('newLightBox');
    var newlboxText = dId('txtNewLightBox');
    
    if(newlboxText)
        newlboxText.value = '';
        
    activelboxForm.style.visibility = 'visible';
    newlbox.style.visibility = 'hidden';

    activelboxForm.style.display = 'block';
    newlbox.style.display = 'none';
}

function updateActiveLightBox()
{
    var cUser = dId('txtLboxUserID');
    var renamelboxText = dId('txtRenameLightBox');

    if(renamelboxText.value == '')
    {
        alert('Please enter a name for the Active LightBox');
        return;
    }
    
    ActionImages.AIMedia.LightboxService.UpdateLightBox(activelbox,cUser.value, renamelboxText.value, SucceededCallbackLboxUpdate, FailedCallbackGeneric);
}

function cancelUpdateLightBox()
{
    var activelboxForm = dId('activeLightBox');
    var renlbox = dId('renameLightBox');
    var renlboxText = dId('txtRenameLightBox');
    
    if(renlboxText)
        renlboxText.value = '';
        
    activelboxForm.style.visibility = 'visible';
    renlbox.style.visibility = 'hidden';
    
    activelboxForm.style.display = 'block';
    renlbox.style.display = 'none'; 
}


// This is the callback function invoked if the Web service
// succeeded.
// It accepts the result object as a parameter.
function SucceededCallbackGeneric(result, userContext, methodName)
{
    // Page element to display feedback.
     //var RsltElem = document.getElementById("basketContent");
     //RsltElem.innerHTML = result;
     if(result == false)
     {
        alert('Unable to execute the requested action');
        return;
     }
     
     switch(methodName)
     {
        case 'AddImageToLightBox':
		        var addToLbox = dId(addtolboxbuttonid);
		        if(addToLbox)
		        {
		            addToLbox.src = 'siteimages/NewRemoveFromLightBox.gif';
		            addToLbox.title = 'Remove from Light Box';
		            addToLbox.alt = 'Remove from Light Box';
		            addToLbox.value = currentimageid.toString();
		            addToLbox.onclick= function() {DeleteImageFromLightBox(addToLbox.value);return false;};
		            addtolboxbuttonid = '';
		            currentimageid = -1;
		        }                

                GetLightBoxImagesHtml();
                break;
        case 'DeleteImageFromLightBox':
		        var addToLbox = dId(addtolboxbuttonid);
		        if(addToLbox)
		        {
		            addToLbox.src = 'siteimages/NewAddToLightBox.gif';
		            addToLbox.title = 'Add To Light Box';
		            addToLbox.alt = 'Add To Light Box';
		            addToLbox.value = currentimageid.toString();
		            addToLbox.onclick= function() {AddImageToLightBox(addToLbox.value);return false;};
		            addtolboxbuttonid = '';
		            currentimageid = -1;
		        }                
		        GetLightBoxImagesHtml();
                break;                
     }
}

function SucceededCallbackLightbox(result, eventArgs)
{
    // Page element to display feedback.
     var RsltElem = dId("basketContent");
     RsltElem.innerHTML = result;    
}

function SucceededCallbackLboxDelete(result, eventArgs)
{
    var albox = dId('currentLightBoxes');
    deleteOption(albox,albox.selectedIndex);
    initializeCurrentLightbox();
}

function SucceededCallbackLboxClear(result, eventArgs)
{
    GetLightBoxImagesHtml();
}

function SucceededCallbackLboxNew(result, eventArgs)
{
    var albox = dId('currentLightBoxes');
    var newlboxText = dId('txtNewLightBox');
    var activelbox = dId('activeLightBox');
    var newlbox = dId('newLightBox');    
    
    addOption(albox,newlboxText.value,result.toString());
    albox.selectedIndex = albox.options.length - 1;

    updateCurrentLightbox(albox);
    GetLightBoxImagesHtml();
    
    newlboxText.value = '';
    
    activelbox.style.visibility = 'visible';
    newlbox.style.visibility = 'hidden';

    activelbox.style.display = 'block';
    newlbox.style.display = 'none';    
}

function SucceededCallbackLboxUpdate(result, eventArgs)
{
    var albox = dId('currentLightBoxes');
    var activelbox = dId('activeLightBox');    
    var renlboxText = dId('txtRenameLightBox');
    var renlbox = dId('renameLightBox');   
    
    albox.options[albox.selectedIndex].text = renlboxText.value;

    renlboxText.value = '';

    activelbox.style.visibility = 'visible';
    renlbox.style.visibility = 'hidden';

    activelbox.style.display = 'block';
    renlbox.style.display = 'none'; 
}

// This is the callback function invoked if the Web service
// failed.
// It accepts the error object as a parameter.
function FailedCallbackGeneric(error)
{
    // Display the error.    
    //var RsltElem = document.getElementById("ResultId");
    //RsltElem.innerHTML = "Service Error: " + error.get_message();
    alert('Error calling web method. Lightboxservice');
}

function FailedCallbackLightbox(error)
{
    // Display the error.    
     var RsltElem = document.getElementById("basketContent");
     RsltElem.innerHTML = 'Error updating Lightbox preview';
}