//vars
var HCcheckNewMessageURL = "http://www.hanuma.ru/cgi-bin/livechat/getpopup.cgi";
var HCblockPopUpURL = "http://www.hanuma.ru/cgi-bin/livechat/blockpopup.cgi";
var HCcheckNewPopupURL = "http://www.hanuma.ru/livechat/popup/";
var HCcookieIDName = "HCLOGIN";
var HCcookieSesName = "HCSESID";
var HCSesID = 0;
if(!HCsrg){HCsrg = 0;}
if(!HCcul){HCcul = 0;}
if(!HCwaittime){HCwaittime = 5000;}
var HCcurIntervalID = 0;
var HCTimeElapse = 0;
var HConFocus = 0;
var HCMainWindowID = "HCContactChatWindow";
var HCMainWindowHeight = 0;
var HCSubWindowArray = new Array();
if(!HCSubWindowVisibleMax){HCSubWindowVisibleMax=3;}
var HCPhotoServerIP = "77.222.56.60";
var HCCSSlink = "http://www.hanuma.ru/livechat/css/chatPopUp.css?6-08-2";
var HCButtonImglink = "http://www.hanuma.ru/livechat/pic/startchatbutton.gif";
var HCfixedWindow;
var HCtimePeriod = HCwaittime;
var HCnumIter = 0;
var HCSubWindowHeight = 163;

function setHCWindowCookie (name, value, expires, path, domain, secure){
  document.cookie = name + "=" + escape(value) +
    ((expires) ? "; expires=" + expires : "") +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    ((secure) ? "; secure" : "");
}

function getHCWindowCookie(name){
	var cookie = " " + document.cookie;
	var search = " " + name + "=";
	var setStr = null;
	var offset = 0;
	var end = 0;
	if (cookie.length > 0) {
		offset = cookie.indexOf(search);
		if (offset != -1) {
			offset += search.length;
			end = cookie.indexOf(";", offset)
			if (end == -1) {
				end = cookie.length;
			}
			setStr = unescape(cookie.substring(offset, end));
		}
	}
	return(setStr);
}

function loadHCStyleSheet(href,media){
  var headID = document.getElementsByTagName("head")[0];         
  var cssNode = document.createElement('link');
  cssNode.type = 'text/css';
  cssNode.rel = 'stylesheet';
  cssNode.href = href;
  cssNode.media = media;
  headID.appendChild(cssNode);
}

function createHCDiv(id, html, width, height, position, left, top, right, bottom, background, border, padding, margin, visibility){
var newdiv = document.createElement('div');
newdiv.setAttribute('id', id);
if(width){newdiv.style.width = width;}
if(height){newdiv.style.height = height;}
if(position){newdiv.style.position = position;}
if(left){newdiv.style.left = left;}
if(top){newdiv.style.top = top;}
if(right){newdiv.style.right = right;}
if(bottom){newdiv.style.bottom = bottom;}
if(background){newdiv.style.background = background;} 
if(border){newdiv.style.border = border;}
if(padding){newdiv.style.padding = padding;}
if(margin){newdiv.style.margin = margin;}
if(visibility){newdiv.style.visibility = visibility;}
if(html){newdiv.innerHTML = html;}
document.body.appendChild(newdiv);
}

function closeHCMainWindow(){
  var HCmainDiv = document.getElementById(HCMainWindowID);
      
  if(HCmainDiv != null)
  {
    for(var swa=0; swa<HCSubWindowArray.length; swa++)
    {
      var HCsubDiv = document.getElementById("ses"+HCSubWindowArray[swa]);
      if(HCsubDiv != null){HCsubDiv.style.display = "none";}
    }
    HCSubWindowArray = [];
    HCMainWindowHeight = 23;
    HCmainDiv.style.height = HCMainWindowHeight+"px";
    HCmainDiv.style.visibility = "hidden";
  }
}

