// DECLARE MAP OBJECTS 
var mapObj = new classMap();
var markersCommunity = new Array();
var markersRestaurant = new Array();
var markersSchools = new Array();
var markersHotel = new Array();
var markersRetail = new Array();
var markersRTABus = new Array();
var markersRTAFerry = new Array();
var markersRTAMetro = new Array();
var pointsmarkersCommunity = new Array();
var pointsmarkersRestaurant = new Array();
var pointsmarkersRetail = new Array();
var pointsmarkersSchools = new Array();
var pointsmarkersRTABus = new Array();
var pointsmarkersHotel = new Array();
var pointsmarkersRTAFerry = new Array();
var pointsmarkersRTAMetro = new Array();
var whichPoints = new Array();
var points = new Array();

function classMap()
{
	return {
		getMapMarkersAll : function( nelat, nelng, swlat, swlng ) { 

			//var infowindow = new google.maps.InfoWindow();

			// FOR EACH MARKER
			$('.markers').each(function()
			{
				// SET TO CURRENT
				if( $(this).hasClass('active') )
              		  	{
					// GET MARKERS FROM SERVER
					markerName = $(this).attr('id');
					//markerName = 'markersCommunity';
					//alert( "markerName = " + markerName );
                       			var params = "func=" + markerName + "&nelat=" + nelat + "&nelng=" + nelng + "&swlat=" + swlat + "&swlng=" + swlng;
                       			//alert( params );                        

					$.ajax(
                     			{
                             			type: "GET",
                             			async: false,
                             			url: "/cgi-bin/mycom.cgi",
                             			data: params,
                             			dataType: "xml",
                             			success: function(data)
                             			{
                                     			if ( $(data).find('RESULT').text() == "TRUE")
                                     			{
								// FOR EACH POINT CHECK POINTS ARRAY
								// IF NO POINTS ARRAY
								//alert( eval( "points" + markerName + ".length"  ) );
								if ( eval( "points" + markerName + ".length == 0" ) )
								{
									//alert(' Array Does NOT Exist' );
									// ADD ALL POINTS TO ARRAY AND SHOW ON MAP
									$(data).find('MARKER').each( function()
									{
										// CREATE GOOGLE LATLNG OBJECT
										rawDigits = $(this).find('POINT').text();
                                                     				LatLng = rawDigits.split(",");
                                                     				LatLng[0] = LatLng[0].replace("(","");
                                                     				LatLng[1] = LatLng[1].replace(")","");
                                                     				latlngmrk = new google.maps.LatLng( parseFloat(LatLng[0]), parseFloat(LatLng[1]) );

										// ADD POINTS TO ARRAY
										eval( "points" + markerName + ".push(rawDigits);" );

										// CREATE GOOGLE MAPS MARKERS
 										switch( markerName )
										{
											case "markersCommunity" : 
												var URL = $(this).find('URL').text();
                                                                                                if( URL.search(/algurm/) != -1 || URL.search(/alzeina/) != -1 || URL.search(/alrahagardens/) != -1 || URL.search(/almuneera/) != -1 || URL.search(/albandar/) != -1 )
                                                                                                {
                                                                                                        var image = '/img/map/gmap-icon-markersCommunity.png';
                                                                                                } else  {
                                                                                                                var image = '/img/gmap-icon.png';
                                                                                                        }
												//alert( $(this).find('OANAME').text() );
												//if( $(this).find('OANAME').text() == "801 Tower"  || $(this).find('OANAME').text() == "802 Tower")
                                                                                		//{       
                                                                                        	//	image = '/img/gmap-icon-red.png';
                                                                                		//} else  { 
                                                                                                //		image = '/img/gmap-icon.png';
                                                                                        	//	}       
												// CREATE GOOGLE MAP MARKER OBJECT
												var marker;
												var oaname = $(this).find('OANAME').text();
					                                                        marker = new google.maps.Marker(
                                                                                        	{
                                                                                                	position: latlngmrk,
                                                                                                	map: map,
                                                                                                	icon: image,
                                                                                                	title: oaname
                                                                                        	});
												// MAP CALL BACKS
                                                                                        	marker.setMap( map );
												var URL = $(this).find('URL').text();
												//marker.contentString = "<table><tr><td valign=center><a href=\"javascript:document.location='/community/" + URL + "';\"><b>" + oaname + "</a></b></td></tr></table>";
 												if( oaname == "Al Gurm" )
                                                                                                {
                                                                                                        marker.contentString = "<table><tr><td valign=center><b>" + oaname + "</b></td></tr><tr><td><img src='/static/images/property-thumbs/gurm.jpg'></td></tr><tr><td style='font-size:8pt'>Located on Coast Road within Abu Dhabi island, this Eco friendly development is situatied on 1.8 million sq/m of mangrove reserve.  It has protected mangrove forest and wildlife with concealed access roads and a strict Wildlife First policy.  It's a gated community with 24 hr security services</td></tr></table>";
                                                                                                } else if( oaname == "Al Raha Gardens" )
                                                                                                        {
                                                                                                                marker.contentString = "<table><tr><td valign=center><b><a href='http://www.my-community.com/community/" + URL + "'>" + oaname + "</a></b></td></tr><tr><td><img src='/static/images/property-thumbs/raha-gardens.jpg'></td></tr><tr><td style='font-size:8pt'>Located on Coast Road within Abu Dhabi island, this Eco friendly development is situatied on 1.8 million sq/m of mangrove reserve.  It has protected mangrove forest and wildlife with concealed access roads and a strict Wildlife First policy.  It's a gated community with 24 hr security services</td></tr></table>";
                                                                                                        } else if( oaname == "Al Zeina Precinct A" )
                                                                                                                {
                                                                                                                        marker.contentString = "<table><tr><td valign=center><b><a href='http://www.my-community.com/community/" + URL + "'>" + oaname + "</a></b></td></tr><tr><td><img src='/static/images/property-thumbs/zeina.jpg'></td></tr><tr><td style='font-size:8pt'>Located on Coast Road within Abu Dhabi island, this Eco friendly development is situatied on 1.8 million sq/m of mangrove reserve.  It has protected mangrove forest and wildlife with concealed access roads and a strict Wildlife First policy.  It's a gated community with 24 hr security services</td></tr></table>";
                                                                                                                } else if( oaname == "Al Muneera" )
                                                                                                                        {
                                                                                                                                marker.contentString = "<table><tr><td valign=center><b>" + oaname + "</b></td></tr><tr><td><img src='/static/images/property-thumbs/muneera.jpg'></td></tr><tr><td style='font-size:8pt'>Located on Coast Road within Abu Dhabi island, this Eco friendly development is situatied on 1.8 million sq/m of mangrove reserve.  It has protected mangrove forest and wildlife with concealed access roads and a strict Wildlife First policy.  It's a gated community with 24 hr security services</td></tr></table>";
                                                                                                                        } else  {
                                                                                                                                        marker.contentString = "<table><tr><td valign=center><b>" + oaname + "</b></td></tr></table>";
                                                                                                                                }

                                                                                        	// ONCLICK SHOW INFO WINDOW
                                                                                        	google.maps.event.addListener(marker, 'click', function()
                                                                                        	{
                                                                                        	        // CLOSE ALL OTHER INFO WINDOWS
                                                                                                	infowindow.setContent( this.contentString );
                                                                                                	infowindow.open(map,this);
                                                                                        	});
												markersCommunity.push(marker);
												break;
											default :
													image = '/img/map/gmap-icon-' + markerName + '.png';
                                                                                                        var marker;
                                                                                                        marker = new google.maps.Marker(
                                                                                                        {
                                                                                                                position: latlngmrk,
                                                                                                                map: map,
                                                                                                                icon: image,
                                                                                                                title: $(this).find('NAME').text()
                                                                                                        });

                                                                                                        marker.setMap(map);
                                                                                                        marker.contentString = "<table><tr><td valign=center><a href='#'>" + $(this).find('NAME').text() + "</a><br>" + $(this).find('DESCRIPTION').text()  + "</td></tr></table>";
                                                                                                        google.maps.event.addListener(marker, 'click', function()
                                                                                                        {
                                                                                                                infowindow.setContent( this.contentString );
                                                                                                                infowindow.open(map,this);
                                                                                                        });
                                                                                                        eval( markerName + ".push(marker)" );
												break;
										} // END SWITCH
									}); // END EACH
								} else	{
										//alert( 'Array Does Exist' );
										$(data).find('MARKER').each( function()	
                                      						{
                                              						// CREATE GOOGLE LATLNG OBJECT
                                              						rawDigits = $(this).find('POINT').text();
                                              						LatLng = rawDigits.split(",");
                                              						LatLng[0] = LatLng[0].replace("(","");
                                              						LatLng[1] = LatLng[1].replace(")","");
                                              						latlngmrk = new google.maps.LatLng( parseFloat(LatLng[0]), parseFloat(LatLng[1]) );

											//alert( 'points array exist' );
											//alert( 'rawDigits -> ' + rawDigits );

											// GO THROUGH EACH POINT AND SEE IF LATLNGMRK IS PRESENT
											var flag = 0;
											for( k=0; k < eval("points" + markerName).length; k++ )
											{
												if ( rawDigits == eval("points" + markerName)[k] ) { flag = 1; }
											} // END FOR EACH POINT
											//alert( 'flag -> ' + flag );

											// IF POINT NOT IN DATABASE -> ADD
											if( flag == 0 )
											{
												// ADD POINTS TO POINT AND MAP
												//alert( 'adding point to points array and adding to map' );

												// ADD POINTS TO ARRAY
                                              							eval( "points" + markerName + ".push( rawDigits );" );

												// CREATE GOOGLE MAPS MARKERS
                                                                                		switch( markerName )
                                                                                		{
                                                                                        		case "markersCommunity" :
														var URL = $(this).find('URL').text();
                                                                                                		if( URL.search(/algurm/) != -1 || URL.search(/alzeina/) != -1 || URL.search(/alrahagardens/) != -1 || URL.search(/almuneera/) != -1 || URL.search(/albandar/) != -1 )
                                                                                                		{
                                                                                                		        var image = '/img/map/gmap-icon-markersCommunity.png';
                                                                                                		} else  {
                                                                                                        		        var image = '/img/gmap-icon.png';
                                                                                                       			}
                                                                                                		// CREATE GOOGLE MAP MARKER OBJECT
                                                                                                		var marker;
														var oaname = $(this).find('OANAME').text();
                                                                                                		marker = new google.maps.Marker(
                                                                                                		{
                                                                                                        		position: latlngmrk,
                                                                                                        		map: map,
                                                                                                        		icon: image,
                                                                                                        		title: oaname
                                                                                                		});
                                                                                                
														// MAP CALL BACKS
                                                                                                		marker.setMap(map);
                                                                                                		var URL = $(this).find('URL').text();
														//marker.contentString = "<table><tr><td valign=center><a href=\"javascript:document.location='/community/" + URL + "';\"><b>" + oaname + "</a></b></td></tr></table>";
 														if( oaname == "Al Gurm" )
                                                                                                		{
                                                                                                        		marker.contentString = "<table><tr><td valign=center><b>" + oaname + "</b></td></tr><tr><td><img src='/static/images/property-thumbs/gurm.jpg'></td></tr><tr><td style='font-size:8pt'>Located on Coast Road within Abu Dhabi island, this Eco friendly development is situatied on 1.8 million sq/m of mangrove reserve.  It has protected mangrove forest and wildlife with concealed access roads and a strict Wildlife First policy.  It's a gated community with 24 hr security services</td></tr></table>";
                                                                                                		} else if( oaname == "Al Raha Gardens" )
                                                                                                		        {
                                                                                                        		        marker.contentString = "<table><tr><td valign=center><b><a href='http://www.my-community.com/community/" + URL + "'>" + oaname + "</a></b></td></tr><tr><td><img src='/static/images/property-thumbs/raha-gardens.jpg'></td></tr><tr><td style='font-size:8pt'>Located on Coast Road within Abu Dhabi island, this Eco friendly development is situatied on 1.8 million sq/m of mangrove reserve.  It has protected mangrove forest and wildlife with concealed access roads and a strict Wildlife First policy.  It's a gated community with 24 hr security services</td></tr></table>";
                                                                                                        		} else if( oaname == "Al Zeina Precinct A" )
                                                                                                                		{
                                                                                                                		        marker.contentString = "<table><tr><td valign=center><b><a href='http://www.my-community.com/community/" + URL + "'>" + oaname + "</a></b></td></tr><tr><td><img src='/static/images/property-thumbs/zeina.jpg'></td></tr><tr><td style='font-size:8pt'>Located on Coast Road within Abu Dhabi island, this Eco friendly development is situatied on 1.8 million sq/m of mangrove reserve.  It has protected mangrove forest and wildlife with concealed access roads and a strict Wildlife First policy.  It's a gated community with 24 hr security services</td></tr></table>";
                                                                                                                		} else if( oaname == "Al Muneera" )
                                                                                                                		        {
                                                                                                                        		        marker.contentString = "<table><tr><td valign=center><b>" + oaname + "</b></td></tr><tr><td><img src='/static/images/property-thumbs/muneera.jpg'></td></tr><tr><td style='font-size:8pt'>Located on Coast Road within Abu Dhabi island, this Eco friendly development is situatied on 1.8 million sq/m of mangrove reserve.  It has protected mangrove forest and wildlife with concealed access roads and a strict Wildlife First policy.  It's a gated community with 24 hr security services</td></tr></table>";
                                                                                                                        		} else  {
                                                                                                                                		        marker.contentString = "<table><tr><td valign=center><b>" + oaname + "</b></td></tr></table>";
                                                                                                                               			 }

                                                                                                		// ONCLICK SHOW INFO WINDOW
                                                                                                		google.maps.event.addListener(marker, 'click', function()
                                                                                                		{
                                                                                                        		// CLOSE ALL OTHER INFO WINDOWS
															infowindow.setContent( this.contentString );
                                                                                                        		infowindow.open(map,this);
                                                                                                		});
                                                                                                		markersCommunity.push(marker);
                                                                                                		break;
                                                                                        		default :
                                                                                                		image = '/img/map/gmap-icon-' + markerName + '.png';
														var marker;
                                                                                                		marker = new google.maps.Marker(
														{ 
															position: latlngmrk, 
															map: map, 
															icon: image, 
															title: $(this).find('NAME').text() 
														});

                                                                                                		marker.setMap(map);
                                                                                                		marker.contentString = "<table><tr><td valign=center><a href='#'>" + $(this).find('NAME').text() + "</a><br>" + $(this).find('DESCRIPTION').text()  + "</td></tr></table>";
                                                                                                		google.maps.event.addListener(marker, 'click', function() 
														{ 
															infowindow.setContent( this.contentString );
															infowindow.open(map,this); 
														});
                                                                                                		eval( markerName + ".push(marker)" );
                                                                                                		break;
                                                                                		} // END SWITCH
											} else 	{
													//alert( 'not adding point' );
												} // END IF FLAG == 0
										}); // END FOR EACH POINT IN ARRAY
									} // END IF !POINTSMARKER
								} else	{  // END IF RESULT == TRUE
										displayError( 'An Error has occured ' + $(data).find('ERROR').text() );
									}
						} // END AJAX SUCCESS CONTAINER
					}); // END AJAX REQUESTER
				} // END IF LEGEND ICON HAS CLASS CURRENT
			}); // END FOREACH EACH LEGEND ICON
		},  // END METHOD GETMAPMARKERSALL

		clearMapMarkers : function( markerArray )
		{
			var num = eval ( markerArray + ".length" );
			eval( "points" + markerArray + ".splice(0," + num + ")" );
			for( i=0; i<num; i++ )
			{
			        eval( markerArray + "[i].setMap(null);" );
			}
		} // END METHOD CLEARMAPMARKERS
	}; // END RETURN

}; // END CLASS GETMAPMARKER

