﻿var isLoadMap = true;
var isLoadSearchForm = true;
resultsPage = true;

function onLoadResults() {
    $("#googleMapVisibility").text(showMap);
    setMyCookie("shownSearchForm", "0");
    if (homeList.length != 0) {
        var curState = getMyCookie("shownGoogleMap");
        if (curState != "1") {
            initializeGeo(false);
            hideGoogleMap();
        }
        else {
            showGoogleMap();
        }
    }
    else {
        $("#mapBgrnd").css("display", "none");
        $("#slideBars").css("display", "none");
    }
    $('#arrowPointer_searchForm').click(showSearchForm);
}

function createMarker(point, html, icon, id, name) {
    if (!icon)
        icon = blueIcon;

    markerOptions = { icon: icon, title: name };
    var marker = new GMarker(point, markerOptions);
    GEvent.addListener(marker, "click",
        function() {
            var markerLatLng = marker.getLatLng();
            if (!map.getInfoWindow().isHidden()) {
                var winInfoLatLng = map.getInfoWindow().getPoint();
                if (winInfoLatLng != markerLatLng) {
                    createAndShowWinInfo(html, id, marker, markerLatLng);
                }
            }
            else {
                createAndShowWinInfo(html, id, marker, markerLatLng);
            }
        }
    );
    return marker;
}

function createAndShowWinInfo(html, id, marker, markerLatLng) {
    map.setCenter(markerLatLng);
    marker.openInfoWindowHtml("<div style='width: 250; heigth: 110;'>" + html + "</div>");
    $("#show").text(showSelected + ":  ");
    $("#showAllMarkers").text(all);
    $("#showOneMarker").text(one);
    $("#separator").text(" | ");
    hideStylesAllMarkers();
    showStylesOneMarker();
    setMyCookie("selectedHomeId", id);
    pageTracker._trackPageview('/tracking/mappopup');
    GEvent.addListener(map.getInfoWindow(), "closeclick", function() {
        delCookie("selectedHomeId");
        hideStylesOneMarker();
        $("#showOneMarker").css("font-weight", "normal");
        showAllMarkers();
    });
}

function hideMarkers() {
    for (var i in homeList) {
        if (homeList[i].marker != null)
            homeList[i].marker.hide();
    }
}

function showMarkers() {
    for (var i in homeList)
        if (homeList[i].marker != null && homeList[i].marker.isHidden())
        homeList[i].marker.show();
}

function getMarkerById(id) {
    for (var i in homeList)
        if (homeList[i].marker != null && homeList[i].id == id)
        return homeList[i].marker;
    return null;
}

function showMarkerWinInfo(id) {
    setMyCookie("selectedHomeId", id);
    if (isLoadMap || !isShownBgrn("shownGoogleMap")) {
        showGoogleMap();
    }
    else
        showMarker(id);
}

function showMarker(id) {
    var marker = getMarkerById(id);
    if (marker != null) {
        var point = marker.getLatLng();
        if ((point.lat() == 0 || point.lng() == 0) || (abs(point.lat()) > 85) || (abs(point.lng()) > 180))
            return false;
        showStylesOneMarker();
        marker.show();
        GEvent.trigger(marker, "click");
    }
}

function showAllMarkers() {
    showMarkers();
    hideStylesAllMarkers();
    if (getMyCookie("selectedHomeId") != null)
        showStylesOneMarker();
}

function showOneMarker() {
    var id = getMyCookie("selectedHomeId");
    if (id != null) {
        hideMarkers();
        showMarker(id);
        showStylesAllMarkers();
        hideStylesOneMarker();
    }
}

function showGoogleMap() {
    try {pageTracker._trackPageview("/tracking/forside/kort_vis"); }
    catch (e) { }

    if (isShownBgrn("shownSearchForm")) {
        $("#searchFormWindow").slideUp("fast", function() {
            slideDownGoogleMap();
        });
    }
    else {
        $("#mapBgrnd").css("display", "block");
        slideDownGoogleMap();
    }
    pageTracker._trackPageview('/tracking/showmap');
}

function showSearchForm() {
    if (isShownBgrn("shownGoogleMap")) {
        $("#google_map").slideUp("fast", function() {
            slideDownSearchForm();
        });
    }
    else {
        $("#mapBgrnd").css("display", "block");
        slideDownSearchForm();
    }
}

function hideGoogleMap() {
    try { pageTracker._trackPageview("/tracking/forside/kort_skjul"); }
    catch (e) { }
    $("#google_map").slideUp("slow", function() {
        $("#mapBgrnd").css("display", "none");
        setHiddenMapStyles();
    });
}

