var map;
var catTree;
var catTreeDisplay;
var slider;
var sliderMenue;
var sliderMap;
var sliderMapClosed = true;
var sliderMapInAction = false;
var searchSuggest;

function checkedCatFkt(checked) {
    //      alert(checked);
    map.adjustAndSetCatContentManager(checked);
}

function linkFkt() {
    sliderMap.close();
}

function moreFkt() {
    if (!sliderMapInAction) {
        sliderMapInAction = true;
        if (sliderMapClosed) {
            $('layer_map').style.left = (parseInt($('layer_map').style.left) - $('layer_map').getWidth() + 50) + "px";
        }
        sliderMap.toggle('layer_map');
    }
}

function closeFkt() {
    if (!sliderMapInAction) {
        sliderMapInAction = true;
        sliderMap.toggle('layer_map');
    }
}

function catPageFkt(id) {
    map.adjustAndSetCatPageContentManager(id);
}

function sliderOpendFkf() {
    sliderMapClosed = false;
    sliderMapInAction = false;
}

function sliderClosedFkf() {
    catTreeDisplay.cfgClose();
    $('layer_map').style.left = 400 + "px";

    sliderMapClosed = true;
    sliderMapInAction = false;
}

function load() {
    var MAP_WIDTH = 600;
    var MAP_HEIGHT = 600;
    //    var X_MAP = 118590;
    //    var Y_MAP = 196850;
    //var SCALE_MAP = 5;
    var X_MAP = 206790;
    var Y_MAP = 180050;
    var SCALE_MAP = 9;
    var TYPE_MAP = 0;
    var CATEGORY = "";

    map = new Map("navCompass", "navScale", "navSlider", "mapDiv", "mapDivScaleMass", "divContent1", "divHome", MAP_WIDTH, MAP_HEIGHT);
    map.setSize(-1 - 18, -100);

    //---

    map.setMoveManager(X_MAP, Y_MAP, SCALE_MAP, TYPE_MAP);
    //map.setContentManager("", "");
    //map.setNavManager();

    //-----

    slider = new Slider("layer_null", "horizontal", "layer_eins", "horizontal", "layer_zwei", "horizontal", "layer_drei", "horizontal", "layer_vier", "horizontal", "layer_fuenf", "horizontal", "layer_sechs", "horizontal", "layer_sieben", "horizontal");
    //slider.toggle("layer_zwei");
    slider.setSliderOffset(0);

    sliderMenue = new Slider("layer_menu", "vertical");

    sliderMap = new Slider("layer_map", "horizontal");
    sliderMap.setSliderOpendFkf(function() {
        sliderOpendFkf();
    });
    sliderMap.setSliderClosedFkf(function() {
        sliderClosedFkf();
    });
    //                            (suggestField,            suggestFile,        suggestOpacity,  suggestSkipLabel, suggestSubmitFkt,  suggestSubmiOnes)
    searchSuggest = new Suggest(document.suchen.suchen, "/mallorca/web/php/suggestSearch.php", 1.0, true, searchSubmit, false);
}

