

var baseURL_local ="http://127.0.0.1:8080/tahoe/";
var baseURL_tahoe ="http://www.tahoestay.com/";
var baseURL_HTTPS ="https://www.tahoestay.com/";


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;
	}
}

/**********************************************************
                       SKI Resorts
 **********************************************************/ 
 

var currArriveDay="";
var currDepartDay=""; 
 
var skiResortArray =new Array();
function SkiResort(){
	this.title="";
	this.lat ="";
	this.lng ="";
	this.iconURL =getBaseURL() + "/js/googlemap/ski.png";
	this.thumbURL ="";
}

function addSkiResort(title, lat, lng, iconURL,thumbURL){
	var resort = new SkiResort();
	resort.title = unescape(title);
	resort.lat =lat;
	resort.lng =lng;
	resort.iconURL = iconURL;
	resort.thumbURL = thumbURL;
	skiResortArray[skiResortArray.length] = resort;
}


/**********************************************************
                       Property
 **********************************************************/ 
 
var propertyArray =new Array();
function TahoeProperty(){
	this.propertyID="";
	this.lat ="";
	this.lng ="";
	this.grade ="";
	this.price =0;
	this.occupancy =0;
	this.bedrooms =0;
	this.bathrooms =0;
	this.iconURL ="http://www.tahoestay.com/js/googlemap/red_bubble.png";
	this.thumbURL ="";
}

function addProperty(propertyID, lat, lng, grade,price,occupancy,bedrooms,bathrooms,iconURL,thumbURL){
	var property = new TahoeProperty();
	property.propertyID = propertyID;
	property.lat = lat;
	property.lng = lng;
	property.grade = grade;
	property.price = price;
	property.occupancy = occupancy;
	property.bedrooms = bedrooms;
	property.bathrooms = bathrooms;
	if (iconURL && !iconURL.equals("")){
		property.iconURL = iconURL;
	}
	property.thumbURL = thumbURL;
	propertyArray[propertyArray.length] = property;
}


/**********************************************************
                       functions
 **********************************************************/ 
 

function isSkiResort(title){
   if (!title){
   	return false;
   }
   return title.indexOf("Ski Resort")>=0;
}

function isCasinos(title){
   if (!title){
   	return false;
   }
   return title.indexOf("Casinos")>=0;
}

	
//break frame text with multi-lines
function breakFrameText(frameText){
	if (!frameText){
		return "";
	}
	
	frameText = frameText.replace("Ski Resort","<br>Ski Resort<br>");
	frameText = frameText.replace("Casinos","<br>Casinos<br>");
	
	if (frameText.indexOf("<br>")==frameText.length-5){
		frameText = frameText.substring(0,frameText.length-4);
	}
	
	return frameText;
}	 


function getPropertyThumbPhoto(propertyID){
	if (!propertyID){
		return "";
	}
	propertyID = propertyID.replace("#","");
	propertyID = propertyID.toLowerCase();
	
	return getBaseURL()+"ltaphotos/thumbs/"+propertyID+".jpg";
}	


function getBubbleHtml_property(property){
	
	var propertyID = property.propertyID;
	propertyID = propertyID.replace(" ","");
	
	var thumbPhoto = getPropertyThumbPhoto(propertyID);	
	
	var detailLink ="searchresults.el?pid&propertyId="+propertyID+"&arriveday="+currArriveDay+"&departday="+currDepartDay;
	
	var topTextColor ="#085173";
	var topLineColor =topTextColor;
	
	var topText = "Property #"+ propertyID;
	
	var html ="<table width='270'  border='0' cellspacing='2' cellpadding='2'>";
	
	html +="\n<tr>";
	
	html +="\n<td>";
	html +="\n<span style='font-family: Arial, Helvetica, sans-serif; font-size: 16px;color: #5A6173;font-weight: bold;'>";
	html +="\n"+topText;
	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='0' cellpadding='0'>";
	html +="\n  <tr><td width='40%' valign='top'>";
	html +="\n <a href='"+detailLink+"' target='_blank'>";
	html +="\n			<img src='"+thumbPhoto+"' border='0'  style='border: 1px solid #000000'>";
	html +="\n </a>";
	html +="\n      </td><td width='5'></td><td align='left' valign='top'>";

	html +="\n  		<table width='100%' border='0' cellspacing='0' cellpadding='1' align='left'>";

	html +="\n 				 <tr><td colspan='2'>";
	html +="\n						<span style='font-family: Arial, Helvetica, sans-serif; font-size: 11px;color: #5A6173;font-weight: bold;'>";
	html +="\n						<b>"+property.grade+"</b>";
	html +="\n						</span>";
	html +="\n 				     </td>";
	html +="\n 				 </tr>";

	html +="\n 				 <tr><td colspan='2'>";
	html +="\n						<span style='font-family: Arial, Helvetica, sans-serif; font-size: 11px;color: #5A6173;font-weight: bold;'>";
	html +=						  "<b>From: $"+parseInt(property.price)+"/day</b>";;
	html +="\n						</span>";
	html +="\n 				     </td>";
	html +="\n 				 </tr>";

	html +="\n 				 <tr><td colspan='2'>";
	html +="\n						<span style='font-family: Arial, Helvetica, sans-serif; font-size: 11px;color: #5A6173;font-weight: normal;'>";
	html +="\n						Sleeps "+property.occupancy+"";
	html +="\n						</span>";
	html +="\n 				     </td>";
	html +="\n 				 </tr>";

	html +="\n 				 <tr><td colspan='2'>";
	html +="\n						<span style='font-family: Arial, Helvetica, sans-serif; font-size: 11px;color: #5A6173;font-weight: normal;'>";
	html +="\n						"+property.bedrooms+" br "+property.bathrooms+" baths";
	html +="\n						</span>";
	html +="\n 				     </td>";
	html +="\n 				 </tr>";


	html +="\n 				 <tr><td colspan='2'>";
	html +="\n						<span style='font-family: Arial, Helvetica, sans-serif; font-size: 11px;color: #5A6173;font-weight: bold;'>";
	html +=		"<a href='"+detailLink+"' target='_blank'>More Details</a>";
	html +="\n						</span>";
	html +="\n 				     </td>";
	html +="\n 				 </tr>";

	html +="<tr><td colspan='2'>&nbsp;</td></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<tr><td height='1'></td></tr>";
	html +="\n</table>";
	
	//document.getElementById("myArea").value =html;
	return html;

}



