var whitelabel = {};

var spinner = {}
spinner.image = new Image();
spinner.image.src = "/images/whitelabel/blackspinner.gif";
spinner.isLoading = false;
spinner.isShowing = false;
var globalConfig = {};
var liveScoreInterval;
var ieHacks ={};

spinner.show = function() {
    spinner.isLoading = true;
    // show spinner if loading hasn't completed after 750ms
    setTimeout('if (spinner.isLoading){$("#displayed_match").html(spinner.image); spinner.isShowing = true;}',750);
}

spinner.hide = function() {
    spinner.isLoading = false;
    if (spinner.isShowing)
        $("#displayed_match").hide();
        $("#displayed_match").fadeIn(500);
        
    spinner.isShowing = false;
}

whitelabel.init = function(config) {
    // load initial match
    $match_id = $("#displayed_match").attr("data-initial-load");
    whitelabel.loadMatch($match_id);
    globalConfig = config;
    // process config options later
}

whitelabel.click_form = function() {
    $("#form").show("blind",{},500);
    $("#livescore").hide("blind",{},500); 
    
    $("#tabs-form-selected").show();
    $("#tabs-live-selected").hide();
}

whitelabel.click_live = function() {
    $("#livescore").show("blind",{},500);
    $("#form").hide("blind",{},500);
    
    $("#tabs-form-selected").hide();
    $("#tabs-live-selected").show();
}

whitelabel.loadMatch = function(match_id) {
    spinner.show();
    $.ajax({    url: "match?match_id=" + match_id, 
                success: function(content){
                      spinner.hide();
                      $("#displayed_match").html(content)
                      
                      //start retrieving scores for this match
                      DartsLiveScore.initScore("live_score_json?match_id=" + match_id);
                      
                      try {
                        curvyCorners.scanStyles();
                          }
                        catch(err){
                            //hide!!!
                        }
                        
                        if (globalConfig.track_events)
                            pageTracker._trackEvent('Matches', 'Click', 'match_id', match_id);
                      }
            })
}

whitelabel.loadAllMatches = function(competition_id) {
    spinner.show();
    $.ajax({    url: "all_matches?competition_id=" + competition_id, 
                success: function(content){
                      spinner.hide();
                      $("#displayed_match").html(content)
                      $("#displayed_match").hide();
                      $("#displayed_match").fadeIn(500);
                      
                      try {
                      curvyCorners.scanStyles();
                        }
                      catch(err){
                          //hide!!!
                      }
                      
                      if (globalConfig.track_events)
                          pageTracker._trackEvent('All Matches', 'Click', 'competition_id', competition_id);
                      }
            })
}

// add click event to each match
$(".match-nav tr").click(function(){
    var match_id = $(this).attr("data-match-id");
    whitelabel.loadMatch(match_id);
    })
          
// add click event to all matches linke                     
$(".other-fixtures-link").click(function(){
 var competition_id = $(this).attr("data-competition-id");
 whitelabel.loadAllMatches(competition_id);
 return false;
})

//bind tab click events
$("#clickableformtab").live('click',function() {
        whitelabel.click_form();
});

$("#clickablelivetab").live('click',function() {
        whitelabel.click_live();
});     

$("#more-head-to-head-link").live('click',function(){
    $("#more-head-to-head-link").hide()
    $("#more-head-to-head").fadeIn(500);
    return false;
});

$(".bet-button").live('click', function(){
    pageTracker._trackEvent('Bets', 'Click', 'Bet button');
    top.location.href = globalConfig.bet_button_href
    return false;
})

