﻿var aCarouselStories = new Array();
var iCurrentStory = 0;
var isPaused = false;
var iCarouselTimeout = 5;   // num of secs till next story

function carouselStory(iID, sTitle, sLinkHREF, sImagePrefix, sByLine, bHasSWF, sText) {
    this.iID =          iID;
    this.sTitle =       sTitle;
    this.sText =        sText;
    this.sLinkHREF =    sLinkHREF;
    this.sImagePrefix = sImagePrefix;
    this.sByLine =      sByLine;
    this.bHasSWF =      bHasSWF;
    return this;
}      

function showCarouselStory(storyNum,stopCarousel) {
    storyNum = (storyNum < 0) ? aCarouselStories.length-1 : storyNum;
    storyNum = (storyNum >= aCarouselStories.length) ? 0 : storyNum;

    if (stopCarousel) {
        playPauseCarousel()
    }

    var thisStory = aCarouselStories[storyNum];
    
    // change the linkbuttons
    for (i=0; i < aCarouselStories.length; i++) {
        var oLinkButton = document.getElementById("homepagenewscarousel_linkbutton"+i);
        oLinkButton.className = "homepagenewscarousel_selector"
        if (i == storyNum) {
            oLinkButton.className += " selected"; 
        }
    }
    
    var oTitle = document.getElementById("homepagenewscarousel_info_title");
    var oDesc = document.getElementById("homepagenewscarousel_info_desc");
    var oLink = document.getElementById("homepagenewscarousel_info_link");
    var oImage = document.getElementById("homepagenewscarousel_image");
    var oByLine = document.getElementById("homepagenewscarousel_imageoverlay_text");   
    var oByLineBG = document.getElementById("homepagenewscarousel_imageoverlay_bg"); 
     
        

    oTitle.innerHTML = thisStory.sTitle;
    oDesc.innerHTML = thisStory.sText;
    oLink.target = "_self";
    
    if (thisStory.sLinkHREF) {        
        oLink.href = thisStory.sLinkHREF;
        if (oLink.href.indexOf("http://") >= 0) { oLink.target = "_blank"; }
    } else {    // it hasn't got a link, so link it to the full detailed story.
        oLink.href = "newsmedia_detailed.aspx?ID=" + thisStory.iID
    }
    
    sImage = ""   
    if (thisStory.sLinkHREF) {
        sImage += "<a href='" + oLink.href + "' target='" + oLink.target + "'>";
    }
    sImage += '<img src="images/news/homepagenewscarousel/' + thisStory.sImagePrefix + '.jpg" width="387" height="266" border="0" />';    
    if (thisStory.sLinkHREF) {
        sImage += "</a>";
    }       
    oImage.innerHTML = sImage
    oByLine.innerHTML = thisStory.sByLine;
    oByLineBG.style.display = (thisStory.sByLine == "") ? "none" : "block";
    
    if (thisStory.bHasSWF) {
        var so = new SWFObject("images/news/flash/" + thisStory.sImagePrefix + ".swf", "swf_homepagenewscarousel_image", "387", "266", "8");
        so.addParam("scale", "noscale");
        so.addParam("menu", "false");  
        so.addParam("wmode", "transparent");
        so.write("homepagenewscarousel_image");            
    }
    
    iCurrentStory = storyNum;
}

function playPauseCarousel(bToggleState) {
    if (bToggleState) {
        isPaused = !isPaused;
    } else {
        isPaused = true;
    }
    
    var oImg = document.getElementById("homepagenewscarousel_playpause");  
    if (isPaused) {   
        oImg.src = "images/homepagenewscarousel_play.gif"
    } else {    
        oImg.src = "images/homepagenewscarousel_pause.gif"
    }
}
function showNextStory() {
    if (!isPaused) {
        showCarouselStory(iCurrentStory + 1);        
    }
}


function initCarousel() {
    // write the LinkButtons to the user interface.
    document.write('<a href="#" onClick="playPauseCarousel(true); return false;" class="homepagenewscarousel_selector" style="float: right"><img src="images/homepagenewscarousel_pause.gif" id="homepagenewscarousel_playpause"  /></a>');
    document.write('<a href="#" onClick="showCarouselStory(iCurrentStory+1,true); return false;" class="homepagenewscarousel_selector" style="float: right">&gt;</a>');
    document.write('<a href="#" onClick="showCarouselStory(iCurrentStory-1,true); return false;" class="homepagenewscarousel_selector first">&lt;</a>');
    for (i=0; i < aCarouselStories.length; i++) {
        var cssClass = "homepagenewscarousel_selector"
        if (i == iCurrentStory) {
            cssClass += " selected";
        }
        document.write('<a href="#" onClick="showCarouselStory(' + i +',true); return false;" class="' + cssClass + '" id="homepagenewscarousel_linkbutton'+i+'">' + (i+1) +'</a>');            
    }            
          
    if (isPaused) { // if it's set to be paused from the start, then set the button state.
       playPauseCarousel();
    }
    setInterval(showNextStory,iCarouselTimeout * 1000);     
    showCarouselStory(iCurrentStory);
}



        