$(document).ready(function() {

    loadVenueCombo('TP52');
    loadVenueCombo('GP42');
    
    $("#image_TP52").click(function() {
        clickTP52();
    });
    
    $("#image_GP42").click(function() {
        clickGP42();
    });
    
    refreshLiveStatus();
    startLiveStatusPoll();
    
});


function clickTP52() {
    if (shockwaveIsPresent) {
        if (confirm('You are about to leave this race. Are you sure?')) {
            selectTP52();
        }
    } else {
        selectTP52();
    }
}

function clickGP42() {
    if (shockwaveIsPresent) {
        if (confirm('You are about to leave this race. Are you sure?')) {
            selectGP42();
        }
    } else {
        selectGP42();
    }
}

function selectTP52() {
    $("#ls_tab_TP52").show();
    $("#ls_tab_GP42").hide();
    tabs_isActive['TP52'] = true;
    tabs_isActive['GP42'] = false;
    updateTabImages();
    clearShockwave();
    if (tabs_liveStatus['TP52'] == '') {
        showNoRaceMessage();
    } else {
        loadShockwave(races_liveCode['TP52']);
    }
}

function selectGP42() {
    $("#ls_tab_TP52").hide();
    $("#ls_tab_GP42").show();
    tabs_isActive['TP52'] = false;
    tabs_isActive['GP42'] = true;
    updateTabImages();
    clearShockwave();
    if (tabs_liveStatus['GP42'] == '') {
        showNoRaceMessage();
    } else {
        loadShockwave(races_liveCode['GP42']);
    }
}

function clearShockwave() {
    $("#ls_shockwave").css('height', '300px').html('');
    shockwaveIsPresent = false;
}

function loadVenueCombo(p_category) {

    $("#ls_comboVenue_" + p_category).html(loadingImage[p_category]);
    
    $("#ls_comboVenue_" + p_category).load('../includes/ajax_livesailing_doVenueCombo.php', {}, function() {
    
        $("#ls_comboVenue_" + p_category + " select").change(function() {
            var venue = $(this).val();
            if (venue != '') {
                loadRacesCombo(venue, p_category);
            } else {
                $("#ls_comboRace_" + p_category).html('');
            }
        })
        
    });
}

var firstShockwaveRun = true;
var liveStatusInterval = false;

function startLiveStatusPoll() {
    liveStatusInterval = window.setInterval(refreshLiveStatus, 1000 * 10);
}

function stopLiveStatusPoll() {
    if (liveStatusInterval) {
        window.clearInterval(liveStatusInterval);
    }
}

var loadingImage = new Array();
loadingImage['TP52'] = '<img src="../images/ls_ajax_loader_TP52.gif" alt="" style="margin: 0 5px 0 5px";>';
loadingImage['GP42'] = '<img src="../images/ls_ajax_loader_GP42.gif" alt="" style="margin: 0 5px 0 5px";>';
loadingImage['white'] = '<img src="../images/loading.gif" alt="" style="margin: 0 5px 0 5px";>';

function decodeRace(p_code) {
    // For debuggin only
    data = new Object();
    data.code = p_code;
    $("#debug").load('../includes/ajax_livesailing_decodeRace.php', data);
}

function loadRacesCombo(p_venue, p_category) {
    data = new Object();
    data.venue = p_venue;
    data.category = p_category;
    
    $("#ls_comboRace_" + p_category).html(loadingImage[p_category]);
    
    $("#ls_comboRace_" + p_category).load('../includes/ajax_livesailing_doRacesCombo.php', data, function() {
        $("#ls_comboRace_" + p_category + " select").change(function() {
            var code = $(this).val();
            if (code != '') {
                loadShockwave(code);
            }
        });
    });
}

function loadShockwave(p_code) {
    shockwaveIsPresent = true;

    //$("#ls_shockwave").html('<img src="http://www.medcup.org/2008/sources/images/livesailing.jpg" alt="" />');
    //return;
    
    data = new Object();
    data.code = p_code;
    $("#ls_shockwave").css('height', '550px').html(loadingImage['white']);
    $("#ls_shockwave").load('../includes/ajax_livesailing_shockWave.php', data);
}

function updateTabImages() {
    updateTab('TP52');
    updateTab('GP42');
}

