//define global variables for non-refresh functionality

var g_maxholders = -1;
var g_tWidth;
var g_tHeight;
var g_tPath = new String();
var readonlyMsg = "The lightbox is read-only!";
var lbAdminSelectLB = "Select a lightbox.";

function AddToLightbox(contextPath, redirect, queryString, imagecode, readonly)
{
    if (readonly)
    {
        alert(readonlyMsg);
    }
    else
    {   var InitiatedFrom = getInitiatedFrom();
        var url = contextPath + "/formsubmits/addtolightbox.html?imagecode=" + imagecode + "&redirect=" + redirect+"&InitiatedFrom="+InitiatedFrom;
        if (queryString != null && queryString != "")
            url = url + "&" + queryString;
        window.location = url;
    }
}

function RemoveFromLightbox(contextPath, redirect, queryString, imagecode, readonly)
{
    if (readonly)
    {
        alert(readonlyMsg);
    }
    else
    {   var InitiatedFrom = getInitiatedFrom();
        var url = contextPath + "/formsubmits/removefromlightbox.html?imagecode=" + imagecode + "&redirect=" + redirect+"&InitiatedFrom="+InitiatedFrom;
        if (queryString != null && queryString != "")
            url = url + "&" + queryString;
        window.location = url;
    }
}

function addLightboxItemFromCart(redirect, imagecode, readonly, LineItemId )
{
    if (readonly) {
        alert(readonlyMsg);
        return true;
    }

    var params = {};
    params['imagecode'] = imagecode;
    params['redirect'] = redirect;

    //var successHandler = successUpdateLightBox.curry('lbRemoveLink'+imagecode,'lbAddLink'+imagecode);
    //AddToLightbox.execute( params, {callback:successHandler});
    AddToLightbox.execute(params, {
			callback:successUpdateLightBox.curryAlt('lbRemoveLink'+imagecode,'lbAddLink'+imagecode) }
    );


}

function successUpdateLightBox(showDivId, hideDivId) {
    var divs = document.getElementsByTagName( 'div' );
    for(var i=0;i<divs.length;i++){
        if(divs.item(i).getAttribute( 'name' ) == hideDivId )
            divs.item(i).style.display = "none";
        if(divs.item(i).getAttribute( 'name' ) == showDivId )
            divs.item(i).style.display = "block";
    }
}

function remLightboxItemFromCart(redirect, imagecode, readonly, LineItemId )
{
    if (readonly) {
        alert(readonlyMsg);
        return true;
    }

    var params = {};
    params['imagecode'] = imagecode;
    params['redirect'] = redirect;


    //var successHandler = successUpdateLightBox.curry("lbAddLink"+imagecode,"lbRemoveLink"+imagecode,LineItemId);
    //RemoveFromLightbox.execute( params, {callback:successHandler});
        
    RemoveFromLightbox.execute(params, {
			callback:successUpdateLightBox.curryAlt('lbAddLink'+imagecode,'lbRemoveLink'+imagecode,LineItemId) }
    );

}

function updLightboxItemFromCart(contextPath, redirect, queryString, imagecode, readonly)
{
    if (readonly)
    {
        alert(readonlyMsg);
    }
    return true;
}

function getInitiatedFrom () {
    InitiatedFrom = "viewEnlarge";
    if ( document.forms["InitiatedFrom_"] ) {
        InitiatedFrom = document.forms["InitiatedFrom_"].InitiatedFrom.value;
    }
    return InitiatedFrom;
}