function loadMap( div, loco )
{
	//alert( get_cookie('mapCenter') );
        // INITIALISE MAP
	//if( get_cookie( 'mapCenter' ) == false || get_cookie( 'mapCenter') == "false" || get_cookie( 'mapCenter' ) == "undefined" || !get_cookie( 'mapCenter') || get_cookie( 'mapCenter' ) == '' )
        //{
		if( loco == '' || !loco )
		{
        		myLatLng = new google.maps.LatLng(24.438711,54.539394);
                	myOptions =     {
                	                        zoom: 13,
                	                        center: myLatLng,
                	                        mapTypeId: google.maps.MapTypeId.SATELLITE
                	                }
                	map = eval( "new google.maps.Map(document.getElementById(\"" + div + "\"), myOptions);" );
		}
		if( loco == "alzeina" )
		{
			myLatLng = new google.maps.LatLng(24.449337,54.583855);
                        myOptions =     {
                                                zoom: 14,
                                                center: myLatLng,
                                                mapTypeId: google.maps.MapTypeId.SATELLITE
                                        }
                        map = eval( "new google.maps.Map(document.getElementById(\"" + div + "\"), myOptions);" );
		}
        //} else  {
          //              rawDigits = get_cookie('mapCenter');
           //             LatLng = rawDigits.split(",");
            //            LatLng[0] = LatLng[0].replace("(","");
            //            LatLng[1] = LatLng[1].replace(")","");
            //            myLatLng = new google.maps.LatLng( parseFloat(LatLng[0]), parseFloat(LatLng[1]) );
            //            zoomLevel = parseInt( get_cookie( 'mapZoom' ) );
             //           myOptions =     {
             //                                   zoom: zoomLevel,
             //                                   center: myLatLng,
             //                                   mapTypeId: google.maps.MapTypeId.SATELLITE
             //                           };
             //   	map = eval( "new google.maps.Map(document.getElementById(\"" + div + "\"), myOptions);" );
             //   }

	infowindow = new google.maps.InfoWindow();

        // SET CALL BACK TRIGGER TO POPULATE MARKERS
        google.maps.event.addListener(map, 'idle', function()
        {
                var bounds = map.getBounds();
               	//alert("Google API Bounds: " + bounds);
                var bounds = bounds.toString();
                var points = bounds.split(",");
                swlat = points[0].replace(/\(/g,"");
                swlng = points[1].replace(/\)/g,"");
                nelat = points[2].replace(/\(/g,"");
                nelng = points[3].replace(/\)/g,"");
                //alert( "South-West Lat: " + swlat + "\nSouth-West Long: " + swlng + "\nNorth-East Lat: " + nelat + "\nNorth-East Long: " + nelng );
                //var mapZoom = map.getZoom();
                //var mapCenter = map.getCenter();
		//set_cookie("mapCenter",mapCenter, 365, "/", "", "", "" );
                //set_cookie("mapZoom",mapZoom, 365, "/", "", "", "" );
                mapObj.getMapMarkersAll( nelat, nelng, swlat, swlng );
	});
}