function blockHCLadyWindow(girlid){
  //check cookie for ID and get new message if exist ID:1-12345
  var IDdata = getHCWindowCookie(HCcookieIDName);
  var IDdataArray = new Array;
  if(IDdata){IDdataArray = IDdata.split(/-/);}
  
  if(IDdataArray[0]>0 && IDdataArray[1]>0)
  {
    var xmlhttpbpu = new CrossXHR();
  
    xmlhttpbpu.onreadystatechange = function(){
  	if(xmlhttpbpu.readyState==4){
  	   if(xmlhttpbpu.status == 200){
  	     //remove lady block
  	     var HCsubDiv = document.getElementById("ses"+girlid);
  	     if(HCsubDiv && HCsubDiv.parentNode && HCsubDiv.parentNode.removeChild){
  			    HCsubDiv.parentNode.removeChild(HCsubDiv);}
  	   }
  		}
  	}
    
    zeit = new Date();
  	ms = (zeit.getHours() * 24 * 60 * 1000) + (zeit.getMinutes() * 60 * 1000) + (zeit.getSeconds() * 1000) + zeit.getMilliseconds();
    
    var rurl = HCblockPopUpURL+"?s="+IDdataArray[0]+"&m="+IDdataArray[1]+"&g="+girlid+"&x="+ms;
  	
    xmlhttpbpu.open('GET', rurl);
    xmlhttpbpu.send(); 
  }
  
  //hide lady block
  for(var swa=0; swa<HCSubWindowArray.length; swa++)
  {
    if(HCSubWindowArray[swa]==girlid)
    {
      var HCsubDiv = document.getElementById("ses"+HCSubWindowArray[swa]);
      if(HCsubDiv != null){
      HCsubDiv.style.display = "none";
      }
      HCSubWindowArray.splice(swa,1);
      HCMainWindowHeight -= HCSubWindowHeight;
      var HCmainDiv = document.getElementById(HCMainWindowID);
      if(HCmainDiv != null)
      {
        HCmainDiv.style.height = HCMainWindowHeight+"px";
        if(HCSubWindowArray.length == 0){HCmainDiv.style.visibility = "hidden";}
      }
    }
  }
}

function showHCWindow(responseText,siteid,menid){
  if(responseText)
  {
    if(!siteid){siteid = 0;}
    if(!menid){menid = 0;}
    var mgArray = responseText.split(/\|/);
    
    var HCsubDiv = document.getElementById("ses"+mgArray[0]);
    
    if(HCsubDiv == null)
    {
      var HCmainDiv = document.getElementById(HCMainWindowID);
      
      if(HCmainDiv != null && HCSubWindowArray.length<HCSubWindowVisibleMax)
      {
        var HCtarget = "_self";
        if(HCcul==1){HCtarget = "_blank";}
        HCSubWindowArray.push(mgArray[0]);
        HCMainWindowHeight += HCSubWindowHeight;
        HCmainDiv.style.height = HCMainWindowHeight+"px";
        var HCTempLadyProfileLink = HCLadyProfileLink; 
        HCTempLadyProfileLink = HCTempLadyProfileLink.replace("#", mgArray[0]);
        HCmainDiv.innerHTML += "<div class='HCSubChatDiv' id='ses"+mgArray[0]+"'>"+
        "<a href='"+HCTempLadyProfileLink+"' target='"+HCtarget+"'><img src='http://"+HCPhotoServerIP+"/photo/"+mgArray[1]+"/"+mgArray[0]+"sb.jpg'></a>"+
        "<p><a href='"+HCTempLadyProfileLink+"' target='"+HCtarget+"'>"+mgArray[2]+"</a><em><a href='#block "+mgArray[0]+"' onClick=\"blockHCLadyWindow("+mgArray[0]+");return false;\">[block]</a></em></p>"+
        "<p><strong>ID: </strong>"+mgArray[0]+"</p>"+
        "<p><strong>Age: </strong>"+mgArray[6]+"</p>"+
        "<p><strong>City: </strong>"+mgArray[4]+"</p>"+
        "<p><strong>Country: </strong>"+mgArray[3]+"</p>"+
        "<p><span><b id='HCSubDivCounter"+mgArray[0]+"'></b></span></p>"+
        "<span><a href='#"+mgArray[0]+"' onClick=\"window.open('/cgi-bin/livechat/livechat.cgi?siteid="+siteid+"&amp;menid="+menid+"&amp;girlid="+mgArray[0]+"','livechatwindow','location=0,status=0,scrollbars=0,resizable=0,width=780,height=510');\"><img src='"+HCButtonImglink+"' /></a></span>"
        +"</div>";
        //create new var
        if(mgArray[7]){
        mgArray[7]=mgArray[7]*3;
        var HCSubDivCounter = document.getElementById("HCSubDivCounter"+mgArray[0]);
        if(HCSubDivCounter != null){HCSubDivCounter.style.width = (120-mgArray[7])+"px";}
        }
        else{mgArray[7]=0;}
        window["HCSubDivCounter"+mgArray[0]] = (120-mgArray[7]);
        HCmainDiv.style.visibility = "visible";
      }
    }
  }
}

