$(document).ready(function() {
  if (!cookieExists("pokerLiveChat")) {
    setTimeout("showLiveChat()",3000);
  }
  setCookie("pokerLiveChat","viewed",365);
  $("#closeLiveChat").click(function(){
    hideLiveChat();
  });
  $("#liveChatForm").submit(function(){
    openLiveChat("#liveChatForm","chat6174495");
    return false;
  });
  $("#liveMessageField").keypress(function (e) {
    if (e.which == 13) {
      openLiveChat("#liveChatForm","chat6174495");
    }
  });
});

function cookieExists(a_CheckName) {
	var l_AllCookies = document.cookie.split( ';' );
	var l_TempCookie = '';
	var l_CookieName = '';
	var l_Return = false;
	
	for (i = 0; i < l_AllCookies.length; i++) {
		l_TempCookie = l_AllCookies[i].split( '=' );
		l_CookieName = l_TempCookie[0].replace(/^\s+|\s+$/g, '');

		if (l_CookieName == a_CheckName)	{
			l_Return = true;
			i = l_AllCookies.length;
		}
	}
	return l_Return;
}

function setCookie(a_Name, a_Value, a_Expires) {
  var l_Today = new Date();
  var l_Expires = a_Expires * 1000 * 60 * 60 * 24;
  var l_ExpiryDate = new Date(l_Today.getTime() + (l_Expires));
  
  document.cookie = a_Name + "=" + escape(a_Value) + "; expires=" + l_ExpiryDate.toGMTString()
}

function hideLiveChat() {
  $("#liveInvite").fadeOut()
}

function showLiveChat() {
  $("#liveInvite").fadeIn()
}

function openLiveChat(a_Form,a_WindowName) {
  var l_Message = escape($("#liveMessageField").val());
  var l_Target = $(a_Form).attr("action") + "?";
  
  $(a_Form + " input").each(function() {
    if (this.type == "hidden") {
      l_Target = l_Target + this.name + "=" + this.value + "&";
    }
  });      
  l_Target = l_Target + "SESSIONVAR!message=" + l_Message;
  window.open(l_Target,a_WindowName,'width=475,height=400,resizable=yes');
}


