﻿GoodNites.ProductFlash = function(serverVars) {
    this.ServerVars = serverVars;
    this.Initialize();
};
GoodNites.ProductFlash.prototype = {
    Initialize: function() {
        var base = '/na/Swf/Products';
        var file = 'ProductsOffers-';

        switch (serverVars.ProductType) {
            case 'Boxer':
                file += 'Boxers.swf';
                break;
            case 'Underwear':
            case 'Landing':
                file += 'Landing.swf?entry=' + serverVars.ProductType;
                break;

            default:
                // Invalid product type
                if (window.console) {
                    window.console.log('GoodNites.ProductFlash requires a valid productType to be passed. Valid values include: Boxer, Underwear, and Landing');
                }
                return;
        }

        // If the user doesn't have the minimum version of Flash required we should
        // display the message to prompt them to install it.
        if (!$.hasFlashPlayerVersion('8')) {
            $(serverVars.ProductFlashSelector + ' p').css('display', 'block');
            return;
        }

        // Setup a global handler to listen for changes to the Flash application state
        // sent through ExternalInterface calls to the client
        window.notifyProductsOffersStateChange = function(oldState, newState) {
            var context = $(serverVars.ProductDetailsSelector);
            if (context) {
                var os = oldState.toLowerCase();
                var ns = newState.toLowerCase();
                if (os == 'landing') {
                    if (ns == 'boys' || ns == 'girls') {
                        $('.ProdDetWrapperLandingUnderwear', context).slideDown();
                        $('.ProdDetWrapperLandingBoxers', context).css('display', 'none');
                    }
                    else {
                        $('.ProdDetWrapperLandingBoxers', context).slideDown();
                        $('.ProdDetWrapperLandingUnderwear', context).css('display', 'none');
                    }
                } else if (ns == 'landing') {
                    if (os == 'boys' || os == 'girls') {
                        $('.ProdDetWrapperLandingUnderwear', context).slideUp();
                    }
                    else {
                        $('.ProdDetWrapperLandingBoxers', context).slideUp();
                    }
                } else if (ns == "boxers") {
                    $('.ProdDetWrapperLandingHat', context).fadeIn(function() {
                        $('.ProdDetWrapperLandingUnderwear', context).css('display', 'none');
                        $('.ProdDetWrapperLandingBoxers', context).css('display', 'block');
                        $('.ProdDetWrapperLandingHat', context).fadeOut();
                    });
                } else if (os == "boxers") {
                    $('.ProdDetWrapperLandingHat', context).fadeIn(function() {
                        $('.ProdDetWrapperLandingUnderwear', context).css('display', 'block');
                        $('.ProdDetWrapperLandingBoxers', context).css('display', 'none');
                        $('.ProdDetWrapperLandingHat', context).fadeOut();
                    });
                }

                if (s) {
                    s.linkTrackVars = 'prop4,prop5';
                    s.linkTrackEvents = 'clicked boys,clicked girls,clicked boxers,clicked back';
                    s.prop4 = 'Products & Offers - Landing Flash'; // Application Name
                    if (newState == 'landing' || oldState == 'boxers') {
                        if (oldState != 'intro') {
                            s.prop5 = 'clicked back';
                        }
                    } else {
                        s.prop5 = 'clicked ' + newState;
                    }
                    if (s.prop5) {
                        console.log('Tracking event: ' + s.prop4 + ': ' + s.prop5);
                        s.tl(this, 'o');
                    }
                }

                console.log("State change: " + newState);
            }
        };

        $(serverVars.ProductFlashSelector).flash({
            swf: base + '/' + file,
            width: 900,
            height: 404,
            params: {
                wmode: 'transparent',
                base: base  /* Set the relative path resolution to the swf directory */
            }
        });
    }
};
GoodNites.Extend(GoodNites.ProductFlash, GoodNites.Core);
