//var timertime = 15000 //def timer time for checking
var timertime = 3000 //def timer time for checking
var chatRequestsTimerId = 0; //id for holder the timer
var credits = -1; //global credits for him
var lastList = ''; // used to compare new vs old size - no need to reload same that's already there
var userActive = false; //holds true if the user is over the div - if false allow auto scrolling back to the top
var inChat = false; //holds if in chat or not - if so, slow things down
var stealth = GetCookieValue('Stealth'); //if stealth, no need to check for requests
var userid = GetCookieValue('ASPSessionID');
var hostessChatCheckerID = 0;
var guy = 0;

var curPage = '';
curPage = new String(document.location);
curPage = curPage.toLowerCase();

function StartUp()
{  
  if(userid != null)
  {
    //delay each request just to keep servers happy
    // also used this to group the sartup items
    ChatRequestsTimer();
    a = 1000;
    setTimeout('updateLastVisit()', a);
    b = 30000;
    setTimeout('ClearStaleRequests()', b);
    CheckForHostessChatRequest();
  }
}

//########### GET REQUESTS ###################
function ChatRequestsTimer()
{
  if(userid != null && (stealth == 0 || stealth == '0'))
  {
    t = timertime;
    chatRequestsTimerId = setTimeout('getChatRequests()', t);
  }
}

function getChatRequests()
{
  try
  {		 
    if(inChat == false || inChat == null)
    { 
      if(timertime > 15000 && credits > 0) 
      {//if the timer had previously been set to slow down and they have credits, bing it back to 15secs
        timertime = 15000;
      }
      else if(timertime < 15000) 
      {//if the timer had previously been set to run immediately (removing someone), bing it back to 15secs
        timertime = 15000;
      }
      else if(timertime < 60000 && credits == 0) 
      {//if they have no money, no sense in checking every 10 secs
        timertime = 60000;
      }
    }
    else
    {
      clearTimeout(chatRequestsTimerId);
      document.getElementById('chatrequests').style.display = 'none';
      return;	
    }
    var url = '../chat/ChatGetRequests.aspx?id=' + Math.random();
    if(window.XMLHttpRequest)
    {
      xmlhttpChatRequests = new XMLHttpRequest();
      with(xmlhttpChatRequests)
      {  
        onreadystatechange = showChatRequests;open("GET",url,true);send(null);
      } 
    }
    else if (window.ActiveXObject)
    {
      xmlhttpChatRequests = new ActiveXObject("Microsoft.XMLHTTP");
      if (xmlhttpChatRequests)
      {
        with(xmlhttpChatRequests)
        {  
          onreadystatechange = showChatRequests;open("GET",url,true);send();
        } 
      }
    }
  }
  catch(e){} 
  ChatRequestsTimer();
}

function showChatRequests()
{
  try
  {
    var chatDiv = document.getElementById('chatrequests'); //top div
    var chatList =document.getElementById('chatrequestlist'); //holds list
    var closeButton = document.getElementById('chatrequestlistclose');	
    var chatTop = 	document.getElementById('chatrequeststop');		
    if (xmlhttpChatRequests.readyState == 4)
    {
      if (xmlhttpChatRequests.status == 200){
        if(chatDiv != null){
          var theResponse = xmlhttpChatRequests.responseText;
          if(theResponse.length > 0){
            //anything there?
            var responseArray =  theResponse.split('|*-*-*|');     
            if(responseArray[1] != null && responseArray[1] != ''){
              //load the balance - use it for the timer
              credits = responseArray[1];
            }				
            var theList = responseArray[0];
            if(theList != lastList || chatDiv.style.display == 'none') //don't reprint if not changed - have to change the methodoligy here (not size)
            {
              if(theList.length > 0){
                chatList.innerHTML = theList;
                var screenHeight = GetHeight();
                with(chatList){
                  if(getElementsByTagName){
                    var divItem = getElementsByTagName('IMG');
                    var countOf = 0;
                    for(var i = 0; i < divItem.length; i++){
                      var imgId = new String(divItem[i].id)
                      if(imgId.indexOf('thumb') > -1){
                        countOf ++
                      }
                    }
                  }
                }      
                var heightOfThumbs =		countOf * 101;
                if(heightOfThumbs < screenHeight){
                  chatDiv.style.height = heightOfThumbs +33 + 'px';
                  chatList.style.height = heightOfThumbs + 'px';
                }
                else{
                  if(screenHeight > 100){
                    chatDiv.style.height = screenHeight - 5 + 'px';
                    chatList.style.height = screenHeight - 38 + 'px';
                  }
                }
                if(chatDiv.style.display == 'none' || chatDiv.style.display == ''){
                  chatDiv.style.width = 11 + 'px';
                  closeButton.style.display = 'none';	
                  chatTop.innerHTML = '';
                  chatDiv.style.display = 'block';	
                  ShowList();
                }							
                //if client is not actively looking at anything, scroll it back to the top
                if(userActive == false){
                  chatList.scrollTop = 0;
                }
                SpecialChatAlertGirlCheck();
              }
              else{
                chatDiv.style.display = 'none';
              }
            }
          }
          else{
            chatDiv.style.display = 'none';
          }
        }
        else{
          clearTimeout(chatRequestsTimerId);
        }
      }
    }
  }
  catch(e){}
}

