var play_id;
// variables play_cinema_waiting
var maxTries = 30;
var firstTime = true;
var timeOutId;
var currentTries = 1;
var currentMovie;
var currentPlayId;

jQuery.bip = new Object();

jQuery.bip.add_to_slip = function(id) {

    var compositeID = id;
    BS.AddSel(compositeID);
}

jQuery.bip.resize  = function (application, height)
{
	switch (application)
	{
		/*
			insert name of div surrounding the application to allow for resize
			
			NB. the div names need to be different if there is more than one of the
			applications on the page
		*/
		
		case 'ApplicationFull' :
			$('#application').css('height', height);
			break;
		case 'ApplicationBetting' :
			$('#application').css('height', height);
			break;
		case 'HighlightFull' :
			$('#application').css('height', height);
			break;
		case 'HighlightNavigation' :
			$('#application').css('height', height);
			break;
		default :
			$('#application').css('height', height);
			break;
	}
		
}

jQuery.bip.navigate_ap = function (ap_id)
{
	/*
		called when an event is clicked on the 'later' tab of the navigation or
		highlight. Needs to navigate through to the HTML page that corresponds to
		the event with the passed in ID
	*/
    window.location.href = "/betting/?nIDEv=" + ap_id;
}

jQuery.bip.navigate_live = function (live_id)
{
	/*
		called when an event is clicked on the 'live' tab of the navigation or
		highlight. Needs to navigate through to the live betting page and pass
		the id through to the application
	*/
    window.location.href = "/inplay/?lid=" + live_id;
}

jQuery.bip.navigate_sport = function(sport_id) {
    
    /*
    called when a sport is clicked on the sports tab in the navigation, it
    needs to navigate through to the HTML sport page
    */
    window.location.href = "/betting/?gi=" + sport_id;
}

jQuery.bip.open_cinema = function(appname)
{
    // hook into the main application to open the cinema component
    getMovieName(appname).open_cinema();
}

jQuery.bip.close_cinema = function(appname)
{
	// hook into the main application to close the cinema component
    getMovieName(appname).close_cinema();
}

jQuery.bip.stop_cinema = function(appname) {
    /*
    hook into the main application or the cinema highlight to stop something playing, will also
    return an event_id if there was something playing
		
		if the main application and the mini cinema are on the same HTML page then they will need to be
    given different ID's, and referenced by two functions, for this to work correctly
    */
	try {
		play_id = getMovieName(appname).stop_cinema();
	}catch(e){
		// the movie is still loading, do nothing
	}
}

jQuery.bip.play_cinema = function(appname) {
    /*
    hook into the main application or the cinema highlight to play a stream - pass in an event id
		
		if the main application and the mini cinema are on the same HTML page then they will need to be
    given different ID's, and referenced by two functions, for this to work correctly
    */
    //getMovieName(appname).play_cinema(play_id);
    play_cinema_waiting(appname); 
}

function play_cinema_waiting(appname) {
    if ((currentPlayId == null) || (currentPlayId == ""))
        currentPlayId = play_id;
	if ((currentPlayId != null) && (currentPlayId != "")){
		try {
			if (firstTime) {
				currentMovie = getMovieName(appname);
				firstTime = false;
			}
			if (currentTries <= maxTries) {
				currentMovie.play_cinema(currentPlayId);
				stopTimer(timeOutId);
				currentPlayId = null;
			}
		} catch (e) {
			currentTries++;
			timeOutId = setTimeout("play_cinema_waiting('" + appname + "')", 1000);
		}

		if (currentTries == maxTries) {
			stopTimer(timeOutId);
			currentPlayId = null;
		}
	}
}

function stopTimer(timeOutId) {
    clearTimeout(timeOutId);
    firstTime = true;
    currentTries = 1;
}

jQuery.bip.track_action = function (area, component, part, language, action, param)
{
	// hook this into RUEI
	var track_string = 'clicks=' + area + '_' + component + '_' + part + '_' + language + '_' + action + '_' +  param;
}


function getMovieName (movieName)
{
	if (navigator.appName.indexOf("Microsoft") != -1)
	{
		return window[movieName]
	}
	else
	{
		return document[movieName]
	}
}
