﻿$(document).ready(function() {
    $('a.save-outing').click(function() {
        _gaq.push(['_trackEvent', 'Events', 'Saved', $(this).attr("value")]);
        $.ajax({
            type: "POST",
            url: "/outing/save/" + $(this).attr("value"),
            data: "",
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            success: function(status) {
                $("#save_outing_button_" + status.Id).hide();
                $("#save_outing_result_" + status.Id).fadeIn("slow");

                var currentSavedItemsCount = $("#cart-sidebar > li").size()
                var oddEventCssClass = "odd";
                if (currentSavedItemsCount % 2 == 0)
                    oddEventCssClass = "even";
                if (currentSavedItemsCount == 0)
                    oddEventCssClass += " last";

                $("#no-saved-outings").hide();
                $("#saved-outings-list").show();

                var newlySavedItemHtml = '<li class="item ' + oddEventCssClass + '">';
                newlySavedItemHtml += '<div class="product-selector"><input type="checkbox" value="' + status.Id + '"/></div>';
                newlySavedItemHtml += '<a class="product-image" href="/uitjes/detail/' + status.Id + '/' + status.CleanNameForUrl + '">' + status.MiniPicTag + '</a>';
                newlySavedItemHtml += '<div class="product-details">';
                newlySavedItemHtml += '<a href="/uitjes/detail/' + status.Id + '/' + status.CleanNameForUrl + '">' + status.Name + '</a>';
                newlySavedItemHtml += '</div></li>';
                $("#cart-sidebar").prepend(newlySavedItemHtml).hide().fadeIn("slow");

            },
            error: function(xhr, status, error) {
                alert("Er is een fout opgetreden. Het uitje is niet bewaard.");
                console.log("ajax error!");
                console.log(error);
            }
        });
        return false;
    });
});





$(document).ready(function() {
    $('a#compare').click(function() {
        if ($(".mini-products-list input:checkbox:checked").length > 1) {
            var ids = "";
            $(".mini-products-list input:checkbox:checked").each(function() {
                ids += $(this).val() + ",";
            });
            if (ids.length > 0)
                ids = ids.substring(0, ids.length - 1);

            window.open('/outing/compare/?ids=' + ids, 'compare', 'top:50,left:50,width=800,height=600,resizable=yes,scrollbars=yes');
        } else {
            alert("Selecteer tenminste 2 uitjes om te vergelijken");
        }
        return false;
    });

    $('a#quote').click(function() {
        if ($(".mini-products-list input:checkbox:checked").length > 0) {
            var ids = "";
            $(".mini-products-list input:checkbox:checked").each(function() {
                ids += $(this).val() + ",";
            });
            if (ids.length > 0)
                ids = ids.substring(0, ids.length - 1);
            window.location = "/quote/create?ids=" + ids;
        } else {
            alert("Selecteer tenminste 1 uitje om een offerte aan te vragen");
        }
        return false;
    });

    $('a#outingsavelist_remove').click(function() {
        if ($(".mini-products-list input:checkbox:checked").length > 0) {
            if (confirm('De geselecteerde uitje(s) zullen worden verwijderd uit u bewaardlijst.')) {
                var ids = "";
                $(".mini-products-list input:checkbox:checked").each(function() {
                    ids += $(this).val() + ",";
                });
                if (ids.length > 0)
                    ids = ids.substring(0, ids.length - 1);

                $.ajax({
                    type: "POST",
                    url: "/outing/remove/" + ids,
                    data: "",
                    dataType: "json",
                    contentType: "application/json; charset=utf-8",
                    success: function(status) {
                        $(".mini-products-list input:checkbox:checked").each(function() {
                            if ($('#save_outing_result_' + $(this).val()) != null) {
                                $('#save_outing_result_' + $(this).val()).hide();
                            }
                            if ($('#save_outing_button_' + $(this).val()) != null) {
                                $('#save_outing_button_' + $(this).val()).show();
                            }
                            $(this).parent().parent().remove();

                        });
                        if ($(".mini-products-list input:checkbox").length < 1) {
                            $('#saved-outings-list').hide();
                            $('#no-saved-outings').show();
                        }

                    },
                    error: function(xhr, status, error) {
                        alert("Er is een fout opgetreden. De uitjes zijn niet verwijderd.");
                    }
                });

            }
        } else {
            alert("Selecteer tenminste 1 uitje om te verwijderen uit u bewaarlijst.");
        }
        return false;
    });
});	