var map;
var myIcons = [];
var geocoder;
var gmarkers = [];
var directions;
var bounds;
var tabStrip;
var custIcon = new GIcon(G_DEFAULT_ICON);
var all = false;

function initialize(width, height, lat, lon, scale) {

    document.getElementById("map").style.width= width + "px";
    document.getElementById("map").style.height= height + "px";
	map = new GMap2(document.getElementById("map"));
    //map.addControl(new GLargeMapControl3D());
    //map.addControl(new GMapTypeControl());
    //map.addControl(new GScaleControl());
	map.setUIToDefault();
	map.disableScrollWheelZoom();
    map.setCenter(new GLatLng(lat, lon), scale);
	map.setMapType(G_PHYSICAL_MAP);
	
	geocoder = new GClientGeocoder();
	directions = new GDirections(map, document.getElementById("directionPanel"));
	gmarkers = [];
	bounds = new GLatLngBounds();
	// Add array with all icons TODO make dynamic
	//$("#map").show();
	//resetTabs();
	if ($("#directionTab") != null){
		$("#directionTab").hide();
	}
	if ($("#locationTab") != null){
		$("#locationTab").hide();
	}
}

// Add function for creating markers
function createMarker(point, content, openingtimes, capContent, capTimes, tooltip, imagepath) {
    var infoTabs;
    // Our info window content
    if (openingtimes != "") {
        infoTabs = [new GInfoWindowTab(capContent, content), new GInfoWindowTab(capTimes, openingtimes)];
    }
    else {
        infoTabs = [new GInfoWindowTab(capContent, content)];
    }

    // marker options object
    var icon;
    if ("" == imagepath) {
        icon = custIcon;
    } else {
        //var image = new Image;
        //image.src = imagepath;

        icon = new GIcon(custIcon, imagepath);
        //icon.iconSize = new GSize(image.width, image.height);
        icon.iconSize = new GSize(24, 24);
        //icon.iconAnchor = new GPoint(image.width / 2, image.height/2);
        icon.iconAnchor = new GPoint(0, 0);
        icon.shadowSize = new GSize(0, 0);
    }
	var marker = new GMarker(point, { icon: icon, title: tooltip });
	// add click event to marker
	GEvent.addListener(marker, "click", function() {
	marker.openInfoWindowHtml(infoTabs);
	});
	//Add to marker array
	gmarkers.push(marker);
}

function onTabSelected(sender, args) {
    //logEvent("OnClientTabSelected: " + args.get_tab().get_text());
    tabStrip = sender;
    var tab = tabStrip.findTabByValue('locationTab');
    if (tab.get_selected()) {
        setBounds();
    } else {
        if (directions != null) {
            tmpBounds = directions.getBounds();
            // Reset zoom level
            if (tmpBounds != null) {
                map.setZoom(map.getBoundsZoomLevel(tmpBounds));
                // Reset center
                map.setCenter(tmpBounds.getCenter());
            }
        }
    }
}

/*function resetTabs() {
    if (tabStrip != null) {
        var tabLoc = tabStrip.findTabByValue('locationTab');
        if (tabLoc != null) {
            tabLoc.set_selected(true);
        }
        var tabDir = tabStrip.findTabByValue('directionTab');
        if (tabDir != null) {
            tabDir.set_selected(false);
        }
    }
}*/

function onClientLoad(sender, args) {
    //Set the tabStrip
    tabStrip = sender;
}
/*
function loadIcons(mapname) {
    custIcon.iconSize = new GSize(24, 24);
    custIcon.shadowSize = new GSize(0, 0);
    custIcon.iconAnchor = new GPoint(12, 12);

   
    // Add array with all icons
    myIcons['home'] = new GIcon(custIcon, mapname + '/home.png');
    myIcons['default'] = new GIcon(custIcon, mapname + '/default.png');
    myIcons['sublocation'] = new GIcon(custIcon, mapname + '/sublocation.png');
}
*/

//kicks off the directions.load method with the correct parameters
function getDirections(fromLat, fromLng, toLat, toLng, travelMode, culture) {
    culture = culture.replace("-","_");
    //document.getElementById("routeField").Text = fromLat + "," + fromLng + "," + toLat + "," + toLng + "," + travelMode;
    var fromAdress = fromLat + ", " + fromLng;
    var toAdress = toLat + ", " + toLng;
    directions.load("from: " + fromAdress + " to: " + toAdress, { travelMode: travelMode, locale: culture });
    $("#directionTab").show();
    if (tabStrip != null) {
        //tabStrip = $find("<%= plc_lt_zoneCenter_pageplaceholder_pageplaceholder_lt_zoneLeft_GoogleLocations_radTabStrip.ClientID %>");


        var tabDir = tabStrip.findTabByValue('directionTab');
        if (tabDir != null) {
            tabDir.set_selected(true);
        }
    }
    $('#print').html('Klik <a href="#" onclick="print(\'directionPanel\');">hier</a> om deze route te printen.');
}


//Function to set the map to the right zoomlevel
function setBounds() {
    //Set all markers and end MapLoad function
    for (var i = 0; i < gmarkers.length; i++) {
        bounds.extend(gmarkers[i].getLatLng());
    }
    var southWest = bounds.getSouthWest();
    var northEast = bounds.getNorthEast();
    // We append a little
    var add = 0.004;
    bounds.extend(new GLatLng(southWest.lat() - add, southWest.lng() - add));
    bounds.extend(new GLatLng(northEast.lat() + add, northEast.lng() + add));

    // Reset zoom level
    map.setZoom(map.getBoundsZoomLevel(bounds));
    // Reset center
    map.setCenter(bounds.getCenter());
}
  
// Create MapLoad function
function mapLoad() {
    if ($("#locationTab")!= null){
	$("#locationTab").show();  
    } 
    if($("#locationPanel")!= null) {
	$("#locationPanel").show();
    }
    //Set focus on correct tab
    if (tabStrip != null){
        var tab = tabStrip.findTabByValue('locationTab');
        tab.set_selected(true);
    }
	//Set all markers and end MapLoad function
	for (var i=0; i<gmarkers.length; i++) {
		map.addOverlay(gmarkers[i]);
    }
    if (!all) {
        setBounds();
    }
}