function getBubbleHtml_text(myText, thumbURL){
	
	var _isCasinos  = isCasinos(myText);
	var _isSkiResort = isSkiResort(myText);
	
	var topTextColor ="#085173";
	var topLineColor =topTextColor;
	var frameColor =_isCasinos?"#7B0863":"#0808CE";
	var frameTextColor =frameColor;
	
	var topText = myText;
	var frameText = breakFrameText(myText);
	
	var html ="<table width='250' height='200' border='0' cellspacing='2' cellpadding='2'>";
	
	html +="\n<tr>";
	
	html +="\n<td>";
	html +="\n<span style='font-family: Arial, Helvetica, sans-serif; font-size: 16px;color: #5A6173;font-weight: bold;'>";
	html +="\n"+topText;
	html +="\n</span>";
	
	html +="\n<br>";
	html +="\n<hr color='"+topLineColor+"' size='2'>";
	html +="\n</td>";
	html +="\n</tr>";
	
	if (thumbURL && thumbURL!=""){
		
		html +="\n<tr>";
		html +="\n<td>";
		html +="\n<img src='"+thumbURL+"' border='0'>";
		html +="\n</td>";
		html +="\n</tr>";
			
	}else{
		html +="\n<tr>";
		html +="\n<td>";
		html +="\n<table width='150' cellspacing='4' cellpadding='4' bgcolor='"+frameColor+"'>";
		html +="\n<tr>";
		html +="\n<td height='100' valign='middle' align='center' bgcolor='#FFFFFF'>";
		
		html +="\n<span style='font-family: Arial, Helvetica, sans-serif; font-size: 16px;color: "+frameTextColor+";font-weight: bold;'>";
		html +="\n"+frameText;
		html +="\n</span>";
		
		html +="\n</td>";
		html +="\n</tr>";
		html +="\n</table>";
		html +="\n</td>";
		
		html +="\n</tr>";
	}
	
	html +="\n</table>";
	
	return html;

}	 
 

