var map = null;
var geocoder = null;
var directionsPanel, directions, here, kirandulas_directions;
var gmarkers = [];

var iconBlue = new GIcon();
iconBlue.image = 'images/mm_20_blue.png';
iconBlue.shadow = 'images/mm_20_shadow.png';
iconBlue.iconSize = new GSize(12, 20);
iconBlue.shadowSize = new GSize(22, 20);
iconBlue.iconAnchor = new GPoint(6, 20);
iconBlue.infoWindowAnchor = new GPoint(5, 1);

var iconRed = new GIcon();
iconRed.image = 'images/mm_20_red.png';
iconRed.shadow = 'images/mm_20_shadow.png';
iconRed.iconSize = new GSize(12, 20);
iconRed.shadowSize = new GSize(22, 20);
iconRed.iconAnchor = new GPoint(6, 20);
iconRed.infoWindowAnchor = new GPoint(5, 1);

var iconGreen = new GIcon();
iconGreen.image = 'images/mm_20_green.png';
iconGreen.shadow = 'images/mm_20_shadow.png';
iconGreen.iconSize = new GSize(12, 20);
iconGreen.shadowSize = new GSize(22, 20);
iconGreen.iconAnchor = new GPoint(6, 20);
iconGreen.infoWindowAnchor = new GPoint(5, 1);

var customIcons = [];
customIcons["program"] = iconRed;
customIcons["szallas"] = iconGreen;
customIcons["vendeglato"] = iconBlue;



function setDirections(fromAddress, toAddress) {
	      directions.load("from: " + fromAddress + " to: " + toAddress);
}

function kirandulas(varosok) {
		//document.write("<!-- " + varosok + " -->");
	    kirandulas_directions.load(varosok);
}


function createMarker(point, type, name, town, address, description, image, id, link, load_type) {  
	var marker = new GMarker(point, {icon: customIcons[type], title: name + " - " + town + ", " + address});  

    GEvent.addListener(marker, "click", function() {    
	here = point.toString();
	here = here.substring(1,here.length-1);
	var html = "<a href=# onclick='document.forms.direction.from.value=here;'>innen</a> || ";
	html = html + "<a href=# onclick='document.forms.direction.to.value=here;'>ide</a><br>";

	var tab = "<div style='width: 480px; height: 150px;'><img src='upload/tn_" + image + "' border=0 align=left style='padding-right: 16px;'><span style='font-weight: bold; color: #444444; text-transform: uppercase'>" + name + "</span><br/>";
	tab += town + ", " + address + "<br/><br />";
	tab += description;
	if (link != "")
	{
		tab += "<a ";
		if (load_type != "0") { tab += "onclick=\"opener.location='" + link + "'\" href=#"; }
		else { tab += "href='" + link + "'"; }
		tab += ">bővebben &raquo;</a>";
	}
	tab += "</div>";


	var infoTabs = [  
	    new GInfoWindowTab("Info", tab),  
	    new GInfoWindowTab("Útvonalterv", html)
	    ];
	
	marker.openInfoWindowTabsHtml(infoTabs);

	}); 
	marker.mycategory = type;
	marker.myid = id;
	marker.point = point;
    gmarkers.push(marker);
	return marker;
}

// == a checkbox has been clicked ==
function boxclick(box,category) {
	if (box.checked) {
       show_markers(category);
    } else {
       hide_markers(category);
    }
}

// == shows all markers of a particular category, and ensures the checkbox is checked ==
function show_markers(category) {
   for (var i=0; i<gmarkers.length; i++) {
     if (gmarkers[i].mycategory == category) {
       gmarkers[i].show();
     }
   }
   // == check the checkbox ==
   document.getElementById(category+"box").checked = true;
}


// == shows a specific marker of a particular category ==
function show_marker_specific(category, id) {
   for (var i=0; i<gmarkers.length; i++) {
     if ((gmarkers[i].mycategory == category) && (gmarkers[i].myid == id)) {
       gmarkers[i].show();
       map.setCenter(gmarkers[i].point, 10);
     }
   }
}

// == hides all markers of a particular category, and ensures the checkbox is cleared ==
function hide_markers(category) {
   for (var i=0; i<gmarkers.length; i++) {
     if (gmarkers[i].mycategory == category) {
       gmarkers[i].hide();
     }
   }
   // == clear the checkbox ==
   document.getElementById(category+"box").checked = false;
   // == close the info window, in case its open on a marker that we just hid
   map.closeInfoWindow();
}


function load(load_type, load_id, varosok) {
 if (GBrowserIsCompatible()) {

	var point, marker_n;
	var addressMarker;
	var loadtype = load_type;


    map = new GMap2(document.getElementById("map"));
    directions = new GDirections(map, document.getElementById("directions"));
	
	kirandulas_directions = new GDirections(map, document.getElementById("directions"));
        
	var geocoder = new GClientGeocoder();
	map.addControl(new GLargeMapControl());
	map.addControl(new GMapTypeControl());
	map.addControl(new GScaleControl());
    map.setCenter(new GLatLng(47.44, 19.14), 8);
	
	map.enableScrollWheelZoom();
	map.enableContinuousZoom();
	map.getContainer().style.overflow="hidden";
	
	function wheelevent(e) {
	 if (!e){
	 e = window.event
	 }
	 if (e.preventDefault){
	 e.preventDefault()
	 }
	 e.returnValue = false;
	}

	GEvent.addDomListener(map.getContainer(), "DOMMouseScroll", wheelevent);
	map.getContainer().onmousewheel = wheelevent; 
      
     // ================================================================
     // === Define the function thats going to process the JSON file ===
     process_it = function(doc) {
       // === Parse the JSON document === 
       var jsonData = eval('(' + doc + ')');
        
		if (load_type == "kirandulasok")
		{
				var kirandulas_directions_n = new GDirections(map);
				kirandulas_directions_n.load(jsonData[0]['route']);

			  GEvent.addListener(kirandulas_directions_n,"load", function() { 
		        var poly=kirandulas_directions_n.getPolyline(); 
		        poly.color = "#FF0000"; 
		        poly.redraw(true) 
		      }); 


			 //  for (var i=0; i<jsonData.length; i++) {
			 //   kirandulas_directions.load(jsonData[i]['route']); }
		}
		else {
       // === Plot the markers ===
       for (var i=0; i<jsonData.length; i++) {
         point = new GLatLng(jsonData[i].lat, jsonData[i].lang);
		 var marker = createMarker(point, jsonData[i].type, jsonData[i].name, jsonData[i].town, jsonData[i].address, jsonData[i].description, jsonData[i].image, jsonData[i].id, jsonData[i].link, loadtype);
         map.addOverlay(marker);
         }
		}

		if (load_type == "0")
		{
        show_markers("szallas");
        show_markers("program");
        show_markers("vendeglato");
		}
        else if (load_type != "kirandulasok") {
			hide_markers("szallas");
	        hide_markers("program");
	        hide_markers("vendeglato");
			if (load_type == "kirandulas")
				{
				kirandulas(varosok);
				}
			else show_marker_specific(load_type, load_id);
		}
	 }          
      
     // ================================================================
     // === Fetch the JSON data file ====    
	if (load_type == "kirandulasok") GDownloadUrl("terkep_json.php?type=kirandulasok", process_it);
	else GDownloadUrl("terkep_json.php", process_it);
     // ================================================================



  }
}