var oNewsletterForm = {};
var oBillboard;
var oBillboardNavSlider;

function submitNewsletterForm() {
    var strFormData = oNewsletterForm.validate();
    if(strFormData !== false)
    {
        $("newsletter_form").submit();
    }
};

var Billboard = new Class({
    initialize: function(oOptions) {
        this.aButtons = [];
        
        this.oContainer = $("billboard");
        this.oBillboardNav = $("billboard_navigation").grab(new Element("ul"));
        this.iCurrentIndex = 0;
        this.bIsPaused = false;
        
        var strPath = "/resources/flash/billboard/SpotxBillboard.swf";
        if(Browser.Engine.trident)
        {
            strPath += "?cacheBreak=" + Math.random();
        }
        var oOptions = {
            width: "100%",
            height: "100%",
            params: {
                base: "."
            },
            vars: {
                cdn_path: oOptions.cdn_path
            },
            properties: {menu: false},
            callBacks: {
                changeBoard: this.changeBoard
            }
        };
        this.swf = new Swiff(strPath, oOptions);
        this.oContainer.grab(this.swf);
        
        // Init the billboard nav slider
        oBillboardNavSlider = new NavSlider($("billboard_navigation").getElement("ul"), $("billboard_slider"));
    },
    
    // Called from the SWF
    // Creates menu buttons for the billboard
    createButton: function(args) {
        var strButtonTitle = args[0];
        var iButtonIndex = args[1];
        
        var oButtonLink = new Element("a").set("html", strButtonTitle);
        oButtonLink.store("index", iButtonIndex);
        oButtonLink.addEvent("click", function() {
            this.changeBoard(iButtonIndex);
            return false;
        }.bind(this));
        
        var oButton = new Element("li").grab(new Element("span").grab(oButtonLink));;
        this.oBillboardNav.getElement("ul").grab(oButton);
        this.aButtons.push(oButton);
    },
    
    // Called from the swf to dynamically set teh button width
    setTotalButtons: function(args) {
        var totalButtons = args[0];
        
        var navWidth = $("billboard_navigation").getWidth();
        var buttonWidth = navWidth / totalButtons;
        $$("#billboard_navigation a").setStyle("width", buttonWidth);
    },
    
    // Change to a new board when a menu button is clicked
    changeBoard: function(iIndex) {
        // setCurrentButton is called from the swf
        Swiff.remote(this.swf.object, "changeBoard", iIndex);
        this.iCurrentIndex = iIndex;
    },
    
    // Set the currently selected button
    setCurrentButton: function(iIndex) {
        this.aButtons.each(function(oButton) {
            oButton.removeClass("current");
        });
        this.aButtons[iIndex].addClass("current");
        
        // update the nav slider
        oBillboardNavSlider.updateCurrent(this.aButtons[iIndex]);
    }
});