﻿$(function () {
    $('.product-container')
        .find('.add-modal-link').bind('click.supere', function () { GetAddModal(this); return false; }).end()
        .find('.remove-product-link').bind('click.supere', function () { RemoveProduct(this); return false; }).end()
        .find('.add-product-link').bind('click.supere', AddProduct).end()
        .find('.add-gift-link').bind('click.supere', AddGift);
    $('.remove-gift-link').bind('click.supere', RemoveGift);
    $('.filter-link').bind('click.supere', FilterProducts);
    $('.unfilter-link').bind('click.supere', ClearFilters);
    $('.country-select-link').bind('click.supere', function () { SetCountry($(this).attr('alt')); return false; });
    $('body').delegate('.x-fidelity-points', 'change.supere', function () { SetFidelityPoints($(this).val()); });

    bindSpinners();
    checkFilters();
    bindSearch();
});

function setupBindings(context) {
    $('.product-container', context)
        .find('.add-modal-link').bind('click.supere', function () { GetAddModal(this); return false; }).end()
        .find('.remove-product-link').bind('click.supere', function () { RemoveProduct(this); return false; }).end()
        .find('.add-product-link').bind('click.supere', AddProduct).end()
        .find('.add-gift-link').bind('click.supere', AddGift);
    $('.remove-gift-link', context).bind('click.supere', RemoveGift);
    $('.filter-link', context).bind('click.supere', FilterProducts);
    $('.unfilter-link', context).bind('click.supere', ClearFilters);
    $('.country-select-link', context).bind('click.supere', function () { SetCountry($(this).attr('alt')); return false; });
    $('.x-fidelity-points').bind('change.supere', function () { SetFidelityPoints($(this).val()); });

    bindSpinners(context);
    checkFilters();
    bindSearch();
}

function bindSearch() {
    function doSearch() {
        window.location.href = catalogUrl + '?search=' + encodeURIComponent($('.search-input').val());
    }

    $(".search-input").one('keydown', function () {
        SEWebService.GetKeywords(siteID, function (keywords) {
            $(".search-input").autocomplete(keywords, {
                width: 110,
                highlight: false,
                multiple: true,
                multipleSeparator: " ",
                scroll: true,
                scrollHeight: 200
            });
        });
    }).keydown(function (e) {
        if (e.keyCode == 13 && !$(".ac_results").is(':visible')) {
            doSearch();
            return false;
        }
    });

    $('a.search-link').click(function () {
        doSearch();
        return false;
    });
}

function bindSpinners(context) {
    $('.dec-quantity', context).bind('click.supere', decQ);
    $('.inc-quantity', context).bind('click.supere', incQ);
}

function decQ() {
    var e = $(this);
    var step = e.attr('data-step') || 1;
    step = parseInt(step, 10);
    var q = e.closest('.product-container').find('.quantity');
    var i = parseInt(q.val() || q.html(), 10);
    if (i > step)
        i -= step;
    if(q.is('input'))
        q.val(i);
    else
        q.html(i);
	$(document).trigger('quantity-changed');
    if (e.is('.cart-list *'))
        SetProductQuantity(e);
}

function incQ() {
    var e = $(this);
    var step = e.attr('data-step') || 1;
    step = parseInt(step, 10);
    var q = e.closest('.product-container').find('.quantity');
    var i = parseInt(q.val() || q.html(), 10);
    i += step;
    if (q.is('input'))
        q.val(i);
    else
        q.html(i);
	$(document).trigger('quantity-changed');
    if (e.is('.cart-list *'))
        SetProductQuantity(e);
}

function checkFilters() {
    var ids = $.query.get('filter').toString().split("|");
    for (count in ids) {
        $('input#fil' + ids[count]).attr("checked", "checked");
    }
}

function FilterProducts() {
    var filter = "";
    $(this).closest('.filter-panel').find("input:checked").each(function(e) {
        filter += $(this).val() + "|";
    });
    filter = filter.slice(0, -1);
    location.href = $.query
        .set('page', false)
        .set('viewmode', false)
        .set('orderby', false)
        .set('pagesize', false)
        .set('filter', filter).toString();
    return false;
}

