var custIcon = new GIcon(G_DEFAULT_ICON);
custIcon.iconSize = new GSize(32, 32);


// === Create an associative array of GIcons() ===
var gicons = [];
//gicons["lagenhet"] = new GIcon(G_DEFAULT_ICON, "http://chart.apis.google.com/chart?chst=d_map_pin_icon&chld=accomm|FF6666");
//gicons["hotell"] = new GIcon(G_DEFAULT_ICON, "http://chart.apis.google.com/chart?chst=d_map_pin_icon&chld=accomm|99CCFF");
//gicons["hus"] = new GIcon(G_DEFAULT_ICON, "http://chart.apis.google.com/chart?chst=d_map_pin_icon&chld=home|99CCFF");
//gicons["hus"] = new GIcon(custIcon, "house2.png");
gicons["hus"] = new GIcon(custIcon, "house6.png");
gicons["hotell"] = new GIcon(custIcon, "hotell.png");
gicons["lagenhet"] = new GIcon(custIcon, "apartment.png");
 
gicons["camping"] = new GIcon(G_DEFAULT_ICON, "http://chart.apis.google.com/chart?chst=d_map_pin_icon&chld=camping|FFFF00");
gicons["shopping"] = new GIcon(G_DEFAULT_ICON, "http://chart.apis.google.com/chart?chst=d_map_pin_icon&chld=shoppingcart|FFFF00");
gicons["flygplats"] = new GIcon(G_DEFAULT_ICON, "http://chart.apis.google.com/chart?chst=d_map_pin_icon&chld=airport|FFFF00");
gicons["information"] = new GIcon(G_DEFAULT_ICON, "http://chart.apis.google.com/chart?chst=d_map_pin_icon&chld=info|FFFF00");
gicons["restaurang"] = new GIcon(G_DEFAULT_ICON, "http://chart.apis.google.com/chart?chst=d_map_pin_icon&chld=restaurant|FFFF00");
gicons["bar"] = new GIcon(G_DEFAULT_ICON, "http://chart.apis.google.com/chart?chst=d_map_pin_icon&chld=bar|FFFF00");
gicons["diverse"] = new GIcon(G_DEFAULT_ICON, "http://chart.apis.google.com/chart?chst=d_map_pin_icon&chld=location|FFFF00");
gicons["skidor"] = new GIcon(G_DEFAULT_ICON, "http://chart.apis.google.com/chart?chst=d_map_pin_icon&chld=ski|FFFF00");
gicons["arkitektur"] = new GIcon(G_DEFAULT_ICON, "http://chart.apis.google.com/chart?chst=d_map_pin_icon&chld=corporate|FFFF00");
gicons["strand"] = new GIcon(G_DEFAULT_ICON, "http://chart.apis.google.com/chart?chst=d_map_pin_icon&chld=swim|FFFF00");
gicons["trevliga_byar"] = new GIcon(G_DEFAULT_ICON, "http://chart.apis.google.com/chart?chst=d_map_pin_icon&chld=location|FFFF00");
gicons["nationalpark"] = new GIcon(G_DEFAULT_ICON, "http://chart.apis.google.com/chart?chst=d_map_pin_icon&chld=floral|FFFF00");
gicons["cykel"] = new GIcon(G_DEFAULT_ICON, "http://chart.apis.google.com/chart?chst=d_map_pin_icon&chld=bicycle|FFFF00");
gicons["golf"] = new GIcon(G_DEFAULT_ICON, "http://chart.apis.google.com/chart?chst=d_map_pin_letter&chld=G|FFFF00|000000");
//gicons["golf"] = new GIcon(custIcon, "golf.png");




//gicons["barcelona"] = new GIcon(G_DEFAULT_ICON, "http://chart.apis.google.com/chart?chst=d_map_spin&chld=3|30|FFFF88|10|_|BCN");
gicons["barcelona"] = new GIcon(custIcon, "bcn2.png");



// arrays to hold copies of the markers and html used by the side_bar
// because the function closure trick doesnt work there
var gmarkers = [];
var htmls = [];
var imagesources = [];
var descriptionurls = [];
// this variable will collect the html which will eventualkly be placed in the side_bar
var side_bar_html = "";
var typeHeader = "";


// A function to create the marker and set up the event window
function createMarker(point, name, html, icontype, imagesource, descriptionurl, typeOfPoint, i){
    // === create a marker with the requested icon ===
    var marker = new GMarker(point, {icon:gicons[icontype],title:name});
    GEvent.addListener(marker, "click", function(){
        marker.openInfoWindowHtml(html + '<br>&nbsp;', {
            maxWidth: 300
        });
    });
    
    // save the info we need to use later for the side_bar
    gmarkers[i] = marker;
    htmls[i] = html + '<br>&nbsp;';
    imagesources[i] = imagesource;
    descriptionurls[i] = descriptionurl;
    
    return marker;
}


function parseValue(name){
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(window.location.href);
    
    if (results == null) 
        return "";
    else 
        return results[1];
}


function checkUncheckOnMap(isChecked){
    applyFilterAndLoadMap();
}

function checkUncheckSubBoende(isChecked){
    if (isChecked == false) {
        document.mapForm.Hus.checked = false;
        document.mapForm.Lagenhet.checked = false;
        document.mapForm.Hotell.checked = false;
    }
    else {
        document.mapForm.Hus.checked = true;
        document.mapForm.Lagenhet.checked = true;
        document.mapForm.Hotell.checked = true;
    }
    applyFilterAndLoadMap();
}

