

var baseURL_local ="http://127.0.0.1:8888/agenttoolboxes/";
var baseURL_tahoe ="http://www.zohut.com/";
var baseURL_HTTPS ="https://www.zohut.com/";
var mapScrollWheel = false;

function parseBaseURL(myURL){

	var slashCount=3;	
	if (myURL.indexOf("127.0.0.1")>=0){
		slashCount=4;
	}

	var returnURL="";
	var count=0;
	for (var i=0; i<myURL.length; i++){
		var c = myURL.charAt(i);
		if (c=="/"){
			count++;
		}
		
		returnURL +=c;
		if (count>=slashCount){
			break;
		}
	}
	return returnURL;

}

function getBaseURL(){
	var currLoc = ""+document.location;
	
	return parseBaseURL(currLoc);
	

	/*
	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() + "zohut/js/googlemap/ski.png";
	this.thumbURL ="";
	this.resortType =0;
	this.phone ="";
	this.website="";
	this.anchorText="";
	this.linkURL ="";
	this.address1 ="";
	this.address2 ="";
	
}

function addSkiResort(title, lat, lng, iconURL,thumbURL,resortType,phone,website,linkURL,address1,address2, anchorText){
	var resort = new SkiResort();
	resort.title = unescape(title);
	resort.lat =lat;
	resort.lng =lng;
	resort.iconURL = iconURL;
	resort.thumbURL = thumbURL;

	if (resortType)	resort.resortType = resortType;
	if (phone) resort.phone = unescape(phone);
	if (website) resort.website = unescape(website);
	if (linkURL) resort.linkURL = unescape(linkURL);
	if (address1) resort.address1 = unescape(address1);
	if (address2) resort.address2 = unescape(address2);
	if (anchorText) resort.anchorText = unescape(anchorText);
		
	skiResortArray[skiResortArray.length] = resort;
}

//allowed hot spots type(resort.resortType), separated by comma
var allowedHotSpotsType ="";
var allowAnyHotSpotsType = false;  //quick method to display any hot spots without reason.
var hideAnyHotSpots = false;       //quick method to disable any hot spots viewing
function isHotSpotsAllowed(resort){

	//alert("ishot, allowedHotSpotsType="+allowedHotSpotsType+", allowAnyHotSpotsType="+allowAnyHotSpotsType);


	if (!resort || resort==null){
		return false;
	}

	
	if (hideAnyHotSpots){
		return false;
	}

	
	if (allowAnyHotSpotsType){
		return true;
	}

	
	if (!allowedHotSpotsType || allowedHotSpotsType==""){
		return false;
	}

	
	var myHotSpotsType = allowedHotSpotsType +",";
	var typeArray = myHotSpotsType.split(",");
	for (var i=0; i<typeArray.length; i++){
		var myType = typeArray[i];
		if (!myType || myType=="" || myType=="0"){
			continue;
		}
		
		var myTypeVal = parseInt(myType);
		if (myTypeVal==resort.resortType){
			return true;
		}
	}
	
	return false;
}


function setAllowedHotSpotsType(allowedTypes,reloadHotSpots){

   allowedHotSpotsType = allowedTypes;
   if (reloadHotSpots){
	   reloadSkiResorts();
   }
}


/**********************************************************
                       Property
 **********************************************************/ 
 
var propertyArray =new Array();
var propertyBubbleHTMLArray = new Array();
function TahoeProperty(){
	this.id=0;
	this.propertyID="";
	this.srcPropertyID="";
	this.lat ="";
	this.lng ="";
	this.grade ="";
	this.price =0;
	this.occupancy =0;
	this.bedrooms =0;
	this.bathrooms =0;
	this.iconURL =getBaseURL() +"zohut/js/googlemap/zohut.png";
	this.thumbURL ="";
	this.idExists =false;
}

function addProperty(id,propertyID, srcPropertyID, lat, lng, grade,price,occupancy,bedrooms,bathrooms,iconURL,thumbURL, idExists){
	var property = new TahoeProperty();
	property.id = id;
	property.propertyID = propertyID;
	property.srcPropertyID = srcPropertyID;
	property.lat = lat;
	property.lng = lng;
	property.grade = grade;
	property.price = price;
	property.occupancy = occupancy;
	property.bedrooms = bedrooms;
	property.bathrooms = bathrooms;
	if (iconURL && iconURL!=""){
		property.iconURL = iconURL;
	}
	property.thumbURL = thumbURL;
	property.idExists = idExists;
	
	var currIndex = propertyArray.length;
	propertyArray[currIndex] = property;
	propertyBubbleHTMLArray[currIndex] = getBubbleHtml_property(property);
}

function setIdExists(id, idExists){


	for (var i=0; i<propertyArray.length;i++){
		var property = propertyArray[i];

		if (property.id==id){
			property.idExists = idExists;
			propertyBubbleHTMLArray[i] = getBubbleHtml_property(property);
			return;
		}
	}
}


/**********************************************************
                       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.replace("#","");
	propertyID = propertyID.toLowerCase();
	
	return getBaseURL()+"data/ltaphotos/thumbs/"+propertyID+".jpg";
}	


function getBubbleHtml_property(property){
	
	if (property.id==-1){
		return "";
	}
	
	var propertyID = property.propertyID;
	propertyID = propertyID.replace(" ","");
	
	var thumbPhoto = getPropertyThumbPhoto(property.srcPropertyID);	
	
	var detailLink ="javascript:viewProperty(\""+propertyID+"\")";
	
	
	var topTextColor ="#085173";
	var topLineColor =topTextColor;
	
	var topText = "Property #"+ propertyID;
	
	var html ="<table width='300' height='200' border='0' cellspacing='2' cellpadding='2' class='map_normal_text'>";
	
	html +="\n<tr>";
	
	html +="\n<td colspan='2'>";
	html +="\n<span class='map_title_text'>";
	html +="\n"+topText;
	html +="\n</span>";
	
	html +="\n<br>";
	html +="\n<hr color='"+topLineColor+"' size='2'>";
	html +="\n</td>";
	html +="\n</tr>";
	
	//life side thumbnail
	html +="\n<tr><td valign='top'>";
	
	html +="\n <a href='"+detailLink+"' >";
	html +="\n			<img src='"+thumbPhoto+"' border='0'>";
	html +="\n </a>";
	
	
	html +="\n      </td>";

	
	//right side detail info
	html+="			<td align='left' valign='top'>";

	html +="\n						<b>"+property.grade+"</b>";
	html +="\n<br>";
	html +=						  "<b>From: $"+parseInt(property.price)+"/day</b>";;
	html +="\n<br>";
	html +="\n						Sleeps "+property.occupancy+"";
	html +="\n<br>";
	html +="\n						"+property.bedrooms+" br "+property.bathrooms+" baths";
	html +="\n<br>";
	html +="\n						<span class='map_url_line'>";
	html +="<a href='"+detailLink+"' >More Details</a>";
	html +="\n						</span>";
	html +="\n 	</td></tr>";


	html +="\n<tr>";
	html +="\n<td colspan='2'>";
	
	html +="<span id='favoritesAct"+property.id+"'>";
	if (!property.idExists){

		html +="<a href=\"javascript:addToFavorites('"+property.id+"','"+property.id+"')\">";
		html +="<img src='zohut/images/favorites_add.jpg' border='0' />";
		html +="</a>";

	}else{

		html +="<a href=\"javascript:removeFromFavorites('"+property.id+"','"+property.id+"')\">";
		html +="<img src='zohut/images/favorites_remove.jpg' border='0' />";
		html +="</a>";

	}
	
	html +="  <span id='processing"+property.id+"' style='display: none'>";
	html +="  <img src='cpanel/images/hourglass.gif' border='0'>";
	html +="  Processing";
	html +="  </span>";
	html +="</span>";	
	html +="</td>";
	html +="\n</tr>";

	html +="\n 	</table>";
	
	
	return html;

}



function getBubbleHtml_hotspots(skiResort){
	
	var myText = skiResort.title;
	var thumbURL = skiResort.thumbURL;

	var linkURL = skiResort.linkURL;
	var hasLinkURL = false;
	if (linkURL!=null && linkURL!=""){
		hasLinkURL = true;
	}

	var website = skiResort.anchorText;
	if (website==""){
		website = skiResort.website;
	}
	if (website!=null && website!=""){
		if (hasLinkURL){
			website ="<a href='"+linkURL+"' target='_blank'>"+website+"</a>";
		}
	}

	var topText = myText;
	if (topText!=null && topText!=""){
		if (hasLinkURL){
			topText ="<a href='"+linkURL+"' target='_blank'>"+topText+"</a>";
		}
	}
	
		
	var _isCasinos  = isCasinos(myText);
	var _isSkiResort = isSkiResort(myText);
	
	var topTextColor ="#085173";
	var topLineColor =topTextColor;
	var frameColor =_isCasinos?"#7B0863":"#0808CE";
	var frameTextColor =frameColor;
	

	var frameText = breakFrameText(myText);
	
	var html ="<table width='300' height='160' border='0' cellspacing='2' cellpadding='2' class='map_normal_text'>";
	
	html +="\n<tr>";
	
	html +="\n<td colspan='2' align='left'>";
	
	html +="\n<span class='map_url'>";
	html +="\n<span class='map_title_text'>";
	html +="\n"+topText;
	html +="\n</span>";
	html +="\n</span>";
	
	html +="\n<br>";
	html +="\n<hr color='"+topLineColor+"' size='2'>";
	html +="\n</td>";
	html +="\n</tr>";
	

	html +="\n<tr>";
	
	//left side td:  thumbnail
	html +="\n<td valign='top' align='left' height='140'>";
	if (thumbURL && thumbURL!=""){
		html +="\n<img src='"+thumbURL+"' border='0'>";
	}else{
		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: Tahoma, Arial; 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>";
	
	//right side td, detail information
	html +="\n<td valign='top' align='left'>";
	
	if (skiResort.phone!=null && skiResort.phone!=""){
		html +=skiResort.phone;
		html +="<br>";
	}
	

	if (skiResort.address1!=null && skiResort.address1!=""){
		html +=skiResort.address1;
		html +="<br>";
	}
	
	if (skiResort.address2!=null && skiResort.address2!=""){
		html +=skiResort.address2;
		html +="<br>";
	}	

	if (website!=null && website!=""){
		html +="\n<span class='map_url_line'>";	
		html +=website;
		html +="\n</span>";
		html +="<br>";
	}
	
		
	html +="\n</td>";
	
	
	html +="\n</tr>";


	
	html +="\n</table>";
	
	return html;

}	 
 

var globalMap;
function getMapObject(){
	if (!globalMap || globalMap==null){
         globalMap = new GMap2(document.getElementById("mapWindow"));
    }
    return globalMap;
}

function loadBigMap(centerLat, centerLng) {
      
       
      // The basics.
      //
      // Creates a map and centers it on Palo Alto.
     
      if (GBrowserIsCompatible()) {
     
           var map = getMapObject();
           currMap = map;

           map.setCenter(new GLatLng(centerLat,centerLng), 12);
          
          // map.addControl(new GSmallMapControl());
          // map.addControl(new GMapTypeControl());
   	
   	  if (mapScrollWheel){
	  	map.enableScrollWheelZoom();
	  }else{
	  	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);
	     
      	   //map.setMapType(G_MAP_TYPE) ;       
	
		  // ---------- property icon  -----------------
	
	
		   // property icons
		   for (var i=0; i<propertyArray.length; i++){
		   
	   
		   		   var property = propertyArray[i];
		   		   if (property.id==-1){
		   		   	continue;
		   		   }
		   			   
				   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(propertyBubbleHTMLArray["+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);	   
		   */

			
			loadSkiResorts(map);

			
		   //best fit map?
 		   bestFitMap(map);

     }
   
}


