﻿/* USPOREDILICE */
/* (c) 2008-2010 Sistrum */

//Request counters
ProductCompare_AjaxRqStarted = 0;
ProductCompare_AjaxRqEnded = 0;

jQuery(document).ready(ProductCompare_PageLoad);

//PAGE LOAD
function ProductCompare_PageLoad() {
    // svi compare checkboxovi u listi
    var allCheckboxes = jQuery("span.compare-checkbox input[type='checkbox']");

    if (allCheckboxes.size() == 0) return;

    //setup
    allCheckboxes.each(function() {
        var checkbox = jQuery(this);
        ProductCompare_MarkSelected("#" + checkbox.attr("id"), checkbox.is(":checked"));
        checkbox.bind("change", { domId: checkbox.attr("id") }, ProductCompare_SelectionEventHander);
    });

    //select all
    jQuery("#compareSelectAll").bind("click", { items: allCheckboxes, isAdd: true }, ProductCompare_AllNoneClick);

    //deselect all       
    jQuery("#compareSelectNone").bind("click", { items: allCheckboxes, isAdd: false }, ProductCompare_AllNoneClick);
}

// select / deselect all click handler
function ProductCompare_AllNoneClick(oEvent) {
    var counter = 0;
    ProductCompare_AjaxRqStarted = 0;
    ProductCompare_AjaxRqEnded = 0;
    ProductCompare_ShowButtons(false);

    oEvent.data.items.each(function() {
        if (oEvent.data.isAdd) {
            //dodaj
            if (this.checked != true) {
                counter++;
                this.checked = true;
                jQuery(this).change();
            }
        }
        else {
            //ukloni
            if (this.checked == true) {
                counter++;
                this.checked = false;
                jQuery(this).change();
            }        
        }
    });
    
    if (counter == 0)
        ProductCompare_ShowButtons(true);
    else
        ProductCompare_AjaxRqStarted = counter;
}

// selection changed event handler
function ProductCompare_SelectionEventHander(oEvent) {
    __usporediliceDebug("ProductCompare_SelectionEventHander");
    
    var selbox = jQuery("#" + oEvent.data.domId);
    if (selbox.length == 0) {
        alert("Došlo je do pogreške prilikom promjene odabira!");
        return;
    }

    var ctype = RequestQueryString("ctype");
    if (!ctype) {
        ctype = document.location.pathname;
        ctype = ctype.substring(ctype.lastIndexOf("/"));
        ctype = ctype.split(".")[1];
    }

    var productid = selbox.parent("span.compare-checkbox").attr("rel");
    var state = selbox.is(":checked") ? "add" : "remove";
    var url = "ProductCompareSaveSession.aspx?"
        + "ctype=" + ctype 
        + "&product_id=" + productid 
        + "&action=" + state 
        + "&checkbox=" + selbox.attr("id");
    
    jQuery.get(url, null, ProductCompare_Return, "text")
}

// ajax call return handler
function ProductCompare_Return(response) {
    __usporediliceDebug("ProductCompare_Return");
    
    if (response && response.indexOf("|") != -1) {
        var checkbox, txresponse, arrResponse;
        
        arrResponse = response.split("|");
        txresponse = arrResponse[0];
        checkbox = arrResponse[1];        

        switch (txresponse) {
            case "ADD_OK":
                ProductCompare_MarkSelected("#" + checkbox, true);
                break;
            case "REMOVE_OK":
                ProductCompare_MarkSelected("#" + checkbox, false);
                break;
            case "SYSTEM_ERROR":
                alert("Došlo je do pogreške u radu aplikacije!");
                break;
            case "NO_CHANGE":
                break;
            case "PARAMS_ERROR":
                alert("Neispravni parametri!");
                break;
            default:
                alert("Došlo je do pogreške prilikom obrade Vašeg zahtjeva!");
                break;
        }
    }
    else
        __usporediliceDebug("ProductCompare_Return ERROR");

    ProductCompare_AjaxRqEnded++;
    
    if (ProductCompare_AjaxRqStarted == ProductCompare_AjaxRqEnded)
        ProductCompare_ShowButtons(true);
}

//  ajax error
function ProductCompare_Failure(oResponse) {
    alert("Došlo je do pogreške prilikom obrade upita!");
    //alert(oResponse.responseText);
}

// UI update handler
function ProductCompare_MarkSelected(id, bSel) {
    __usporediliceDebug("ProductCompare_MarkSelected " + id + " - " + bSel.toString());
    var parent = jQuery(id).parents("li")[0];

    if (parent) {
        if (bSel) {
            jQuery(parent).addClass("compare-selected");
        }
        else {
            jQuery(parent).removeClass("compare-selected");
        }
        __usporediliceDebug(jQuery(parent).attr("class"));
    }
    else
        __usporediliceDebug("parent nije pronađen!");
}

// show / hide UI selection buttons
function ProductCompare_ShowButtons(on) {
    if (on == true) {
        jQuery("a.comparebtn").show();
        jQuery("#compareSelectAll").show();
        jQuery("#compareSelectNone").show();
    }
    else {
        jQuery("a.comparebtn").hide();
        jQuery("#compareSelectAll").hide();
        jQuery("#compareSelectNone").hide();    
    }
}



// debug
function __usporediliceDebug(msg) {
    //alert(msg);
}

/*EOF*/