//########### END GET REQUESTS ###################
//###### UPDATE LAST VISIT #########

function LastVisitTimer()
{//updates the whosonline table with the time show he doesn't stale
  t =180000;
  setTimeout("updateLastVisit()", t);
}

function updateLastVisit()
{
	try
	{  
		var url = '../profile/profile_updatelastvisit.aspx?id=' + Math.random();
		if(window.XMLHttpRequest)
		{
			xmlhttpUpdateLastVisit = new XMLHttpRequest();
			with(xmlhttpUpdateLastVisit)
			{  
				onreadystatechange = restartUpdate;open("GET",url,true);send(null);
			} 
		}
		else if (window.ActiveXObject)
		{
			xmlhttpUpdateLastVisit = new ActiveXObject("Microsoft.XMLHTTP");
			if (xmlhttpUpdateLastVisit)
			{
				with(xmlhttpUpdateLastVisit)
				{ 
					onreadystatechange = restartUpdate;open("GET",url,true);send();
				}
			}	
		}
	}
	catch(e){}
	LastVisitTimer(); 
}

function restartUpdate()
{ //doesn't seem to fire in firefox without a onreadystatechange even though I don't do anything
}

//###### END UPDATE LAST VISIT #########

//########## CLEAR STALE REQUESTS ############
function ClearStaleRequests()
{//go and send a message to the girls who have been waiting for a reply
  try
  {
    var url = '../chat/ChatClearStaleRequests.aspx?id=' + Math.random();
    if(window.XMLHttpRequest)
    {
      xmlhttpClearStaleRequests = new XMLHttpRequest();
      with(xmlhttpClearStaleRequests)
      {
        onreadystatechange = StaleRequestsCleared;open("GET",url,true);send(null);
      }
    }
    else if (window.ActiveXObject)
    {
      xmlhttpClearStaleRequests = new ActiveXObject("Microsoft.XMLHTTP");
      if (xmlhttpClearStaleRequests)
      {
        with(xmlhttpClearStaleRequests)
        {
          onreadystatechange = StaleRequestsCleared;open("GET",url,true);send();
        }
      }
    }
  }
  catch(e){}
  ClearStaleRequestsTimer(); 
}
function StaleRequestsCleared()
{//doesn't seem to fire in firefox without a onreadystatechange even though I don't do anything
// if (xmlhttpClearStaleRequests.readyState == 4) 
//  {
//   if (xmlhttpClearStaleRequests.status == 200) 
//   {
////    getChatRequests();
//   }
//  } 
}

function ClearStaleRequestsTimer()
{
  t = 90000;
  setTimeout("ClearStaleRequests()", t);
}

//########## END CLEAR STALE REQUESTS ############

function ShowList()
{
	var chatDiv = document.getElementById('chatrequests');
	var closeButton = document.getElementById('chatrequestlistclose');	
	var chatTop = 	document.getElementById('chatrequeststop');
  var curWidth = chatDiv.offsetWidth;
  if(curWidth >= 208)
  {
		closeButton.style.display = 'block';	
		chatTop.innerHTML = 'These ladies would like to chat!';
		return
	}
	else
	{			 
		curWidth = curWidth + 5;		
		chatDiv.style.width = 	curWidth + 'px';			
		zz = 5;
		setTimeout('ShowList()', zz);	
	}
}