function loadBigMap(centerLat, centerLng) {
      
      // 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(centerLat,centerLng), 12);
          
           //map.addControl(new GSmallMapControl());
           //map.addControl(new GMapTypeControl());


	  map.enableScrollWheelZoom();
	  //map.disableScrollWheelZoom();	  
	  
	  map.addControl(new GLargeMapControl3D());
	
	  // Add GHierarchicalMapTypeControl
	  map.addMapType(G_PHYSICAL_MAP);
	  var hControl = new GHierarchicalMapTypeControl();
	  hControl.addRelationship(G_SATELLITE_MAP, G_HYBRID_MAP, "Labels", false);
	  map.addControl(hControl);
	
	  // Add ContextMenuControl to the map
	  //map.addControl(new ContextMenuControl());



   
      	   //map.setMapType(G_MAP_TYPE) ;       
	
		  // ---------- property icon  -----------------
	
	
		   // property icons
		   for (var i=0; i<propertyArray.length; i++){
		   
	   
		   		   var property = propertyArray[i];
		   			   
				   var blueIcon = new GIcon(G_DEFAULT_ICON);
				   blueIcon.image =  property.iconURL;

				   var myPropertyID = property.propertyID;
				   myPropertyID  = myPropertyID.replace(" ","");
				   
			       
			            
				   // Set up our GMarkerOptions object
				   var markerOptions = { title: myPropertyID, icon:blueIcon };

				   var point = new GLatLng(property.lat,property.lng);
				   //map.addOverlay(new GMarker(point, markerOptions));

				   //add maker
				   eval("var myMarker_property_"+i+" =new GMarker(point, markerOptions);");

				   eval("var html_property_"+i+" =getBubbleHtml_property(property);");


				   eval("GEvent.addListener(myMarker_property_"+i+", \"click\", function() {myMarker_property_"+i+".openInfoWindowHtml(html_property_"+i+");})");

				   eval("map.addOverlay(myMarker_property_"+i+");");

			}
			    
	
		   /*
			geourlIcon.image = "http://labs.google.com/ridefinder/images/mm_20_red.png";
			geourlIcon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
			geourlIcon.iconSize = new GSize(12, 20);
			geourlIcon.shadowSize = new GSize(22, 20);
			geourlIcon.iconAnchor = new GPoint(6, 20);
			geourlIcon.infoWindowAnchor = new GPoint(5, 1);	   
		   */

			
		   //ski resorts and casinos	
		   for (var i=0; i<skiResortArray.length; i++){
		   		var skiResort = skiResortArray[i];
	
	
	 		    // ---------- ski icon  -----------------
			    var skiIcon = new GIcon();
			    skiIcon.image =  skiResort.iconURL;
			    skiIcon.iconSize = new GSize(40, 40);
			    skiIcon.shadowSize = new GSize(30, 20);
				skiIcon.iconAnchor = new GPoint(5, 1);
				skiIcon.infoWindowAnchor = new GPoint(10, 10);	   
			
	 		    //ski marker
			    var skiMarkerOptions = { title: skiResort.title, icon:skiIcon };
			    var skiPoint = new GLatLng(skiResort.lat,skiResort.lng);
	            //map.addOverlay(new GMarker(skiPoint, skiMarkerOptions));
			    
			    //add maker
			    eval("var myMarker_"+i+" =new GMarker(skiPoint, skiMarkerOptions);");
			    eval("var html_"+i+" =getBubbleHtml_text(skiResort.title,skiResort.thumbURL);");
		            eval("GEvent.addListener(myMarker_"+i+", \"click\", function() {myMarker_"+i+".openInfoWindowHtml(html_"+i+");})");
			    eval("map.addOverlay(myMarker_"+i+");");
		   }

			
		   //best fit map?
 		   bestFitMap(map);

     }
   
}


/**********************************************
    calculate center and best zoom level for gmap
 *********************************************/
var currMap;
var mapCenterLat;
var mapCenterLng;
var mapZoom;


function bestFitMap(map){

		if (mapCenterLat && mapCenterLng && mapZoom ){
		
  			map.setCenter(new GLatLng(mapCenterLat,mapCenterLng));
			map.setZoom(mapZoom);

			return;		
		}
		
		
	    var bounds = new GLatLngBounds;
		for (var i=0; i<propertyArray.length; i++){
			try{
				var property = propertyArray[i];		
				var point = new GLatLng(property.lat,property.lng);
			    bounds.extend(point);
			    
			    //GLog.write(""+i+" - "+property.lat+","+property.lng);
			 }catch(e){
			    alert(e.message);
			}
		}
  		map.setCenter(bounds.getCenter());
		map.setZoom(map.getBoundsZoomLevel(bounds));
  
		//bestFitCenterLat = bounds.getCenter().lat();
		//bestFitCenterLng = bounds.getCenter().lng();
		//bestFitCenterZoom =map.getZoom();
  		
  		
		/*
		//calculate maxlng, minlng, maxlat, minlat from property list
		var minLng= + 1000;
		var maxLng= - 1000;
		var maxLat= - 1000;
		var minLng= + 1000;
		
		for (var i=0; i<propertyArray.length; i++){
			try{
				var property = propertyArray[i];
				var lat = parseFloat(property.lat);
				var lng = parseFloat(property.lng);
				
				if (lng>maxLng) maxLng =lng;
				if (lng<minLng) minLng =lng;
				
				if (lat>maxLat) maxLat =lat;
				if (lat<minLat) minLat =lat;
								
			}catch(e){
			}
		}
	
		
		//calc center and zoom level from gmap
		var center = new GPoint( (maxLat+minLat)/2, (maxLng+minLng)/2 );
		var delta = new GSize( maxLng-minLng, maxLat-minLat);
		
		var minZoom = map.spec.getLowestZoomLevel(center, delta, map.viewSize);
		
		map.centerAndZoom(center, minZoom); 
		*/
}




