﻿
App.ProductList = function () {

    /* Private */

    /* Properties */

    var cmp = {};


    /* Methods */

    var init = function () {

        App.setProductBanners();

        $('.productWrapper').css('height', $('.productList').height());

        // Sorting
        TVI.event('.pagination select', 'change', function () {

            data = {};

            // Build the data

            if (TVI.Request.QueryString['keywords'] != undefined) {
                data['keywords'] = $('.searchHeading .searchResult').text();
            }
            if (TVI.Request.QueryString['categoryid'] != undefined) {
                data['category'] = TVI.Request.QueryString['categoryid'];
            } else if (App.categoryID != undefined && App.categoryID != 0) {
                data['category'] = App.categoryID;
            }
            if (TVI.Request.QueryString['mode'] != undefined) {
                data['mode'] = TVI.Request.QueryString['mode'];
            }

            for (i in TVI.Request.QueryString) {
                if (i.indexOf('gift') > -1 || i.indexOf('price') > -1) {
                    data[i] = TVI.Request.QueryString[i];
                }
            }

            data['pageUrl'] = window.location.pathname;
            if (App.pageNo != 'view all') {
                data['pageNo'] = 1;
            } else {
                data['pageNo'] = 'view all';
            }
            data['sort'] = $(this).val();

            if (TVI.Request.QueryString['brand'] != undefined) {
                data['brand'] = TVI.Request.QueryString['brand'];
            }

            showProducts(data, 'none');

        });

        TVI.event('.pagination a', 'click', function () {

            data = {};

            // Build the data

            if (TVI.Request.QueryString['keywords'] != undefined) {
                data['keywords'] = $('.searchHeading .searchResult').text();
            }
            if (TVI.Request.QueryString['categoryid'] != undefined) {
                data['category'] = TVI.Request.QueryString['categoryid'];
            } else if (App.categoryID != undefined && App.categoryID != 0) {
                data['category'] = App.categoryID;
            }
            if (TVI.Request.QueryString['keywords'] != undefined) {
                data['keywords'] = $('.searchHeading .searchResult').text();
            }
            if (TVI.Request.QueryString['mode'] != undefined) {
                data['mode'] = TVI.Request.QueryString['mode'];
            }

            data['pageUrl'] = window.location.pathname;


            // Work out page no & direction
            if ($(this).text() == 'next') {
                data['pageNo'] = App.pageNo + 1;
            }
            else if ($(this).text() == 'first') {
                data['pageNo'] = 1;
            }
            else if ($(this).text() == 'prev') {
                data['pageNo'] = App.pageNo - 1;
            }
            else if ($(this).text() == 'last') {
                data['pageNo'] = App.totalPages;
            }
            else if ($(this).text() == 'view all') {
                data['pageNo'] = 'view all';
            }
            else {
                data['pageNo'] = parseInt($(this).text());
            }

            var direction = 'left';
            if (data['pageNo'] == 'view all') {
                direction = 'fade';
            }
            else if (data['pageNo'] < App.pageNo) {
                direction = 'right';
            }


            data['sort'] = $('.pagination select').val();

            if (TVI.Request.QueryString['brand'] != undefined) {
                data['brand'] = TVI.Request.QueryString['brand'];
            }

            for (i in TVI.Request.QueryString) {
                if (i.indexOf('gift') > -1 || i.indexOf('price') > -1) {
                    data[i] = TVI.Request.QueryString[i];
                }
            }


            showProducts(data, direction);

        });


    };

    var timeout = 0;

    TVI.event('.ratingList li', 'click', function () {

        if ($(this).is('.selected')) {
            $(this).removeClass('selected');
        } else {
            $(this).addClass('selected');
        }

        clearTimeout(timeout);

        timeout = setTimeout(function () {
            data = {};

            var priceRanges = "";
            $('.priceList .selected').each(function () {
                priceRanges += $(this).attr('data-priceRange') + ',';
            });

            data['priceRanges'] = priceRanges;

            var stars = "";
            $('.ratingList .selected').each(function () {
                stars += $(this).attr('data-starCount') + ',';
            });

            data['stars'] = stars;

            if (TVI.Request.QueryString['keywords'] != undefined) {
                data['keywords'] = $('.searchHeading .searchResult').text();
            }
            if (TVI.Request.QueryString['categoryid'] != undefined) {
                data['category'] = TVI.Request.QueryString['categoryid'];
            } else if (App.categoryID != undefined && App.categoryID != 0) {
                data['category'] = App.categoryID;
            }
            if (TVI.Request.QueryString['keywords'] != undefined) {
                data['keywords'] = $('.searchHeading .searchResult').text();
            }
            if (TVI.Request.QueryString['mode'] != undefined) {
                data['mode'] = TVI.Request.QueryString['mode'];
            }

            if (App.pageNo != 'view all') {
                data['pageNo'] = 1;
            } else {
                data['pageNo'] = 'view all';
            }
            data['sort'] = $('.pagination select').val();

            showProducts(data, 'fade');
        }, 500);


    });

    timeout = 0;

    TVI.event('.priceList li', 'click', function () {

        if ($(this).is('.selected')) {
            $(this).removeClass('selected');
        } else {
            $(this).addClass('selected');
        }

        clearTimeout(timeout);

        timeout = setTimeout(function () {
            data = {};

            var priceRanges = "";
            $('.priceList .selected').each(function () {
                priceRanges += $(this).attr('data-priceRange') + ',';
            });

            data['priceRanges'] = priceRanges;

            var stars = "";
            $('.ratingList .selected').each(function () {
                stars += $(this).attr('data-starCount') + ',';
            });

            data['stars'] = stars;


            if (TVI.Request.QueryString['keywords'] != undefined) {
                data['keywords'] = $('.searchHeading .searchResult').text();
            }
            if (TVI.Request.QueryString['categoryid'] != undefined) {
                data['category'] = TVI.Request.QueryString['categoryid'];
            } else if (App.categoryID != undefined && App.categoryID != 0) {
                data['category'] = App.categoryID;
            }
            if (TVI.Request.QueryString['keywords'] != undefined) {
                data['keywords'] = $('.searchHeading .searchResult').text();
            }
            if (TVI.Request.QueryString['mode'] != undefined) {
                data['mode'] = TVI.Request.QueryString['mode'];
            }

            if (App.pageNo != 'view all') {
                data['pageNo'] = 1;
            } else {
                data['pageNo'] = 'view all';
            }
            data['sort'] = $('.pagination select').val();

            showProducts(data, 'fade');
        }, 500);


    });


    function showProducts(data, direction) {

        $('.viewAll').after('<div class="pagingLoading" style="float: right; display: block;"></div>');

        // Disable buttons until finished!
        $('.pagination .middle a').remove();

        $('.productsLoading').hide().fadeTo('fast', 0.8);

        TVI.ajax({

            url: '/handlers/app.products.aspx/getProducts',
            data: data,
            responseType: 'text',
            success: function (d) {
                var returningHtml = $(d);

                App.pageNo = data['pageNo'];

                $('.pagination').html(returningHtml.find('.pagination').html());

                var incomingProductsClass = "";
                var animateAmount = "";

                if (direction == 'left' || direction == 'right') {

                    $('.productList').css('float', 'left');

                    if (direction == 'left') {
                        animateAmount = '-760px';
                        $('.productWrapper').append('<div class="incomingProducts productList">' + returningHtml.find('.productList').html() + '</div>');
                    } else if (direction == 'right') {
                        $('.productWrapper').prepend('<div class="incomingProducts productList">' + returningHtml.find('.productList').html() + '</div>');
                        $('.productWrapper').css('left', '-742px');
                        animateAmount = '0px';
                    }

                    $('.productWrapper').css('width', '1920px');
                    TVI.Components.initialize();
                    App.setProductBanners();
                    App.formatProducts(function () {
                        $('.productsLoading').remove();

                        $('.productWrapper').animate({ left: animateAmount, height: $('.incomingProducts').height() }, 1000, 'easeOutQuint', function () {

                            // Remove old products page
                            $('.productList:not(.incomingProducts)').addClass('outgoing');

                            $('.productList').removeClass('incomingProducts');
                            $('.outgoing').remove();
                            $('.productWrapper').css('left', '0');


                        });

                    });
                }
                else {

                    $('.productWrapper .productList').fadeOut('fast', function () {

                        $('.productWrapper').html(returningHtml.find('.productList')).animate({ height: $('.productList').height() }, 1000, 'easeOutQuint');
                        TVI.Components.initialize();
                        App.setProductBanners();
                        App.formatProducts(function () {
                            $('.productWrapper .productList').fadeIn('fast');
                            $('.productsLoading').remove()
                        });
                        

                    });

                }

                $('.pagination select').val(data['sort']);
                App.pageNo = data['pageNo'];

            }

        });

    }


    /* Public */

    TVI.apply(cmp, {

    });


    TVI.ready(init);


    return cmp;


} ();
