var lobbyTabs;	


function getUrlVar( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}

//account selector
var selector1 = getUrlVar( 'selector1' );

//set default account below 0=show

if(selector1 == ""){selector1 =0};



//games selector
//set default games below 0=bingo
//set default games below 1=personal offers
//set default games below 2=recommended games
//set default games below 2=progressive jackpot
var selector2 = Number(getUrlVar( 'selector2' ));
if (selector2 == ""){selector2 =0};

//To display or not MyAccount info below variable is used.
var  display = true;

//set default games below 0=bingo
var selector3 = Number(getUrlVar( 'selector3' ));
if (selector3 == ""){selector3 =0};


$(document).ready(function(){
	$(".accordion1 h3").eq(selector1).addClass("active");
	$(".accordion1 .panel").eq(selector1).show();
	
	$(".accordion1 h3").click(function(){
		$(this).next(".panel").slideToggle("fast").siblings(".panel:visible").slideUp("fast");
		$(this).toggleClass("active");
		$(this).siblings("h3").removeClass("active");
	});

	getLobbyContent();	
});


var gamedetailsloaded=false;
var gamesdetailxml=null;

function assingClicks()
{
	highLightLinks();

	if(!display){
		$(".accordion1 h3").removeClass("active");
		$(".accordion1 .panel").hide();
	}
	if(location.href.indexOf('tab0')!= -1){
		$(".accordion1 h3").eq(selector1).addClass("active");
		$(".accordion1 .panel").eq(selector1).show();
	}
	display = false;

	$(".accordion2 h3").eq(selector2).addClass("active");
	$(".accordion2 .panel").eq(selector2).show();
	
	$(".accordion3 h3").eq(selector2).addClass("active");
	$(".accordion3 .panel").eq(selector2).show();


	$(".accordion2 h3").click(function()
	{
		$(this).next(".panel").slideToggle("fast").siblings(".panel:visible").slideUp("fast");
		$(this).toggleClass("active");
		$(this).siblings("h3").removeClass("active");
	});	

	$(".accordion3 h3").click(function(){
		$(this).next(".panel").slideToggle("fast").siblings(".panel:visible").slideUp("fast");
		$(this).toggleClass("active");
		$(this).siblings("h3").removeClass("active");
	});	

	/*if(session_id != 'null')
	{
		//linkGames.jsp will get AUTHENTICATION for Paraly Slot games. This should be exist.
		var lobbyAreaScript = document.createElement('SCRIPT');
		lobbyAreaScript.src="/commonsys/lobbyver20.jsp";
		$("head")[0].appendChild(lobbyAreaScript);

		//getG2Games.jsp will get the list of functions with StreamName, one for each Stream.
		var g2BingoScript = document.createElement('SCRIPT');
		g2BingoScript.src="/commonsys/getG2Games.jsp";
		$("head")[0].appendChild(g2BingoScript);

	}*/
	if (!gamedetailsloaded) {
		getBingoGameDetails();
	} else {
		$(gamesdetailxml).find('game').each(function()
		 {
			try
			{	
				fillPlaceHolder($(this), false, $(this).attr('roomName'));
	}catch(e){
				//alert(e);
			}
		 });
		 setValues();
	}

	try
	{
		assignLinks();
	}catch(e){}
}

// this function, will get the FEEDS for all Streams
function getBingoGameDetails(roomName, startcountdown)
{
	$.ajax({
           type: "GET",
           url: "/rss/next_game_jp_details_ibn_eur.jsp?random=" + Math.round(Math.random()*1000000000)+"&roomName="+roomName,
           dataType: "xml",
           success: function(xml) {
				gamesdetailxml=xml;
				$(xml).find('GameDetails').each(function() {
				try
				{	
					if(!startcountdown)
					{
						if(roomName){
							$(xml).find('game').each(function() {
								if(roomName == $(this).attr('roomName')){
									fillPlaceHolder($(this), true, $(this).attr('roomName'));
								}
							});
						}else{
							$(xml).find('game').each(function() {
								fillPlaceHolder($(this), true, $(this).attr('roomName'));						
							});
						}
						//assignLoungeValues($(this), true);
					}else{
						$(xml).find('game').each(function() {
							fillPlaceHolder($(this), false, $(this).attr('roomName'));
						});
						//assignLoungeValues($(this), false);
					}
					gamedetailsloaded=true;
					if(roomName == undefined || roomName == "refresh")
						setTimeout("getBingoGameDetails('refresh',"+ true+")", 60000);
					setValues();
				}
				catch(e){}
			}); 		
		}	  
	});	
}

