var searchfinished = false;
var flyerfinished = false;

function makeXmlHttpRequest(url, callback) {
    var xmlHttp = null;
    try {
        // Mozilla, Opera, Safari sowie Internet Explorer (ab v7)
        xmlHttp = new XMLHttpRequest();
    } catch(e) {
        try {
            // MS Internet Explorer (ab v6)
            xmlHttp  = new ActiveXObject("Microsoft.XMLHTTP");
        } catch(e) {
            try {
                // MS Internet Explorer (ab v5)
                xmlHttp  = new ActiveXObject("Msxml2.XMLHTTP");
            } catch(e) {
                xmlHttp  = null;
            }
        }
    }
    if (xmlHttp) {
        xmlHttp.open('GET', url, true);
        xmlHttp.onreadystatechange = function () {
            if (xmlHttp.readyState == 4) {
                if( xmlHttp.status == 200 ) {
					callback(xmlHttp, url, 1);
				} else {
                    callback(xmlHttp, url, 0);
				}
            }
        };
        xmlHttp.send(null);
    }
};

function search() {
    var params = window.location.search;
    params = params.replace(/\&cso=1/g, "");
    var url = 'search.php' + params;
    makeXmlHttpRequest(url, searchcallback);
};

function searchcallback(xmlHttp, url, ok) {
    if(ok == 1) {
        createAnchor("searchresults", xmlHttp.responseText.substr(0, (xmlHttp.responseText.length-2)));
        hidenext1 = xmlHttp.responseText.substr((xmlHttp.responseText.length-2), 2);
        if(hidenext1 == 1) {
            $("a[id='next1']").css("display", "none");
        }
        searchfinished = true;
        if(flyerfinished == true) searchmarkers();
    } else {
        createAnchor("searchresults", "Fehler beim Abfragen der Suchergebnisse.");
    }
};

function searchmarkers() {
    var params = window.location.search;
    params = params.replace(/\&cso=1/g, "");
    var url = 'searchmarkers.php' + params;
    makeXmlHttpRequest(url, searchmarkerscallback);
};

function searchmarkerscallback(xmlHttp, url, ok) {
    if(ok == 1) {
        //Markers in Karte einbauen
        //createAnchor("searchresults", xmlHttp.responseText);
        var lines = xmlHttp.responseText.split("\n");
        //createAnchor("searchresults", lines.length);
        //var dbg = "Test ";
        var icon = null;
        for(i=0; i<lines.length; i++) {
            var fields = lines[i].split(";");
            //dbg += " " + fields.length;
            if(fields.length == 5) {
                var cid = fields[0];
                var markerimg = fields[1];
                var lat = fields[2];
                var lng = fields[3];
                var title = fields[4];
                //dbg += " " +cid;
                //dbg += " " +markerimg;
                //dbg += " " +lat;
                //dbg += " " +lng;
                //dbg += " " +title;
                map.addOverlay(createMarker(lat, lng, title, markerimg, cid));
				if(typeof window.map2 != 'undefined')
				{
					map2.addOverlay(createMarker(lat, lng, title, markerimg, cid));
				}
			}
        }
        //dbg += " Test";
        //createAnchor("searchresults", dbg);
    }
};

function createMarker(lat, lng, title, markerimg, cid) {
    var newIcon = new GIcon();
    newIcon.image = 'logo/' + markerimg;
    var size = { width: 33, height: 43 };
    newIcon.iconSize = new GSize(size.width, size.height);
    newIcon.iconAnchor = new GPoint(size.width >> 1, size.height);
    newIcon.shadow = 'template/default/images/marker-shadow.png';
    newIcon.infoWindowAnchor = new GPoint(size.width >> 1, 2);
    size = { width: 57, height: 43 };
    newIcon.shadowSize = new GSize(size.width, size.height);
    var marker = new GMarker(new GLatLng(lat, lng), {title: title, icon: newIcon});
    title = title.replace(/, /g, "%3Cbr /%3E");
    var html = unescape('%3Cimg src="logo/' + markerimg + '" style="float:left; padding-right:5px; padding-bottom:40px;"%3E%3Cb%3E' + title + '%3C/b%3E %3Cbr /%3E%3Cbr /%3E %3Ca href="index.php?cid=' + cid + '"%3Ezum Flyer%3C/a%3E');
    GEvent.addListener(marker, 'click', function() {
        marker.openInfoWindowHtml(html);
    });
    return marker;
}


function snipoftheday() {
    var params = window.location.search;
    params = params.replace(/\&cso=1/g, "");
    var url = 'snipoftheday.php' + params;
    makeXmlHttpRequest(url, snipofthedaycallback);
};

function snipofthedaycallback(xmlHttp, url, ok) {
    if(ok == 1) {
        createAnchor("snipoftheday", xmlHttp.responseText);
        var params = window.location.search;
        params = params.replace(/\&cso=1/g, "");
        var url = 'snipofthedayflash.php' + params;
        makeXmlHttpRequest(url, snipofthedayflashcallback);
    } else {
        createAnchor("snipoftheday", "Fehler beim Abfragen der Suchergebnisse.");
    }
};

function snipofthedayflashcallback(xmlHttp, url, ok) {
    if(ok == 1) {
        createAnchor("testflash", xmlHttp.responseText);
    } else {
        createAnchor("testflash", "Fehler beim Abfragen der Suchergebnisse.");
    }
};

function flyers() {
    var params = window.location.search;
    params = params.replace(/\&cso=1/g, "");
    var url = 'flyers.php' + params;
    makeXmlHttpRequest(url, flyerscallback);
};

function flyerscallback(xmlHttp, url, ok) {
    if(ok == 1) {
        createAnchor("flyers", xmlHttp.responseText.substr(0, (xmlHttp.responseText.length-2)));
        hidenext0 = xmlHttp.responseText.substr((xmlHttp.responseText.length-2), 2);
        if(hidenext0 == 1) {
            $("a[id='next0']").css("display", "none");
        }
        flyerfinished = true;
        if(searchfinished == true) searchmarkers();
    } else {
        createAnchor("flyers", "Fehler beim Abfragen der Suchergebnisse.");
    }
};

function coupons() {
    var params = window.location.search;
    params = params.replace(/\&cso=1/g, "");
    var url = 'couponsearch.php' + params;
    makeXmlHttpRequest(url, couponscallback);
};

function couponscallback(xmlHttp, url, ok) {
    if(ok == 1) {
        createAnchor("coupons", xmlHttp.responseText);
    } else {
        createAnchor("coupons", "Fehler beim Abfragen der Suchergebnisse.");
    }
};

function createAnchor(e,text) {
    var div = document.getElementById(e);
    div.innerHTML = text;
}