function ajax_checkNewHCPopup(siteid,menid,sesid){
  var xmlhttppu = new CrossXHR();
  
  xmlhttppu.onreadystatechange = function(){
	if(xmlhttppu.readyState==4){
	   if(xmlhttppu.status == 200){
  	   var ispopup = parseInt(xmlhttppu.responseText);
  	   if(ispopup==1){ajax_checkNewHCMessage(siteid,menid,sesid);}
	   }
		}
	}
  
  zeit = new Date();
	ms = (zeit.getHours() * 24 * 60 * 1000) + (zeit.getMinutes() * 60 * 1000) + (zeit.getSeconds() * 1000) + zeit.getMilliseconds();
  
  var rurl = HCcheckNewPopupURL+siteid+"-"+menid+".html?ms="+ms;
	
  xmlhttppu.open('GET', rurl);
  xmlhttppu.send();
}

function ajax_checkNewHCMessage(siteid,menid,sesid){
  var xmlhttpnm = new CrossXHR();
  
  xmlhttpnm.onreadystatechange = function(){
	if(xmlhttpnm.readyState==4){
	   if(xmlhttpnm.status == 200){
        ///show window with timer
        if(xmlhttpnm.responseText)
        {
          var mgHCWindowArray = xmlhttpnm.responseText.split(/<\+>/);
          for(var hcw=0; hcw<mgHCWindowArray.length; hcw++)
          {if(mgHCWindowArray[hcw]){showHCWindow(mgHCWindowArray[hcw],siteid,menid);}}
          HCtimePeriod=HCwaittime;
        }
      }
		}
	}
	
	zeit = new Date();
	ms = (zeit.getHours() * 24 * 60 * 1000) + (zeit.getMinutes() * 60 * 1000) + (zeit.getSeconds() * 1000) + zeit.getMilliseconds();
	
	var rurl = HCcheckNewMessageURL+"?siteid="+siteid+"&menid="+menid+"&sesid="+sesid+"&mg=m&srg="+HCsrg+"&cul="+HCcul+"&x="+ms;
	
  xmlhttpnm.open('GET', rurl);
  xmlhttpnm.send();
}

function checkNewHCMessage(){
    //check cookie for ID and get new message if exist ID:1-12345
    var IDdata = getHCWindowCookie(HCcookieIDName);
    var IDdataArray = new Array;
    if(IDdata){IDdataArray = IDdata.split(/-/);}
    
    //get ses
    var HCSesID = getHCWindowCookie(HCcookieSesName);
    if(HCSesID || HCSesID != null || HCSesID != undefined){HCSesID = parseInt(HCSesID);}
    else{HCSesID = 0;}

    //make ses id
    if(HCSesID == 0)
    {
      var zeit = new Date();
      var ms = (zeit.getHours() * 24 * 60 * 1000) + (zeit.getMinutes() * 60 * 1000) + (zeit.getSeconds() * 1000) + zeit.getMilliseconds();
      HCSesID = ms;
  		zeit.setTime(zeit.getTime()+(1*24*60*60*1000));
  		var expires = "; expires="+zeit.toGMTString();
  		var domain = window.location.hostname;
  		if(domain.search(/^www/)>-1){domain = domain.replace(/^www/, "");}
  		else{domain = "."+domain;}
      setHCWindowCookie(HCcookieSesName,HCSesID,expires,"/",domain);
    }
    
    if(IDdataArray[0]>0 && IDdataArray[1]>0 && HCSesID>0)
    {ajax_checkNewHCPopup(IDdataArray[0],IDdataArray[1],HCSesID);}
}