function updLightboxItem(contextPath, redirect, queryString, imagecode, readonly, popup)
{
    //alert("updLightboxItem()");
    if (readonly)
    {
        alert(readonlyMsg);
        return true;
    }

    theWindow = window;
    if (popup) {
        theWindow = window.opener;
        theWindow.g_popup = popup;
    }

    theWindow.g_contextPath = contextPath;
    theWindow.g_redirect = redirect;
    theWindow.g_queryString = queryString;
    theWindow.g_imagecode = imagecode;
    theWindow.g_maxholders = -1;
    var iconImage = eval("document.images['lb" + imagecode + "']");
    //used in conditional check, the object represents the button that is clicked

    if (popup) {
        if (iconImage.src.indexOf("enlargedimg/addtolightbox") != -1)
        {
            theWindow.g_type = "addlb";
            try {
                var targetIcon = eval("theWindow.document.images['lb" + imagecode + "']");
                var InitiatedFrom = getInitiatedFrom();
                targetIcon.src = contextPath + "/formsubmits/addtolightbox.html?imagecode=" + imagecode  + "&InitiatedFrom="+InitiatedFrom + "&" + new Date().getTime();
            }
            catch(e) {
                submitEnlargeAction("addtolightbox", contextPath, queryString, imagecode, false);
            }
            return false;
        }

        if (iconImage.src.indexOf("enlargedimg/removefromlightbox") != -1)
        {
            theWindow.g_type = "rmvlb";
            try {
                var targetIcon = eval("theWindow.document.images['lb" + imagecode + "']");
                var InitiatedFrom = getInitiatedFrom();
                targetIcon.src = contextPath + "/formsubmits/removefromlightbox.html?imagecode=" + imagecode + "&InitiatedFrom="+InitiatedFrom+ "&" + new Date().getTime();
            }
            catch(e) {
                submitEnlargeAction("removefromlightbox", contextPath, queryString, imagecode, false);
            }
            return false;
        }

        return false;
    }

    if (iconImage.src.indexOf("lightbox_off.gif") != -1 || iconImage.src.indexOf("removefromlightbox") != -1)
    {   var InitiatedFrom = getInitiatedFrom();

        var dest = contextPath + "/formsubmits/addtolightbox.html?imagecode=" + imagecode+"&InitiatedFrom="+InitiatedFrom;
        theWindow.g_type = "addlb";
        iconImage.src = dest + "&" + new Date().getTime();
        return false;
    }

    if (iconImage.src.indexOf("lightbox_on.gif") != -1 || iconImage.src.indexOf("addtolightbox") != -1)
    {   var InitiatedFrom = getInitiatedFrom();

        var dest = contextPath + "/formsubmits/removefromlightbox.html?imagecode=" + imagecode+"&InitiatedFrom="+InitiatedFrom;
        theWindow.g_type = "rmvlb";
        iconImage.src = dest + "&" + new Date().getTime();
        return false;
    }

    // [roger] - 2004.07.21. change the way using hidden image because it will be interfered by mouseout event
    //theWindow.hidImage = theWindow.document.images["hiddenImage"];
    //if ( navigator.userAgent.toLowerCase().indexOf("mozilla")!=-1 && parseInt(navigator.appVersion)==5 )
    //    theWindow.hidImage.src = "";

    //theWindow.hidImage.src = dest+"&"+new Date().getTime();
    return false;
}

function mLB(contextPath, redirect, imagecode, readonly, maxholders, tPath, tWidth, tHeight, popup)
{
    var imgServer = imageServer;
    if (imgServer.indexOf("image") == -1)
        imgServer = "#thumbserverRaptor('')";

    return modLightboxItem(contextPath, redirect, document.special.queryAdd.value, imagecode, readonly, maxholders, imgServer + tPath, tWidth, tHeight, popup);
}

