﻿// UWEBSHOP EXAMPLE SCRIPTS

$(document).ready(function () {

    $.fn.GetJSONForCategory = function (CategoryId, shopAlias) {
        SuperSimpleWebshop.GetJSONForCategory(CategoryId, shopAlias,
      function (data) {
          alert(data.Title);
      },
      function (data) {
          alert('Error' + data.status);
      });
    };

    $.fn.GetJSONForProduct = function (ProductId, shopAlias) {
        SuperSimpleWebshop.GetJSONForProduct(ProductId, shopAlias,
      function (data) {
          alert(data.Title);
      },
      function (data) {
          alert('Error' + data.status);
      });
    };

    $.fn.GetJSONForPricing = function (PricingId, shopAlias) {
        SuperSimpleWebshop.GetJSONForPricing(PricingId, shopAlias,
      function (data) {
          alert(data.Title);
      },
      function (data) {
          alert('Error' + data.status);
      });
    };

    $.fn.AddUpdateOrderline = function (shopAlias, id, action, quantity, variants) {
        SuperSimpleWebshop.addUpdateOrderLine(shopAlias, id, action, quantity, variants,
      function (data) {
          $('#minibasket dl dd').text(data.Order.TotalAmount);
          $('#minibasket dl dd').effect("bounce", { times: 3 }, 300);
      },
      function (data) {
          alert('Error during order creation: ' + data.status);
      });
    };

    // shopdirect links
    $('.shopdirect a').click(function (event) {
        // stop href from firing
        event.preventDefault();
        var shopAlias = $('body').attr('id');
        var pricingID = $(this).attr('id');
        $.fn.AddUpdateOrderline(shopAlias, pricingID, 'add', 1, '');
        $(this).effect("pulsate", { times: 1 }, 200);
    });

    // productpage to basket links
    $('.pricingblock > form > input[type=submit]').click(function (event) {
        // stop href from firing
        event.preventDefault();
        var items = '';
        $(this).parent().find('select[name*=variant] option:selected').each(function (i) {
            if (!$(this).attr("disabled")) {
                items += $(this).val() + ',';
            }
        });
        var shopAlias = $('body').attr('id');
        var pricingID = $(this).parent().find('input[name=product]').val();
        var action = $(this).parent().find('input[name=action]').val();
        var quantity = $(this).parent().find('input[name=quantity]').val();
        var variants = items.substring(0, items.length - 1);

        $.fn.AddUpdateOrderline(shopAlias, pricingID, action, quantity, variants);
        $(this).effect("pulsate", { times: 1 }, 400);
    });

    //equalize height of product blocks for demo skin
    $.fn.equalHeight = function (group) {
        var tallest = 0;
        group.each(function () {
            var thisHeight = $(this).height();
            if (thisHeight > tallest) {
                tallest = thisHeight;
            }
        });
        group.height(tallest);
    };

    //equalize height of product blocks for demo skin
    if ($('ul.itemgrid li.productblock').length) {
        $.fn.equalHeight($("ul.itemgrid li.productblock"));
    };

    //equalize height of pricing blocks for demo skin
    if ($('li.pricingblock').length) {
        $.fn.equalHeight($("li.pricingblock"));
    };

});
