function popupwindow (url, name, width, height) {
	window.open(url,name,'width='+width+',height='+height+',resizable=no,scrollbars=no,directories=no,menubar=no,taskbar=no,toolbar=no');
}
function open_table(id) {
  popupwindow('/poker_table/open/' + id,'','860','625');
}
function open_affiliate_table(tid, aid, nid) {
  popupwindow('/poker_table/open/' + tid + '?aid='+aid+(nid != null ? '&nid='+nid : ''),'','860','625');
}
function open_sitngo_table(id) {
  popupwindow('/poker_table/open_sit_go/' + id,'','860','625');
}
function open_active_table(id, name) {
  popupwindow('/poker_table/open/' + id + '?active=true',name,'860','625');
}
function toggleVisibility(whichLayer)
{
if (document.getElementById)
{
var style2 = document.getElementById(whichLayer).style;
style2.display = style2.display? "":"none";
}
else if (document.all)
{
var style2 = document.all[whichLayer].style;
style2.display = style2.display? "":"none";
}
else if (document.layers)
{
var style2 = document.layers[whichLayer].style;
style2.display = style2.display? "":"none";
}
}
function lobbyMouseOver (obj) {
	var objtext = 'Open Table';
	var objhtml = 'Open Table';
	obj.innerHTML=objhtml;
}

function closeWindow () {
	window.close();
}

function showSplash(_container, _action) {
    _items=jQuery(_container).children();
    _active=jQuery(_items).siblings('div:visible').get(0)|| false;
    if (_action=='next') {
        _next = (_items.index(_active)+1 == _items.length) ? jQuery(_items).get(0) : jQuery(_active).next();
    } else if (_action=='first') {
        _next = jQuery(_items).get(0);
    } else {
        _next = (_items.index(_active)==0) ? jQuery(_items).get(_items.length-1) : jQuery(_active).prev();
    }
    jQuery(_next).children('div').each( function() { x = new Image(); x.src=jQuery(this).css('background-image').replace(/url\(/,'').replace(/\)$/,''); });
    if (_active) { jQuery(_active).hide(); }
    jQuery(_next).show();
}

jQuery(document).ready(function(){
	jQuery("p#forgot2").click(function(){
		jQuery("div#forgot1").show("slow");jQuery("p#forgot2").remove();
	});
	jQuery("li#rch").click(function(){
		jQuery("li#rb").show("slow");jQuery("li#rc").show("slow");jQuery("li#rch").remove();
	});
	jQuery("i#verificationInfoButton").click(function(){
		jQuery("td#verificationInfo").show("slow");
	});
});


function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else var expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}

function eraseCookie(name) {
  createCookie(name,"",-1);
}
function existsCookie(name) {
  return !! readCookie(name);
}

var Activity = (function() {
  var last_activity = new Date().getTime();
  var on_next_activity = null;
  var tracking = null;

  return {
    start_tracking: function() {
      if (tracking)  return;

      jQuery(document).mousemove(function() {
        last_activity = new Date().getTime();

        var f = on_next_activity;
        on_next_activity = null;
        if (f)  f();
      })
      tracking = true;
    },
    inactive_for: function() { return(new Date().getTime() - last_activity); },
    on_next_activity: function(f) { on_next_activity = f; }
  };
})();

var Lobby = (function() {
  var interval_id = null;
  var polling = false;
  var polling_interval = 10000;
  var inactivity_timeout = 20000;

  var stop_polling = function() {
    polling = false;
    clearInterval(interval_id);
  }

  var poll = function() {
    jQuery.ajax({url: '', dataType: 'script'})
  }

  var _start_polling = function() {
    polling = true;

    Activity.start_tracking();

    interval_id = setInterval(function() {
      poll();
    }, polling_interval);

    // stop polling after inactivity timeout, restart on next activity
    setInterval(function() {
      if (polling && Activity.inactive_for() > inactivity_timeout) {
        stop_polling()
        Activity.on_next_activity(Lobby.start_polling_immediately);
      }
    }, 1000)
  }

  return {
    start_polling: function() {
      _start_polling();
    },

    start_polling_immediately: function() {
      _start_polling();
      poll();
    }
  };
})();


function play_sound(name) {
  jQuery('body').append("<embed src='/sounds/" + name + ".wav' hidden='true' autostart='true' loop='false' />");
}
