var ProductAlerts = (function() {

    var globalAlertCount = 0;
    var carouselVisibleItems = 3;
    var alertLeftPos ='';
    var wishlist_link = '/' + channel + '/wishlist.nap';

    var initDropdown = function(data) {
		var alertCount = data.count;
        alertLeftPos = $("#header-content").contents().find("#alert-number").position();
        var $alertsDropdown = $('#alerts-dropdown');
        var newAlertsmsg = (alertCount === 0) ? "<h3>You have 0 new Wish list alerts</h3>" : (alertCount > 1) ? "<h3>You have " + alertCount + " NEW Wish list Alerts</h3>" : "<h3>You have 1 NEW Wish list Alert</h3>";

        globalAlertCount = alertCount;

        alertLeftPos = alertLeftPos.left - 97.5;

        $alertsDropdown.find('h3').remove();
        $alertsDropdown.find('h2').after(newAlertsmsg);

        getAlertProducts();

        $('#alerts-dropdown').css('left', alertLeftPos);
        $('#alerts-dropdown').slideToggle('fast');
    };

    var insertAlertProducts = function(data) {

        if(data.listItems.length > 0) {

            var $alertsDropdown = $('#alerts-dropdown');
            var $carouselContainer = $('#carousel-container');
            var slug = '', pid = '', alertProduct = '', counter = 0, alertsLimit = 20;
            var viewAllAlerts = '<li id="view_all_alerts"><a href="' + wishlist_link + '">VIEW ALL PRODUCT ALERTS<br /> IN YOUR WISH LIST</a></li>';

            $alertsDropdown.removeClass('no-alerts');
            $carouselContainer.html('<div class="prev" id="alert-carousel-prev"></div><div class="alert-carousel"><ul></ul></div><div class="next" id="alert-carousel-next"></div>');

            for(var k in data.listItems) {

                slug = '';
                pid = data.listItems[k].productId;

                if(data.listItems[k].itemSlug == 'BACK') {
                    slug = '<img src="/images/slugs/product_list/back_in_stock.gif" alt="back in stock" />';
                }
                if(data.listItems[k].itemSlug == 'LOW_STOCK') {
                    slug = '<img src="/images/slugs/product_list/low_stock.gif" alt="low stock" />';
                }
                if(data.listItems[k].itemSlug == 'BACK_ON_SALE') {
                    slug = '<img src="/images/slugs/product_list/back_sale.gif" alt="back in stock on sale" />';
                }
                if(data.listItems[k].itemSlug == 'LOW_STOCK_ON_SALE') {
                    slug = '<img src="/images/slugs/product_list/low_sale.gif" alt="low stock on sale" />';
                }
                if(data.listItems[k].itemSlug == 'ON_SALE') {
                    slug = '<img src="/images/slugs/product_list/sale.gif" alt="on sale" />';
                }

                counter++;

                alertProduct = '<li><a ' + ((data.listItems[k].viewed) ? 'class="read-alert" ' : 'class="unread-alert" ') + 'href="/product/' + pid + '"></a><img src="/images/products/' + pid + '/' + pid + '_in_xs.jpg" alt="" class="product-image" /><br />' + slug + '<br /><span class="designer">' + data.listItems[k].designerName + '</span>' + ((counter == data.listItems.length && counter < alertsLimit) ? '' : '<hr />' ) + '</li>'

                $("#alerts-dropdown ul").append(alertProduct);

                if(counter > (alertsLimit-1)) break;

            }

            if(data.listItems.length >= alertsLimit)
            {
                $("#alerts-dropdown ul").append(viewAllAlerts);
            }

            if(data.listItems.length > carouselVisibleItems) {
                $('.alert-carousel').napCarousel({
                    btnNext: "#alert-carousel-next",
                    btnPrev: "#alert-carousel-prev",
                    vertical: true,
                    circular: false,
                    visible: carouselVisibleItems,
                    scroll: 1
                });
            } else {
                $('#alert-carousel-prev').hide();
                $('#alert-carousel-next').hide();
                $('.alert-carousel').css('margin', '10px auto');
            }

            if(data.listItems.length >= alertsLimit)
            {
                $('.alert-carousel').height(336);
            }

        } else {

            $('#alerts-dropdown').addClass("no-alerts");
            $('#carousel-container').html("We will alert you here as soon as items are low in stock, back in stock or on sale so you don't miss out!");

        }

    };

    var  getUnreadAlertCount = function() {
        // get the most up to date count of unread alerts from the server
        AlertService.getCurrentUserUnviewedAlertCount({
            callback: initDropdown,
            timeout: 3000,
            errorHandler: function() {
                //do nothing
            }
        });
    };

    var getAlertProducts = function(){
        AlertService.getCurrentUserAlerts({
            callback: insertAlertProducts,
            timeout: 10000,
            errorHandler: function(){
                // do nothing
            }
        });
    };

    var insertAlertsHtml = function() {

        var alertDropdownHtml = [
            '<div id="alerts-dropdown">',
                '<div class="arrow-div"><img src="/nap/build/2011.17.00/images/generic/up_light_gr_arrow.gif" alt="" /></div>',
                '<div id="alerts-dropdown-inner">',
                    '<div class="close" id="alerts-close-button"></div>',
                    '<h2><a href="' + wishlist_link + '">Don\'t miss <span class="italics">out!</span></a></h2>',
                    '<div id="carousel-container">',
                        '<div class="alert-carousel" id="alert-carousel">',
                            '<div id="ajax-loader-container"><img src="/nap/build/2011.17.00/images/wishlist/ajax-loader-small.gif" /></div>',
                        '</div>',
                    '</div>',
                    '<a href="' + wishlist_link + '" class="view-wishlist">view my wish list</a>',
                '</div>',
            '</div>'
        ].join('');

        $(alertDropdownHtml).appendTo('#header');

        $("#alerts-close-button").click(function(){
            $('#alerts-dropdown').slideUp('fast');
        });

        $(document).click(function () {
            $('#alerts-dropdown').slideUp('fast');
        });

        $('#alerts-dropdown').click(function(event) {
            event.stopPropagation();
        });
    };

    return {
        getAlertProducts: getAlertProducts,
        getUnreadAlertCount: getUnreadAlertCount,
        insertAlertsHtml: insertAlertsHtml
    };
}());

