/**
 *  Flip functions for Download Webcast
 *
 *  @param  tts             Tabs corresponding to 1 day
 *  @param  dates           Tags displaying the date
 *  @param  artistes        List of artistes for the current day
 *  @param  bandname        Now Playing Band Name
 *
 *  @param  current_date    Current date
 *  @param  current_day    Current day
 *  @param  current_hours    Current hours
 *  @param  current_minutes    Current minutes
 *  @param  current_seconds Current seconds
 */

var tts = new Array();
var dates = new Array();
var artistes = new Array();
var bandname = '';

var current_date = null;
var current_day = null;
var current_hours = null;
var current_minutes = null;
var current_seconds = null;


/**
 *  Main initialisation function
 */
function init() { 
    
    BrowserDetect.init();
    
    if(document.getElementById && document.createTextNode) { 
        var entries = document.getElementsByTagName("div"); 
        for(i=0;i<entries.length;i++) {
            if (entries[i].className=="timeTable") {
                assignTimeTableBehaviour(entries[i]);
            } else if (entries[i].className=="date") {
                assignDateBehaviour(entries[i]);
            } else if (entries[i].className=="nowPlayingBandname") {
                bandname = entries[i];
            }
        }
    }
    getTime();
    checkActiveArtiste();
    
    var objs = document.getElementsByTagName("object");
    for(i=0;i<objs.length;i++) {
        if(objs[i].className == "videoPlayer") {
            Video.init(objs[i]);
        }
    }
    
} 

function assignTimeTableBehaviour(div) { 
    tts.push(div);
}

function assignDateBehaviour(div) {
    dates.push(div);
}


/**
 *  Functions that display the time
 */
function getTime() {
    var datesNb = dates.length;
    current_date = new Date();
    current_day = current_date.getDate();
    current_hours = current_date.getHours();
    current_minutes = current_date.getMinutes();
    current_seconds = current_date.getSeconds();
    
    current_hours = checkTime(current_hours);
    current_minutes = checkTime(current_minutes);
    current_seconds = checkTime(current_seconds);
    
    window.setTimeout("getTime()", 1000);
}

function checkTime(i)
{
    if (i<10) {i="0" + i;}
    return i;
}


/**
 *  Function that activates the currently playing artiste
 *  Has to be be switched off after the festival
 */
function checkActiveArtiste() {
    var day_id = "";
    
    // Clear Boxes and Frame
    bandname.innerHTML = '...';
    window.frames[0].location.href="blank.html";
    
    /* [DEBUG] Manually set day for tests
    current_day = 8;
    current_hours = 16;
    current_minutes = 42; */
    
    switch(current_day) {
        case 8: day_id = "DAY_2"; break;
        case 9: day_id = "DAY_3"; break;
        case 10: day_id = "DAY_4"; break;
    }
    
    /**
     *  Activate current artist
     */
    if(day_id != "") {
    
        // Toggle to the current day
        toggleTab(day_id);
        
        // Get tabs IDs
        for(tabs=0;tabs<tts.length;tabs++) {
            if(tts[tabs].id == day_id) {
                // For the current tab ID, get all artists
                var artists = tts[tabs].getElementsByTagName("div");
                for(i=0;i<artists.length;i++) {
                    if (artists[i].className == "streamtime" || artists[i].className == "streamtime_active") {
                        
                        // Check if the artist is currently playing
                        var times = artists[i].getElementsByTagName("span");
                        var on_hours = parseInt(times[0].innerHTML.substring(0, 2))*100;
                        var on_minutes = parseInt(times[0].innerHTML.substring(3, 5));
                        var on_time = on_hours + on_minutes;
                        var off_hours = parseInt(times[1].innerHTML.substring(0, 2))*100;
                        var off_minutes = parseInt(times[1].innerHTML.substring(3, 5));
                        var off_time = off_hours + off_minutes;
                        var current_time = (parseInt(current_hours)*100) + parseInt(current_minutes);
                        
                        if(current_time >= on_time && current_time <= off_time) {
                            
                            // Activate artist
                            artists[i].className = "streamtime_active";
                            artists[i-1].className = "streamband_active";
                            
                            // If it is a webcast artist, swap image
                            var key = artists[i-1].getElementsByTagName("span")[2];
                            if(key != null) {
                                key.className = "webcast_stage_key_current";
                            }
                            
                            // Get artist GUID
                            var artiste_guid = artists[i-1].getElementsByTagName("span")[0].innerHTML;
                            
                            // Get artist name
                            var artiste_name = artists[i-1].getElementsByTagName("span")[1].innerHTML;
                            
                            // Update the "Now Playing" box
                            bandname.innerHTML = artiste_name;
                            
                            // Update the iFrame
                            window.frames[0].location.href="artistDetails.aspx?aid=" + artiste_guid;
                            
                        } else if (artists[i].className == "streamtime_active") {
                        
                            // Deactivate artist
                            artists[i].className = "streamtime";
                            artists[i-1].className = "streamband";
                            
                            // If it is a webcast artist, swap image
                            var key = artists[i-1].getElementsByTagName("span")[2];
                            if(key != null) {
                                key.className = "webcast_stage_key";
                            }
                        }
                    }
                }
            }
        }
        
    } else {
        toggleTab("DAY_2");
    }
    
    document.title = "Download Festival 2007 - Live Webcast";
    //window.setTimeout("checkActiveArtiste()", 60000);
    
}


/**
 *  Function that toggles between the DAY tabs
 */
function toggleTab(ID)
{
	var tot = tts.length;
	for(i=0;i<tot;i++) {
	    if(tts[i].id == ID) {
            tts[i].style.display = 'block';
	    } else {
	        tts[i].style.display = 'none';
	    }
	}
}