function modLightboxItem(contextPath, redirect, queryString, imagecode, readonly, maxholders, tPath, tWidth, tHeight, popup)
{
    if (readonly)
    {
        alert(readonlyMsg);
        return true;
    }

    var theWindow = window;
    if (popup) {
        theWindow = window.opener;
        //theWindow = main;
        theWindow.g_popup = popup;
    }
    theWindow.g_contextPath = contextPath;
    theWindow.g_redirect = redirect;
    theWindow.g_queryString = queryString;
    theWindow.g_imagecode = imagecode;
    theWindow.g_tWidth = tWidth;
    theWindow.g_tHeight = tHeight;
    theWindow.g_tPath = tPath;

    if (maxholders)
        theWindow.g_maxholders = maxholders;
    else
        theWindow.g_maxholders = -1;

    var iconImage = eval("document.images['lb" + imagecode + "']");
    //used in conditional check, represents the button that is clicked
    //theWindow.hidImage = theWindow.document.images["hiddenImage"];
    //alert(iconImage.src);


    if (popup) {
        var hiddenImage = theWindow.document.images["hiddenImage"];
        //in the case of clicking lb out of view enlarged page of the search page.
        if (iconImage.src.indexOf("enlargedimg/addtolightbox") != -1)
        {
            theWindow.g_type = "addlb";
            try {
                // AlbertW Nov 6, 2007: used by the enlarge image in the shopping cart. Div is defined in results_cart.html
                var targetAddLink = theWindow.document.getElementsByName('lbAddLink' + imagecode );
                var targetRemoveLink = theWindow.document.getElementsByName('lbRemoveLink' + imagecode );
                if ( targetRemoveLink.length > 0 && targetAddLink.length > 0 ) {
                    for (var i = 0; i < targetAddLink.length; i++) {
                       targetAddLink[i].style.display = "none";
                    }

                    for (var j = 0; j <= targetRemoveLink.length; j++) {
                        targetRemoveLink[j].style.display = "block";
                    }
                }

                var targetIcon = eval("theWindow.document.images['lb" + imagecode + "']");
                if (targetIcon.src.indexOf("lightbox_off.gif") != -1 || targetIcon.src.indexOf("removefromlightbox") != -1)
                {
                    // AlbertW 07.03.08: commented for UEX-287 : duplicate call to add lb action
                    // hiddenImage.src = contextPath + "/formsubmits/addtolightbox.html?imagecode=" + imagecode + "&"+ new Date().getTime();
                    targetIcon.src = "/images/lightbox_on.gif";
                }
            }
            catch(e) {
                submitEnlargeAction("addtolightbox", contextPath, queryString, imagecode, false);
            }
        }

        if (iconImage.src.indexOf("enlargedimg/removefromlightbox") != -1)
        {
            theWindow.g_type = "rmvlb";
            try {
                // AlbertW Nov 6, 2007: used by the enlarge image in the shopping cart. Div is defined in results_cart.html
                var targetAddLink = theWindow.document.getElementsByName('lbAddLink' + imagecode );
                var targetRemoveLink = theWindow.document.getElementsByName('lbRemoveLink' + imagecode );
                if ( targetRemoveLink.length > 0 && targetAddLink.length > 0 ) {
                    for (var i = 0; i < targetAddLink.length; i++) {
                       targetAddLink[i].style.display = "block";
                    }

                    for (var j = 0; j <= targetRemoveLink.length; j++) {
                        targetRemoveLink[j].style.display = "none";
                    }
                }

                var targetIcon = eval("theWindow.document.images['lb" + imagecode + "']");
                if (targetIcon.src.indexOf("lightbox_on.gif") != -1 || targetIcon.src.indexOf("addtolightbox") != -1)
                {
                    // AlbertW 07.03.08: commented for UEX-287 : duplicate call to add lb action
                    //hiddenImage.src = contextPath + "/formsubmits/removefromlightbox.html?imagecode=" + imagecode + "&" +new Date().getTime();
                    targetIcon.src = "/images/lightbox_off.gif";
                }
            }
            catch(e) {
                submitEnlargeAction("removefromlightbox", contextPath, queryString, imagecode, false);
            }
        }

    }

    if (iconImage.src.indexOf("lightbox_off.gif") != -1 || ( ( ( ! popup ) && iconImage.src.indexOf("removefromlightbox") != -1 ) || ( popup && iconImage.src.indexOf("addtolightbox") != -1 ) ))
    {
        var InitiatedFrom = getInitiatedFrom();

        dest = contextPath + "/formsubmits/addtolightbox.html?imagecode=" + imagecode + "&InitiatedFrom="+InitiatedFrom;
        theWindow.g_type = "addlb";
        //alert(theWindow.type);
        if (popup) {
            window.document.images['hiddenImage'].src = dest + "&" + new Date().getTime();
            window.location.reload();
        } else {
            iconImage.src = dest + "&" + new Date().getTime();
        }

        afterAddLBLoaded(popup, imagecode);
        //start update lightbox previewed thumbnail

        //took out by Roger
        //if ( navigator.userAgent.toLowerCase().indexOf("mozilla")!=-1 && parseInt(navigator.appVersion)==5 )
        //    theWindow.hidImage.src = "";
        //theWindow.hidImage.src = dest+"&"+new Date().getTime();

         if ( redirect == "shoppingcart" || redirect == "viewlightbox" ) {
            window.opener.location.reload();
        }

        return false;
    }

    if (iconImage.src.indexOf("lightbox_on.gif") != -1 || ( ( ( ! popup ) && iconImage.src.indexOf("addtolightbox") != -1 ) || ( popup && iconImage.src.indexOf("removefromlightbox") != -1 ) ))
    {   var InitiatedFrom = getInitiatedFrom();
        
        dest = contextPath + "/formsubmits/removefromlightbox.html?imagecode=" + imagecode + "&InitiatedFrom="+InitiatedFrom;
        theWindow.g_type = "rmvlb";
        if (popup) {
            window.document.images['hiddenImage'].src = dest + "&" + new Date().getTime();
            window.location.reload();
        } else {
            iconImage.src = dest + "&" + new Date().getTime();
        }
        //alert(theWindow.type);

        //start update lightbox previewed thumbnail
        afterRmvLBLoaded(popup);

        //if ( navigator.userAgent.toLowerCase().indexOf("mozilla")!=-1 && parseInt(navigator.appVersion)==5 )
        //    theWindow.hidImage.src = "";
        //theWindow.hidImage.src = dest+"&"+new Date().getTime();

        if ( redirect == "shoppingcart" || redirect == "viewlightbox" ) {
            window.opener.location.reload();
        }
        
        return false;
    }

    return false;
}

