﻿// JScript File

Type.registerNamespace('IglooSite');

IglooSite._ProductCompare = function()
{ 
    this.imagesPath = '';   
}

IglooSite._ProductCompare.prototype = {
    testCall: function() {
        Services.ProductCompareSvc.HelloWorld(Function.createDelegate(this, this.testCall_compl));
    },

    testCall_compl: function(a) {
        var dlg = $find("behDialogCompareProducts");
        dlg.removeWaitMessage();
    },

    addProductToCompare: function(productId, elFrom) {
        var dlg = $find("behDialogCompareProducts");
        if (dlg.get_IsVisible()) {
            dlg.close();
        }
        dlg.show(elFrom);

        Services.ProductCompareSvc.AddProductForCompare(productId, Function.createDelegate(this, this.onReturn_addProductToCompare));
    },

    removeProductToCompare: function(productId) {
        var dlg = $find("behDialogCompareProducts");
        dlg.showWaitMessage();
        Services.ProductCompareSvc.RemoveProductForCompare(productId, Function.createDelegate(this, this.onReturn_addProductToCompare));
    },

    onReturn_addProductToCompare: function(ret) {
        var dlgBeh = $find("behDialogCompareProducts");
        var elDlg = dlgBeh.get_element();

        if (ret.length > 0) {
            $get("varAnyProduct", elDlg).style.display = "block";
            $get("varNoProduct", elDlg).style.display = "none";

            Pythagoras.ClearChildren($get("lblProdCount", elDlg));
            if (ret.length == 1) {
                $get("lblProdCount", elDlg).appendChild(document.createTextNode("1 produkt"));
            } else if (ret.length < 4) {
                $get("lblProdCount", elDlg).appendChild(document.createTextNode(ret.length.toString() + " produkty srovnatelného typu"));
            } else {
                $get("lblProdCount", elDlg).appendChild(document.createTextNode(ret.length.toString() + " produktů srovnatelného typu"));
            }

            Pythagoras.ClearChildren($get("ulProdsToCompare", elDlg));

            for (var i = 0; i < ret.length; ++i) {
                var ul = document.createElement("li");
                ul.style.margin = "5px 0";
                ul.style.fontWeight = "700";
                var txt = document.createTextNode(ret[i].ProductFullName + " ");
                ul.appendChild(txt);
                $get("ulProdsToCompare", elDlg).appendChild(ul);
                var hr = document.createElement("a");
                hr.href = "javascript:void(0);";
                ul.appendChild(hr);
                txt = document.createTextNode(" ");
                ul.appendChild(txt);
                var idProd = ret[i].ProductKey;
                var fc = this.makeRemoveClosure(idProd);
                $addHandler(hr, "click", Function.createDelegate(this, fc));

                var ico = document.createElement("img");
                ico.src = this.removeBtnPath;
                ico.style.verticalAlign = "top";

                hr.appendChild(ico);
            }

        } else {
            $get("varAnyProduct", elDlg).style.display = "none";
            $get("varNoProduct", elDlg).style.display = "block";
        }

        dlgBeh.removeWaitMessage();

    },

    makeRemoveClosure: function(idParam) {
        var p = idParam + '';
        return function() { this.removeProductToCompare(p); };
    },

    hookClickEvent: function(hrElement, productId) {

        if (hrElement) {

            var cbkObj = {

                pid: productId,

                elFrom: hrElement,

                callback: function() {
                    IglooSite.ProductCompare.addProductToCompare(this.pid, this.elFrom);
                }
            };

            $addHandler(hrElement, "click", Function.createDelegate(cbkObj, cbkObj.callback));

        }
    }
}

IglooSite.ProductCompare = new IglooSite._ProductCompare();


if (typeof(Sys) != 'undefined') Sys.Application.notifyScriptLoaded(); 
