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

wwd.news =
{
    panels:[],
    current:0,
    total:0,
    panelWidth:0,
    loaded:false,
    autoTimeout:26000,
    autoTimer:null,
    autoTimerActive:false,

    initialize:function()
    {
        var thisptr = this;
        this.setTicker();

        // 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).css("left", -1*$("#panel-container").width());

        if (this.panels.length > 1) {
            /* 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(); } );
        } else {
            $('#panel-pagination').hide();
        }

        this.showPanel();

        //set up autoscroll
        this.autoTimer = setTimeout('wwd.news.autoScroll()', this.autoTimeout);
        this.autoTimerActive = true;
        $("#news").mouseenter(function(){
            clearTimeout(thisptr.autoTimer);
            thisptr.autoTimerActive = false;
        }).mouseleave(function(){
            thisptr.autoTimer = setTimeout('wwd.news.autoScroll()', thisptr.autoTimeout);
            thisptr.autoTimerActive = true;
        });

    },
    setTicker:function()
    {
        var m_names = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
        var d = new Date();
        var curr_date = d.getDate();
        var curr_month = d.getMonth();
        var curr_year = d.getFullYear();
        $('#date').html(m_names[curr_month] + ' ' + curr_date + ', ' + curr_year);
    },
    showPanel:function()
    {
        $("#panel-container").animate( {
            left: 0-(this.panelWidth*this.current) + 'px'
          }, {
            duration: 1200,
            specialEasing: { left: 'easeInOutCubic' },
            complete: 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,'easeInOutCubic' );
        }
    },

    previousPanel:function()
    {
        wwd.logger.log("News.previousPanel()");
        this.current--;
        if ( this.current <= 0 ) {
            this.current = 0;
        }
        this.movePanel(true);
    },

    nextPanel:function()
    {
        wwd.logger.log("News.nextPanel()");
        this.current++;
        if ( this.current >= this.panels.length-1 ) {
            this.current = this.panels.length-1;
        }
        this.movePanel(true);
    },

    autoScroll:function()
    {
        this.current++;
        this.movePanel(false);
        if (this.autoTimerActive) {
            this.autoTimer = setTimeout('wwd.news.autoScroll()', this.autoTimeout);
        } else {
            clearTimeout(this.autoTimer);
        }
    },

    movePanel:function(trackStats)
    {
        $("#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 == this.panels.length-1 ) {
            $("#panel-next").css("opacity",0);
            $("#panel-next").hover( function() { $(this).css("opacity",0); }, function() { $(this).css("opacity",0); } );
        }
        if ( this.current == 0 ) {
            $("#panel-back").css("opacity",0);
            $("#panel-back").hover( function() { $(this).css("opacity",0); }, function() { $(this).css("opacity",0); } );
        }
        if ( this.current > this.panels.length-1 ) {
            this.current = 0;
            $("#panel-back").css("opacity",0);
            $("#panel-back").hover( function() { $(this).css("opacity",0); }, function() { $(this).css("opacity",0); } );
        }
        if ( this.current < 0 ) {
            this.current = this.panels.length-1;
            $("#panel-next").css("opacity",0);
            $("#panel-next").hover( function() { $(this).css("opacity",0); }, function() { $(this).css("opacity",0); } );
        }
        if (trackStats) {
            wwd.stats.trackAction("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}, 400, 'easeInOutCubic');
}

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

var didNews = false;

$(document).ready( function() {
    startNews();
});

function startNews() {
    if (!didNews) {
        didNews = true;
        wwd.news.initialize();
        wwd.jsHover('featured-images-wrapper','div','over');
        var ticker = new ScrollPanels("ticker-container", 3000);
    }
}

//just in case the page hangs...
setTimeout('startNews()', 2500);
setTimeout('startNews()', 5000);
