/*
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//	Copyright (C) 2007 Cro-Cec, Inc. dba Digital Solutions.
//	A complete description of Digital Solutionsâ€™ copyright notice can be found online at: 
//	http://www.digitalsolutionslc.com/copyright_notice.php 
//		
//	Digital Solutions is a premier marketing and web development company in Las Cruces, New Mexico. 
//	We offer professional web design including flash and database web sites, graphic design, marketing materials, 
//	and video production. 
//
//	If you enjoyed this website and are looking for custom web development, give us a call at (505) 523-7661.
//		
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
*/
//<![CDATA[ 
var map_listings = null;				 
 
var mls_num = [];  
var street_num = [];  
var street_address = [];  
var price = [];  
var beds = [];  
var baths = [];  
var square_footage = [];  
var description = [];  
var virtual_tour_url = [];  
var home_latitude = [];  
var home_longitude = [];    
var photo = [];   
var i_marker_listings = 0;				
var MyMarkers_listings =[];			
var markers_listings =[];					
var point_listings = [];
var marker;
var overlayInstance = null;
var map;
var client;
var lastMarkerLocation;
var panorama;
var mapT;

function load_listings(url_google) 
{ 	
 
	if (GBrowserIsCompatible()) 
	{
		// Google Streetview Code
		client = new GStreetviewClient();
		
		
		
		///
		
		map_listings = new GMap2(document.getElementById("map_listings")); 
		map_listings.addControl(new GLargeMapControl());
		map_listings.addControl(new GMapTypeControl()); 
		map_listings.setCenter(new GLatLng(35.10446, -106.62969), 11 );
		map_listings.setMapType(G_NORMAL_MAP);		
		map_listings.disableDoubleClickZoom();
		map_listings.addControl(new GOverviewMapControl());
		map_listings.addControl(new GScaleControl());
	 	 //
		 
		 var latlng = new GLatLng(35.178807, -106.62969);
		var guyIcon = new GIcon(G_DEFAULT_ICON);
		guyIcon.iconSize = new GSize(49, 52); 
		guyIcon.iconAnchor = new GPoint(25, 35);  // near base of guy's feet
		guyIcon.infoWindowAnchor = new GPoint(25, 5);  // top of guy's head 
  		guyIcon.image = "http://maps.google.com/intl/en_us/mapfiles/cb/man_arrow-0.png";
  		guyIcon.transparent = "http://maps.google.com/intl/en_us/mapfiles/cb/man-pick.png";
  		guyIcon.imageMap = [
        26,13, 30,14, 32,28, 27,28, 28,36, 18,35, 18,27, 16,26,
        16,20, 16,14, 19,13, 22,8
     	];
		
		marker = new GMarker(latlng, {icon: guyIcon, draggable: true});
 		map_listings.addOverlay(marker);
 		lastMarkerLocation = latlng;
  		GEvent.addListener(marker, "dragend", onDragEnd);
  		GEvent.addListener(marker, "click", openPanoramaBubble);
  		//toggleOverlay();
		 //
		 
		var url_listings = url_google;
		//prompt("url_listings",url_listings);
		GDownloadUrl(url_listings, function(data) 
		{ 
			
			var xml_listings = GXml.parse(data);
			markers_listings = xml_listings.documentElement.getElementsByTagName("markers_listing");
			//alert(markers_listings.length);
			var current_id_listings;  
			for (i_marker_listings = 0; i_marker_listings < markers_listings.length; i_marker_listings++) 
			{
				 
				current_id_listings 							= markers_listings[i_marker_listings].getAttribute("mls_num");
				mls_num[current_id_listings] 					= markers_listings[i_marker_listings].getAttribute("mls_num");
				street_num[current_id_listings] 				= markers_listings[i_marker_listings].getAttribute("street_num");
				street_address[current_id_listings] 			= markers_listings[i_marker_listings].getAttribute("street_address");
				price[current_id_listings] 						= markers_listings[i_marker_listings].getAttribute("price"); 
				beds[current_id_listings] 						= markers_listings[i_marker_listings].getAttribute("beds");
				baths[current_id_listings] 						= markers_listings[i_marker_listings].getAttribute("baths");
				square_footage[current_id_listings] 			= markers_listings[i_marker_listings].getAttribute("square_footage");
				description[current_id_listings] 				= markers_listings[i_marker_listings].getAttribute("description"); 
				virtual_tour_url[current_id_listings] 			= markers_listings[i_marker_listings].getAttribute("virtual_tour_url");
				home_latitude[current_id_listings] 				= markers_listings[i_marker_listings].getAttribute("home_latitude");
				home_longitude[current_id_listings] 			= markers_listings[i_marker_listings].getAttribute("home_longitude"); 		
				photo[current_id_listings] 						= markers_listings[i_marker_listings].getAttribute("photo"); 
				  
				point_listings[current_id_listings] 		= new GLatLng(home_latitude[current_id_listings],-home_longitude[current_id_listings],false);
				MyMarkers_listings[current_id_listings] 	= createMarker_listings(point_listings[current_id_listings],	mls_num[current_id_listings],	street_num[current_id_listings],	street_address[current_id_listings],	price[current_id_listings],	beds[current_id_listings],	baths[current_id_listings],	square_footage[current_id_listings],	description[current_id_listings] ,	virtual_tour_url[current_id_listings],	home_latitude[current_id_listings],	home_longitude[current_id_listings],	photo[current_id_listings], current_id_listings);
				
				map_listings.addOverlay(MyMarkers_listings[current_id_listings]);
			}
		});
		 
	}
	
}
function createMarker_listings(point_listings, mls_num, street_num, street_address, price, beds, baths, square_footage, description , virtual_tour_url, home_latitude, home_longitude, photo, current_id_listings) 
{
  
	var icon_listings 					= new GIcon();
    icon_listings.image 				= "http://www.johncastillo.com/listings/google_maps/images/flag.png";
    icon_listings.shadow 				= "http://www.johncastillo.com/listings/google_maps/images/shadow-flag.png";
    icon_listings.iconSize 				= new GSize(35.0, 35.0);
    icon_listings.shadowSize 			= new GSize(53.0, 35.0);
    icon_listings.iconAnchor 			= new GPoint(17.0, 17.0);
    icon_listings.infoWindowAnchor 		= new GPoint(17.0, 17.0);
	
 	//alert(mls_num);
	
	var marker_listings = new GMarker(point_listings, icon_listings);
	var info 	= "<b>MLS #:  " + mls_num +  "</b><br/>Price:  " + price + "</b><br/>Beds:  " + beds+ "</b><br/>Baths:  " + baths + "</b><br/>Sq Ft:  " + square_footage + "</b><br/>" + street_num + " " + street_address + "<br/><br/><a href=\"javascript:activateWindowListing('listing.php?num="+ mls_num +"','" + street_num + " " + street_address + "');\">View More &raquo;</a><br /><br />";
    var photo2 	= "<center><a href='javascript:activateWindowListing('listing.php?num="+ mls_num +"','" + street_num + " " + street_address + "');'><img src='" + photo + "' width='250' border='0'  /></a><br><br /><br /></center>";
   // var desc 	= "<b>description</b>";
    
	var infoTabs = [
          new GInfoWindowTab("Info", "test"),
          new GInfoWindowTab("Photo", "test"),
         // new GInfoWindowTab("Description", desc) 
        ];
	
	GEvent.addListener(marker_listings, 'click', function() 
	{
		marker_listings.openInfoWindowHtml(info);
		map_listings.setCenter(point_listings, 14,G_NORMAL_MAP);
	}); 
		
	return marker_listings;
}