function checkUncheckSubOther(isChecked){
    var elem = document.getElementById('mapForm').elements;
    for (var i = 0; i < elem.length; i++) {
        if ((elem[i].type == 'checkbox') && (elem[i].id != "Hus") && (elem[i].id != "Lagenhet") && (elem[i].id != "Hotell")) {
            eval('document.mapForm.' + elem[i].id + '.checked=' + isChecked);
        }
    }
    applyFilterAndLoadMap();
}

function checkUncheckBoende(isChecked){
    if (isChecked) {
        document.mapForm.Boende.checked = true;
    }
    else {
        if ((document.mapForm.Lagenhet.checked == false) && (document.mapForm.Hotell.checked == false) && (document.mapForm.Hus.checked == false)) {
            document.mapForm.Boende.checked = false;
        }
    }
    applyFilterAndLoadMap();
}


function loadNewMap(newregion){
    //Save filter-selection
    var elem = document.getElementById('mapForm').elements;
    for (var i = 0; i < elem.length; i++) {
        if ((elem[i].type == 'checkbox') && (elem[i].checked)) {
            if (str) {
                str += ',' + elem[i].name;
            }
            else {
                var str = elem[i].name;
            }
        }
    }
    createCookie('filterSelection', str, 1);
    window.location = "index.html?region=" + newregion;
}

// This function picks up the click and opens the corresponding info window
function myclick(i){
    gmarkers[i].openInfoWindowHtml(htmls[i]);
}

function showHelp(){
    document.getElementById("map").innerHTML = '<iframe src="map_helptext.htm" padding="10" frameborder="0" scrolling="yes" width="100%" height="780">';
}

function loadMarkers(){
    for (var j = 0; j < data.count; j++) {
        var marker = createMarker(new GLatLng(data.markers[j].latitude, data.markers[j].longitude), data.markers[j].label, data.markers[j].html, data.markers[j].icontype, data.markers[j].objectimage, data.markers[j].descriptionurl, data.markers[j].type, j);
        marker.show();
        markers.push(marker);
    }
    mgr = new MarkerManager(map);
    mgr.addMarkers(markers, 1);
    mgr.refresh();
}

function applyFilterAndLoadMap(){
    html = "";
    side_bar_html = "";
    var typeOfPoint = "";
    
    var numberShowing = 0;
    for (var j = 0; j < data.count; j++) {
        if (eval('document.mapForm.' + data.markers[j].type + '.checked')) {
            markers[j].show();
            numberShowing++;
            // add a bold header for a new type to the side_bar html
            if ((typeHeader == '') || (typeHeader != data.markers[j].type)) {
                typeHeader = data.markers[j].type;
                if (typeHeader == 'Lagenhet') {
                    side_bar_html += '<b>Lägenheter</b><br>';
                }
                else 
                    if (typeHeader == 'Trevliga_byar') {
                        side_bar_html += '<b>Trevliga byar</b><br>';
                    }
                    else {
                        side_bar_html += '<b>' + data.markers[j].type + '</b><br>';
                    }
            }
            
            side_bar_html += '<a href="javascript:myclick(' + j + ')">' + data.markers[j].label + '</a><br>';
        }
        else {
            markers[j].hide();
        }
    }
    // put the assembled side_bar_html contents into the side_bar div
    document.getElementById("side_bar").innerHTML = 'Points of Interest shown: <b>' + numberShowing + '</b><br>' + side_bar_html;
    document.getElementById('regioner').value = newregion;
	
    mgr.refresh();
}


// the hide() method saves the markers current position and then 
// positions the marker outside the map
GMarker.prototype.hide = function(){
    if (this.getPoint().lat() < 90) {
        try {
            this.savePoint = this.getPoint();
            this.setPoint(new GLatLng(90, 0));
        } 
        catch (e) {
        }
    }
}
// the show() method puts the marker back in the original position
GMarker.prototype.show = function(){
    if (this.getPoint().lat() == 90) {
        if (this.savePoint) {
            try {
                this.setPoint(this.savePoint);
                this.savePoint = null;
            } 
            catch (e) {
            }
        }
    }
}

// the isHidden() method checks if the marker is put outside the map and if it does
// returns true
GMarker.prototype.isHidden = function(){
    if (this.getPoint().lat() == 90) {
        return true;
    }
    else {
        return false;
    }
}

function createCookie(name, value, days){
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        var expires = "; expires=" + date.toGMTString();
    }
    else 
        var expires = "";
    document.cookie = name + "=" + value + expires + "; path=/";
}

function readCookie(name){
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') 
            c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) == 0) 
            return c.substring(nameEQ.length, c.length);
    }
    return null;
}

function eraseCookie(name){
    createCookie(name, "", -1);
}

function showhide(id){
    if (document.getElementById) {
        obj = document.getElementById(id);
        if (obj.style.display == "none") {
            obj.style.display = "";
        }
        else {
            obj.style.display = "none";
        }
    }
}

function toggleDiv(divid){
    if (document.getElementById(divid).style.display == 'none') {
        document.getElementById(divid).style.display = 'block';
    }
    else {
        document.getElementById(divid).style.display = 'none';
    }
}