function updateTab(p_category) {
    var image = $("#image_" + p_category);
    var path = '../images/tabs/';
    
    if (tabs_liveStatus[p_category] == 'live') {
        var live = '_live';
    } else {
        var live = '';
    }
    
    if (tabs_isActive[p_category]) {
        var active = 'active';
    } else {
        var active = 'inactive';
    }
    
    var imagePath = path + 'r_' + p_category + '_' + active + live + '.gif';
    
    image.attr('src', imagePath);
}

function showNoRaceMessage() {
    clearShockwave();
    $("#ls_shockwave").html(loadingImage['white']);
    $("#ls_shockwave").load('../includes/ajax_livesailing_noRaces.php');
}

function refreshLiveStatus() {
    $.getJSON('../includes/ajax_livesailing_liveStatus.php', {}, function(json) {
        if (json['TP52']['live']) {
            tabs_liveStatus['TP52'] = 'live';
        } else {
            tabs_liveStatus['TP52'] = '';
        }
        if (json['GP42']['live']) {
            tabs_liveStatus['GP42'] = 'live';
        } else {
            tabs_liveStatus['GP42'] = '';
        }
        
        updateTabImages();
        
        if (firstShockwaveRun) {
            // Decide if we need to load the shockwave player or not
            if (json['TP52']['live'] && json['GP42']['live']) {
                // Both races are live, so show TP52
                selectTP52();
                loadShockwave(json['TP52']['code']);
                
            } else if (json['TP52']['live']) {
                // Only TP52 is live
                selectTP52();
                loadShockwave(json['TP52']['code']);
                
            } else if (json['GP42']['live']) {
                // Only GP42 is live
                selectGP42();
                loadShockwave(json['GP42']['code']);
            } else {
                // No live races
                selectTP52();
                showNoRaceMessage();
            }
            
            firstShockwaveRun = false;
            
        } else {
        
            // Check for new TP52 races            
            if (tabs_previousLiveStatus['TP52'] == '' && tabs_liveStatus['TP52'] == 'live') {
                newTP52Race = true;
            } else {
                newTP52Race = false;
            }
            
            // Check for new GP42 races            
            if (tabs_previousLiveStatus['GP42'] == '' && tabs_liveStatus['GP42'] == 'live') {
                newGP42Race = true;
            } else {
                newGP42Race = false;
            }
            
            if (newTP52Race && newTP52Race) {
                // There are two new races so prompt user for TP52 only
                stopLiveStatusPoll();
                if (confirm('There is a LIVE TP52 race going on right now. Would you like to watch it?')) {
                    selectTP52();
                    loadShockwave(json['TP52']['code']);
                }
                startLiveStatusPoll();
            } else if (newTP52Race && !newGP42Race) {
                // There is a new TP52 race only
                stopLiveStatusPoll();
                if (confirm('There is a LIVE TP52 race going on right now. Would you like to watch it?')) {
                    selectTP52();
                    loadShockwave(json['TP52']['code']);
                }
                startLiveStatusPoll();
            } else if (!newTP52Race && newGP42Race) {
                // There is a new GP42 race only
                stopLiveStatusPoll();
                if (confirm('There is a LIVE GP42 race going on right now. Would you like to watch it?')) {
                    selectGP42();
                    loadShockwave(json['GP42']['code']);
                }
                startLiveStatusPoll();
            }
        }
        
        // Store codes for later user outside this function
        races_liveCode['TP52'] = json['TP52']['code'];
        races_liveCode['GP42'] = json['GP42']['code'];
        
        tabs_previousLiveStatus['TP52'] = tabs_liveStatus['TP52'];
        tabs_previousLiveStatus['GP42'] = tabs_liveStatus['GP42'];
        
        // Update status bars
        $("#ls_TP52_status").html(json['TP52']['message']);
        $("#ls_GP42_status").html(json['GP42']['message']);
    });
}

var shockwaveIsPresent = false;

var tabs_liveStatus = new Array();
tabs_liveStatus['TP52'] = 'live';
tabs_liveStatus['GP42'] = 'live';

var tabs_isActive = new Array();
tabs_isActive['TP52'] = true;
tabs_isActive['GP42'] = false;

var tabs_previousLiveStatus = new Array();
tabs_previousLiveStatus['TP52'] = '';
tabs_previousLiveStatus['GP42'] = '';

var races_liveCode = new Array();
races_liveCode['TP52'] = '';
races_liveCode['GP42'] = '';

var newTP52Race = false;
var newGP42Race = false;
