if ( typeof(wwd)=="undefined" ) { wwd = {}; }

wwd.news =
{
    panels:[],
    current:0,
    total:0,
    panelWidth:0,
    loaded:false,

    initialize:function()
    {
        // check that the markup exists
        if ( $("#news").length <= 0 ) {
            return;
        }

        this.panels = $("#panel-container .panel-wrapper");
        this.total = this.panels.length;

        this.panelWidth = $(this.panels[0]).width();
        $("#panel-container").width(this.panelWidth * this.panels.length);
        $("#panel-container").css("left", -1*$("#panel-container").width());
        /**
         * set up the panel back button
         */
        $("#panel-back").css("opacity",0);
        $("#panel-back").hover( function() { $(this).css("opacity",0); },
                                    function() { $(this).css("opacity",0); } );
        $("#panel-back").click( function() {wwd.news.previousPanel(); } );

        /**
         * set up the panel next button
         */
        $("#panel-next").css("opacity",.8);
        $("#panel-next").hover( function() { $(this).css("opacity",1); },
                                    function() { $(this).css("opacity",.8); } );
        $("#panel-next").click( function() {wwd.news.nextPanel(); } );

        this.showPanel();
    },

    showPanel:function()
    {
        $("#panel-container").animate(
          { left:0-(this.panelWidth*this.current) + "px" }, 1200, 'easeInOutQuad', function() {
              if ( !wwd.news.loaded ) {
                  wwd.news.loaded = true;
                  wwd.events.dispatchEvent("newsLoadComplete");
              }
          }
        );
        this.updateThumb();
    },

    updateThumb:function()
    {
        if ( this.panels.length <= 1 ) {
            $("#panel-slider").hide();
        }
        else {
            $("#panel-slider").show();
            var width = Math.round( this.panelWidth / this.total );
            var pos = this.current * width;
            $("#panel-slider-thumb").animate( {
                "left": pos,
                "width": width
            },400,'easeInOutQuad' );
        }
    },

    previousPanel:function()
    {
        wwd.logger.log("News.previousPanel()");
        this.current--;
        $("#panel-back").css("opacity",.45);
        $("#panel-back").hover( function() { $(this).css("opacity",1); },
                                    function() { $(this).css("opacity",.45); } );
        $("#panel-next").css("opacity",.45);
        $("#panel-next").hover( function() { $(this).css("opacity",1); },
                                    function() { $(this).css("opacity",.45); } );
        if ( this.current <= 0 ) {
            this.current = 0;
        $("#panel-back").css("opacity",0);
        $("#panel-back").hover( function() { $(this).css("opacity",0); },
                                    function() { $(this).css("opacity",0); } );
        }
        vs.StatEvent("&today_module","panel " + (this.current + 1));
        this.showPanel();
    },

    nextPanel:function()
    {
        wwd.logger.log("News.nextPanel()");
        this.current++;
        $("#panel-back").css("opacity",.45);
        $("#panel-back").hover( function() { $(this).css("opacity",1); },
                                    function() { $(this).css("opacity",.45); } );
        $("#panel-next").css("opacity",.45);
        $("#panel-next").hover( function() { $(this).css("opacity",1); },
                                    function() { $(this).css("opacity",.45); } );
        if ( this.current >= 2 ) {
            this.current = 2;
        $("#panel-next").css("opacity",0);
        $("#panel-next").hover( function() { $(this).css("opacity",0); },
                                    function() { $(this).css("opacity",0); } );
        }
        vs.StatEvent("&today_module","panel " + (this.current + 1));
        this.showPanel();
    },

    getPanelIndex:function( panel )
    {
        for ( var i=0;i<this.panels.length;i++ ) {
            if ( this.panels[i].id == panel.id ) {
                return i;
            }
        }
        return -1;
    }
}

function ScrollPanels( panels, interval )
{
    this.windowWidth = $("#" + panels + " .panel-window").width();
    this.panelContainer = $("#" + panels + " .panel-window .panels");
    this.numPanels = this.panelContainer.children().length;

    var thisptr = this;
    if ( $("#" + panels + " .next").length > 0 && $("#" + panels + " .previous").length > 0 ) {
        $("#" + panels + " .next").click(function(){
            thisptr.move("next");
            return false;
        });
        $("#" + panels + " .previous").click(function(){
            thisptr.move("previous");
            return false;
        });

        this.interval = interval ? interval : 3000;
        this.timer = new Timer();
        this.start();
    }
}

ScrollPanels.prototype.start = function( direction )
{
    this.timer.setInterval(this, "scroll", this.interval);
}

ScrollPanels.prototype.scroll = function( direction )
{
    if( direction == "next" || direction == null){
        var lastPanel = $(this.panelContainer.children()[this.numPanels - 1]);
        lastPanel.remove();
        this.panelContainer.css( "left","-" + this.windowWidth + "px");
        this.panelContainer.prepend(lastPanel);
    } else {
        var lastPanel = $(this.panelContainer.children()[0]);
        lastPanel.remove();
        this.panelContainer.css( "left", this.windowWidth + "px");
        this.panelContainer.append(lastPanel);
    }
    this.panelContainer.animate(
        {
            "left": 0
                },
        200,
        'easeInOutQuad'
    );
}

ScrollPanels.prototype.move = function( direction ) {
    this.timer.kill();
    this.scroll( direction );
    this.timer.setTimeout(this, "start", 7000);
}

$(document).ready(
    function() {
        wwd.news.initialize();
        wwd.jsHover('featured-images-wrapper','div','over');
        var ticker = new ScrollPanels("ticker-container", 3000);
    }
);

