﻿
var timeoutID;
var pdpFilmstripCount = 0;
var pdpFilmstripIndex = 0;
var working = false;

$(function() {

    $('.button-search-trigger').click(function() {
        handleSearch($(this).parent().children('input'));
    });

    $('.txt-search-trigger').keypress(function(e) {
        if (e.which == 13) {
            handleSearch($(this).parent().children('input'));
            return false;
        }
    });

    $('.button-add-to-cart').click(function() {
        handleGroupAddToCart(this);
    });

    $('#uc_ink_finder_ddlInk_Brand').change(function() {
        handleInkFinder(this);
    });

    $('#btnFindSuppliesSelect').click(function() {
        handleInkFinder(this);
    });

    $('#aNav-All-Brands').hover(
		function() { $('#divNav').addClass('all-brands'); },
		function() { $('#divNav').removeClass('all-brands'); }
	);

    /*
    $('#aNav-All-Departments').hover(
    function() {
    $('#divNav').addClass('all-departments');
    timeoutID = window.setTimeout(function() {
    $('#divNav-All-Departments').fadeIn(300);
    }, 150);
    },
    function() {
    $('#divNav').removeClass('all-departments');
    window.clearTimeout(timeoutID);
    timeoutID = window.setTimeout(function() {
    $('#divNav-All-Departments').fadeOut(100);
    }, 150);
    }
    );

    $('#divNav-All-Departments').hover(
    function() {
    window.clearTimeout(timeoutID);
    $('#divNav').addClass('all-departments');
    $('#divNav-All-Departments').fadeIn(300);
    },
    function() {
    $('#divNav').removeClass('all-departments');
    $('#divNav-All-Departments').fadeOut(100);
    }
    );
    */

    $('#divPage').click(function() {
        $('#divNav-All-Departments').fadeOut(100);
    });

    $('#aNav-All-Brand-Store-Avery').hover(
		function() { $('#divNav').addClass('brand-store-avery'); },
		function() { $('#divNav').removeClass('brand-store-avery'); }
	);

    $('#aNav-All-Brand-Store-Brother').hover(
		function() { $('#divNav').addClass('brand-store-brother'); },
		function() { $('#divNav').removeClass('brand-store-brother'); }
	);

    $('#aNav-All-Brand-Store-HP').hover(
		function() { $('#divNav').addClass('brand-store-hp'); },
		function() { $('#divNav').removeClass('brand-store-hp'); }
	);

    $('#aNav-All-Brand-Store-Postit').hover(
		function() { $('#divNav').addClass('brand-store-postit'); },
		function() { $('#divNav').removeClass('brand-store-postit'); }
	);

    $('#aNav-All-Brand-Store-Xerox').hover(
		function() { $('#divNav').addClass('brand-store-xerox'); },
		function() { $('#divNav').removeClass('brand-store-xerox'); }
	);

    $('#aFilmstrip-Button-Left').click(function() {
        if ($('#Filmstrip-Wrapper').parent().parent().attr('id') == 'Two-Third-Filmstrip')
            $('#Filmstrip-Wrapper').animate({ 'left': '+=204px' }, 'slow');
        else
            $('#Filmstrip-Wrapper').animate({ 'left': '+=220px' }, 'slow');
        pdpFilmstripIndex--;
        handleFilmstripButton();
    });

    $('#aFilmstrip-Button-Right').click(function() {
        if ($('#Filmstrip-Wrapper').parent().parent().attr('id') == 'Two-Third-Filmstrip')
            $('#Filmstrip-Wrapper').animate({ 'left': '-=204px' }, 'slow');
        else
            $('#Filmstrip-Wrapper').animate({ 'left': '-=220px' }, 'slow');
        pdpFilmstripIndex++;
        handleFilmstripButton();
    });

    pdpFilmstripCount = $('.filmstrip-column').length;

    $('#aFilmstrip-Button-Left').hide();

    if (pdpFilmstripCount < 4) {
        $('#aFilmstrip-Button-Right').hide();
    }

});

function handleFilmstripButton() {
    if (pdpFilmstripIndex > 0)
        $('#aFilmstrip-Button-Left').show();
    if (pdpFilmstripIndex == 0)
        $('#aFilmstrip-Button-Left').hide();
    if (pdpFilmstripIndex == (pdpFilmstripCount - 2))
        $('#aFilmstrip-Button-Right').hide();
    if (pdpFilmstripIndex < (pdpFilmstripCount - 2))
        $('#aFilmstrip-Button-Right').show();
}

function handleFacetButton(e) {
    location.href = $(e).parent().next('a').attr('href');
}

function handleFacetMoreButton(e) {
    $(e).parent().parent().children('li').removeClass('hidden');
    $(e).parent().hide();
}

function handleMoreProductsRequest(e) {
    if (!working) {
        working = true;
        productListPageNumber++;
        var productNextCount = productTotalCount - (50 * productListPageNumber);

        $.get(productListLink + '&page-number=' + productListPageNumber,
            function(data) {
                $(data).appendTo('#divProductList');
            });

        if (productNextCount < 1)
            $(e).parent().hide();
        else if (productNextCount < 51)
            $(e).text('Show ' + productNextCount + ' more products');

        working = false;
    }
}

