var Dentyne = {
    /**
     * Initialize Dentyne.
     */
    'init': function() {
        $('#content > footer > nav > ul > li:last-child').addClass('last-child');
    },

    /**
     * 
     */
    'Promos': {
        /**
         * Initialize Promos.
         *
         * Add click event handlers to light boxes.
         */
        'init': function() {
            $('#dentyne-ice a').click(function() {
                Dentyne.Lightbox.open($(this));
                return false;
            });

            $('#promo-content a.video, #promo-content a.watch').click(function() {
                Dentyne.Lightbox.open($(this));
                return false;
            });
        }
    },

    /**
     * Products specific JavaScript.
     */
    'Products': {
        /**
         * The number of products per row.
         *
         * @var int
         */
        'productsPerRow': 3,

        /**
         * Initialize Products.
         */
        'init': function() {
            Dentyne.Products._addRowClasses();
        },

        /**
         * Add non semantic classes to products list elements so they can be
         * stylized appropriately based on the design.
         *
         * @return void
         */
        '_addRowClasses': function() {
            var products = $('#products > ul > li');
            var perRow   = Dentyne.Products.productsPerRow;
            var row      = 1;
            products.each(function(){
                var index = parseInt(products.index(this));
                if(index >= perRow && parseInt(index % perRow) == 0) {
                    row++;
                }
                $(this).addClass('row-' + row);
            });
        }
    },

    /**
     * Lightbox functionality.
     */
    'Lightbox': {
        'close': function() {
            // Fade out panel and overlay, then remove from DOM
            var $$pipeline = $([
                document.getElementById("pipeline-lightbox"),
                document.getElementById("dentyne-pure-details"),
                document.getElementById("video-player")
            ]);
            if($$pipeline.length) {
                $$pipeline.fadeOut(300, function() {
                    $("#pipeline-lightbox, #video-player").remove(); 
                });
            }
        },
        'open': function(invokingObj) {
            if(!document.getElementById("pipeline-lightbox")) {
                if(undefined != invokingObj) {
                    if(invokingObj.hasClass('video') || invokingObj.hasClass('watch')) {
                        var contentId = 'video-player';
                        $(document.body).append([
                            '<div id="'+contentId+'">',
                                '<iframe src="' + invokingObj.attr('href') + '"></iframe>',
                                '<a href="#" class="exit">Close</a>',
                            '</div>'
                        ].join(''));
                    } else {
                        var contentId = invokingObj.attr('href').replace('#', '');
                        var thing     = $(document.getElementById(contentId));
                        thing.remove();
                        $(document.body).append(thing);
                    }
                }
                $(document.body).append('<div id="pipeline-lightbox"></div>');

                var panel = $(document.getElementById(contentId));
                panel.fadeIn(300);

                // Close lightbox if close/exit link is clicked
                panel.children("a.exit").click(function() {
                    Dentyne.Lightbox.close();
                    return false;
                });
                // Close lightbox if background overlay is clicked
                $(document.getElementById("pipeline-lightbox")).click(function() {
                    Dentyne.Lightbox.close();
                });
                // Close lightbox if escape key is pressed
                $(document.documentElement).keydown(function(e) {
                    if(e.keyCode == 27) {
                        Dentyne.Lightbox.close();
                    }
                });
            }
        }
    }
}

$(function(){
    // Always initialize.
    Dentyne.init();

    // Initialize products on the products page only.
    if($('body#home-page').length > 0) {
        Dentyne.Promos.init();
    }

    // Initialize products on the products page only.
    if($('body#products-page').length > 0) {
        Dentyne.Products.init();
    }
});
