

var baseURL_local ="http://127.0.0.1:8080/agenttoolboxes/";
var baseURL_tahoe ="http://www.agenttoolboxes.com/";
var baseURL_HTTPS ="https://www.agenttoolboxes.com/";


var defaultMapZoomLevel =-1;

function getBaseURL(){
	var currLoc = ""+document.location;
	currLoc = currLoc.toLowerCase();
	if (currLoc.indexOf("http://")==0){
		if (currLoc.indexOf("http://127.0.0.1")==0){
			return baseURL_local;
		}else{
			return baseURL_tahoe;
		}
	}else{
		return baseURL_HTTPS;
	}
}

/**********************************************************
                       Map Location
 **********************************************************/ 
 
var mapLocationArray =new Array();
function MapLocation(){

	this.id=0;
	this.agentId=0;
	this.number="";
	this.title="";
	this.address="";
	this.address2="";
	
	this.phone="";
	this.website="";
		
	this.lat="";
	this.lng="";

	this.linkURL ="";
	
	this.iconURL ="";
	this.thumbURL ="";
	
	this.description ="";
	
	this.hidden = false;
}


function addMapLocation(mapLocation){
	
	mapLocation.number = unescape(mapLocation.number);
	mapLocation.title = unescape(mapLocation.title);
	mapLocation.address = unescape(mapLocation.address);
	mapLocation.address2 = unescape(mapLocation.address2);
	mapLocation.description = unescape(mapLocation.description);

	mapLocation.phone = unescape(mapLocation.phone);
	mapLocation.website = unescape(mapLocation.website);
	
	var defaultICON =getBaseURL()+"agentstore/js/googlemap/red_bubble.png";
	if (mapLocation.iconURL==""){
		mapLocation.iconURL = defaultICON;
	}	
	
	if (mapLocation.linkURL=="http://" ||mapLocation.linkURL=="https://"){
		mapLocation.linkURL="";
	}
	
	mapLocationArray[mapLocationArray.length] = mapLocation;
}

	


function getBubbleHtml_mapLocation(mapLocation){
	
	var thumbPhoto = mapLocation.thumbURL;
	
	var detailLink =mapLocation.linkURL;
	
	
	var topTextColor ="#085173";
	var topLineColor =topTextColor;
	
	var topText =  mapLocation.title;
	
	var html ="<table width='300' height='200' border='0' cellspacing='2' cellpadding='2'>";
	
	html +="\n<tr>";
	
	html +="\n<td>";
	html +="\n<span style='font-family: Tahoma, Arial; font-size: 16px;color: #5A6173;font-weight: bold;'>";
	if (detailLink && detailLink!=""){
			html +="\n <a href='"+detailLink+"' target='_blank' >";
	}
	html +="\n"+topText;
	if (detailLink && detailLink!=""){
			html +="\n </a>";
	}
	
	html +="\n</span>";
	
	html +="\n<br>";
	html +="\n<hr color='"+topLineColor+"' size='2'>";
	html +="\n</td>";
	html +="\n</tr>";
	
	html +="\n<tr><td>";
	
	html +="\n  <table width='100%' border='0' cellspacing='2' cellpadding='2'>";
	html +="\n  <tr>";
	
	if (thumbPhoto && thumbPhoto!=""){
		html +="<td width='40%'>";
		if (detailLink && detailLink!=""){
			html +="\n <a href='"+detailLink+"'  target='_blank'>";
		}
		html +="\n			<img src='"+thumbPhoto+"' border='0'>";
		if (detailLink && detailLink!=""){
			html +="\n </a>";
		}
		html +="\n      </td>";
	}

	
	html +="<td align='left'>";
	html +="\n  		<table width='100%' border='0' cellspacing='2' cellpadding='2' align='left'>";

	html +="\n 				 <tr><td colspan='2'>";
	html +="\n						<span style='font-family: Tahoma, Arial; font-size: 11px;color: #5A6173;font-weight: bold;'>";
	html +="\n						<b>"+mapLocation.title+"</b>";
	html +="\n						</span>";
	html +="\n 				     </td>";
	html +="\n 				 </tr>";

	html +="\n 				 <tr><td colspan='2'>";
	html +="\n						<span style='font-family: Tahoma, Arial; font-size: 11px;color: #5A6173;font-weight: bold;'>";
	html +=						  "<b>"+mapLocation.address+"<br>"+mapLocation.address2+"</b>";;
	html +="\n						</span>";
	html +="\n 				     </td>";
	html +="\n 				 </tr>";

	html +="\n 				 <tr><td colspan='2'>";
	html +="\n						<span style='font-family: Tahoma, Arial; font-size: 11px;color: #5A6173;font-weight: normal;'>";
	html +="\n						"+mapLocation.description+"";
	html +="\n						</span>";
	html +="\n 				     </td>";
	html +="\n 				 </tr>";


	html +="\n 			</table>";
	html +="\n 	</td></tr>";
	html +="\n 	</table>";
	
	//html +="\n</span>";
	html +="\n  </td></tr>";
	
	
	html +="\n</td>";
	
	html +="\n</tr>";
	html +="\n</table>";
	
	//document.getElementById("testArea").value = html;
	
	return html;

	
}


 