function chatoptionsHover(obj, dir)
{//highlites the cell on mouseover
  var chatIcon = null;
  //chatIcon = obj.firstChild.firstChild.nextSibling;
  chatIcon = obj.firstChild.nextSibling;	
	var chatIconSrc = '';
	if(chatIcon != null && chatIcon.tagName == 'IMG')
	{
		chatIconSrc = new String(chatIcon.src);
	}
  if(dir == 'out')
  {
    flavor = '';
    obj.style.color = '';
    if(chatIconSrc != '' && chatIconSrc != null)
    {
      chatIcon.src = chatIconSrc.replace('_orgbkgd','');
    }
  }
  else
  {
    flavor = '#ffcc00';
    obj.style.color = 'Blue';
    if(chatIconSrc != '' && chatIconSrc != null)
    {
      chatIcon.src = chatIconSrc.replace('sm.gif','sm_orgbkgd.gif');
    }
  }
  obj.style.backgroundColor = flavor;  
}

function OpenChat(accountnumber,balance)
{
  account = accountnumber; //update globals
  credits = balance; //update globals
  var creditsDiv = document.getElementById('buycredits');
  if(credits > 0)
  {
    if(creditsDiv.style.display == 'block')
    {//if the credit div was showing, hide it if they have some
      creditsDiv.style.display = 'none';
    }
    var chatPath = '../chat/' + account + '.htm';
    features='height=500,width=600,menubar=0,personalbar=0,resizable=1,scrollbars=0,status=0,titlebar=0,toolbar=0';
    window.open(chatPath,"chatwindow" + accountnumber,features);
    inChat = true;
    closeRequests();
  }
  else
  {   //no credits - give opp to buy some
    var top = 0;
    top = GetScrollY();
    creditsDiv.style.top = top + 'px';
    creditsDiv.style.display = 'block';
  }
}

function hideBuyCredits()
{
  document.getElementById('buycredits').style.display = 'none';
}

function buycredits()
{
  parent.top.location = '../login/sendtossl.asp?destination=credits';
}

function ResetTimer(desiredtime)
{//start a new timer to do something faster (like reload)
  clearTimeout(chatRequestsTimerId);
  chatRequestsTimerId= 0;
  timertime = desiredtime; 
	ChatRequestsTimer();
}

function viewProfile(account)
{//load profile in main frame 
	try
	{	
		if(curPage.indexOf('searches/') == -1 && curPage.indexOf('email/') == -1)
		{
			top.location = '../blackbook/default.asp?toid=' +	account;
		}	
		else
		{
			var navMainLocal = new String(parent.NavMain.location);
			navMainLocal = navMainLocal.toLowerCase();	
			if (!(navMainLocal.indexOf('profile.aspx') > -1 && navMainLocal.indexOf(account) > -1))
			{	
				top.frames['NavMain'].location = '../profile/profile.aspx?toid=' + account;
			}	
		}			
	}
	catch(e){}	
}

function noThanks(account)
{
  try
  {   //send a message back to the girl then reload the list without her
   var url = '../chat/ChatNoThanks.aspx?Girl=' + account + '&id=' + Math.random(); 
    if(window.XMLHttpRequest)
    {
      xmlhttpnoThanks = new XMLHttpRequest();
      with(xmlhttpnoThanks)
      {
        onreadystatechange = ReloadChatRequests;open("GET",url,true);send(null);
      }
    }
    else if (window.ActiveXObject)
    {
      xmlhttpnoThanks = new ActiveXObject("Microsoft.XMLHTTP");
      if (xmlhttpnoThanks)
      {
        with(xmlhttpnoThanks)
        {
          onreadystatechange = ReloadChatRequests;open("GET",url,true);send();
        }
      }
    }
  }
  catch(e){}
}

function ReloadChatRequests()
{//;use this to do a quick reload instead of waiting on the timer to refire
	try
  {
    if(xmlhttpnoThanks.readyState == 4)
    {
      if(xmlhttpnoThanks.status == 200)
      {ResetTimer(500);}
		  }
	 }catch(e){} 
}

function closeRequests()
{//close the list - start showing again in 30 secs
  document.getElementById('chatrequests').style.display = 'none';
  ResetTimer(60000); 
}

function UserActive(obj, dir)
{
	if(posx < 200 && posy < 415)
	{userActive = true;}
	else
	{userActive = false;}			
}

function GetHeight()
{
	var scrollHeight = 	document.documentElement.clientHeight;
	return scrollHeight;
}

function HostessTimer()
{
  if(userid != null)
  {
    guy = userid;
    t = 60000;
    hostessChatCheckerID = setTimeout('CheckForHostessChatRequest()', t);
  }
}