function mapit( communityid )
{

        if( document.getElementById('map_canvas') )
        {
                params = "func=mapit&communityid=" + communityid;

                $.ajax(
                {
                        type: "POST",
                        async: false,
                        url: "/cgi-bin/mycom.cgi",
                        data: params,
                        dataType: "xml",
                        success: function(data)
			{
				$(data).find('RESULTS').each( function()
				{
					var result = $(this).find('RESULT').text();
                                	if (result == "TRUE")
                                	{
                                	        var latlng = $(this).find('GEOPOINT').text();
                                	        var ll = latlng.split(",");
                                	        ll[0] = ll[0].replace(/\(/,"");
                                	        ll[1] = ll[1].replace(/\(/,"");
						ll[2] = parseFloat(ll[0]) + 0.007;
						latlngcnt = new google.maps.LatLng( parseFloat(ll[2]), parseFloat(ll[1]) );
						latlngmrk = new google.maps.LatLng( parseFloat(ll[0]), parseFloat(ll[1]) );
                                	        contentString = "<b>" + currentCommunityName + "</b>";
                                	}
				});
                        }
                });

        } else  {
			//alert('no map canvas');
                        return;
                }


        var image = '/img/gmap-icon.png';
        var myOptions = {
                                zoom: 14,
                                center: latlngcnt,
                                mapTypeId: google.maps.MapTypeId.SATELLITE
                        };

        var map = new google.maps.Map( document.getElementById('map_canvas'), myOptions );

        var infowindow = new google.maps.InfoWindow(
        {
                content: contentString
        });

        var marker = new google.maps.Marker(
        {
                position: latlngmrk,
                map: map,
                icon: image
        });

        infowindow.open(map, marker);
}

function initialiseMap( div )
{
        // create the map
        var myOptions =  {
                        zoom: 12,
                        center: new google.maps.LatLng(24.272005,54.303017),
                        mapTypeControl: true,
                        mapTypeControlOptions: {style: google.maps.MapTypeControlStyle.DROPDOWN_MENU},
                        navigationControl: true,
                        mapTypeId: google.maps.MapTypeId.SATELLITE
                        }
	
        mapid = "new google.maps.Map(document.getElementById(\"" +  div + "\"), myOptions)";
	map = eval( mapid );

	infowindow = new google.maps.InfoWindow({ size: new google.maps.Size(150,50) });
        google.maps.event.addListener(map, 'click', function() { infowindow.close(); });

        google.maps.event.addListener(map, 'click', function(event)
        {
                //call function to create marker
                if ( typeof marker != 'undefined' )
                {
                        marker.setMap(null);
                        marker = null;
                }

                marker = createMarker(event.latLng, "name", "<font size=1 face=arial><b>Location</b><br>"+event.latLng);
                geoloco = event.latLng.toString();
                geoloco = geoloco.replace(/\(/g,"");
                geoloco = geoloco.replace(/\)/g,"");
                geoloco = geoloco.replace(/" "/g,"");
                $('#geolnglat').val( geoloco )
        });
}

function createMarker(latlng, name, html)
{
        var contentString = html;
        var marker = new google.maps.Marker({
                position: latlng,
                map: map,
                zIndex: Math.round(latlng.lat()*-100000)<<5
        });

        google.maps.event.addListener(marker, 'click', function(){
                infowindow.setContent(contentString);
                infowindow.open(map,marker);
        });

        google.maps.event.trigger(marker, 'click');
        return marker;
}