function timeHCProcessing(){
  HCTimeElapse += 1000;
  
  //set new counter data
  for(var swa=0; swa<HCSubWindowArray.length; swa++)
  {
    if(window["HCSubDivCounter"+HCSubWindowArray[swa]]>0)
    {
      window["HCSubDivCounter"+HCSubWindowArray[swa]]-=3;
      var HCSubDivCounter = document.getElementById("HCSubDivCounter"+HCSubWindowArray[swa]);
      if(HCSubDivCounter != null){HCSubDivCounter.style.width = window["HCSubDivCounter"+HCSubWindowArray[swa]]+"px";}
    }
    else
    {
      var HCsubDiv = document.getElementById("ses"+HCSubWindowArray[swa]);
      if(HCsubDiv != null){
      HCsubDiv.style.display = "none";
      if(HCsubDiv && HCsubDiv.parentNode && HCsubDiv.parentNode.removeChild){
				  HCsubDiv.parentNode.removeChild(HCsubDiv);
			  }
      }
      HCSubWindowArray.splice(swa,1);
      HCMainWindowHeight -= HCSubWindowHeight;
      var HCmainDiv = document.getElementById(HCMainWindowID);
      if(HCmainDiv != null)
      {
        HCmainDiv.style.height = HCMainWindowHeight+"px";
        if(HCSubWindowArray.length == 0){HCmainDiv.style.visibility = "hidden";}
      }
    }
  }
  
  //get new pop up
  if(HCTimeElapse == HCtimePeriod)
  {
    HCTimeElapse = 0;
    HCnumIter += 1;
    if(HCnumIter == (HCtimePeriod/1000)){HCtimePeriod+=1000;HCnumIter=0;}
    if(HConFocus == 1 && HCSubWindowArray.length<HCSubWindowVisibleMax){checkNewHCMessage();}
  }
}

function startHCWindowInterval(){
  //Set Interval, set cookie
  HCcurIntervalID = window.setInterval("timeHCProcessing()", 1000);//every second
}

window.onblur=function(e){
  if(HConFocus==1){HConFocus = 0;}
}

window.onfocus=function(e){
  if(HConFocus==0){HConFocus = 1;HCtimePeriod = HCwaittime;}
}

