Type.registerNamespace('WindowsSite.Ajax.Controls');

WindowsSite.Ajax.Controls.HeroPanelBehavior = function( element ) 
{
    WindowsSite.Ajax.Controls.HeroPanelBehavior.initializeBase( this, [element] );
    
    this.contentRecievedDelegate = Function.createDelegate( this, this._onContentRecieved );
    this.errorDelegate = Function.createDelegate( this, this._onError );
    this.fadeOutStepTick = Function.createDelegate( this, this._fadeOutStep );
    this.fadeInStepTick = Function.createDelegate( this, this._fadeInStep );
    this.transitionHeroStepTick = Function.createDelegate( this, this._transitionStep);
    this.currentItemSelectedIndex = 0;
    this.fadeIntervalTime = 80;
    this.transitionHeroTime = 240;
}

WindowsSite.Ajax.Controls.HeroPanelBehavior.prototype =
{
    initialize: function() {
        this.fadeElement = document.getElementById(this.get_FadeElementID());
        WindowsSite.Ajax.Controls.HeroPanelBehavior.callBaseMethod(this, 'initialize');


        this.imageDiv = document.createElement('div');
        this.imageDiv.innerHTML = this.get_HeroContentList()[0];
        this.imageDiv.setAttribute("class", "zoomEqualsOne");
        this.imageDiv.setAttribute("className", "zoomEqualsOne");
        this.set_FadeElementID("HeroImageDiv");
        this.imageDiv.id = this.get_FadeElementID();
        this.get_element().appendChild(this.imageDiv);

        //generate array that keeps track of web trends hero auto impressions:
        this.HasHeroAutoImpressionBeenRecorded = new Array();
        for (i = 0; i < this.get_HeroContentList().length; i++) {
            this.HasHeroAutoImpressionBeenRecorded[i] = false;
        }
        eval(this.get_OmnitureImpressionsCommandsList()[0]);
        this.HasHeroAutoImpressionBeenRecorded[0] = true; //the first hero is automatically impressed already
    },

    dispose: function() {
        WindowsSite.Ajax.Controls.HeroPanelBehavior.callBaseMethod(this, 'dispose');
    },


    _onControlCommand: function(sender, args) {
        if (args != null) {
            this._commandName = args.get_CommandName();
            this._commandArgument = args.get_CommandArgument();
        }
        switch (this._commandName) {
            case "HeroNavClicked":
                this.DisplayNewHero(this._commandArgument);
                break;
            default:
                WindowsSite.Ajax.Controls.ContentPanelBehavior.callBaseMethod(this, '_onControlCommand');
                break;
        }
    },

    _onContentRecieved: function(result, userContext) {
        var newDiv = document.createElement('div');
        newDiv.innerHTML = result;
        newDiv.setAttribute("class", "zoomEqualsOne");
        newDiv.setAttribute("className", "zoomEqualsOne");
        newDiv.id = this.get_FadeElementID();
        this.get_element().removeChild(this.get_element().firstChild);
        this.get_element().appendChild(newDiv);

        this.FadeIn();
    },

    _onError: function(result) {
        this.get_element().innerHTML = "<div class='contentPanelError'>There was an error retrieving data.</div>";
    },

    ChangeHeroContent: function() {
        // show the transition hero, then in the end of the transition hero interval, FadeIn the next hero
        this.SetOpacity(4);
        if (this.fadeInterval != null) {
            clearInterval(this.fadeInterval);
        }
        this.currentOpacityValue = 10; //display transition hero full opacity
        this.fadeInterval = setInterval(this.transitionHeroStepTick, this.transitionHeroTime);
    },

    CheckForIndexEdgeCases: function() {   //checks for edge cases in setting selected hero index
        if (this.currentItemSelectedIndex == -1) {
            this.currentItemSelectedIndex = this.get_HeroIDList().length - 1;
        }
        else if (this.currentItemSelectedIndex == this.get_HeroIDList().length) {
            this.currentItemSelectedIndex = 0;
        }
    },

    DisplayNewHero: function(newHeroIndex) {
        if (newHeroIndex == "HeroLeftArrowClicked") {
            this.currentItemSelectedIndex = this.currentItemSelectedIndex - 1;
            this.CheckForIndexEdgeCases();
        }
        else if (newHeroIndex == "HeroRightArrowClicked") {
            this.currentItemSelectedIndex = this.currentItemSelectedIndex + 1;
            this.CheckForIndexEdgeCases();
        }
        else if (newHeroIndex == "AutoDisplayNextHero") //this logic is purely for webtrends
        {
            this.currentItemSelectedIndex = this.currentItemSelectedIndex + 1;
            this.CheckForIndexEdgeCases();
            //hero was auto rotated, we need to track an impression for web trends
            if (!this.HasHeroAutoImpressionBeenRecorded[this.currentItemSelectedIndex]) {
                var impressionCommand = this.get_WebTrendsImpressionsCommandsList()[this.currentItemSelectedIndex];
                // HACK: Fix broken auto-impressions temporarily
                //eval(impressionCommand.replace('clickimpression', 'autoimpression'));
                this.HasHeroAutoImpressionBeenRecorded[this.currentItemSelectedIndex] = true;

                eval(this.get_OmnitureImpressionsCommandsList()[this.currentItemSelectedIndex]);

            }
        }
        else {
            if (this.currentItemSelectedIndex == parseInt(newHeroIndex) - 1) return;
            this.currentItemSelectedIndex = parseInt(newHeroIndex) - 1;
            this.CheckForIndexEdgeCases();
        }

        this.FadeOut();
    },

    FadeOut: function() {
        if (this.fadeInterval != null) {
            clearInterval(this.fadeInterval);
        }
        this.currentOpacityValue = 10;
        this.fadeInterval = setInterval(this.fadeOutStepTick, this.fadeIntervalTime);
    },

    FadeIn: function() {
        if (this.fadeInterval != null) {
            clearInterval(this.fadeInterval);
        }
        this.currentOpacityValue = 4;
        this.fadeInterval = setInterval(this.fadeInStepTick, this.fadeIntervalTime);
    },

    _transitionStep: function() {
        clearInterval(this.fadeInterval);
        this.imageDiv.innerHTML = this.get_HeroContentList()[this.currentItemSelectedIndex];

        this.FadeIn();
    },

    _fadeOutStep: function() {
        this.SetOpacity(this.currentOpacityValue);
        this.currentOpacityValue = parseInt(this.currentOpacityValue) - 1;

        if (this.currentOpacityValue <= 3) {
            clearInterval(this.fadeInterval);
            var heroID = this.get_HeroIDList()[this.currentItemSelectedIndex];
            this.ChangeHeroContent();
        }
    },
    _fadeInStep: function() {
        this.SetOpacity(this.currentOpacityValue);
        this.currentOpacityValue = parseInt(this.currentOpacityValue) + 1;
        if (this.currentOpacityValue > 10) {
            clearInterval(this.fadeInterval);
        }
    },
    SetOpacity: function(opacityValue) {
        this.fadeElement = document.getElementById(this.get_FadeElementID());
        this.fadeElement.style.opacity = opacityValue / 10;
        this.fadeElement.style.filter = 'alpha(opacity=' + opacityValue * 10 + ')';
    },



    //Properties:
    get_ContentFilePath: function() {
        return this._contentFilePath;
    },
    set_ContentFilePath: function(value) {
        if (this._contentFilePath != value) {
            this._contentFilePath = value;
            this.raisePropertyChanged('ContentFilePath');
        }
    },

    get_HeroIDList: function() {
        return this._heroIDList;
    },
    set_HeroIDList: function(value) {
        if (this._heroIDList != value) {
            this._heroIDList = value ? Array.parse(value) : [];
            this.raisePropertyChanged("HeroIDList");
        }
    },

    get_HeroContentList: function() {
        return this._heroContentList;
    },
    set_HeroContentList: function(value) {
        if (this._heroContentList != value) {
            this._heroContentList = value ? Array.parse(value) : [];
            this.raisePropertyChanged("HeroContentList");
        }
    },

    get_FadeElementID: function() {
        return this._fadeElementID;
    },
    set_FadeElementID: function(value) {
        if (this._fadeElementID != value) {
            this._fadeElementID = value;
            this._fadeElementID = this._fadeElementID.replace(/\$/g, "_");
            this.raisePropertyChanged('FadeElementID');
        }
    },

    get_WebTrendsClickThroughCommandsList: function() {
        return this._webTrendsClickThroughCommandsList;
    },
    set_WebTrendsClickThroughCommandsList: function(value) {
        if (this._webTrendsClickThroughCommandsList != value) {
            this._webTrendsClickThroughCommandsList = value ? Array.parse(value) : [];
            this.raisePropertyChanged("WebTrendsClickThroughCommandsList");
        }
    },

    get_WebTrendsImpressionsCommandsList: function() {
        return this._webTrendsImpressionsCommandsList;
    },
    set_WebTrendsImpressionsCommandsList: function(value) {
        if (this._webTrendsImpressionsCommandsList != value) {
            this._webTrendsImpressionsCommandsList = value ? Array.parse(value) : [];
            this.raisePropertyChanged("WebTrendsImpressionsCommandsList");
        }
    },

    get_OmnitureClickThroughCommandsList: function() {
        return this._omnitureClickThroughCommandsList;
    },
    set_OmnitureClickThroughCommandsList: function(value) {
        if (this._omnitureClickThroughCommandsList != value) {
            this._omnitureClickThroughCommandsList = value ? Array.parse(value) : [];
            this.raisePropertyChanged("OmnitureClickThroughCommandsList");
        }
    },

    get_OmnitureImpressionsCommandsList: function() {
        return this._omnitureImpressionsCommandsList;
    },
    set_OmnitureImpressionsCommandsList: function(value) {
        if (this._omnitureImpressionsCommandsList != value) {
            this._omnitureImpressionsCommandsList = value ? Array.parse(value) : [];
            this.raisePropertyChanged("OmnitureImpressionsCommandsList");
        }
    }
}

WindowsSite.Ajax.Controls.HeroPanelBehavior.registerClass('WindowsSite.Ajax.Controls.HeroPanelBehavior', WindowsSite.Ajax.Controls.ContentPanelBehavior );