//function loadBigMap(centerLat, centerLng) {
function loadBigMap() {
      
      var defaultCenterLat =38.92830055730587;
      var defaultCenterLng =-120.01445531845093;
      var defaultZoom =12;
      

      
      // The basics.
      //
      // Creates a map and centers it on Palo Alto.
     
      if (GBrowserIsCompatible()) {
     
           var map = new GMap2(document.getElementById("mapWindow"));
           currMap = map;

           map.setCenter(new GLatLng(defaultCenterLat,defaultCenterLng), defaultZoom);
          
           map.addControl(new GSmallMapControl());
           map.addControl(new GMapTypeControl());
   
      	   //map.setMapType(G_MAP_TYPE) ;       
	
		  // ---------- mapLocation icon  -----------------
	
	
		   // mapLocation icons
		   for (var i=0; i<mapLocationArray.length; i++){
		   

	   
		   		   var mapLocation = mapLocationArray[i];
		   		   if (mapLocation.hidden){
		   		    	continue;
		   		   }

	   
		   			   
				   var blueIcon = new GIcon(G_DEFAULT_ICON);
				   blueIcon.image =  mapLocation.iconURL;

   

				   var iconTitle = mapLocation.title;

				   // Set up our GMarkerOptions object
				   var markerOptions = { title: iconTitle, icon:blueIcon };


   

				   var point = new GLatLng(mapLocation.lat,mapLocation.lng);
				   //map.addOverlay(new GMarker(point, markerOptions));


				   //add maker
				   eval("var myMarker_mapLocation_"+i+" =new GMarker(point, markerOptions);");

	   

				   eval("var html_mapLocation_"+i+" =getBubbleHtml_mapLocation(mapLocation);");

	   
 				   var listener ="GEvent.addListener(myMarker_mapLocation_"+i+", \"click\", function() {myMarker_mapLocation_"+i+".openInfoWindowHtml(html_mapLocation_"+i+");})";
				   eval(listener);

 
				   eval("map.addOverlay(myMarker_mapLocation_"+i+");");


			}
	
			
		   //best fit map?
 		   bestFitMap(map);

     }

   
   
	var focusObj=document.getElementById('_email');
	if (focusObj){focusObj.focus(); focusObj.select()}

   
}


/**********************************************
    calculate center and best zoom level for gmap
 *********************************************/

function bestFitMap(map){

	
		
	    var bounds = new GLatLngBounds;
		for (var i=0; i<mapLocationArray.length; i++){
			try{
				var mapLocation = mapLocationArray[i];		
				
				var point = new GLatLng(mapLocation.lat,mapLocation.lng);
			    bounds.extend(point);

			 }catch(e){
			    alert(e.message);
			}
		}
		map.setCenter(bounds.getCenter());
		
		if (defaultMapZoomLevel<0){
			map.setZoom(map.getBoundsZoomLevel(bounds));
		}else{
			map.setZoom(defaultMapZoomLevel);
		}

  

}




