var carousel_items = 14; // Max num of blocks to display
var newNum;
var showsHtml = "";
var scrolling;
if(showsArray.length>carousel_items) scrolling=true; 
else scrolling = false;

function initCarousel() 
{
    var showCarouselImages='';
    for (var i=0; i<showsArray.length; i++) {
        showCarouselImages += '.c'+i+' { background-image:url(/global/img/carousel/'+showsArray[i][0].split(' ').join('')+'.gif); }\r\n';
    }
    var tempDiv = document.createElement('div');
    tempDiv.innerHTML = '<p>x</p><style type="text/css">'+showCarouselImages+'</style>';
    document.body.appendChild(tempDiv.childNodes[1]);

    if(scrolling) showsHtml = '<a id="left" title="More shows"></a>';
    showsHtml += '<ul id="carousel">';    
    for (i=0; i<carousel_items; i++) {   
        showsHtml += '<li class="c'+i+'" id="c'+i+'"><a href="'+showsArray[i][1]+'" title="'+showsArray[i][0]+'">'+showsArray[i][0]+'</a></li>'; 
    }
    showsHtml += '</ul>'
    if(scrolling) showsHtml += '<a id="right" title="More shows"></a>';
    
    var w = ((44+15)*carousel_items);
    if(scrolling) w += 44+16+44;
    $("#carousel-wrap").html(showsHtml).width(w);
    $("#carousel").kwicks({max:156, spacing:15, duration:500, easing:'easeOutQuad'});
    
    if(scrolling) {
        $("#right").click(function(){ moveCarousel() });
        $("#left").click(function(){ moveCarousel(true) });
    }
};

function moveCarousel(left)
{
    for(i=0; i<carousel_items; i++) {
        var currClass = $("#c"+i).attr("class").replace("active","");
        newNum = Number(currClass.substr(1,currClass.length));
        if(left) {
            if(newNum==0) newNum=showsArray.length;
            newNum--;
        } else {
            if(newNum==(showsArray.length-1)) newNum=-1;
            newNum++;
        }        
        $("#c"+i).removeClass(currClass).addClass("c"+newNum).html('<a href="'+showsArray[newNum][1]+'" title="'+showsArray[newNum][0]+'">'+showsArray[newNum][0]+'</a>');
    }
}