window.onload=function(e){
if(HConFocus == 0){HConFocus = 1;}

startHCWindowInterval();

var html = "";
var width = "276px";
var height = "10px";
var left = 0;
var top = 0;
var right = "10px";
var bottom = "10px";

loadHCStyleSheet(HCCSSlink,"screen");
createHCDiv(HCMainWindowID,html,width,height,"absolute",left,top,right,bottom,0,0,0,0,"hidden");
//create header
var HCmainDiv = document.getElementById(HCMainWindowID);    
if(HCmainDiv != null)
{
  var HCmainDivTitle = "These ladies want to chat NOW!";
  HCMainWindowHeight += 19;
  HCmainDiv.style.height = HCMainWindowHeight+"px";
  HCmainDiv.innerHTML = "<h2><span>"+HCmainDivTitle+"</span><img class='HCContactChatWindowClose' src='http://www.hanuma.ru/livechat/pic/close.gif' onClick=\"closeHCMainWindow();\" alt='Close' /></h2>";
}

HCfixedWindow = 
{
    hasInner: typeof(window.innerWidth) == 'number',
    hasElement: document.documentElement != null
       && document.documentElement.clientWidth,

    menu: document.getElementById
        ? document.getElementById(HCMainWindowID)
        : document.all
          ? document.all[HCMainWindowID]
          : document.layers[HCMainWindowID]
};

HCfixedWindow.computeShifts = function()
{
    HCfixedWindow.shiftX = HCfixedWindow.hasInner
        ? pageXOffset
        : HCfixedWindow.hasElement
          ? document.documentElement.scrollLeft
          : document.body.scrollLeft;
    if (HCfixedWindow.targetLeft > 0)
        HCfixedWindow.shiftX += HCfixedWindow.targetLeft;
    else
    {
        HCfixedWindow.shiftX += 
            (HCfixedWindow.hasElement
              ? document.documentElement.clientWidth
              : HCfixedWindow.hasInner
                ? window.innerWidth - 20
                : document.body.clientWidth)
            - HCfixedWindow.targetRight
            - HCfixedWindow.menu.offsetWidth;
    }

    HCfixedWindow.shiftY = HCfixedWindow.hasInner
        ? pageYOffset
        : HCfixedWindow.hasElement
          ? document.documentElement.scrollTop
          : document.body.scrollTop;
    if (HCfixedWindow.targetTop > 0)
        HCfixedWindow.shiftY += HCfixedWindow.targetTop;
    else
    {
        HCfixedWindow.shiftY += 
            (HCfixedWindow.hasElement
            ? document.documentElement.clientHeight
            : HCfixedWindow.hasInner
              ? window.innerHeight - 20
              : document.body.clientHeight)
            - HCfixedWindow.targetBottom
            - HCfixedWindow.menu.offsetHeight;
    }
};

HCfixedWindow.moveMenu = function()
{
    HCfixedWindow.computeShifts();

    if (HCfixedWindow.currentX != HCfixedWindow.shiftX
        || HCfixedWindow.currentY != HCfixedWindow.shiftY)
    {
        HCfixedWindow.currentX = HCfixedWindow.shiftX;
        HCfixedWindow.currentY = HCfixedWindow.shiftY;

        if (document.layers)
        {
            HCfixedWindow.menu.left = HCfixedWindow.currentX;
            HCfixedWindow.menu.top = HCfixedWindow.currentY;
        }
        else
        {
            HCfixedWindow.menu.style.left = HCfixedWindow.currentX + 'px';
            HCfixedWindow.menu.style.top = HCfixedWindow.currentY + 'px';
        }
    }

    HCfixedWindow.menu.style.right = '';
    HCfixedWindow.menu.style.bottom = '';
};

HCfixedWindow.floatMenu = function()
{
    HCfixedWindow.moveMenu();
    setTimeout('HCfixedWindow.floatMenu()', 1);
};

// addEvent
HCfixedWindow.addEvent = function(element, listener, handler)
{
    if(typeof element[listener] != 'function' || 
       typeof element[listener + '_num'] == 'undefined')
    {
        element[listener + '_num'] = 0;
        if (typeof element[listener] == 'function')
        {
            element[listener + 0] = element[listener];
            element[listener + '_num']++;
        }
        element[listener] = function(e)
        {
            var r = true;
            e = (e) ? e : window.event;
            for(var i = 0; i < element[listener + '_num']; i++)
                if(element[listener + i](e) === false)
                    r = false;
            return r;
        }
    }

    //if handler is not already stored, assign it
    for(var i = 0; i < element[listener + '_num']; i++)
        if(element[listener + i] == handler)
            return;
    element[listener + element[listener + '_num']] = handler;
    element[listener + '_num']++;
};

HCfixedWindow.supportsFixed = function()
{
    var testDiv = document.createElement("div");
    testDiv.id = "testingPositionFixed";
    testDiv.style.position = "fixed";
    testDiv.style.top = "0px";
    testDiv.style.right = "0px";
    document.body.appendChild(testDiv);
    var offset = 1;
    if (typeof testDiv.offsetTop == "number"
        && testDiv.offsetTop != null 
        && testDiv.offsetTop != "undefined")
    {
        offset = parseInt(testDiv.offsetTop);
    }
    if (offset == 0)
    {
        return true;
    }

    return false;
};

HCfixedWindow.init = function()
{
    if (HCfixedWindow.supportsFixed())
        HCfixedWindow.menu.style.position = "fixed";
    else
    {
        var ob = 
            document.layers 
            ? HCfixedWindow.menu 
            : HCfixedWindow.menu.style;

        HCfixedWindow.targetLeft = parseInt(ob.left);
        HCfixedWindow.targetTop = parseInt(ob.top);
        HCfixedWindow.targetRight = parseInt(ob.right);
        HCfixedWindow.targetBottom = parseInt(ob.bottom);

        if (document.layers)
        {
            menu.left = 0;
            menu.top = 0;
        }
        HCfixedWindow.addEvent(window, 'onscroll', HCfixedWindow.moveMenu);
        HCfixedWindow.floatMenu();
    }
};

HCfixedWindow.init();
}
