var gmarkers = [];
var infoWindows = [];
var st_htmls = [];
var to_htmls = [];
var allLats = [];
allLats[0] = [];
allLats[1] = [];
allLats[2] = [];
allLats[3] = [];
allLats[4] = [];
allLats[5] = [];
var cntMaps = 0;
var divsMap = [];
var divsDir = [];
var map = [];
var directionsDisplay = [];
				
function initialize(lat,lng,zoom,div,directions,type,markerz) {
	divsMap[cntMaps] = div;
	divsDir[cntMaps] = directions;
	directionsDisplay[cntMaps] = new google.maps.DirectionsRenderer();
	var myOptions = {
		zoom: zoom,
		center: new google.maps.LatLng(lat,lng),
		mapTypeId: google.maps.MapTypeId[type]
	}
	map[cntMaps] = new google.maps.Map(document.getElementById(div),myOptions);
	directionsDisplay[cntMaps].setMap(map[cntMaps]);
	directionsDisplay[cntMaps].setPanel(document.getElementById(directions));
	setMarkers(map[cntMaps], markerz);
	var nbMarkers = allLats[cntMaps].length;
	var latlngbounds = new google.maps.LatLngBounds();
		 for ( var i = 0; i < allLats[cntMaps].length; i++ ) {
			latlngbounds.extend( allLats[cntMaps][i] );
		  }
	if(nbMarkers > 1) {
		  map[cntMaps].setCenter(latlngbounds.getCenter());
		  map[cntMaps].fitBounds(latlngbounds);
	}
	/*else {
		infoWindows[nbMarkers-1].open(map[cntMaps],gmarkers[nbMarkers-1]);
		//var tmpVar = map.getBounds();
		//map.fitBounds(latlngbounds);
		//alert(tmpVar);
		//alert(latlngbounds);
	}*/
	cntMaps++;
}

function setMarkers(map, locations) {
  // Add markers to the map 
  var cntIcon = 1;
  
  for (var i = 0; i < locations.length; i++) {
	var beach = locations[i];
	allLats[cntMaps].push(new google.maps.LatLng(beach[0],beach[1]));
					
	//Infowindow: Link to Directions
	//st_htmls[i+1] = beach[5]=='false' ? beach[2] : beach[2] + '<a href="javascript:tohere('+(i+1)+')" class="phase_map_content_link">Click here for Driving Directions</a>';
	st_htmls[infoWindows.length] = beach[5]=='false' ? beach[2] : beach[2] + '<a href="javascript:tohere('+(cntMaps)+','+(infoWindows.length)+','+(gmarkers.length)+')" class="phase_map_content_link"><img src="http://www.phase.com.au/google_maps/images/bt-directions.gif" alt="Click here for Driving Directions" /></a>';
	
	//Infowindow: Directions
	
	to_htmls[infoWindows.length] = beach[2] + '<span style="display:block;font-weight:bold;margin:10px 0 5px 0; font-size:14px">Get Driving Directions</span>' +
	   '<span style="font-size:12px">Start address:<form action="javascript:calcRoute(\''+cntMaps+'\')">' +
	   '<input type="text" style="width:150px;" name="saddr" id="saddr" value="" /><br>' +
	   '<input value="Get Directions" TYPE="SUBMIT">' +
	   '<input type="hidden" id="daddr" value="'+beach[0] + ',' + beach[1] + 
	   '"/>';
					   
					   
	if(!beach[3]) {
		//customIcon = 'http://www.phase.com.au/google_maps/icons/marker-'+(cntIcon)+'.png';
		customIcon = '';
		cntIcon++;
	}
	else {
		customIcon = 'http://www.phase.com.au/google_maps/icons/'+beach[3];
	}
	showUp = beach[4] ? true : false;
	var myLatLng = new google.maps.LatLng(beach[0], beach[1]);
	var marker = new google.maps.Marker({
		icon: customIcon,
		position: myLatLng,
		map: map,
		zIndex: i+1
	});
	
	//if(cntMaps == 1) alert(map);
	
	gmarkers.push(marker);
	attachInfo(marker,st_htmls[infoWindows.length],map,showUp);
	/*if(showUp) {
		  var infowindow = new google.maps.InfoWindow(
		  { content: st_htmls[i+1],
		 	size: new google.maps.Size(50,50)
		 });
		 infowindow.open(map,marker);
	}*/
  }
 // alert(map.getCenter());
}

//Attach infowindow to marker
function attachInfo(marker, content,map, autoshow,width,height,removeListener) {
	if(!autoshow) var autoshow = false;
  	var infowindow = new google.maps.InfoWindow({ content: content});
	infowindow.set("isdomready", true);
	
	infoWindows.push(infowindow);
		
	if(!removeListener) {
		google.maps.event.addListener(marker, 'click', function() {
			infowindow.open(map,marker);
		});
	}
	//if(autoshow) infowindow.open(map,marker);
}

function calcRoute(i) {
	var start = document.getElementById("saddr").value;
	var end = document.getElementById("daddr").value;
	$("#"+divsDir[i]).fadeIn();
	var request = {
		origin:start, 
		destination:end,
		travelMode: google.maps.DirectionsTravelMode.DRIVING
	  };
	 directionsService.route(request, function(response, status) {
	if (status == google.maps.DirectionsStatus.OK) {
	  directionsDisplay[i].setDirections(response);
	}
	$("#"+divsDir[i]).prepend('<div id="phase_maps_close_'+i+'" style="float:right;display:block;padding:0 0 15px 0"><a href="javascript:void(0)" onclick="$(\'#'+divsDir[i]+'\').css(\'display\',\'none\');$(\'#phase_maps_close_'+i+'\').remove();"><img src="http://www.phase.com.au/google_maps/images/btclose.gif" title="Close directions" /></a></div>');
 });
}
				
function tohere(i,j,k) {
	infoWindows[j].set("isdomready", true);
	infoWindows[j].content = to_htmls[j];
	//infoWindows[j].height = '90px';
	infoWindows[j].open(map[i],gmarkers[k]);
	//attachInfo(gmarkers[i-1],to_htmls[i],map,true,50,100,true);
}