//The parameters passed to the function are as fallows:
//url - the url of the script that we wanna request. You can use either full or relative to your server urls.
//elmnts - this is either the id of the element that will contain the response from our request, or can also be a whole array of elements
//loadingMsg - if this parameter is set, after the request has been send, the content of the changed element(s) will be set to whatever this variable is set, i.e. "Loading! Please wait...".
function ajaxObj(url, elmnts, loadingMsg) {
    
    this.obj = new Object();
    this.url = url;
    this.loadInto = elmnts;
 
    if(loadingMsg) {
        if(typeof(this.loadInto) != 'object')
            document.getElementById(this.loadInto).innerHTML = loadingMsg;
        else {
            for(i in this.loadInto) {
                document.getElementById(this.loadInto[i]).innerHTML = loadingMsg;
            }
        }
    }
 
    //This prototype is used to create our request, send it and handle it
    this.init();
    
}

//This function tries if different objects are available, untill it finds one that works, cause all the major browsers use different techniques to send the request
ajaxObj.prototype.create = function() {

    try {
        xmlHttp = new XMLHttpRequest();
    } catch(e) {
        try {
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
        } catch(e) {
            try {
                xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
            } catch(e) {
                return false;
            }
        }
    } 
 
    this.obj = xmlHttp;
    
}

ajaxObj.prototype.handle = function() {
 
    var o = this.obj;
    var into = this.loadInto;
 
    o.onreadystatechange = function() {
    
        if(o.readyState == 4) {
            if(typeof(into) != 'object') 
                document.getElementById(into).innerHTML = o.responseText;
            else {
                
                temp = o.responseText.split("@@");
                for(i in into) {
                    document.getElementById(into[i]).innerHTML = temp[i];
                }
                
            }
            
        }
        
    }

}

//This prototype simply sends the request to the desired url
ajaxObj.prototype.send = function() {
    this.obj.open('GET', this.url, true);
    this.obj.send(null);
}

//This prototype calls all the other ones in the proper order. It's not really necessary, cause we can just call 
//this prototypes from within the object constructor, but I use it, cause I'm planning to extend this object in the future. 
ajaxObj.prototype.init = function() {
    
    this.obj = null;
    this.create();
    this.handle();
    this.send();

}

function ajaxDetailsInfo(url) {

    new ajaxObj(url, 'overlayInfo_main', "<div class='loading'>&nbsp;</div>");
    document.getElementById("overlayInfo_content").style.display='block';
    document.getElementById("overlayInfo_overlay").style.display='block';

    var c1 = document.getElementById("overlayInfo_content");
    var cwidth = c1.offsetWidth;
    var cheight = c1.offsetHeight;

    var cc = document.getElementById("overlayInfo_overlay");
    var fheight = cc.offsetHeight;
    var fwidth = cc.offsetWidth;

    c1.style.left = (fwidth/2 - cwidth/2) + "px";
    c1.style.top = (fheight/2 - cheight/2) + "px";

}

function jsDetailsInfo(content) {

    document.getElementById("overlayInfo_main").innerHTML = content;
    document.getElementById("overlayInfo_content").style.display='block';
    document.getElementById("overlayInfo_overlay").style.display='block';

    var c1 = document.getElementById("overlayInfo_content");
    var cwidth = c1.offsetWidth;
    var cheight = c1.offsetHeight;

    var cc = document.getElementById("overlayInfo_overlay");
    var fheight = cc.offsetHeight;
    var fwidth = cc.offsetWidth;

    c1.style.left = (fwidth/2 - cwidth/2) + "px";
    c1.style.top = (fheight/2 - cheight/2) + "px";

}

function ajaxDateDetails(liga, datum, zeit, heimteam, gastteam, ort, ergebnis, saetze) {
    
    var content = "<table class='ajax_table' border='0' cellspacing='0' cellpadding='0'>";

    content += "<tr><td class='ajax_header'>Liga</td><td>" + liga + "</td>";
    content += "</tr><tr><td class='ajax_header'>Datum</td><td>" + datum + "</td>";
    content += "</tr><tr><td class='ajax_header'>Uhrzeit</td><td>" + zeit + "</td>";
    content += "</tr><tr><td class='ajax_header'>Heim-Team</td><td>" + heimteam + "</td>";
    content += "</tr><tr><td class='ajax_header'>Gast-Team</td><td>" + gastteam + "</td>";
    content += "</tr><tr><td class='ajax_header'>Spielort</td><td>" + ort + "</td>";
    if(ergebnis != null && ergebnis.length > 0) {
        content += "</tr><tr><td class='ajax_header'>Ergebnis</td><td>" + ergebnis + "</td>";
    }
    if(saetze != null && saetze.length > 0) {
        var xx = saetze.split("|");
        for(var a = 0; a < xx.length; ++a) {
            content += "</tr><tr><td class='ajax_header'>Satz " + (a+1) + "</td><td>" + xx[a] + "</td>";
        }
    }
    
    content += "</tr></table>";
    jsDetailsInfo(content);
    
}

function ajaxTeamDetails(id) {
    ajaxDetailsInfo("./ajax/teamsDetails.php?id=" + id);
}

function ajaxSubpageFromMenu(menu, pid) {

    var url = "subpages.php?pid=" + pid;
    document.cookie = "pid=" + pid;
    deleteCookie("args");
    
    new ajaxObj(url, 'ajaxContent', "<div class='loading'>&nbsp;</div>");

    ddMenu(menu, -1);

}

function ajaxSubpage(pid) {
    ajaxSubpageWithArgs(pid, null);
}

function ajaxSubpageWithArgs(pid, args) {

    var url = "subpages.php?pid=" + pid;
    document.cookie = "pid=" + pid;
    if(args != null) {
        url += "&" + args;
        document.cookie = "args=" + args;
    } else
        deleteCookie("args");
    new ajaxObj(url, 'ajaxContent', "<div class='loading'>&nbsp;</div>");

}

function ajaxSubpageFromMenuWithArgs(menu, pid, args) {

    var url = "subpages.php?pid=" + pid + "&" + args;
    document.cookie = "pid=" + pid;
    document.cookie = "args=" + args;
    new ajaxObj(url, 'ajaxContent', "<div class='loading'>&nbsp;</div>");
    if(menu != null) ddMenu(menu, -1);

}
 

function deleteCookie(cookie) {
    
    var d = new Date();
    document.cookie = cookie + "=0;expires=" + d.toGMTString() + ";" + ";";

}

var visibleContentIdx = null;
    
function showNewsContent(contentid) {

    if(visibleContentIdx != null) {

        var p1 = document.getElementById("content_" + visibleContentIdx);
        if(p1 != null) p1.style.display = "none";
        var a1 = document.getElementById("content_link_" + visibleContentIdx);
        if(a1 != null) a1.style.display = 'block';
        visibleContentIdx = null;

    }

    var p2 = document.getElementById("content_" + contentid);
    if(p2 != null) p2.style.display = "block";
    var a2 = document.getElementById("content_link_" + contentid);
    if(a2 != null) a2.style.display = 'none';
        visibleContentIdx = contentid;

}

function showNewsEntry(id) {
    ajaxSubpageFromMenuWithArgs(null, 1, "id=" + id);
}

function showTeamList(id) {
    ajaxSubpageFromMenuWithArgs(null, 12, "id=" + id);
}



function pageSwitch(pid, limit) { 
    ajaxSubpageFromMenuWithArgs(null, pid, "limit=" + limit);
}
