﻿var xmlReq;
var callback;
var exceptionCallback;
function updateInfo(cb, excb) 
{    
    callback = cb;
    exceptionCallback = excb;
    try
    {
        xmlReq = new XMLHttpRequest();
        xmlReq.onreadystatechange = loadCheck;
     //   xmlReq.open("GET", "http://localhost:57845/FeelRO/Statistics.ashx", true);
        xmlReq.open("GET", "http://feelro.net/Statistics.ashx", true);
        xmlReq.send(null);                 
    }
    catch(e)
    {
        //alert(e);
        exceptionCallback();
    }
}

function loadCheck() 
{
    if(xmlReq.readyState < 4) return;
    if(xmlReq.status == 200) 
    {        
        if(callback) callback();
    }
    else
    {
        //alert(xmlReq.status);
        exceptionCallback();
    }
    xmlReq.abort();
}
function cb()
{
    var text = xmlReq.responseText;
    var re = /Users\sonline:\s(.*)/;
    re.exec(text);
    var res = RegExp.$1;    
    if(res == "-1" ||  res == "-2") res = "---";
    onlineCounter.innerHTML = "Total: " + res;
    re = /Login\sserver:\s(.*)/;
    re.exec(text);
    res = RegExp.$1;
    if(res == "Online")
    {
        loginServIndicator.innerHTML = "Online";
        loginServIndicator.style.color = "#99FF33";
    }
    else
    {
        loginServIndicator.innerHTML = "Offline";
        loginServIndicator.style.color = "#990000";
    }
    re = /Char\sserver:\s(.*)/;
    re.exec(text);
    res = RegExp.$1;
    if(res == "Online")
    {
        charServIndicator.innerHTML = "Online";
        charServIndicator.style.color = "#99FF33";
    }
    else
    {
        charServIndicator.innerHTML = "Offline";
        charServIndicator.style.color = "#990000";
    }
    re = /Map\sserver:\s(.*)/;
    re.exec(text);
    res = RegExp.$1;
    if(res == "Online")
    {
        mapServIndicator.innerHTML = "Online";
        mapServIndicator.style.color = "#99FF33";
    }
    else
    {
        mapServIndicator.innerHTML = "Offline";
        mapServIndicator.style.color = "#990000";
    }
    setTimeout('updateInfo(callback, exceptionCallback)', 30000);
}
function excb()
{
    setTimeout('updateInfo(callback, exceptionCallback)', 30000);
}


     
//<!-- TODO:window implement window.onresize = setTable-->;
function setTable()
{
    var fullScreenH = document.getElementById('fullScreenDiv').clientHeight;
    var h = document.getElementById('content').clientHeight;    
    var cell = document.getElementById('tableCell');  
    //alert(fullScreenH + " " + h + " " + cell.clientHeight);
    if(cell.clientHeight < h)
    {
        document.getElementById('table').style.height = "auto"; 
        cell.style.height = h + "px";                
    }
    // for lower screen resolutions - to extend it far then left menu
    if(cell.clientHeight < 650)
    {
       
        document.getElementById('table').style.height = "auto"; 
        cell.style.height = "650px";
        return;
    }
    
    // for ie that extends it too far (TODO: still doesn't work good)
    if(cell.clientHeight == fullScreenH)
    {
        document.getElementById('table').style.height = "auto"; 
        cell.style.height = Math.max(Math.max(fullScreenH - 299 - 89 - 38, 650),h) + "px";
    }
    
}
     
//Soft scrolling
var x = 0, minX, maxX, stopX, speed = 0;
var ds = 0;
var scrollingTarget;
var on = false;
var interval;

var additionalScroll = false;
var pressing = false;

function Start(dir)
{    
    if(!on)
    {     
        x = RemovePx(scrollingTarget.style.left);   
        if((x >= maxX && dir == 1) || (x <= minX && dir == -1)) return;
        on = true;        
        //additionalScroll = false;
        ds = dir;
        stopX = x + 50*ds;
        interval = setInterval("ControlSpeed();Move();", 20);
    }    
    //else
    //{
        
        //Pressed again, let's add
    //    additionalScroll = true;
   // }
}

function ControlSpeed()
{
    speed += ds; 
    //alert(speed.toFixed(1));       
    //if(speed > speedMax) speed = speedMax;
    //if(speed < -speedMax) speed = -speedMax;
    if(speed.toFixed(1) == 0) 
    {
        on = false;
        clearInterval(interval);
        if(additionalScroll || pressing) Start(-ds);
         
            
        //alert(x);
        
    }
   
}

function Move()
{
    x += speed;
    if((x > stopX && ds > 0) || (x < stopX && ds < 0))
        ds = -ds;
    scrollingTarget.style.left = Math.round(x) + "px";     
}

function RemovePx(str)
{
    var value = str.substr(0, str.length-2);
    return parseInt(value);    
}