var geo_user_asked = 0;

var links = new Array();
links['en'] = new Array();
links['it'] = new Array();
links['fr'] = new Array();
links['de'] = new Array();
links['es'] = new Array();
links['pt-br'] = new Array();
links['nl'] = new Array();

links['en']['game1'] = links['nl']['game1'] = links['pt-br']['game1'] = '/slots.html';
links['en']['game2'] = links['nl']['game2'] = links['pt-br']['game2'] =  '/video-poker.html';
links['en']['game3'] = links['nl']['game3'] = links['pt-br']['game3'] =  '/blackjack.html';
links['en']['game4'] = links['nl']['game4'] = links['pt-br']['game4'] =  '/roulette.html';
links['en']['game5'] = links['nl']['game5'] = links['pt-br']['game5'] =  '/slots-tournaments.html';
links['en']['game6'] = links['nl']['game6'] = links['pt-br']['game6'] =  '/jackpots.html';


links['fr']['game1'] = 'machines-a-sous.html';
links['fr']['game2'] = links['it']['game2'] = links['de']['game2'] = links['es']['game2'] = 'video-poker.html';
links['fr']['game3'] = links['it']['game3'] = links['de']['game3'] = links['es']['game3'] = 'blackjack.html';
links['fr']['game4'] = links['it']['game4'] = links['de']['game4'] = links['es']['game4'] = 'roulette.html';
links['fr']['game5'] = links['es']['game5'] = 'slots-tournaments.html';
links['fr']['game6'] = links['de']['game6'] = 'jackpots.html';

links['it']['game1'] = links['de']['game1'] = links['es']['game1'] = 'slots.html';
links['it']['game5'] = links['de']['game5'] = 'tournaments_schedule.html';
links['it']['game6'] = 'slots-progressive.html';
links['es']['game6'] = 'progressive-slots.html';

//alert(Drupal.settings.language);
var suo_link="sub.html";
if(typeof seo_lang != "undefined")
{
if(seo_lang==true)
	suo_link="/sub.html";
}
//if(Get_Cookie('bTag').indexOf('ajaff03676_4400_1209') != -1 && Drupal.settings.language=='en') 
//	suo_link='moneybookers_welcome.html';
	
function createRequestObject() {
  var xmlhttp = null;
  try {
    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
  } catch(e) {
    try {
      xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    } catch(f) {
      xmlhttp = null;
    }
  }
  if(!xmlhttp && typeof XMLHttpRequest != "undefined") {
    xmlhttp = new XMLHttpRequest();
    xmlhttp.overrideMimeType('text/xml');
  }
  return  xmlhttp;
}
var httpObj = createRequestObject();

function handleResponse() {
  try {
    if((httpObj.readyState == 4) && (httpObj.status == 200)) {
      var response = httpObj.responseText;
      var newContent = document.createElement('div');
      newContent.style.zIndex = 1000;
      var dest = document.getElementById('FlashMovie');
      if(!dest) dest = document.getElementById('content');
      dest = document.body;
      if(dest) {
//       strresp = response.substring(response.search('<div class="content">')+21,response.search('<!--tutorial ends-->'));
       strresp = response.substring(response.search('<body class="mainPage">'+23),response.search('</body>'));
       dest.innerHTML = strresp;
      }
      dwnl();
    }
  }
  catch(e){ }
}

function sndReq() {
  dwnl();
  tutorial_win=window.open('getstarted.html');
  if (tutorial_win)
    tutorial_win.focus();
  return;

  httpObj.open('GET', 'getstarted.html');
  httpObj.onreadystatechange = handleResponse;
  httpObj.send(null);
}

function Get_Cookie( check_name ) {
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f

	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );


		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

		// if the extracted name matches passed check_name
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found )
	{
		return null;
	}
}

function startDownloadWithTutor() {
  sndReq();
}

function startDownloadWithTutorInNewWin() {
  dwnl();
  window.open("/getstarted.html");
}

function location_submit(url) {
	location.href = url;
}

function ShowBtag() {
	var btg = ureadCookie('bTag');
	if(!btg) btg = 'default';
	return false;
}

function dwnl() {  
  // Wait for pcm.
  if (typeof(pcm)=='undefined' || pcm.ready == 0) {
    window.setTimeout('dwnl()',100);
    return;
  }
  data = pcm.data;
  bTag = pcm.data.btag;
  // Geo issue
  if(typeof(data.geo) != 'undefined' && typeof(data.geo.country_code) != 'undefined' && data.geo.country_code == 'US') {
    geo_go();
    return false;
  }

  if(typeof(data.tracking) != 'undefined') {
	if (typeof(data.tracking.download) != 'undefined' && parseInt(data.tracking.download) > 0) {
	  pcm.track(data.tracking.download);
	  setTimeout("start_dwnl()",1000);
	  return false;
	}
  }  
    
  start_dwnl();
  return false;
}

function dwnl_m() {  
  // Wait for pcm.
  if (typeof(pcm)=='undefined' || pcm.ready == 0) {
    window.setTimeout('dwnl_m()',100);
    return;
  }
  data = pcm.data;
  bTag = pcm.data.btag;
  start_dwnl();
  return false;
}