function loadSkiResorts(map){
try{
			if (!map || map==null){
				 map = getMapObject();
			}
	
			if (!map || map==null){
				return;
			}
			
		   allMarkersArray=[];
		   //ski resorts and casinos	
		   for (var i=0; i<skiResortArray.length; i++){
		   		var skiResort = skiResortArray[i];
		   		//alert("xxx test ski - "+skiResort.resortType+" - "+skiResort.title);
		   		
				if (!isHotSpotsAllowed(skiResort)){
					continue;
				}
	
	 		    // ---------- 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_hotspots(skiResort);");
		            eval("GEvent.addListener(myMarker_"+i+", \"click\", function() {myMarker_"+i+".openInfoWindowHtml(html_"+i+");})");
			    eval("map.addOverlay(myMarker_"+i+");");
			    
			    eval("allMarkersArray[allMarkersArray.length]=myMarker_"+i);
			    
		   }
}catch(e){
	alert(e.message);
}		   
}

var allMarkersArray = [];  // global 
function reloadSkiResorts(map){
try{

			if (!map || map==null){
				 map = getMapObject();
			}

			
			if (!map || map==null){
				return;
			}

			
		   //clear ski resports
		   for (var i=0; i<allMarkersArray.length; i++){

			    //add maker
			    try{
				    map.removeOverlay(allMarkersArray[i]);
				}catch(e){
					alert(e.message);
				}
		   }

		   
		   //reload ski resorts
		   loadSkiResorts();
		   
		   
}catch(e){
	alert(e.message);
}			   
		   
}


