var http = null;
var timeout = null;
var update_delay= 45; // in Sekunden
var update_delay_min=update_delay;
var update_delay_max=7*update_delay;
var switch_to_max=300; // in Sekunden, nach letzter Benutzeraktion
var countdown = update_delay;
var debug = false;

var tooltip = null;
var tooltip_timeout = null;
var x = null;
var y = null;
var window_width = null;
var last_action = null;

function createRequestObject() {
    var req = null;
    if (typeof XMLHttpRequest != "undefined") {
            req = new XMLHttpRequest();
    }  
	if (!req && typeof ActiveXObject != "undefined") {
	try {
            req=new ActiveXObject("Microsoft.XMLHTTP");
        } catch (e1) {
            try {
        	req=new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e2) {
                try {
                    req=new ActiveXObject("Msxml2.XMLHTTP.4.0");
                } catch (e3) {
                    req=null;
                }
            }
        }
    }

    return req;
}
					
http = createRequestObject();

function sendRequest(action, id) {
    try {
	if (http) {
	    http.open('get', '/?action='+action+'&id='+id);
	    if (action=="profile") {
			http.onreadystatechange = handleResponseProfile;
	    } else {
			http.onreadystatechange = handleResponse;
	    }
	    http.send(null);
	}
    } catch(e) {
	if (debug) alert("Fehler in sendRequest\n"+e);
    }
}
						    

function handleResponseProfile() {
    try {
    if (http && http.readyState == 4 && http.status == 200) {
	    var nodes  = http.responseXML;
	    var profile = nodes.getElementsByTagName("p")[0].firstChild.data;
	    showTooltip(profile);
    }
    } catch(e) {
	if (debug) alert("Fehler in handleResponseProfile\n"+e);
    }

}



function countDown() {
    clearTimeout(timeout);
    if (countdown == 0) {
	document.getElementById('ajax_noload').style.display='none';
	document.getElementById('ajax_load').style.display='inline';
	sendRequest('update', 0);
	countdown=update_delay;
    } else if (countdown < update_delay) {
	document.getElementById('ajax_load').style.display='none';
	document.getElementById('ajax_noload').style.display='inline';
    }
    document.getElementById('ajax_text').innerHTML='noch ' + countdown + ' Sek.';
    countdown--;
    checkLastAction();
    timeout = setTimeout('countDown()', 1000);
}

function doUpdate() {
    if (!http) return;
    clearTimeout(timeout);
    countdown=0;
    updateLastAction();
    countDown(); 
}

function initUpdate() {
    if (!http) {
		document.getElementById('ajax_text').innerHTML="nicht mglich!";
		return;
    }
    timeout = setTimeout('countDown()', 1000);
    initTooltip();
    updateLastAction();
}

function updateLastAction() {
    var date = new Date();
    last_action=date.getTime()/1000;
    update_delay=update_delay_min;
}

function checkLastAction() {
	var date = new Date();
	var test=date.getTime()/1000;
	if  (last_action+switch_to_max<test) {
		update_delay=update_delay_max;
	}
}

function initTooltip() {
    tooltip = document.getElementById('ajax_tooltip');
    document.onmousemove = updateTooltipPosition;
}

function showAjaxText(flag) {
    if (flag) {
	document.getElementById('ajax_text').style.display='inline';
    } else {
	document.getElementById('ajax_text').style.display='none';
    }    
}

function updateTooltipPosition(e) {
	try {
	var browser=navigator.userAgent.toLowerCase();
	if (document.all && browser.indexOf("opera")==-1 && browser.indexOf("konqueror")==-1 && browser.indexOf("safari")==-1) {
	    //x=window.event.clientX + document.body.scrollLeft;
	    //x=window.event.x + document.body.scrollLeft;
	    x=event.clientX;//+document.body.scrollLeft;
	    
	    y=window.event.y + document.body.scrollTop;
	    window_width=document.documentElement.clientWidth;
	} else {
	    x=e.pageX;
	    y=e.pageY;
	    window_width=window.innerWidth;
	}
	
	if (tooltip != null) {
	    if (x<window_width/2) {
		tooltip.style.left = (x + 20) + "px";
		tooltip.style.top = (y + 20) + "px";
	    } else {
		tooltip.style.left = (x - 180 - 20) + "px";
		tooltip.style.top = (y + 20) + "px";
	    }
	}
	} catch(e) {}
}


function showProfileTooltipSchmelz() {
    if (!http) return;
    showTooltip('<h1>Spielkönig Schmelz</h1><h1>Bistro LaKö Schmelz</h1><center><img src="images/locations/IMG_6809 [320x200].JPG" width="150" border="0" alt=""></center>');
}
function showProfileTooltipLebach() {
    if (!http) return;
    showTooltip('<h1>Spielkönig Lebach</h1><center><img src="images/locations/IMG_6830 [320x200].JPG" width="150" border="0" alt=""></center>');
}
function showProfileTooltipDillingen() {
    if (!http) return;
    showTooltip('<h1>Spielkönig Dillingen</h1><center><img src="images/locations/IMG_6896 [320x200].JPG" width="150" border="0" alt=""></center>');
}
function showProfileTooltipSaarlouis() {
    if (!http) return;
    showTooltip('<h1>Spielkönig Saarlouis</h1><center><img src="images/locations/IMG_6882 [320x200].JPG" width="150" border="0" alt=""></center>');
}
function showProfileTooltipMerzig() {
    if (!http) return;
    showTooltip('<h1>Spielkönig Merzig</h1><center><img src="images/locations/IMG_6910 [320x200].JPG" width="150" border="0" alt=""></center>');
}
function showProfileTooltipMettlach() {
    if (!http) return;
    showTooltip('<h1>Spielkönig Mettlach</h1><center><img src="images/locations/IMG_6929 [320x200].JPG" width="150" border="0" alt=""></center>');
}
function showProfileTooltipLosheim() {
    if (!http) return;
    showTooltip('<h1>Spielkönig Losheim</h1><center><img src="images/locations/IMG_6953 [320x200].JPG" width="150" border="0" alt=""></center>');
}
function showProfileTooltipSulzbach() {
    if (!http) return;
    showTooltip('<h1>Spielkönig Sulzbach I</h1><h1>Spielkönig Sulzbach II</h1><center><img src="images/locations/IMG_6866 [320x200].JPG" width="150" border="0" alt=""></center>');
}


function hideProfileTooltip(id) {
    clearTimeout(tooltip_timeout);
    http.abort();
    hideTooltip();
}


function showTooltip(msg) {
    try {    
    tooltip.style.display = "block";
    tooltip.innerHTML=msg;
    } catch(e) {}
}

function hideTooltip() {
    try {
    tooltip.style.display = "none";
    } catch(e) {}
}