function start_dwnl() {
 var tracked = '/AllJackpots.exe';
	  
 if (Drupal.settings.language && Drupal.settings.language != 'en') {
	var  prefix = Drupal.settings.language.substring(0,2);
	if (prefix == 'ja')
	  prefix = 'jp';
	  tracked = '/' + prefix + tracked;
  }
  urchinTracker(tracked);
//  window.top.location.href="/dwnl.php?filename=alljackpotsviper.exe&bTag="+pcm.data.btag+'&ul='+Drupal.settings.language;
  var newloc="/dwnl.php?filename=alljackpotsviper.exe&bTag="+pcm.data.btag+'&ul='+Drupal.settings.language;
  if (pcm.data.btag5)
    newloc += "&bTag5=" + pcm.data.btag5
  if (pcm.data.id)
  	newloc += "&bTag4=" + pcm.data.id
  window.open(newloc,'dwnl');
}

function openGame(game){
	window.open('demogame.html?gameid='+game+'&bTag='+Get_Cookie('bTag'),'game','width=665,height=680,resizable=yes,scrollbars=1,top=0,left=0');
}

// JS functions for FSCommands
function Download_DoFSCommand(command, args) {
  //alert(command + "  " + args);
  //alert(seo_lang);
//  if(seo_lang==true)
//  	suo_link="/sub.html";
//  else suo_link="sub.html";
  if(command == 'click'){
    if(args == 'suo'){
    	if(Get_Cookie('bTag').indexOf('ajaff03676_4400_1209') != -1 && Drupal.settings.language=='en') 
			suo_link='moneybookers_welcome.html';
     location.href=suo_link;
    }
    if(args == 'download')
      dwnl();
  }
}

function Games_DoFSCommand(command, args){
  //alert(command + "  " + args);
  if(command == 'click'){
    if(args == 'game1')
      location.href = links[Drupal.settings.language]['game1'];
    if(args == 'game2')
      location.href = links[Drupal.settings.language]['game2'];
    if(args == 'game3')
      location.href = links[Drupal.settings.language]['game3'];
    if(args == 'game4')
      location.href = links[Drupal.settings.language]['game4'];
    if(args == 'game5')
      location.href = links[Drupal.settings.language]['game5'];
    if(args == 'game6')
      location.href = links[Drupal.settings.language]['game6'];
  }
}

function Instant_DoFSCommand(command, args){
  if(command == 'click') {
     if(args == 'instant') {	 
    	instant_play();
     }
  }            
}

function live_dealer_DoFSCommand(command, args){
  if(command == 'click') {
     if(args == 'LD_Download') {	 
    	dwnl();
     }
  }            
}

function instant_play() {
	// Wait for pcm.
	  if (typeof(pcm)=='undefined' || pcm.ready == 0) {
	    window.setTimeout('instant_play', 100);
	    return;
	  }
	  data = pcm.data;
  // Geo issue
  if (do_redirect()) {
    return false;
  }
    
    guid = Get_Cookie('PCMGUID');
    if(Drupal.settings.language == 'fr') {
      var new_url = "https://bigflash.gameassists.co.uk/french?bTag=" + Get_Cookie('bTag');
      urchinTracker('/fr/launchflash');
    }else if(Drupal.settings.language == 'it'){
      var new_url = "https://bigflash.gameassists.co.uk/bigflash_italian/t3start.asp?bTag=" + Get_Cookie('bTag');
      urchinTracker('/it/launchflash');
    }else if(Drupal.settings.language == 'es'){
      var new_url = "https://bigflash.gameassists.co.uk/spanish?bTag=" + Get_Cookie('bTag');
      urchinTracker('/es/launchflash');
    } else {
      var new_url = "https://bigflash.gameassists.co.uk/bigflash/t3start.asp?bTag=" + Get_Cookie('bTag');
      urchinTracker('/launchflash');
    }
    if(guid!=null)
      new_url +="&trackingguid="+guid;
    if (pcm.data.btag5)
      new_url += "&bTag5=" + pcm.data.btag5
    if (pcm.data.id)
   	  new_url += "&bTag4=" + pcm.data.id
    window.open(new_url,'','width=770,height=535,toolbar=no,menubar=no,resizable,scrollbars=yes,location=no,status=1');
    return false;
}

function InnerDownload_DoFSCommand(command, args){
 // alert(command + "  " + args);
  if(command == 'click' &&  args == 'download')
    dwnl();
}

function do_redirect() {
	  if (geo_user_asked) {
	    return false;
	  }
	  geo_user_asked = true;
	  var data = pcm.data;
	  
	  if(typeof(data.geo) == 'undefined' || typeof(data.geo.country_code) == 'undefined' || data.geo.country_code != 'US') {
	    return false;
	  } 
	  
	  if (!document.getElementById('geo_question')) {
	    var qdiv = document.createElement('div');
	    qdiv.id = 'geo_question';
	    $(document.body).append('<div id="geo_question"></div>');
	    $('#geo_question').load('/geo_popup.html');
	  }
	  else {
		$('#geo_question').show();
	  }
	  
	  return true;
	}

	function geo_go() {
	  data = pcm.data;
	  id = parseInt(data.id)
	  if (!id) {
		id = new Date().getTime();
	  }
	  newref = "http://downloads.jackpotcapital.com/affiliates/aiddownload.asp?casinoID=272&gAID=23874&subGid="+id+"&bannerID=6946";
	  var tracked = location.pathname;
	  if (location.search) {
	    tracked += location.search;
	  }
	  if (tracked.charAt(tracked.length-1)!='/') {
	    tracked += '/';
	  }
	  tracked += 'jackpotcapital';
	  urchinTracker(tracked);
			    
	  location.href = newref;
	  return false;
	}

	function geo_close() {
	  $('#geo_question').hide();
	  geo_user_asked = false;
	}