function map_listing(map_mls_num) 
{
	if(document.getElementById("mls_map_row").style.display = 'none'){toggle_map();}
	GEvent.trigger(MyMarkers_listings[map_mls_num], "click");
	map_listings.setCenter(point_listings[map_mls_num], 14,G_NORMAL_MAP);
}


/// Streetview Functions

function openPanoramaBubble() {
  var contentNode = document.createElement('div');
  contentNode.style.textAlign = 'center';
  contentNode.style.width = '500px';
  contentNode.style.height = '300px';
  contentNode.innerHTML = 'Loading panorama';
  marker.openInfoWindow("<div id='pano' style='width:500px;height:300px; float:leftl'></div>", {maxContent: contentNode, maxTitle: "Full screen"});

  panorama = new GStreetviewPanorama(document.getElementById("pano"));
  panorama.setLocationAndPOV(marker.getLatLng(), null);
  GEvent.addListener(panorama, "error", handleNoFlash);
    
	function handleNoFlash(errorCode) {
 	 if (errorCode == 600) {
    	contentNode.innerHTML='Im sorry, there is no street view data for that particular location.';
    return;
  }
  if (errorCode == 603) {
    	contentNode.innerHTML='Im sorry, you must have flash installed in your browser.';
    return;
  }
}  
  GEvent.addListener(panorama, "newpano", onNewLocation);
  GEvent.addListener(panorama, "yawchanged", onYawChange); 

  var iw = map_listings.getInfoWindow();
  GEvent.addListener(iw, "maximizeend", function() {
    panorama.setContainer(contentNode);  
    window.setTimeout("panorama.checkResize()", 5);
  });
}

////

function toggleOverlay() {
  if (!overlayInstance) {
    overlayInstance = new GStreetviewOverlay();
    map_listings.addOverlay(overlayInstance);
  } else {
    map_listings.removeOverlay(overlayInstance);
    overlayInstance = null;
  }
}

function onYawChange(newYaw) {
  var GUY_NUM_ICONS = 16;
  var GUY_ANGULAR_RES = 360/GUY_NUM_ICONS;
  if (newYaw < 0) {
    newYaw += 360;
  }
  guyImageNum = Math.round(newYaw/GUY_ANGULAR_RES) % GUY_NUM_ICONS;
  guyImageUrl = "http://maps.google.com/intl/en_us/mapfiles/cb/man_arrow-" + guyImageNum + ".png";
  marker.setImage(guyImageUrl);
}

function onNewLocation(lat, lng) {
  var latlng = new GLatLng(lat, lng);
  marker.setLatLng(latlng);
}

function onDragEnd() {
  var latlng = marker.getLatLng();
  if (panorama) {
    client.getNearestPanorama(latlng, onResponse);
  }
}

function onResponse(response) {
  if (response.code != 200) {
	  if(lastMarkerLocation == "" || lastMarkerLocation == null)
	  {
		lastMarkerLocation = "35.178807, -106.62969";
	  }
    marker.setLatLng(lastMarkerLocation);
  } else {
    var latlng = new GLatLng(response.Location.lat, response.Location.lng);
    marker.setLatLng(latlng);
    lastMarkerLocation = latlng;
    openPanoramaBubble();
  }
}