/* 
function mLB_flash(contextPath,redirect,imagecode,readonly,maxholders,tPath,tWidth,tHeight,popup)
 {
    var imgServer = imageServer;
    if(imgServer.indexOf("image")==-1)
        imgServer = "#thumbserverRaptor('')";

    return modLightboxItem_flash(contextPath,redirect,'',imagecode,readonly,maxholders,imgServer+tPath,tWidth,tHeight,popup);
 }

 function modLightboxItem_flash(contextPath,redirect,queryString,imagecode,readonly,maxholders,tPath,tWidth,tHeight,popup)
 {
     if ( readonly )
    {
        alert(readonlyMsg);
        return true;
    }

     var theWindow = window;
     if ( popup ) {
         theWindow = window.opener;
         //theWindow = main;
         theWindow.g_popup = popup;
     }
     theWindow.g_contextPath = contextPath;
     theWindow.g_redirect = redirect;
     theWindow.g_queryString = queryString;
     theWindow.g_imagecode = imagecode;
     theWindow.g_tWidth = tWidth;
     theWindow.g_tHeight = tHeight;
     theWindow.g_tPath = tPath;

     if (maxholders)
         theWindow.g_maxholders = maxholders;
     else
         theWindow.g_maxholders = -1;

     var iconImage = eval("document.images['lb"+imagecode+"']");  //used in conditional check, represents the button that is clicked
     //theWindow.hidImage = theWindow.document.images["hiddenImage"];
     //alert(iconImage.src);

     if (iconImage==null) { // [scosta] April 12, 2006 - to ease slideshow functionality
	// fake an add
	//alert(iconImage);
	iconImage = new Image();
	iconImage.src = "lightbox_off.gif";
	}

     if(popup) {
         var hiddenImage = theWindow.document.images["hiddenImage"];  //in the case of clicking lb out of view enlarged page of the search page.
         if ( iconImage.src.indexOf("enlargedimg/addtolightbox")!=-1 )
         {
             theWindow.g_type = "addlb";
             try {
                 var targetIcon = eval("theWindow.document.images['lb"+imagecode+"']");
                 if ( targetIcon.src.indexOf("lightbox_off.gif")!=-1 || targetIcon.src.indexOf("removefromlightbox")!=-1 )
                 {
                     hiddenImage.src = contextPath + "/formsubmits/addtolightbox.html?imagecode="+imagecode+"&"+new Date().getTime();
                     targetIcon.src = "/images/lightbox_on.gif";
                 }
             }
             catch(e) {
                 submitEnlargeAction("addtolightbox", contextPath, queryString, imagecode, false);
             }
             return false;
         }

         if ( iconImage.src.indexOf("enlargedimg/removefromlightbox")!=-1 )
         {
             theWindow.g_type = "rmvlb";
             try {
                 var targetIcon = eval("theWindow.document.images['lb"+imagecode+"']");
                 if ( targetIcon.src.indexOf("lightbox_on.gif")!=-1 || targetIcon.src.indexOf("addtolightbox")!=-1 )
                 {
                     hiddenImage.src = contextPath + "/formsubmits/removefromlightbox.html?imagecode="+imagecode+"&"+new Date().getTime();
                     targetIcon.src = "/images/lightbox_off.gif";
                 }
             }
             catch(e) {
                 submitEnlargeAction("removefromlightbox", contextPath, queryString, imagecode, false);
             }
             return false;
         }

         return false;
     }

     //alert("theWindow.g_maxholders="+theWindow.g_maxholders);
     if ( iconImage.src.indexOf("lightbox_off.gif")!=-1 || iconImage.src.indexOf("removefromlightbox")!=-1 )
     {
         dest = contextPath + "/formsubmits/addtolightbox.html?imagecode="+imagecode;
         theWindow.g_type = "addlb";
         //alert(theWindow.type);
         iconImage.src = dest+"&"+new Date().getTime();

         afterAddLBLoaded(); //start update lightbox previewed thumbnail

         //took out by Roger
         //if ( navigator.userAgent.toLowerCase().indexOf("mozilla")!=-1 && parseInt(navigator.appVersion)==5 )
         //    theWindow.hidImage.src = "";
         //theWindow.hidImage.src = dest+"&"+new Date().getTime();
         return false;
     }

     if ( iconImage.src.indexOf("lightbox_on.gif")!=-1 || iconImage.src.indexOf("addtolightbox")!=-1 )
     {
         dest = contextPath + "/formsubmits/removefromlightbox.html?imagecode="+imagecode;
         theWindow.g_type = "rmvlb";
         iconImage.src = dest+"&"+new Date().getTime();
         //alert(theWindow.type);

         //start update lightbox previewed thumbnail
         afterRmvLBLoaded();

         //if ( navigator.userAgent.toLowerCase().indexOf("mozilla")!=-1 && parseInt(navigator.appVersion)==5 )
         //    theWindow.hidImage.src = "";
         //theWindow.hidImage.src = dest+"&"+new Date().getTime();
         return false;
     }

     return false;
 }
*/

