
var InternetExplorer = navigator.appName.indexOf("Microsoft") != -1;
var realWin;

var windows = {};
var logoutwindows = {};

var windowsi = 0;

function myResizeTo(w,h)
{
  var difX=0;
  var difY=0;

  if (InternetExplorer)
  {
    window.resizeTo(w,h);
    window.resizeBy(w-document.body.clientWidth,h-document.body.clientHeight);
  }
  else
  {
    difX = window.outerWidth-window.innerWidth;
    difY = window.outerHeight-window.innerHeight;
    window.resizeTo(w+difX,h+difY);
  }
}

function closeWindows()
{
  for (var i in windows)
  {
    if (windows[i])
    {
      windows[i].close();
    }
  }
}

// Handle all the the FSCommand messages in a Flash movie

function Casino_DoFSCommand(command, args)
{
  var CasinoObj = InternetExplorer ? Casino : document.Casino;
  if (command == 'getURL' || command == 'message')
  {
    var temp = new Array();
    temp = args.split('<separator>');
    var winwidth = 0;
    var winheight = 0;
    var wtimeout = 0;
    var screenheight = screen.height;
    var screenwidth = screen.width;
    temp[1] = temp[1] ? temp[1] : 'ThisIsATestWindow';
    windowsi ++;

    if (temp[3])
    {
      var size = temp[3].split('X');
      if (size[0] && size[1])
      {
        winwidth = size[0];
        winheight = size[1];
      }
    }
    if (temp[2])
    {
      wtimeout = parseInt(temp[2]) * 1000;
    }
  }

  if (command == "focus")
  {
    focus();
  }
  else if (command == "getURL")
  {
    for (var i in windows )
    {
      if (i == temp[1] && temp[1] != 'cashier')
      {
        windows[i].close();
        break;
      }
    }
    if (temp[1] == 'cashier' && windows['cashier'] && !windows['cashier'].closed)
    {
      windows['cashier'].focus();
    }
    else
    {
      var win = new Object();
      win['content'] = '<div id="HTMLLayoutFormPlace" style="display: none">' + temp[0] + '</div>';
      win['focus'] = true;
      win['name'] = temp[1];
      win['winwidth'] = winwidth ? winwidth : 800;
      win['winheight'] = winheight ? winheight : 600;
      newwin = newWindowObject (win);
      postParams(newwin);
    }
  }
  else if (command == "close")
  {
    self.close();
  }
  else if (command == "message")
  {
    var win = new Object();
    win['name'] = temp[1] + windowsi;
    win['content'] = temp[0];
    win['top'] = ((screenheight/2)-(240));
    win['left'] = ((screenwidth/2)-(320));
    win['setoncontextmenu'] = true;
    win['winclosetimeout'] = wtimeout;
    win['winfocus'] = true;
    if (temp[1] == 'messageoftheday' || temp[1] == 'login')
    {
      win['winwidth'] = winwidth ? winwidth : 200;
      win['winheight'] = winheight ? winheight : 400;
      newwin = newWindowObject (win);
    }
    else if (temp[1] == 'logout')
    {
      win['winwidth'] = winwidth ? winwidth : 400;
      win['winheight'] = winheight ? winheight : 400;
      logoutwindows[win.name] = win;
      window.onunload = function ()
      {
        showLogoutMsg();
        closeWindows();
      };
    }
  }
  else if (command == "HURL")
  {
    var iframetag = '<iframe id="casinosiframe" src="'+args+'" width="0px" height="0px" border="0px" frameborder="0" style="DISPLAY: none; LEFT: 0px; POSITION: absolute; TOP: 0px"></iframe>';
    var l = document.getElementById('casinosiframediv');
    l.innerHTML = iframetag;
  }
  else if (command == "closeWindows")
  {
    closeWindows();
    showLogoutMsg();
  }
  else if (command == "playRealOffline")
  {
    // Misconfigured
  }
}

function postParams (form)
{
	var elForm = form.document.getElementById('SubmitCashierData');

	if (elForm == null)
	{
		return false;
	}
	else
	{
		elForm.submit();
		return true;
	}
}

function newWindowObject (p)
{
  p.resizable = p.resizable ? 'yes' : 'no';
  p.top = p.top ? p.top : '';
  p.left = p.left ? p.left : '';
  p.url = p.url ? p.url : '';
  p.winwidth = p.winwidth ? p.winwidth : 800;
  p.winheight = p.winheight ? p.winheight : 600;
  var win = window.open(p.url, p.name, "width="+p.winwidth+",height="+p.winheight+",toolbar=no,menubar=no,scrollbars=yes,resizable="+p.resizable+",top="+p.top+",left="+p.left);
  windows[p.name] = win;
  if (p.content.length > 0 && p.url == '')
  {
    win.document.open();
    win.document.write(p.content);
    win.document.close();
  }

  if (p.setoncontextmenu)
  {
    win.document.getElementsByTagName('body')[0].oncontextmenu = function (){return false;};
  }
  if (p.winclosetimeout > 0)
  {
    win.setTimeout(function (){win.close();}, p.winclosetimeout);
  }
  if (p.winfocus)
  {
    win.focus();
  }
  return win;
}

function showLogoutMsg()
{
  for (var i in logoutwindows)
  {
    newwin = newWindowObject (logoutwindows[i]);
    if (windows[i]) windows[i] = null;
  }
}

// Hook for Internet Explorer
if (navigator.appName && navigator.appName.indexOf("Microsoft") != -1 &&
  navigator.userAgent.indexOf("Windows") != -1 && navigator.userAgent.indexOf("Windows 3.1") == -1) {
    document.write('<SCRIPT LANGUAGE=VBScript\> \n');
    document.write('on error resume next \n');
    document.write('Sub Casino_FSCommand(ByVal command, ByVal args)\n');
    document.write('  call Casino_DoFSCommand(command, args)\n');
    document.write('end sub\n');
    document.write('</SCRIPT\> \n');
}