/**********************************************
    calculate center and best zoom level for gmap
 *********************************************/
var currMap;
var mapCenterLat;
var mapCenterLng;
var mapZoom;


function bestFitMap(map){


		var mapArea =getCurrMapArea();
		if (mapArea){

  			map.setCenter(new GLatLng(mapArea.lat,mapArea.lng));
			map.setZoom(mapArea.zoom);
			return;		
		
		}



		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(""+property.propertyID+" - "+property.lat+","+property.lng);
			 }catch(e){
			    alert(e.message);
			}
		}



  		if (propertyArray.length>1){
  			map.setCenter(bounds.getCenter());
			map.setZoom(map.getBoundsZoomLevel(bounds));
		}else{
			if (propertyArray.length==1){
		  		map.setCenter(bounds.getCenter());
				map.setZoom(12);		  		
		  	}else{
		  		//if no property, will default to center hotspots
		  		if (skiResortArray.length>0){
		  		
				    var myBounds = new GLatLngBounds;
					for (var i=0; i<skiResortArray.length; i++){
						try{
							var skiResort = skiResortArray[i];		
							var point = new GLatLng(skiResort.lat,skiResort.lng);
						    myBounds.extend(point);
						    
						    //GLog.write(""+i+" - "+property.lat+","+property.lng);
						 }catch(e){
						    alert(e.message);
						}
					}		  		

		  			map.setCenter(myBounds.getCenter());
					map.setZoom(map.getBoundsZoomLevel(myBounds));				
		  		}
		  	}

		}

  
		//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); 
		*/
}


/*********************************************
  for some specific area, will have specific lat,lng and zoom scale
 ********************************************/


var mapAreaArray =new Array();
var currLoc_area="";
var currLoc_loc="";
function MapArea(){
	this.area="";
	this.location="";
	this.lat ="";
	this.lng ="";
	this.zoom=12;
}

function getCurrMapArea(){

   //alert("area="+currLoc_area+",loc="+currLoc_loc);

	for (var i=0; i<mapAreaArray.length; i++){
		var mapArea = mapAreaArray[i];
		
		if (mapArea.area==currLoc_area && mapArea.location==currLoc_loc){
			return mapArea;
		}
	}
	
	return null;
	
}

function addMapArea(area,location,lat,lng,zoom){
	var mapArea = new MapArea();
	mapArea.area = area;
	mapArea.location = location;
	mapArea.lat =lat;
	mapArea.lng =lng;
	mapArea.zoom =zoom;
	mapAreaArray[mapAreaArray.length] = mapArea;
}


 
 
 