function afterAddLBLoaded(popup, imagecode)
{
    //alert("afterAddLBLoaded()");
    //iconImage.src = "/images/lightbox_on.gif";

    if (popup && window.opener.lightboxImgArray) {
        if (imagecode && window.opener.document.images['lb' + imagecode]) {
            return;
        }
        lightboxImgArray = window.opener.lightboxImgArray;
        g_maxholders = window.opener.g_maxholders;
        g_tPath = window.opener.g_tPath;
        g_tWidth = window.opener.g_tWidth;
        g_tHeight = window.opener.g_tHeight;
    }

    if (g_maxholders == -1 || document.layers)
        return;

    addImage = new Image();
    // addImage.src = g_tPath;    // AlbertW Sep 22, 08: Not sure its needed but commenting this line solve KTWO-150 : clicking add to lightbox from search page adds 2 streamer actions
    addImage.width = g_tWidth;
    addImage.height = g_tHeight;

    if ((navigator.appVersion.indexOf("Mac") != -1) && (navigator.userAgent.indexOf("MSIE") != -1 ))
    {
        if (lightboxImgArray.length) {
            newLBImgArray = new Array(lightboxImgArray.length + 1);
            newLBImgArray[0] = addImage;
            for (i = 1; i < newLBImgArray.length; i++)
                newLBImgArray[i] = lightboxImgArray[i - 1];

            lightboxImgArray = new Array(newLBImgArray.length);
            for (j = 0; j < newLBImgArray.length; j++)
                lightboxImgArray[j] = newLBImgArray[j];
        }
        else {
            lightboxImgArray = new Array(1);
            lightboxImgArray[0] = addImage;
        }
    }
    else {
        //alert("push to front");
        lightboxImgArray.unshift(addImage);
    }

    //alert(g_maxholders);

    for (count = 1; count <= g_maxholders; count++)
    {
        lbpreviewid = "lbpreview" + count;
        //preImg = eval("document.images['lbpreview"+count+"']");

        if (popup) {
            preImg = window.opener.document.images[lbpreviewid];
        } else {
            preImg = document.images[lbpreviewid];
        }

        if (!isDefined(preImg)){
            continue;  //the safety if to account for the fact that we do not have lightbox preview anymore
        }

        //alert(preImg);
        if (count == 1) {
            preImg.src = g_tPath;
            tWidth = g_tWidth;
            tHeight = g_tHeight;
        }
        else {
            if (lightboxImgArray[count - 1]) {
                preImg.src = lightboxImgArray[count - 1].src;
                tWidth = lightboxImgArray[count - 1].width;
                tHeight = lightboxImgArray[count - 1].height;
            }
            else
                return false;
        }

        //alert("tWidth="+tWidth+";tHeight="+tHeight);
        if (tWidth > tHeight)
        {
            preImg.height = tHeight * 40 / tWidth;
            preImg.width = 40;
            //alert("width>height. width="+preImg.width+" height="+preImg.height+" src="+preImg.src);
        }
        else
        {
            preImg.width = tWidth * 40 / tHeight;
            preImg.height = 40;
            //alert("width<=height. width="+preImg.width+" height="+preImg.height+" src="+preImg.src);
        }
    }
}

