﻿// JScript File
function Mediachase_CompareProducts()
{
    // Properties
    this.CompareViewPageUrl = "";

    this.OpenCompareView = function(metaClassName) {
        var url = this.CompareViewPageUrl;
        if (metaClassName != null && metaClassName != '') url += '?mc=' + metaClassName;
        /*var win = window.open(url,'compare','width=1000,height=800,resizable=yes,scrollbars=yes');    
        win.focus();*/
        window.location = url;
    };

    this.CallServerCompare = function(source, productId) {
        var callbackString = "Callback=Add2Compare";
        var search = window.location.search;
        var requestUrl = search != '' ? window.location.href + "&" + callbackString :
                    window.location.href + "?" + callbackString;
        //alert('CallServerCompare: '+requestUrl);

        $.getJSON(requestUrl,
			  { CheckValue: source.checked, ProdId: productId },
			  function(result) {
			      CSCompareProducts.ReceiveServerData(result);
			  });


    };

    this.ReceiveServerData = function(resultString) {
        try {
            var res = resultString[0];  //Sys.Serialization.JavaScriptSerializer.deserialize(resultString);
            var checkObj = $get(res.chbId);
            if (checkObj != null)
                checkObj.checked = res.check;

            if (res.refId != '') {
                __doPostBack(res.refId, '');
            }
            CSCompareProducts.ShowMessage("sitemessage", res.resultMsg, res.isError);
        }
        catch (e) {
            try {
                CSCompareProducts.ShowMessage("sitemessage", "Error while processing callback data:" + e, true);
            }
            catch (e1) {
                alert("Unknown error:" + e, true);
            }
        }
    };

    this.ShowMessage = function(id, text, error) {
        if (!$("#" + id).length)
            alert("Compare element requires div element on the page to display confirmation messages. Please add <div id=\"sitemessage\"></div> anywhere on the page.");

        if (error) {
            $("#" + id).html("<div id=\"sitemessagebox\" class=\"ui-widget\" style=\" margin-left: 5px; margin-right: 5px;\"><div class=\"ui-state-error ui-corner-all\" style=\"padding: 6px;\"><p><span class=\"ui-icon ui-icon-alert\" style=\"float: left; margin-right: 0.3em;\"></span><span></span></p></div></div>");
            $("#" + id + " span:eq(1)").text(text);
            $("#" + id).css("top", "10%").fadeIn("slow");
        }
        else {
            $("#" + id).html("<div id=\"sitemessagebox\" class=\"ui-widget\" style=\" margin-left: 5px; margin-right: 5px;\"><div class=\"ui-state-highlight ui-corner-all\" style=\"padding: 6px;\"><p><span class=\"ui-icon ui-icon-info\" style=\"float: left; margin-right: 0.3em;\"></span><span></span></p></div></div>");
            $("#" + id + " span:eq(1)").text(text);
            $("#" + id).css("top", "10%").fadeIn("slow").fadeOut(500);
        }

        /*
        if (time_to_show > 0)
        setTimeout("CSCompareProducts.HideMessage()", time_to_show);
        */
    }

    this.HideMessage = function(id) {
        $("#" + id).animate({ top: "-10000px" });
        $("#sitemessage").empty();
    }
};

var CSCompareProducts = new Mediachase_CompareProducts();