function StartCountDown(myDiv,nextGame,roomName){
	gsecs		= Math.floor(nextGame/1000);
	gsecs=(gsecs<10)?0:gsecs;
	CountBack(myDiv,gsecs,nextGame,roomName);
  }
  
function Calcage(secs, num1, num2){
	s = ((Math.floor(secs/num1))%num2).toString();
	if (s.length < 2) 
	{	
	  s = "0" + s;
	}
	return (s);
  }
  
function CountBack(myDiv, secs, nextGame, roomName){
	if(nextGame > 0){
		var DisplayStr;
		var DisplayFormat = "%%M%%:%%S%%";
		DisplayStr = DisplayFormat.replace(/%%M%%/g,		Calcage(secs,60,60));
		DisplayStr = DisplayStr.replace(/%%S%%/g,		Calcage(secs,1,60));
		if(secs > 0)
		{
			if($("#"+myDiv))
			{
				 $("#"+myDiv).html(DisplayStr);
				setTimeout("CountBack('" + myDiv + "'," + (secs-1) + ",'" + nextGame + "','"+roomName+"')", 999);			
			}
		}
		else
		{
			if($("#"+myDiv))
				 $("#"+myDiv).html("Ahora");
			setTimeout("getBingoGameDetails('"+roomName+"',false)", 10000);
		}
	}else{
		$("#"+myDiv).html("--");	
	}
  }

/*
this will populate the FEEDS values in respective HTML DIV positions.
Param: xmlObj - is the XML element with the feeds values
Param: startcountdown - if this is true, it will call the StartCountDown() with the nextGameStart timer.
					  - if this is false, it wont append the Timer DIV.
Param: roomName - this is used to get the particular HTML DIV. generally this is StreamName.
*/
function fillPlaceHolder(xmlObj,startcountdown, roomName)
  {
	  var players = roomName+"players";
	  var price   = roomName+"price";
	  var prize   = roomName+"prize";
	  var timer   = roomName+"timer";
	  var jackpot = roomName+"jackpot";
	

	//alert(players+"\n"+price+"\n"+prize+"\n"+timer+"\n"+jackpot);
	  if($(xmlObj).attr('numOfPlayers') != undefined && $("#"+players).length > 0)
			$("#"+players).html($(xmlObj).attr('numOfPlayers'));
		else
		{
			if($("#"+players).length > 0)
				$("#"+players).html("--");
		}

		if($(xmlObj).attr('ticketPrice') != undefined && $("#"+price).length > 0)
			$("#"+price).html("&euro;"+$(xmlObj).attr('ticketPrice'));
		else
		{
			if($("#"+price).length > 0)
				$("#"+price).html("--");
		}
		
		if($(xmlObj).attr('prize') != undefined && $("#"+prize).length > 0){
			 $("#"+prize).html("&euro;"+parseFloat($(xmlObj).attr('prize')).toFixed(2));
		}
		else
		{
			if($("#"+prize).length > 0)
				$("#"+prize).html("--");
		}
		
		if (startcountdown || ($("#"+timer).html() == "--"))
		{
			if($(xmlObj).attr('nextGameStarting') != undefined && $("#"+timer).length > 0)
				StartCountDown(timer,$(xmlObj).attr('nextGameStarting'),roomName)
			else{
				if( $("#"+timer).length > 0)
					 $("#"+timer).html("--");
			}
		}else{
			/*  Get the nextGameStarting data.
				if Element Exists and 
				if nextGameStarting is 'undefined' set the Element data as '--'
				and if nextGameStarting is Negative set the Element data as 'Ahora'
			*/
			if($("#"+timer).length > 0) {
				if ($(xmlObj).attr('nextGameStarting') == undefined) {
					$("#"+timer).html("--");
				} else if (parseInt($(xmlObj).attr('nextGameStarting')) < 0) {
					$("#"+timer).html("Ahora");
				}
			}
		}

		if($(xmlObj).attr('jackpotValue') != undefined && document.getElementById(jackpot)){
			document.getElementById(jackpot).innerHTML		= "&euro;"+parseFloat($(xmlObj).attr('jackpotValue')).toFixed(2);
		}
		else
		{
			if(document.getElementById(jackpot))
				document.getElementById(jackpot).innerHTML		= "--";
		}		
  }


