﻿var productGroupID = -1;

var clickAnyCheckbox = true;
var clickSelectedProductsCheckbox = true;


//prepare and add handlers for actions on page
$(document).ready
    (
        function() {
            //for each checkbox click
            $(".ResultItemTable input[type='checkbox']").bind('click',
                    function() {
                        //take product group from class attribute
                        productGroupID = $(this).attr("class");
                        var selectedCheckboxID = $(this).attr("id");

                        if ($(this).attr("checked")) {
                            //if item is checked then disable all checkboxes from other product groups
                            $(".ResultItemTable input:checkbox[class!='" + productGroupID + "']").each(function() {
                                $(this).attr("disabled", true);
                                $(this).parent().addClass("divOpacity");
                                $(this).parent().attr("title", disabledAltText);
                            });
                            //since there is at least one product for compare enable Compare button
                            $("#btnCompare").attr("disabled", false);
                            //since there is at least one product which can be added into favrotites enable favorites button                            }
                            $("#btnAddToFavorites").attr("disabled", false);

                            //when at least one product is checked selected products button is enabled
                            clickAnyCheckbox = true;
                            $("#chbSelectedProducts").attr("checked", true);
                            $("#chbSelectedProducts").attr("disabled", false);
                            clickAnyCheckbox = false;

                            saveSelection($(this).attr("identifier"), productGroupID, 'add');
                        }
                        else {//current clicked checkbox is unchecked
                            var foundChecked = false;

                            //check does exist some other checkboxes which are checked
                            $(".ResultItemTable input:checkbox[class='" + productGroupID + "']").not(document.getElementById(selectedCheckboxID)).each(function() {
                                if ($(this).attr("checked")) {
                                    foundChecked = true;
                                }
                            });
                            //if no other checkboxes from the same product group are checked enable all chekboxes in repeater
                            if (!foundChecked) {
                                $(".ResultItemTable input:checkbox[class!='" + productGroupID + "']").each(function() {
                                    $(this).parent().removeClass("divOpacity");
                                    $(this).parent().attr("title", "");
                                    $(this).attr("disabled", false);
                                });

                                //when no product is checked selected products checkbox at the bottom is unchecked and disabled
                                $("#chbSelectedProducts").attr("disabled", false);
                                $("#chbSelectedProducts").attr("checked", false);
                                $("#btnAddToFavorites").attr("disabled", true);

                                //if no product is checked Compare button is disabled
                                $("#btnCompare").attr("disabled", true);
                            }

                            saveSelection($(this).attr("identifier"), productGroupID, 'remove');
                        }
                    }
            );

            //select all functionality
            $("#chbSelectedProducts").bind('click',
                    function() {
                        if (!clickAnyCheckbox) {
                            if (!$(this).attr("checked")) {

                                clickSelectedProductsCheckbox = true;

                                //when selected products checkbox is unchecked:

                                //remove all selected product from session
                                var selectedProductsFromPage = "";
                                var checkedItems = 0;
                                $("input[type=checkbox]").each(function() {
                                    if ($(this).attr("checked")) {
                                        if (checkedItems == 0) {
                                            selectedProductsFromPage += $(this).attr("identifier");
                                        }
                                        else {
                                            selectedProductsFromPage += "," + $(this).attr("identifier");
                                        }
                                        checkedItems++;
                                    }
                                });

                                saveSelection(selectedProductsFromPage, productGroupID, 'remove');

                                // 2. uncheck all checkboxes
                                $("input[type=checkbox]").attr("checked", false);
                                // 3. enable all checkboxes
                                $("input[type=checkbox]").attr("disabled", false);
                                $("input[type=checkbox]").parent().removeClass("divOpacity").attr("title", "");
                                // 4. selected products checkbox is disabled
                                $("#chbSelectedProducts").attr("disabled", true);
                                // 5. add to favorites is disabled
                                $("#btnAddToFavorites").attr("disabled", true);
                                // 6. compare button is disabled
                                $("#btnCompare").attr("disabled", true);

                                clickSelectedProductsCheckbox = false;
                            }
                        }
                    }
            );
            $("#btnAddToFavorites").bind('click',

                function() {
                    if (productGroupID > -1) {
                        var ids = "";
                        var countItems = 0;
                        $(".ResultItemTable input:checkbox[class='" + productGroupID + "']").each(function() {
                            if ($(this).attr("checked")) {
                                if (countItems == 0) {
                                    ids += $(this).attr("identifier");
                                }
                                else {
                                    ids += "," + $(this).attr("identifier");
                                }
                                countItems++;
                            }
                        });
                        document.location.href = "/pricefinder/marktplatz/favorites_processor.aspx?action=add&ids=" + ids + "&from=" + from;
                    }
                }
            );


            var compareWindow;

            $("#btnCompare").bind('click',
                function() {
                    if (productGroupID > -1) {
                        if (compareWindow != null && compareWindow.closed == false) {
                            compareWindow.close();
                        }
                        compareWindow = window.open("/pricefinder/marktplatz/CompareProducts.aspx", "_blank", "height=600,width=800,status=no,toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=yes", true);
                    }
                }
            );



            //clean selections
            $("#ResultTable input:checkbox").attr("checked", false);

            if (selectedItems.length > 0) {

                var selectedArray = selectedItems.split(",");

                var selectedItemOnCurrentPage = false;

                //select selected checkboxes, if any in a case of back by pager, or by pressing back in browser
                for (var i = 0; i < selectedArray.length; i++) {
                    if ($("input:checkbox[identifier=" + selectedArray[i] + "]").length > 0) {
                        $("input:checkbox[identifier=" + selectedArray[i] + "]").attr("checked", "checked");
                        selectedItemOnCurrentPage = true;
                        productGroupID = $("input:checkbox[identifier=" + selectedArray[i] + "]").attr("class");
                    }
                }
                if (selectedItemOnCurrentPage) {
                    $("#btnCompare").attr("disabled", false);
                    $("#btnAddToFavorites").attr("disabled", false);
                    $("#chbSelectedProducts").attr("disabled", false);
                    $("#chbSelectedProducts").attr("checked", true);

                    //if item is checked then disable all checkboxes from other product groups
                    $(".ResultItemTable input:checkbox[class!='" + productGroupID + "']").each(function() {
                        $(this).attr("disabled", true);
                        $(this).parent().addClass("divOpacity");
                        $(this).parent().attr("title", disabledAltText);
                    });
                }
            }

            clickAnyCheckbox = false;
            clickSelectedProductsCheckbox = false;
        }
    );

    function saveSelection(identifier, productGroupID, operation) {
        //ajax call
        $.ajax({
            type: "POST",
            url: "/pricefinder/intro.aspx/CompareProductsList",
            data: "{'productID':'" + identifier + "', 'categoryID':'" + productGroupID + "', 'operation':'"+ operation +"'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json"
        });        
    }