function searchSubmit() {
    var text = searchSuggest.getSelectedText();
    var id = searchSuggest.getSelectedId();
    //        alert("text: " + text + " - id: " + id);
    // text: Paris - id: A,324,47240300010751
    // text: Calle de 457 - id: S,47240048054013,47240300010809
    // text: Cala Rajada - id: T,47240300010774,47240300010774
    //alert("text: " + text + " - id: " + id);
    if (text == "-") {
        map.setAdrContentManager("-", false);
    } else {
        if (id.substr(0, 2) == "T," || id.substr(0, 2) == "K," || id.substr(0, 2) == "A," || id.substr(0, 2) == "S,") {
            if (id.substr(0, 2) == "T,") {
                if (id == "T,,") {
                    new Ajax.Request("/mallorca/web/ajax/ajaxTranslateCity.php", {
                        method: "post",
                        asynchronous: true,
                        parameters: "&text=" + encodeURI(text),

                        onFailure: function(response) {
                            alert(response.responseText);
                        },

                        onSuccess: function(response) {
                            if (response.responseText.length > 0) {
                                go("/mallorca-karte/" + response.responseText, id);
                            }
                        }
                    });
                } else {
                    id = id.substr(2);
                    idx = id.indexOf(",");
                    if (idx != -1) {
                        id = id.substr(0, idx + 1);
                        map.setAdrContentManager(id, false);
                    }
                }
            } else {
                if (id.substr(0, 2) == "K,") {
                    id = id.substr(2);
                    idx = id.indexOf(",");
                    if (idx != -1) {
                        locationId = id.substr(idx + 1);
                        id = id.substr(0, idx);
                        if (locationId != -1) {
                            map.setCategoryInAddress(id, locationId + ",");
                        } else {
                            new Ajax.Request("/mallorca/web/ajax/ajaxTranslateCategory.php", {
                                method: "post",
                                asynchronous: true,
                                //                                parameters: "&text=" + encodeURI(text),
                                parameters: "&id=" + id,

                                onFailure: function(response) {
                                    alert(response.responseText);
                                },

                                onSuccess: function(response) {
                                    if (response.responseText.length > 0) {
                                        //                                        alert(response.responseText);
                                        go(response.responseText, id);
                                    }
                                }
                            });
//                            go(response.responseText, id);
                            //                            map.adjustAndSetCatPageContentManager(id);
                        }
                    }
                } else {
                    if (id.substr(0, 2) == "A,") {
                        //go("/poi/" + text, id);
                        //check case with multi poi search e.g. "lidl auf mallorca"
                        if (id.substr(0, 5) == "A,-1,") {
                            var  url = id.substring(5);
//                            alert(url);
                            //                            map.setAdrContentManagerAndHideRedundancyIcons(adressen, true);
                            go(url, 0);
                        } else {//usual case - only one adresse was chosen
                            //extract poi id
                            id = id.substr(2);
                            commaIndex = id.indexOf(",");
                            id = id.substr(0, commaIndex);
                            new Ajax.Request("/mallorca/web/ajax/ajaxTranslateAddress.php", {
                                method: "post",
                                asynchronous: true,
                                //                                parameters: "&text=" + encodeURI(text),
                                parameters: "&id=" + id,

                                onFailure: function(response) {
                                    alert(response.responseText);
                                },

                                onSuccess: function(response) {
                                    if (response.responseText.length > 0) {
                                        //                                        alert(response.responseText);
                                        go(response.responseText, id);
                                    }
                                }
                            });
                        }
                    } else {
                        id = id.substr(2);
                        idx = id.indexOf(",");
                        if (idx != -1) {
                            id = id.substr(0, idx + 1);
                            map.setAdrContentManager(id, false);
                        }
                    }
                }
            }
        } else {
            if (text != "" && id == "") {
                new Ajax.Request("/mallorca/web/ajax/ajaxSearchSubmit.php", {
                    method: "post",
                    asynchronous: true,
                    parameters: "&text=" + encodeURI(text),

                    onFailure: function(response) {
                        alert(response.responseText);
                    },

                    onSuccess: function(response) {
                        found = false;
                        //alert(response.responseText);
                        ajaxResponse = response.responseText.split('\|');
                        if (ajaxResponse.length > 0) {
                            if (ajaxResponse[0] == "A" || ajaxResponse[0] == "T" || ajaxResponse[0] == "S") {
                                if (ajaxResponse[0] == "S") {
                                    map.setAdrContentManager(ajaxResponse[1], false);
                                } else {
                                    map.setAdrContentManager(ajaxResponse[1], true);
                                }
                                found = true;
                            }
                            if (ajaxResponse[0] == "K") {
                                idx = ajaxResponse[1].indexOf(",");
                                if (idx != -1) {
                                    locationId = ajaxResponse[1].substr(idx + 1);
                                    id = ajaxResponse[1].substr(0, idx);
                                    if (locationId > 0) {
                                        map.setCategoryInAddress(id, locationId + ",");
                                    } else {
                                        map.adjustAndSetCatPageContentManager(id);
                                    }
                                }
                                found = true;
                            }
                            if (ajaxResponse[0] == "L") {
                                if (ajaxResponse[1] != -1 && ajaxResponse[2] != -1) {
                                    //alert(ajaxResponse[1] + " -" + ajaxResponse[2]);
                                    map.setForBoundsAndGo(parseInt(ajaxResponse[1]), parseInt(ajaxResponse[2]), parseInt(ajaxResponse[1]), parseInt(ajaxResponse[2]));
                                    found = true;
                                }
                            }
                        }

                        if (!found) {
                            if ($("divContent1")) {
                                $("divContent1").update("<br /><strong>Für Ihre Suchanfrage haben wir<br />leider kein eindeutiges Ergebnis gefunden.</strong>");
                            }
                            if ($("divContent2_content")) {
                                $("divContent2_content").update("");
                            }
                            split('|', ' ', ' ', ' ', ' ', ' ', ' ', ' ');
                        }
                    }
                });
            } else {
                map.setAdrContentManager("", false);
            }
        }
    }

    document.suchen.suchen.focus();
}

