var IE = false;
var oCallImg;
if (navigator.appName == "Microsoft Internet Explorer" ) IE = true;


function getTxtFromElm(oThis) {
    if (typeof(oThis.innerText) == 'undefined') {
	if (oThis.firstChild) {
	    return oThis.firstChild.nodeValue
	} else {
	    return "";
	}
    } else {
	return oThis.innerText
    }
}

function setTxtToElm(oThis, txt) {
    if (typeof(oThis.innerText) == 'undefined' && oThis.firstChild) {
	oThis.firstChild.nodeValue = txt;
    } else {
	oThis.innerText = txt;
    }
}

var isLoading=false;
var isQueue = false;
var loadQueue = new Array();
var ocallUrl_timer;
var ocallUrl_timer_onload;

function callUrl_onload() {
    if (oCallImg.src == 'about:blank')
	return;
    if (ocallUrl_timer_onload) {
	window.clearTimeout(ocallUrl_timer_onload);
	ocallUrl_timer_onload = "";
    }
    isLoading=false;
    if (isQueue) {
	window.clearTimeout(ocallUrl_timer);
        callUrl_Queue();
    }
//    alert("onload=" + oCallImg.src);
}

function callUrl_Queue() {
    if (!isLoading) {
	for (var z=0; z<loadQueue.length;z++) {
	    if (loadQueue[z] != '') {
		callUrl(loadQueue[z]);
		loadQueue[z] = '';
		ocallUrl_timer = window.setTimeout("callUrl_Queue()", 200);
		return;
	    }
	}
	isQueue = false;
    } else {
    	ocallUrl_timer = window.setTimeout("callUrl_Queue()", 200);
    }
}

function encode(text) {
    return encodeURIComponent(text);
    text = text.replace(/&/g, "%26");
    text = text.replace(/=/g, "%3D");
    text = text.replace(/#/g, "%23");
    text = text.replace(/\//g, "%2F");
    if (IE) {
	return text;
    } else {
	return escape(text);
    }
}

function callUrl(url) {
    if (typeof(oCallImg) == 'undefined') {
        oCallImg = document.createElement("img");
        oCallImg.style.display = "none";
	oCallImg.src = "about:blank";
	oCallImg.onload = callUrl_onload;
	oCallImg.onerror = callUrl_onload;
        document.getElementsByTagName("body")[0].appendChild(oCallImg);
    }
//    url = url.replace(/%/g, "%25");
    if (url.indexOf("?") == -1) {
	url += "?rille=" + (new Date()).getTime();
    } else {
	url += "&rille=" + (new Date()).getTime();
    }
    if (isLoading) {
	loadQueue[loadQueue.length] = url;
	if (!isQueue) {
	    ocallUrl_timer = window.setTimeout("callUrl_Queue()", 200);
	    isQueue = true;
//	    alert("queue");
	}
    } else {
	isLoading=true;
	oCallImg.src = url;
	ocallUrl_timer_onload = window.setTimeout('callUrl_onload()', 5000);
    }
}

function getMargin(oTo) {
    var parent = oTo;
    var margin = new Object();
    margin.left = 0;
    margin.top = 0;
    while (parent) {
        margin.left += parent.offsetLeft;
        margin.top += parent.offsetTop;
        parent = parent.offsetParent;	
    }
    return margin;
}

function getMousePos(event) {
    var pos = new Object();
    if (IE) {
        pos.x = event.clientX;
        pos.y = event.clientY;
    } else {
        pos.x = event.pageX;
        pos.y = event.pageY;
    }
    return pos;
}

function gdate(t) {
    var tObj = new Date();
    tObj.setTime(t);
    return f2z(tObj.getFullYear()) + "-" + f2z(tObj.getMonth()) + "-" + f2z(tObj.getDate()) + " "  +
        f2z(tObj.getHours()) + ":" + f2z(tObj.getMinutes()) + ":" + f2z(tObj.getSeconds());
}
    
function f2z(i) {
    if (i < 10) {
        i = "0" + i;
    }
    return i;
}

    function gdate_parse(text) {
	// 2008-09-02 21:30:12
	if (!text) {return "??"}
	var oTime = new Date();
	oTime.setFullYear(parseInt(text.substr(0,4), 10));
	oTime.setMonth(parseInt(text.substr(5,2), 10));
	oTime.setDate(parseInt(text.substr(8,2), 10));

	oTime.setHours(parseInt(text.substr(11,2), 10));
	oTime.setMinutes(parseInt(text.substr(14,2), 10));
	oTime.setSeconds(parseInt(text.substr(17,2), 10));
	return oTime.getTime();
    }
    
    function time_diff(t) {
	if (t == 0) {
	    return "jetzt";
	} else if (t <0) {
	    return "vorhins";
	}
	var iT = t/1000;

	var iSec = iT % 60;
	iT = (iT-iSec) / 60;

	var iMin = iT % 60;
	iT = (iT-iMin) / 60;

	var iHour = iT % 24;
	iT = (iT-iHour) / 24;

	var iDay = iT % 7;
	iT = (iT-iDay) / 7;
	
	var iWeek = iT;
	
	var text = "";
	
	if (iWeek > 0) { text += (text.length == 0 ? "" : " " ) + f2z(iWeek) + " Woche" + (iWeek == 0 ? "" : "n")}
	if (iDay > 0) { text += (text.length == 0 ? "" : " " ) + f2z(iDay) + " Tag" + (iDay == 0 ? "" : "n")}
	if (iHour > 0) { text += (text.length == 0 ? "" : " " ) + f2z(iHour) + " Stunde" + (iHour == 0 ? "" : "n")}
	if (iMin > 0) { text += (text.length == 0 ? "" : " " ) + f2z(iMin) + " Minute" + (iMin == 0 ? "" : "n")}
	if (iSec > 0) { text += (text.length == 0 ? "" : " " ) + f2z(Math.round(iSec)) + " Sekunde" + (iSec == 0 ? "" : "n")}

	return text;
    }

    function get_url_json(url, func_resp) {
        new Ajax.Request(url, {
                method:'get',
                asynchronous:'true',
                contentType:"application/json",
                onSuccess: function(transport){
                    var response = transport.responseText;
                    if (response) {
                        var json = transport.responseText.evalJSON();
                        if(json.executeError) {
                            eval(func_resp + "(-1, null, 'json parse error')");
                        } else {
                            eval(func_resp + "(1, json)");
                        }
                    } else {
                        eval(func_resp + "(-1, null, 'empty response')");
                    }

                },
                onFailure: function() {
                    eval(func_resp + "(-1, null, 'request-error')");
                }
            });
    }