function hideSearchForm() {
    $("#searchFormWindow").slideUp("slow", function() {
        $("#mapBgrnd").css("display", "none");
        setHiddenStylesSearchform();
    });
}

function isLoadingGoogleMap() {
    if (isLoadMap) {
        isLoadMap = false;
        initializeMap();
        return true;
    }
    return false;
}

function isLoadingSearchForm() {
    if (isLoadSearchForm) {
        isLoadSearchForm = false;
        initializeSearchForm();
        return true;
    }
    return false;
}

function slideDownGoogleMap() {
    setShownMapStyles();
    $("#google_map").slideDown("slow", function() {
        if (!isLoadingGoogleMap()) {
            var markerId = getMyCookie("selectedHomeId");
            if (markerId != null)
                showMarker(markerId);
        }
    });
}

function slideDownSearchForm() {
    setShownStylesSearchForm();
    $("#searchFormWindow").slideDown("slow", function() {
        isLoadingSearchForm();
    });
}

function hideStylesAllMarkers() {
    $("#showAllMarkers").css("text-decoration", "none");
    $("#showAllMarkers").css("cursor", "auto");
    $("#showAllMarkers").css("font-weight", "bold");
    $("#showAllMarkers").unbind('click', showAllMarkers);
}

function showStylesAllMarkers() {
    $("#showAllMarkers").css("text-decoration", "underline");
    $("#showAllMarkers").css("cursor", "pointer");
    $("#showAllMarkers").css("font-weight", "normal");
    $("#showAllMarkers").bind('click', showAllMarkers);
}

function hideStylesOneMarker() {
    $("#showOneMarker").css("text-decoration", "none");
    $("#showOneMarker").css("cursor", "auto");
    $("#showOneMarker").css("font-weight", "bold");
    $("#showOneMarker").unbind('click', showOneMarker);
}

function showStylesOneMarker() {
    if (homeList.length > 1) {
        $("#showOneMarker").css("text-decoration", "underline");
        $("#showOneMarker").css("cursor", "pointer");
        $("#showOneMarker").css("font-weight", "normal");
        $("#showOneMarker").bind('click', showOneMarker);
    }
}

function isShownBgrn(cookieName) {
    if (getMyCookie(cookieName) == "1") {
        return true;
    }
    else {
        return false;
    }
}

function initializeSearchForm() {
    try {
        bornholmFormType = 1;
        currencyCode = "DKK";
        var arrivalDate = $("input#arrivalDate").val();
        var departureDate = $("input#departureDate").val();
        pageLoadSearchForm(arrivalDate, departureDate);
    }
    catch (e) { 
    }
}

function setShownMapStyles() {
    setMyCookie("shownGoogleMap", "1");
    $("#arrowPointer").html("<img src='/images/close-pointer.png' style='float: right; cursor: pointer;' alt='' onclick='hideGoogleMap()'/>");
    $("#googleMapVisibility").text(hideMap);
    $("#show").text(showSelected + ":  ");
    $("#separator").text(" | ");
    $("#showAllMarkers").text(all);
    $("#showOneMarker").text(one);
    $("#googleMapVisibility").unbind();
    $("#googleMapVisibility").click(hideGoogleMap);
    setHiddenStylesSearchform();
}

function setHiddenMapStyles() {
    setMyCookie("shownGoogleMap", "0");
    $("#arrowPointer").html("<img src='/images/open-pointer.png' style='float: right; cursor: pointer;' alt='' onclick='showGoogleMap()'/>");
    $("#googleMapVisibility").text(showMap);
    $("#show").text("");
    $("#separator").text("");
    $("#showAllMarkers").text("");
    $("#showOneMarker").text("");
    $("#googleMapVisibility").unbind();
    $("#googleMapVisibility").click(showGoogleMap);
}

function setShownStylesSearchForm() {
    setMyCookie("shownSearchForm", "1");
    $('#arrowPointer_searchForm').unbind("click", showSearchForm);
    $('#arrowPointer_searchForm').click(hideSearchForm);
    $("#searchFormWindowVis").text(hide);
    $("#arrowPointer_form").html("<img src='/images/close-pointer_form.png' style='float: right; cursor: pointer;' />");
    setHiddenMapStyles();
}

function setHiddenStylesSearchform() {
    setMyCookie("shownSearchForm", "0");
    $('#arrowPointer_searchForm').unbind("click", hideSearchForm);
    $('#arrowPointer_searchForm').click(showSearchForm);
    $("#searchFormWindowVis").text(show);
    $("#arrowPointer_form").html("<img src='/images/open-pointer_form.png' style='float: right; cursor: pointer;' />");
}