function ClearFilters() {
    location.href = "?";
    return false;
}

function GetAddModal(e) {
    var elem = $(e).closest('.product-container');
    var productId = elem.find('.product-id').val();
    ShowAddModal(productId);
}

function ShowAddModal(productId) {
    $('.add-modal').remove();
    SEWebService.GetAddModal(siteID, productId,
    function (data) {
        $('body').append($("<div class='add-modal product-container'></div>").html(data));
        $('.add-modal .add-product-link').bind('click', AddProduct);
        bindSpinners();
        $(document).trigger('add-modal-success');
    },
    OnError);
}

function AddProduct() {
    var $this = $(this);
    var elem = $this.closest('.product-container');
    var productId = elem.find('.product-id').val();
    var optionId = elem.find('.option-id').val() || 0;
    var quantity = elem.find('.quantity').val() || elem.find('.quantity').html() || 1;
    SEWebService.AddProduct(siteID, productId, quantity, optionId, function(data) {
        $(document).trigger('cart-changed', $this);
        $(document).trigger('product-added', $this);
    }, OnError);
    return false;
}

function AddComplexProduct(productId, quantity, optionId, itemsIds, infos) {
    SEWebService.AddComplexProduct(siteID, productId, quantity, optionId, itemsIds, infos, function (data) {
        $(document).trigger('cart-changed');
        $(document).trigger('product-added');
    }, OnError);
    return false;
}

function AddGift() {
    var elem = $(this).closest('.product-container');
    var productId = elem.find('.product-id').val();
    var optionId = elem.find('.option-id').val() || 0;
    var quantity = elem.find('.quantity').val() || elem.find('.quantity').html() || 1;
    SEWebService.AddGift(siteID, productId, quantity, optionId, function(data) {
        $(document).trigger('gifts-changed');
    }, OnError);
    return false;
}

function RemoveGift() {
    SEWebService.RemoveGift(siteID, function(data) {
        $(document).trigger('gifts-changed');
    }, OnError);
    return false;
}

function SetProductQuantity_old(e) {
    var elem = $(e).closest('.product-container');
    var productId = elem.find('.product-id').val();
    var optionId = elem.find('.option-id').val();
    var quantity = elem.find('.quantity').val() || elem.find('.quantity').html() || 1;
    SEWebService.SetProductQuantity(siteID, productId, quantity, optionId, function() {
        $(document).trigger('cart-changed');
    }, OnError);
    return false;
} 

function SetProductQuantity(e) {
    var elem = $(e).closest('.product-container');
    var tempId = elem.find('.temp-id').val();
    var quantity = elem.find('.quantity').val() || elem.find('.quantity').html() || 1;
    SEWebService.SetProductQuantity(siteID, tempId, quantity, function () {
        $(document).trigger('cart-changed');
    }, OnError);
    return false;
}

function RemoveProduct_old(e) {
    var elem = $(e).closest('.product-container');
    var productId = elem.find('.product-id').val();
    var optionId = elem.find('.option-id').val();
    SEWebService.RemoveProduct(siteID, productId, optionId, function() {
        $(document).trigger('cart-changed');
    }, OnError);
    elem.slideUp(300);
    return false;
} 

function RemoveProduct(e) {
    var elem = $(e).closest('.product-container');
    var tempId = elem.find('.temp-id').val();
    SEWebService.RemoveProduct(siteID, tempId, function () {
        $(document).trigger('cart-changed');
    }, OnError);
    elem.slideUp(300);
    return false;
}

function OnError(data) {
    if (console) console.log(data.get_message());
}


function SetCountry(contryCode){
    SEWebService.SetCurrentCountry(contryCode, function (data) {
        window.location.reload();
    }), OnError;
}

function SetFidelityPoints(points) {
    SEWebService.SetFidelityPoints(siteID, points, function (data) {
        $(document).trigger('cart-changed');
    }, OnError);
}