//hits the LobbyTabs.jsp, to get the Games along with Groups.
function getLobbyContent()
{
	var url = "/commonsys/lobby/LobbyTabs.jsp?lobbyContainer=lobbyTabs&location=Lobby";
	
	//add request parameter displayAll (if 'Y', JSP returns visible=N games too).
	if(window.displayAll)
		url  +="&displayAll="+displayAll;
	
	//the given code will navigate to the exact selected Lobby Tab.
	var myUrl = location.href
	if(myUrl.indexOf('?') > -1 && myUrl.indexOf('popup') == -1)
		url  = url+"&activeTab="+myUrl.split('?')[1];

	var lobbyContentScript = document.createElement('SCRIPT');
	lobbyContentScript.src = url;
	$("head")[0].appendChild(lobbyContentScript);
}

function getXMLFromData(response)
{
	if (window.ActiveXObject)
    {
            xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
            xmlDoc.async="false";
            xmlDoc.loadXML(response);
			return xmlDoc; 
    }
       //for Firefox
     else
    {
           //create the xml docuemnt object in firefox
           var parser=new DOMParser();
           xmlDoc=parser.parseFromString(response,"text/xml");
			return xmlDoc; 
     }
}

function assignLoungeValues(xmlObj, countdown)
 {
		
		fillParlayPlaceHolder(xmlObj, 'bingoLounge', 'Bingo 25p', jackpot3, countdown);

		if($(xmlObj).find('bingoLounge').attr('nextGameStarting') == undefined && document.getElementById("gameNotAvl"))
			document.getElementById("gameNotAvl").innerHTML	= "Bingo Lounge room opens daily from midday to midnight";
		else{
			if(document.getElementById("gameNotAvl"))
				document.getElementById("gameNotAvl").innerHTML	= "";	
		}
}

/*If we have Steam 'X' in HTML and feeds are not return for this Stream,
	then, the respective Stream DIVs will be placed '--'.
*/
function setValues(){
	  $("span").each(function() {
		var flag = true;
		if($(this).attr('name') != undefined && $(this).attr('name').indexOf('timer') != -1){
			var roomName = $(this).attr('name').split('timer')[0];
			if(gamesdetailxml){
				$(gamesdetailxml).find('game').each(function() {
					if(roomName == $(this).attr('roomName')){
						flag = false;					
					}
				});
			}			
		}
		if(flag){
			var players = roomName+"players";
			var price   = roomName+"price";
			var prize   = roomName+"prize";
			var timer   = roomName+"timer";
			var jackpot = roomName+"jackpot";
			if($("#"+timer).html() == "Ahora"){
				$("#"+players).html("--");
				$("#"+price).html("--");
				$("#"+prize).html("--");
				$("#"+timer).html("--");
				$("#"+jackpot).html("--");
			}
		}
      });  
  }
  //function for highlighting the tabs for cantabingo spanish 
  function highLightLinks()
  {
		 var tabLink		= lobbyTabs.get("activeTab").get("dataSrc");
		 var tabLink2		= tabLink.split("groupName")[1];
		 var linkActivate	= tabLink2.split("&")[0];
		 var linkcolor		= linkActivate.split("=")[1];
		 var funcName		="addClassToAnchor";
		
		if (eval("typeof " + funcName + " == 'function'"))
		{
			addClassToAnchor(linkcolor);
		}
  }