function CheckForHostessChatRequest()
{
  try
  {
    if(parent.inChat == false || parent.inChat == null)
    {
      var url = '../hostesschat/HostessCheckForRequests.aspx?guy=' + guy + '&id=' + Math.random();
      if(window.XMLHttpRequest)
      {
        xmlhttpGetHostessChatRequest = new XMLHttpRequest();
        with(xmlhttpGetHostessChatRequest)
        {
          onreadystatechange = ShowHostess;open("GET",url,true);send(null);
        } 
      }
      else if (window.ActiveXObject)
      {
        xmlhttpGetHostessChatRequest = new ActiveXObject("Microsoft.XMLHTTP");
        if (xmlhttpGetHostessChatRequest)
        {
          with(xmlhttpGetHostessChatRequest)
          {
            onreadystatechange = ShowHostess;open("GET",url,true);send();
          }
        }
      }
    }
  }
  catch(e){}
  HostessTimer(); 
}

function ShowHostess()
{
  try
  {
    if (xmlhttpGetHostessChatRequest.readyState == 4)
    {
      if (xmlhttpGetHostessChatRequest.status == 200)
      {
        var theResponse = xmlhttpGetHostessChatRequest.responseText;
        if(theResponse == '1'){GetHostess();}
      }
    }
  }catch(e){}
}

function OpenVideo(accountnumber,balance)
{
  account = accountnumber; //update globals
  credits = balance; //update globals
  var creditsDiv = document.getElementById('buycredits');
  if(credits > 0)
  {
    if(creditsDiv.style.display == 'block')
    {//if the credit div was showing, hide it if they have some
      creditsDiv.style.display = 'none';
    }
    // var chatPath = '../im/' + account + 'v1.htm';
    var chatPath = '../chat/v' + account + '.htm';
    features = 'height=500,width=600,menubar=0,personalbar=0,resizable=0,scrollbars=0,status=0,titlebar=0,toolbar=0,location=0';
    window.open(chatPath,"chatwindow" + accountnumber,features);
    inChat = true;
    closeRequests();
  }
  else
  {   //no credits - give opp to buy some
    var top = 0;
    top = GetScrollY();
    creditsDiv.style.top = top + 'px';
    creditsDiv.style.display = 'block';
  }
}

var chatAlertWindowOpened = null;
function SpecialChatAlertGirlCheck()
{
  var chatGirl = GetCookieValue('SpecialChatAlertGirl');
  if(chatGirl != null && chatGirl != '' && chatGirl != 0 && chatGirl != '0')
  {
    if(document.getElementsByTagName)
    {
      var tdItems = document.getElementById('chatrequestlist').getElementsByTagName('TD');
      for(var i = 0; i < tdItems.length; i++)
      {
        var theID = new String(tdItems[i].id);
        if(theID == chatGirl)
        {
          var herName = tdItems[i].innerHTML;
          var chatWindowName = 'SpecialChatAlert' + chatGirl;
          chatwindowpath = '../chat/' + chatGirl + '-alert-' + herName + '.htm';
          features = 'height=100,width=250,menubar=0,personalbar=0,resizable=0,scrollbars=0,status=0,titlebar=0,toolbar=0,location=0';
          chatAlertWindow = window.open(chatwindowpath,chatWindowName,features);
          chatAlertWindow.focus();
          return; 
        }
      }
    }
  }
}

function LeaveChatHandler(girl)
{
  try
  {
    var url ='../chat/ChatClear.aspx?girl=' + girl + '&id=' + Math.random();
    if(window.XMLHttpRequest)
    {
      xmlhttpLeave = new XMLHttpRequest();
      with(xmlhttpLeave) 
      { 
        onreadystatechange = EvalDeparture;open("GET",url,false);send(null);
      }
    }
    else if (window.ActiveXObject)
    {
      xmlhttpLeave = new ActiveXObject("Microsoft.XMLHTTP");
      if (xmlhttpLeave)
      {
        with(xmlhttpLeave) 
        {  
          onreadystatechange = EvalDeparture;open("GET",url,true);send();
         }
       }
    }
	}
	catch(e){}
}

function EvalDeparture()
{
  if(xmlhttpLeave.readyState == 4)
  {
    if (xmlhttpLeave.status == 200)
    {
      clearTimeout(chatRequestsTimerId);
      inChat = false;
      timertime	= 250;
      ChatRequestsTimer();
    }
  }
}
