// inRunning Javascript API

jQuery.inRunning = {
  website : VC_SITE,
  language : VC_LANG,
  price_format : VC_USER_PRICE_FORMAT,
  currency_code : VC_USER_CURRENCY_SYMBOL,
  inrunning_results_url : INRUNNING_RESULTS_URL,
  inrunning_goto_event_url : INRUNNING_GOTO_EVENT_URL,
  inrunning_goto_live_event_url : INRUNNING_GOTO_LIVE_EVENT_URL,
  inrunning_external_url : INRUNNING_EXTERNAL_URL,
  inrunning_level3_url : INRUNNING_LEVEL3_URL,
  inrunning_diary_url : INRUNNING_DIARY_URL,
  inrunning_remote_url: INRUNNING_REMOTE_URL,

  // Use this to interact with the flash app
  get_movie_name: function(movieName) {
    if (navigator.appName.indexOf("Microsoft") != -1) {
      return window[movieName]
    }
    else {
      return document[movieName]
    }
  },
  
  // Resize the inrunning div.
  resize : function(fheight) {
    $J('#navigation').css('height', fheight);
		$J('object#bip').attr({height: fheight});
  },

  // To remove all bets pass an empty array etc
  // To add a bet pass the full array with the new bet included
  // etc.
  update_bets: function(arr) {
    var flash_app = this.get_movie_name('bip');
    if (flash_app)
    {
      flash_app.update_bets(arr);
    }
  },
  
  // Navigate to the coupon page of an event
  goto_event: function(event_id) {
    window.location = this.inrunning_goto_event_url + "/" + event_id;
  },
  
  // Navigate to the event in the live app
  goto_live_event: function(event_id) {
    var queryStringStartIndex = this.inrunning_goto_live_event_url.indexOf('?');
    var urlPart = this.inrunning_goto_live_event_url.substring(0, queryStringStartIndex)
    var queryStringPart = this.inrunning_goto_live_event_url.substring(queryStringStartIndex)
    window.location = urlPart + "/" + event_id + queryStringPart;
  },
  
  // Navigate to the inrunning diary
  goto_diary: function() {
    this.goto_live_event('DIARY');
  },
  
  // Navigate to the results page of a sport
  get_results: function(sport_id) {
    var maps = {"100" : "", "400" : "golf", "200" : "racing", "600" : "tennis", "366" : "greyhounds"}
    var sport = maps[sport_id] || "";
    MM_openBrWindow(this.inrunning_results_url + sport, "", "width=860,height=465,scrollbars=yes");
  },
  
  // Display an external feed for an event
  display_external_feed: function(event_id) {
    MM_openBrWindow(this.inrunning_external_url + "/" + event_id, "", "width=860,height=465,scrollbars=yes")
  },
  
  // Display a level3 feed for an event
  display_level3_feed: function(event_id) {
    MM_openBrWindow(this.inrunning_level3_url + "/" + event_id, "", "width=810,height=615,status=no,toolbar=no,location=no,menubar=no,resizable=no")
  },
    
  // Place a Bet
  // Example:
  // jQuery.inRunning.place_bet("2649960200", "3.000", "1353932302", "3", "1", "DR Congo", "DR Congo v Libya")
  place_a_bet : function(outcome_id, price_description, price_id, price_decimal, place_terms_deduction, outcome_description, event_description, each_way, starting_price, market_description) {
    add_to_slip(outcome_id, 
                price_description,
                price_id,
                price_decimal,
                this.price_format,
                this.currency_code,
                true,
                place_terms_deduction,
                outcome_description,
                event_description,
                each_way,
                starting_price,
				market_description);
  },

  // Remove a Bet.
  // Example:
  // jQuery.inRunning.remove_a_bet('2283248200', '1175852102');
  remove_a_bet : function(outcome_id, price_id) {
    remove_from_slip(this.website, this.language, outcome_id, price_id);
  },

  // Remove all bets from betslip.
  remove_all_bets : function(){
    remove_all(this.website, this.language);
  },

  // Scroll page to top.
  scroll_to_top : function() {
    scroll(0,0);
  },

  //Get the size of the inrunning div.
  get_size : function() {
    return jQuery('#inrunning').innerHeight();
  },

  //This accepts price update notifications from the flash appGet the size of the inrunning div.
  notify_price_update : function(outcome_id, price_description, price_id, price_decimal) {
    ; // Just a stub for now
  }


}

function getMovie(movieName) {
  return window[movieName] || document[movieName];
}

try {
		var flashHeight = getMovie('bip').getSWFHeight();
		$J('#navigation').css('height', flashHeight);
		$J('object#bip').attr({height: flashHeight});
	}
catch(err) {
		$J('#navigation').css('height', 100);
		$J('object#bip').attr({height: 100});
	}

