﻿/// <reference name="MicrosoftAjax.js" />
/// <reference path="jquery-1.4.2.min.js" />
/// <reference path="projectorPoint.js" />

$(document).ready(function() {
    $("a.addToCart").click(function(event) {
        event.preventDefault();
        var $this = $(this);
        ProjectorPoint.Web.WebServices.DefaultService.AddProductToCart($this.attr("data-productId"), 1, function() {
            if (typeof (projectorPoint) != 'undefined') {
                projectorPoint.refreshMiniCart();
            }
            else {
                window.location = "/Basket.aspx";
            }
        }, common.ajaxRequestFailed);
    });

    var filterProducts = function() {
        //var newUrl = window.location.toString();
        var newUrl = $(".productFinder").attr("data-pageUrl");
        if (newUrl.indexOf("?") > 0) {
            newUrl = newUrl.substring(0, newUrl.indexOf("?"));
        }
        var categoryId = $(".productFinder .selectCategory").val();
        newUrl += "?CategoryId=" + categoryId;
        var manufacturerId = $(".productFinder .selectManufacturer").val();
        if (manufacturerId != "0") {
            newUrl += "&ManufacturerId=" + manufacturerId;
        }

        var choiceIds = new Array();
        $(".productFinder .attributeFilterCheck:checked").each(function() {
            choiceIds.push($(this).val());
        });
        if (choiceIds.length > 0) {
            newUrl += "&AttributeFilters=" + choiceIds.join(",");
        }

        var priceRanges = new Array();
        $(".productFinder .priceRangeCheck:checked").each(function() {
            priceRanges.push($(this).val());
        });
        if (priceRanges.length > 0) {
            newUrl += "&PriceRanges=" + priceRanges.join(",");
        }

        window.location = newUrl;
    };

    $(".productFinder .selectManufacturer").change(filterProducts);
    $(".productFinder .selectCategory").change(filterProducts);
    $(".productFinder .priceRangeCheck").click(filterProducts);
    $(".productFinder .attributeFilterCheck").click(filterProducts);
    $(".productFinder .seeMore").click(function(event) {
        event.preventDefault();
        $(this).hide().parents("ul:first").children("li").show();
    });
});
