var compareProductsPanel = null;
var compareProductsMenu = null;

function compareProducts(isDynamicPage, wsWebsiteEndpoint, uid, productId, checkBoxField, labelsArr) {
    var productIdFromCookie = getCookie("cookie_scart_compareProductIds");
    var productIdFromCookie_arr = new Array();

    if (checkBoxField.checked) {
        if (productIdFromCookie != null) {
            setCookie("cookie_scart_compareProductIds", productIdFromCookie + "," + productId, 1);
            // show dynamic menu with Compare link
            showCompareProductsMenu(checkBoxField, uid, wsWebsiteEndpoint, isDynamicPage, labelsArr);
        } else {
            setCookie("cookie_scart_compareProductIds", productId, 1);
        }
    } else {
        if (productIdFromCookie != null) { productIdFromCookie_arr = productIdFromCookie.split(","); }

        productIdFromCookie = "";
        for (i = 0; i < productIdFromCookie_arr.length; i++) {
            if (productIdFromCookie_arr[i] != productId) {
                if (productIdFromCookie != "") { productIdFromCookie += "," }
                productIdFromCookie += productIdFromCookie_arr[i];
            }
        }
        setCookie("cookie_scart_compareProductIds", productIdFromCookie, 1);
    }
}

function showCompareProductsMenu(elementInstance, uid, wsWebsiteEndpoint, isDynamicPage, labelsArr) {
    var productIdFromCookie = getCookie("cookie_scart_compareProductIds");
    var productIdFromCookie_arr = productIdFromCookie.split(",");

    // create dynamic <div>
    var divElement = document.getElementById("compareProductsMenuContainer");
    if ( ! divElement) {
        divElement = document.createElement('div');
        divElement.setAttribute('id', 'compareProductsMenuContainer');
        divElement.setAttribute('class', 'yui-skin-sam');
        divElement.setAttribute('className', 'yui-skin-sam');
        document.body.insertBefore(divElement, null);
    }

    // construct new menu
    if (compareProductsMenu != null) { compareProductsMenu.destroy(); }
    var context_arr = new Array(elementInstance, "tl", "bl");
    compareProductsMenu = new YAHOO.widget.Menu("compareProductsMenu", { zindex:99999, context:context_arr, iframe:false, constraintoviewport:true });

    // add menu items
    compareProductsMenu.addItems([
        {text: labelsArr[0], url: "#compareNow", onclick: { fn: onCompareProductsMenuItemClick, obj: [uid, wsWebsiteEndpoint, isDynamicPage] } },
        {text: labelsArr[1], url: "#resetCompare", onclick: { fn: onCompareProductsMenuItemClick, obj: [uid, wsWebsiteEndpoint, isDynamicPage] } },
        {text: labelsArr[2], url: "#compareLater", onclick: { fn: onCompareProductsMenuItemClick, obj: [uid, wsWebsiteEndpoint, isDynamicPage] } }
    ]);
    if (labelsArr[3] != '') {
        var header = labelsArr[3].split("%s");
        compareProductsMenu.setItemGroupTitle(header[0] + productIdFromCookie_arr.length + header[1], 0);
    }

    // show the menu
    compareProductsMenu.render(divElement.id);
    compareProductsMenu.show();
}

function onCompareProductsMenuItemClick(p_sType, p_aArgs, p_oValue) {
    var productIdFromCookie = getCookie("cookie_scart_compareProductIds");    
    var productIdFromCookie_arr = productIdFromCookie.split(",");

    // menuItem: compare products now
    if (this.index == 0) {
        var uid = p_oValue[0];
        var wsWebsiteEndpoint = p_oValue[1];
        var isDynamicPage = p_oValue[2];
        showCompareProductsWindow(uid, wsWebsiteEndpoint, isDynamicPage);
    }
    // menuItem: reset compare products
    else if (this.index == 1) {
        for (i = 0; i < productIdFromCookie_arr.length - 1; i++) {
            current_checkbox = document.getElementById("compareProduct_" + productIdFromCookie_arr[i]);
            if (current_checkbox != null) { current_checkbox.checked = false; }
        }
        setCookie("cookie_scart_compareProductIds", productIdFromCookie_arr[(productIdFromCookie_arr.length - 1)], 1);    
    }
}

function showCompareProductsWindow(uid, wsWebsiteEndpoint, isDynamicPage) {
    var productIdFromCookie = getCookie("cookie_scart_compareProductIds");
    var productIdFromCookie_arr = productIdFromCookie.split(",");

    showLoadingWindow("Loading, Please Wait...");

    // construct the web service request
    var wsRequest = "action=getCompareProductsContent&wsParams[multipart_result]=uid=" + uid + ",isDynamicPage=" + (isDynamicPage ? "1" : "0");
    wsRequest += ",clientWidth=" + YAHOO.util.Dom.getViewportWidth() + ",clientHeight=" + YAHOO.util.Dom.getViewportHeight();
    for (i = 0; i < productIdFromCookie_arr.length; i++) { wsRequest +=  "," + "productCompareId" + i + "=" + productIdFromCookie_arr[i]; }

    // construct the callback
    var asyncResponseCallback_compareProducts = {
        success: function(o) {
            var resultContent = getMultipartResultById(o.responseText, "multipart_result");
            if (resultContent == "") { resultContent = geWsErrorDescription(o.responseText, true); }
            hideLoadingWindow();

            // construct and show the dynamic panel
            if (compareProductsPanel != null) { compareProductsPanel.hide(); } else { compareProductsPanel = new YAHOO.widget.Panel("compareProductsPanel", { zindex:1001, visible:false, fixedcenter:true, draggable:true, close:true, modal:true, constraintoviewport:true } ); }
            compareProductsPanel.setHeader("Compare Products");
            compareProductsPanel.setBody(resultContent);
            compareProductsPanel.render(document.body);
            compareProductsPanel.show();

            // execute response JavaScript after the response text is shown
            launchJavascript(o.responseText);
        },
        failure: function(o) { hideLoadingWindow(); alert("Failed compareProducts (" + o.status + " " + o.statusText + ")"); }
    }

    // make the AJAX call
    var transaction = YAHOO.util.Connect.asyncRequest('POST', wsWebsiteEndpoint, asyncResponseCallback_compareProducts, wsRequest);
    if (transaction == null) { hideLoadingWindow(); }
}

function isProductSelectedForCompare(productId, checkBoxField) {
    var productIdFromCookie = getCookie("cookie_scart_compareProductIds");
    var productIdFromCookie_arr = new Array();

    if (productIdFromCookie != null) { productIdFromCookie_arr = productIdFromCookie.split(","); }

    for (i = 0; i < productIdFromCookie_arr.length; i++) {
        if (productIdFromCookie_arr[i] == productId) {
            checkBoxField.checked = true;
            break;
        }
    }
}