﻿
function setScrollerWidth() {
    var numItems = $("div.home_scroll_strip div").size();
    $("div.home_scroll_strip").css("width", numItems * 318);
}

function initBillboard() {
    $("#home_billboard").hover(function () { bb.showControls(true) }, function(){ bb.showControls(false) });
    $('#billboard_ctrl_left').click(function () { bb.go('left'); bb.pause(); });
    $('#billboard_ctrl_right').click(function () { bb.go('right'); bb.pause(); });
    $('#billboard_playpause').click(function () { bb.play() });
    bb.play();
}

function Billboard( numImgs, speed ) {
    this.currImg = 1;
    this.currPos = -960;
    this.numImgs = numImgs;
    this.playSpeed = speed;
    this.playInterval = null;
    this.isPlaying = false;
}

Billboard.prototype.showControls = function (show) {
    if (show) {
        $("#billboard_ctrl_left").fadeIn();
        $("#billboard_ctrl_right").fadeIn();
        $("#billboard_playpause").fadeIn();
    }
    else {
        $("#billboard_ctrl_left").hide();
        $("#billboard_ctrl_right").hide();
        $("#billboard_playpause").hide();
    }
}

Billboard.prototype.go = function (dir) {
    var width = 960;
    var newPos = (dir == "right") ? this.currPos - width : this.currPos + width;
    var newImg = (dir == "right") ? this.currImg + 1 : this.currImg - 1;
    if (newImg == this.numImgs + 2) {
        $("#billboard").css("left", "-960px");
        newPos = -960 - width;
        newImg = 2;
    }
    else if (newImg == 0) {
        $("#billboard").css("left", -((this.numImgs+1)*width));
        newPos = -(this.numImgs * width);
        newImg = this.numImgs;
    }
    $("#billboard").animate( { left: newPos } );
    this.currPos = newPos;
    this.currImg = newImg;
}

Billboard.prototype.play = function () {
    if (this.isPlaying) {
        this.pause();
    }
    else {
        this.playInterval = setInterval("bb.go('right')", this.playSpeed);
        this.isPlaying = true;
    }
}

Billboard.prototype.pause = function () {
    clearInterval(this.playInterval);
    this.isPlaying = false;
}