function afterRmvLBLoaded(popup)
{
    //alert("afterRmvLBLoaded()");
    //iconImage.src = "/images/lightbox_off.gif";


    if (popup && window.opener.lightboxImgArray) {
        lightboxImgArray = window.opener.lightboxImgArray;
        g_maxholders = window.opener.g_maxholders;
        g_tPath = window.opener.g_tPath;
        g_tWidth = window.opener.g_tWidth;
        g_tHeight = window.opener.g_tHeight;
    } else if (popup == false) {
        return false;
    }

    if (g_maxholders == -1 || document.layers)
        return;
    else {
        //if ( popup ) theWindow = window.opener;
        rmvCount = 0;
        for (count = 1; count <= g_maxholders; count++)
        {

            if (popup) {
                preImg = eval("window.opener.document.images['lbpreview" + count + "']");
            } else {
                preImg = document.images['lbpreview' + count];
            }

            if (!isDefined(preImg)){
                continue;  //the safety if to account for the fact that we do not have lightbox preview anymore
            }

            if (lightboxImgArray[count])
            {
                tWidth = lightboxImgArray[count].width;
                tHeight = lightboxImgArray[count].height;
                if (rmvCount > 0) {
                    preImg.src = lightboxImgArray[count].src;
                    if (tWidth > tHeight)
                    {
                        preImg.height = tHeight * 40 / tWidth;
                        preImg.width = 40;
                    }
                    else
                    {
                        preImg.height = 40;
                        preImg.width = tWidth * 40 / tHeight;
                    }
                }
                else {
                    if (preImg.src.indexOf(g_imagecode) != -1)
                    {
                        rmvCount = count;
                        //alert("rmvCount="+rmvCount);
                        preImg.src = lightboxImgArray[count].src;
                        if (tWidth > tHeight)
                        {
                            preImg.height = tHeight * 40 / tWidth;
                            preImg.width = 40;
                        }
                        else
                        {
                            preImg.height = 40;
                            preImg.width = tWidth * 40 / tHeight;
                        }
                    }
                }
            }
            else
            {
                preImg.width = 1;
                preImg.height = 1;
                preImg.src = "/images/axis/whitespace.gif";
            }
        }
    }

    if ((navigator.appVersion.indexOf("Mac") != -1) && (navigator.userAgent.indexOf("MSIE") != -1 ))
    {
        newLBImgArray = new Array(lightboxImgArray.length - 1);
        n = 0;
        for (i = 0; i < lightboxImgArray.length; i++) {
            if (lightboxImgArray[i].src.indexOf(g_imagecode) == -1) {
                newLBImgArray[n] = lightboxImgArray[i];
                n++;
            }
        }
        lightboxImgArray = new Array(newLBImgArray.length);
        for (j = 0; j < newLBImgArray.length; j++)
            lightboxImgArray[j] = newLBImgArray[j];
    }
    else
    {
        for (i = 0; i < lightboxImgArray.length; i++)
        {
            if (lightboxImgArray[i].src.indexOf(g_imagecode) != -1) {
                //alert("removed "+i);
                lightboxImgArray.splice(i, 1);
                break;
            }
        }
    }
}

function submitViewLB() {

    var frm = document.forms["frmlightboxviewer"];
    var selIdx = frm.sellb.selectedIndex;
    var id = frm.sellb.options[selIdx].value;
    if (id == null || id == "")
    {
        alert(lbAdminSelectLB)
        return false;
    }
    frm.id.value = id;

    return true;
}
