﻿
App = function () {

    /* Private */

    /* Properties */

    var cmp = {};

    TVI.Components.Button.styles.small = {
        leftEndWidth: 3,
        leftIconWrapperWidth: 26,
        leftPadding: 8,
        leftPaddingWithIcon: 2,
        rightEndWidth: 3,
        rightIconWrapperWidth: 26,
        rightPadding: 8,
        rightPaddingWithIcon: 2,
        iconClass: 'icon',
        hoverClass: 'buttonsmallHover',
        activeClass: 'buttonsmallHover'
    };

    TVI.Components.Button.styles.blue = {
        leftEndWidth: 3,
        leftIconWrapperWidth: 26,
        leftPadding: 8,
        leftPaddingWithIcon: 2,
        rightEndWidth: 3,
        rightIconWrapperWidth: 26,
        rightPadding: 8,
        rightPaddingWithIcon: 2,
        iconClass: 'icon',
        hoverClass: 'buttonblueHover',
        activeClass: 'buttonblueHover'
    };

    /* Methods */

    var init = function () {


        /* Constructor */

        TVI.Components.initialize();

        if ($('.newsletterBox').length != 0) {
            var newsletterForm = new TVI.Form({

                ID: 'newsletterForm',
                url: TVI.handlers + 'account.aspx/newsletter',
                success: function () {

                    $('#newsletterForm').html('<b>Thank you for signing up</b>');

                }

            });
        }

        if (cmp.catID != 0) {

            $('#cat' + cmp.catID).show();

        }

        var sanitised = function (input) {
            return input.replace(/-/g, "_").replace(/ /g, "-").replace(/&/g, "[AND]").replace(/\?/g, "[QUES]");
        }

        cmp.searchForm = new TVI.Form({

            ID: 'search',
            submit: function () {
                window.location = '/search.aspx?keywords=' + cmp.searchForm.field('text').val();
            }

        });

        $('.brandSearch').change(function () {

            window.location = '/search.aspx?brand=' + $(this).val();

        });

        $('.priceSearch').change(function () {

            window.location = '/search.aspx?price=' + $(this).val();

        });

        // Popup 100% height
        var height = $(document).height();
        $('.popupWrapper').height(height);

        var closeBasketTimeout;

        //        $('#topNav .basket, #basket .basketClosed').live('mouseover mouseout', function (event) {
        //            clearTimeout(closeBasketTimeout);
        //            if (event.type == 'mouseover') {
        //                if (!$('#basket .basketOpen').is(':visible')) {
        //                    $('#basket .basketClosed').slideUp('fast', function () {
        //                        $('#basket .basketOpen').slideDown('fast');
        //                        $('.basket a').addClass('basketOpen');
        //                    });
        //                }
        //            } else {
        //                closeBasketTimeout = setTimeout(function () {
        //                    if ($('#basket .basketOpen').is(':visible')) {
        //                        $('#basket .basketOpen').slideUp('fast', function () {
        //                            $('#basket .basketClosed').slideDown('fast');
        //                            $('.basket a').removeClass('basketOpen');
        //                        });
        //                    }
        //                }, 200);
        //            }
        //        });


        $('#topNav .basket, #subNav #basket').live('mouseover mouseout', function (event) {
            clearTimeout(closeBasketTimeout);
            if (event.type == 'mouseover') {
                if (!$('#basket .basketOpen').is(':visible')) {
                    $('#basket .basketClosed').slideUp('fast', function () {
                        $('#basket .basketOpen').slideDown('fast');
                        $('.basket span').addClass('basketOpen');
                    });
                }
            } else {
                closeBasketTimeout = setTimeout(function () {
                    if ($('#basket .basketOpen').is(':visible')) {
                        $('#basket .basketOpen').slideUp('fast', function () {
                            $('#basket .basketClosed').slideDown('fast');
                            $('.basket span').removeClass('basketOpen');
                        });
                    }
                }, 300);
            }
        });

        $('#topNav .basket, #subNav #basket').live('click', function (event) {
                if (!$('#basket .basketOpen').is(':visible')) {
                    $('#basket .basketClosed').slideUp('fast', function () {
                        $('#basket .basketOpen').slideDown('fast');
                        $('.basket span').addClass('basketOpen');
                    });
                }
                
                else {
                    $('#basket .basketOpen').slideUp('fast', function () {
                        $('#basket .basketClosed').slideDown('fast');
                        $('.basket span').removeClass('basketOpen');
                    });
                }
               
        });

        //        $('#basket .basketOpen').live('mouseover mouseout', function (event) {
        //            if (event.type == 'mouseover') {

        //                clearTimeout(closeBasketTimeout);

        //            } else {
        //                closeBasketTimeout = setTimeout(function () {
        //                    if ($('#basket .basketOpen').is(':visible')) {
        //                        $('#basket .basketOpen').slideUp('fast', function () {
        //                            $('#basket .basketClosed').slideDown('fast');
        //                            $('.basket a').removeClass('basketOpen');
        //                        });
        //                    }
        //                }, 200);
        //            }
        //        });



        // Form rating
        $('.formRating .star').click(function () {

            $(this).parent().removeClass().addClass('rating ' + $(this).attr('data-rating'));

            $('#productCommentForm-rating-control').val($(this).attr('data-rating').replace('star', ''));

            return false;
        });

        // Product dropdowns
        $('.productDropdown .productDropdownTop').click(function () {

            // If closed then open// If open then close
            if (!$(this).parents('.productDropdown').find('.productDropdownBottom').is(':visible')) {
                $(this).parents('.productDropdown').find('.productDropdownBottom').slideDown();
            } else {
                $(this).parents('.productDropdown').find('.productDropdownBottom').slideUp();
            }

            return false;
        })

        // GALLERY
        // Append extra <a> tags for lightbox
        $('.gallery .thumbnail').each(function () {

            var largeImage = $(this).attr('data-largeImage');

            if (largeImage != $('.gallery .mainImgContainer A:first').attr('href')) {
                $('<a class="lightbox" rel="gallery" href="' + largeImage + '"></a>').appendTo('.gallery .mainImgContainer');
            }

        });

        // Add lightbox
        $('.lightbox').fancybox({
            padding: '20'
        });

        // Functionality
        $('.gallery .thumbnail').click(function () {

            var galleryContainer = $(this).parents('.gallery').children('.mainImgContainer');

            // Change main image
            galleryContainer.children('A:first').children('img').attr('src', $(this).attr('data-mainImage'));

            // Move correct href to top of stack
            galleryContainer.children('A[href*="' + $(this).attr('data-largeImage') + '"]').attr('href', $('.gallery .mainImgContainer A:first').attr('href'));

            galleryContainer.children('A:first').attr('href', $(this).attr('data-largeImage'));

            return false;

        });


        //FAQ
        // Open/close correct FAQ
        $('.faq .question').click(function () {

            // If open, close and remove 'open' class, If not open, open and add 'open' class
            if ($(this).parent().hasClass('open')) {
                $(this).parent().removeClass('open').find('.answer').slideUp();
            } else {
                // Close all others
                $('.faq .answer').slideUp();
                $('.faq').removeClass('open');

                $(this).parent().addClass('open').find('.answer').slideDown();
            }

            return false;
        });
        // Add hover for button
        $('.faq .question').hover(
			function () {
			    $(this).addClass('hover');
			},
			function () {
			    $(this).removeClass('hover');
			}
		);
        // Add active for button
        $(".faq .question").mousedown(
			function () {
			    $(this).addClass('active');
			}).mouseup(
			function () {
			    $(this).removeClass('active');
			}
		);

        // Delete from basket
        TVI.event('#basket .deleteButton', 'click', function (e) {

            data = {};

            // Build the data
            data['basketItemIndex'] = $(this).closest('.basketItem').attr('data-basketitemindex');

            var that = $(this);

            TVI.ajax({

                url: '/handlers/product.aspx/deleteBasketItem',
                data: data,
                success: function (d) {
                    // We get the basket back
                    $('#basket').html(d.miniBasket);
                    TVI.Components.initialize();

                }, failure: function (d) {
                    for (var i = 0; i < d.errors.length; i++) {
                        $(this).closest('li').find('.errors').text(d.errors[i].message + "<br />").show();
                    }
                }

            });

        });

        //Update basket item
        TVI.event('#basket .refreshButton', 'click', function (e) {

            data = {};

            // Build the data
            data['basketItemIndex'] = $(this).closest('.basketItem').attr('data-basketItemIndex');
            data['quantity'] = $(this).closest('.basketItem').find('input').val();

            if (isNaN(data['quantity'])) {
                $(this).closest('.basketItem').find('.error').text("Invalid Quantity").show();
                return false;
            }

            var el = $(this);

            TVI.ajax({

                url: '/handlers/product.aspx/updateBasketItem',
                data: data,
                success: function (d) {
                    // We get the basket back
                    $('#basket').html(d.miniBasket);
                    TVI.Components.initialize();

                }, failure: function (d) {

                    el.closest('.basketItem').find('.error').text(d.errors[0].message).show();

                }

            });

        });

    };

    var menu = function (name) {
        TVI.ready(function () {

            $('#topNav .' + name + ' a').addClass('selected');

        });
    };


    /* Public */

    TVI.apply(cmp, {

        catID: 0,

        menu: function (name) {

            menu(name);

        },
        formatProducts: function () {

            // Product items
            var height = 175;
            $('.item').each(function () {
                if ($(this).height() > height) {
                    height = $(this).height();
                }
            });
            $('.item').css('height', height);
            $('.specialOfferContainer').css('bottom', height - 149);

        }

    });


    TVI.ready(init);


    return cmp;

} ();