function handleInkFinder(ddl) {

    var _manufacturer = '';

    if ($(ddl).attr('id') == 'uc_ink_finder_ddlInk_Brand') {
        _manufacturer = $(ddl).val();

        $('#uc_ink_finder_ddlInk_Model >option').remove();
        $('#uc_ink_finder_ddlInk_Model').append($('<option></option>').val('').html('Finding models...'));
    }

    if ($(ddl).attr('id') == 'btnFindSuppliesSelect') {

        if (!$('#uc_ink_finder_ddlInk_Model').val())
            return;

        var url = $('#uc_ink_finder_ddlInk_Model').val();

        if ($('#uc_ink_finder_ddlInk_Brand').length)
            $('#uc_ink_finder_ddlInk_Brand').get(0).selectedIndex = 0;

        $('#uc_ink_finder_ddlInk_Model >option').remove();
        $('#uc_ink_finder_ddlInk_Model').append($('<option></option>').val('').html($('#uc_ink_finder_ddlInk_Model').attr('alt')));

        location.href = url;
    }

    $.ajax({
        type: 'GET',
        url: '/_webcontrols/asynchronous/ink-finder-type-line-model.html',
        data: {
            manufacturer: _manufacturer
        },
        success: function(data) {

            var inventoryCompatibility = jQuery.parseJSON(data);

            $('#uc_ink_finder_ddlInk_Model >option').remove();
            $('#uc_ink_finder_ddlInk_Model').append($('<option></option>').val('').html($('#uc_ink_finder_ddlInk_Model').attr('alt')));

            for (var i = 0; i < inventoryCompatibility.Model.length; i++) {
                $('#uc_ink_finder_ddlInk_Model').append($('<option></option>').val('/' + inventoryCompatibility.Model[i].Link).html(inventoryCompatibility.Model[i].Name));
            }

            $('#uc_ink_finder_ddlInk_Model').removeClass('ddl-default');

            if ($(ddl).attr('id') == 'uc_ink_finder_ddlInk_Line_Type') {
                $('#uc_ink_finder_ddlInk_Model').focus();
            }
        }
    });
}

function handleSearch(obj) {

    var query = obj.val().trim().toLowerCase();

    if (query.length == 0)
        return;

    var constraint = '';
    var manufacturer = '';

    if ($(obj).attr('constraint'))
        constraint = $(obj).attr('constraint');

    if ($(obj).attr('manufacturer'))
        manufacturer = $(obj).attr('manufacturer');

    switch (constraint) {

        case 'none':
            location.href = '/search-results.html?q=' + encodeURI(query);
            break;

        case 'manufacturer':
            location.href = '/search-results.html?manufacturer=' + encodeURI($(obj).attr('manufacturer')) + '&q=' + encodeURI(query);
            break;

        case 'ink-and-toner':
            location.href = '/search-results-ink-and-toner.html?q=' + encodeURI(query);
            break;

    }
}

function handleGroupQuantity(obj) {
    var totalquantity = 0;
    $('.form-quantitybox-input').each(function() {
        if (parseInt($(this).val())) {
            totalquantity += parseInt($(this).val());
        }
    });
    if (totalquantity == 1) {
        $('.button-add-to-cart').children().children('input').val('Add ' + totalquantity + ' item to Cart');
    } else if (totalquantity > 1) {
        $('.button-add-to-cart').children().children('input').val('Add ' + totalquantity + ' items to Cart');
    } else {
        $('.button-add-to-cart').children().children('input').val('Choose a quantity first');
    }
}

function handleGroupAddToCart(obj) {
    var iterator = 0;
    var arId = new Array();
    var arQuantity = new Array();

    $('.form-quantitybox-input').each(function() {
        if (parseInt($(this).val())) {
            arId[iterator] = $(this).attr('inventoryitemid');
            arQuantity[iterator] = $(this).val();
            iterator++;
        }
    });

    $.get(
        "/_webcontrols/asynchronous/add-to-cart.aspx",
        {
            id: arId,
            quantity: arQuantity
        },
        function() {
            if ($('#divCart').css('opacity') == '0') {
                $.get(
                    "/_webcontrols/asynchronous/persistent-cart.aspx",
                    function(data) {
                        $('#divCart').html("");
                        $(data).appendTo('#divCart');
                        $('#divCart').animate({ bottom: '0px', opacity: 1 }, 333);
                    });

            } else {
                $('#divCart').animate({ bottom: '-82px', opacity: 0 }, 333,
                    function() {
                        $.get(
                            "/_webcontrols/asynchronous/persistent-cart.aspx",
                            function(data) {
                                $('#divCart').html("");
                                $(data).appendTo('#divCart');
                                $('#divCart').animate({ bottom: '0px', opacity: 1 }, 333);
                            });
                    });
            }
        });
    }