function setHome(idAdr, idCat) {
    var elements = null;
    for (i = 0; i < document.forms.length; i++) {
        if (document.forms[i].name == "toolTipInfo") {
            elements = document.forms[i];
        }
    }

    var checked = false;

    if (elements != null) {
        for (var i = 0; i < elements.length; i++) {
            if (elements[i].type == "checkbox") {
                if (elements[i].id != "tti-" + idAdr) {
                    elements[i].checked = false;
                } else {
                    checked = elements[i].checked;
                }
            }
        }
    }

    map.setHome(idAdr + ",", idCat + ",", checked, true);
}

function removeHome() {
    map.removeHome();
}

function lightboxOpen(src) {
    if (typeof lightboxOverlay == 'undefined' && typeof lightboxContent == 'undefined' || lightboxOverlay == null && lightboxContent == null) {
        //alert("lightbox();");
        docBody = document.getElementsByTagName('body')[0];

        lightboxOverlay = document.createElement('div');
        lightboxOverlay.id = 'lightboxOverlay';

        lightboxContent = document.createElement('div');
        lightboxContent.id = 'lightboxContent';
        lightboxContent.innerHTML =
                "  <iframe src='" + src + "' frameborder='0' width='800' height='475' allowtransparency='true'></iframe>" +
                        "  <p class='popuplink'><a href='javascript:lightboxClose()'>Bitte hier schlie&szlig;en!</a></p>";
        docBody.appendChild(lightboxOverlay);
        docBody.appendChild(lightboxContent);
    }
}

function lightboxClose() {
    $(lightboxOverlay).remove();
    $(lightboxContent).remove();

    lightboxOverlay = null;
    lightboxContent = null;
}

function setCookie(name, wert, domain, expires, path, secure) {
    var cook = "name=" + unescape(wert);
    cook += (domain) ? "; domain=" + domain : "";
    cook += (expires) ? "; expires=" + expires : "";
    cook += (path) ? "; path=" + path : "";
    cook += (secure) ? "; secure" : "";
    document.cookie = cook;
}

function deleteCookie(name, domain, path) {
    var cook = "name=; expires=Thu, 01-Jan-70 00:00:01 GMT";
    cook += (domain) ? "domain=" + domain : "";
    cook += (path) ? "path=" + path : "";
    document.cookie = cook;
}

function getCookie(name) {
    var cook = null;
    var suche = name + "=";
    var i = 0
    var ende;

    alert(document.cookie);

    while (cook == null && i < document.cookie.length) {
        if (document.cookie.substring(i, i + suche.length) == suche) {
            ende = document.cookie.indexOf(";", i + suche.length);
            ende = (ende > -1) ? ende : document.cookie.length;
            cook = document.cookie.substring(i + suche.length, ende);
        }
        i++;
    }
    return cook;
}

