 String.prototype.toProperCase = function(){
     return this.toLowerCase().replace(/\w+/g,function(s){
          return s.charAt(0).toUpperCase() + s.substr(1);
     })
}     
      var markers = [];
      var gmarkers = [];
      var htmls = [];
      var i = 0;
      var map;
	  
      var side_bar_html = "";
	 // var hidden_list = "";

      function makeredmarker(){
	
	  var baseIcon = new GIcon();
		baseIcon.shadow = "/mapfiles/orange_marker.png";
		baseIcon.iconSize = new GSize(20, 23);
		baseIcon.shadowSize = new GSize(20, 23);
		baseIcon.iconAnchor = new GPoint(20, 23);
		baseIcon.infoWindowAnchor = new GPoint(9, 2);
		baseIcon.infoShadowAnchor = new GPoint(18, 25);  
	  return baseIcon;
      }
      
      function makeyellowmarker(){
	 var baseIcon = new GIcon();
		baseIcon.shadow = "/mapfiles/red_marker.png";
		baseIcon.iconSize = new GSize(20, 23);
		baseIcon.shadowSize = new GSize(20, 23);
		baseIcon.iconAnchor = new GPoint(20, 23);
		baseIcon.infoWindowAnchor = new GPoint(9, 2);
		baseIcon.infoShadowAnchor = new GPoint(18, 25);  
	  return baseIcon;
      }
     	
 // ====== set up marker mouseover tooltip div ======
      var tooltip = document.createElement("div");
      tooltip.setAttribute("id","div_marker_tooltip");
      tooltip.style.border="1px #2E68AF solid";
      tooltip.style.backgroundColor="#ffffff";
      tooltip.style.fontWeight="bold";
      tooltip.style.overflow="hidden";

      
function generatepagebar(numproperty,page,page_limit){
	
var numofpages = (numproperty / page_limit);
			var limt = page_limit * page;	
					
			if (page == "1" && limt > numproperty){
            var text = 'Results 1 - '+numproperty+' of '+numproperty+' ';
			}else if (page == "1"){
            var text = 'Results 1 - '+page_limit+' of '+numproperty+' ';
			}else
			{
				
				
			  	if (limt > numproperty) { 
            	var text = 'Results '+(limt - page_limit)+' - '+numproperty+'  of '+numproperty+' ';
            	var txt = (limt - page_limit)+' - '+numproperty;
			  
			  	}else{
            	var text = 'Results '+(limt - page_limit)+' - '+limt+'  of '+numproperty+' ';
            	var txt  = (limt - page_limit)+' - '+limt;
			  	}
			}
			
			
		var end = Math.ceil(numofpages);
	
			
			var nexpage = parseInt(page) + 1;
			var prevpage = parseInt(page) - 1;
			if (page != 1){ 
				
				if (limt < numproperty){
					text = text + '<a title="Prev Page" href="javascript:prevpage('+prevpage+');"  onclick="">&laquo; Previous</a> | ';	
					}else{
					text = text + '<a title="Prev Page" href="javascript:prevpage('+prevpage+');"  onclick="">&laquo; Previous</a>';	
					}
			}
			if (limt < numproperty) { 
			text = text + '<a title="Next Page" href="javascript:nextpage('+nexpage+');"  onclick=""> Next &raquo;</a>';	
			}
			var viewer = $('pages');
            viewer.innerHTML=text;   
		
          return txt;
}




function setcheckbox(action,city,beds,baths,minprice,maxprice,proptype,subproptype,sqft) {
	var response = GXmlHttp.create();
	response.open('get', '/rpc.php?action=find&city='+city+'&beds='+beds+'&baths='+baths+'&minprice='+minprice+'&maxprice='+maxprice+'&proptype='+proptype+'&subproptype='+subproptype+'&sqft='+sqft, true);
	response.onreadystatechange = function(){
        if(response.readyState == 4){
    	var xml = response.responseXML;
    	}
    }
	response.send(null); 
}

       
function sndReq(action,city,beds,baths,minprice,maxprice,proptype,subproptype,sqft,page,sortbuy) {
	
		//alert(action); 
	if (action == "map"){
		map = new GMap($("map-container-inner"));
	    map.addControl(new GLargeMapControl());
	    map.addControl(new GMapTypeControl());
	    //map.setCenter(new GLatLng(37.876208,-122.532105), 13);
		
	    // move it to the map div
        //document.getElementById("map-container-inner").appendChild(tooltip);
        // hide it
        //tooltip.style.visibility="hidden"; 
        //xlmDocGet(action,city,beds,baths,minprice,maxprice,proptype,subproptype,sqft,page);	
	 	}else{
		side_bar_html = "";
		//hidden_list = "";
		
		var http = GXmlHttp.create();
        http.open('get', '/rpc.php?action=find&city='+city+'&beds='+beds+'&baths='+baths+'&minprice='+minprice+'&maxprice='+maxprice+'&proptype='+proptype+'&subproptype='+subproptype+'&page='+page+'&sqft='+sqft+'&sort='+sortbuy, true);
        http.onreadystatechange = function(){
        if(http.readyState == 4){
    	var xml = http.responseXML;
    	//alert(http.responseText);
 		var update  = xml.documentElement.getElementsByTagName("Result");
    	var numproperty = update[0].getAttribute("numprop");
    	var element = 'foo';
    	generatepagebar(numproperty,page,50);
    	
    	var numofpages = (numproperty / 50);
    	var limt = 50 * parseInt(page);
    		if (limt - 50 == 0 && limt > numproperty){ 
            	var txt = '1 - '+numproperty;
            }else if (limt - 50 == 0){
            	var txt = '1 - '+limt;            
            }  	
    		else if (limt > numproperty) { 
          		var txt = (limt - 50)+' - '+numproperty;
			  	}else{
            	var txt  = (limt - 50)+' - '+limt;
			  	}
    	  	

				 	
				var offset = (50 * (parseInt(page) )) - 50;
			
 		changeText(numproperty,element,txt);
 		
 		
 		  	xlmDocGet("map",city,beds,baths,minprice,maxprice,proptype,subproptype,sqft,offset,sortbuy); 
 		  	
    	} 
	}
	 	
	http.send(null);
	 		
	 	}
}
  
       
  
      function changeText(text,element,txt) {
  
         
             var viewer = $(element);
             viewer.innerHTML=" > "+text + " properties found matching your search.  Viewing results "+txt+" below...";   
       
  		
      }
     
    function swapimage(tester) {
  
    tester.src= 'http://www.tarmls.com/listingpics/bigphoto/061/p_nophoto.jpg'
	}


       function createMarker(point,icon,price,beds,baths,mlsnum,city,type,image,subproptype,sqft,piccount) {
       	//alert(image);
		var html = "";
		//var urlname = city.replace(/\s+/g,'-');
        var marker = new GMarker(point,icon);
        var imgurl = "0" + mlsnum.substring((mlsnum.length)-2);
		
        var imageone = "";
        var imagetwo = "";
        var imagethree = "";
        
        //if (parseInt(piccount) > 2){
        if (piccount > 2){
        var imgurl1 = "http://mediac.rapmls.com/norcalmls/listingpics/bigphoto/"+imgurl+"/"+mlsnum+"_02.jpg";
   		imageone = "<img src='"+imgurl1+"' width='40px' height='40px' style='padding:2px' onmouseover='document.getElementById(\"mainpic\").src=this.src' onmouseout='document.getElementById(\"mainpic\").src=\""+image+"\"'/>";
        }
       	
        //if (parseInt(piccount) > 3){
        if (piccount > 3){
        var imgurl2 = "http://mediac.rapmls.com/norcalmls/listingpics/bigphoto/"+imgurl+"/"+mlsnum+"_03.jpg";
		imagetwo = "<img src='"+imgurl2+"' width='40px' height='40px' style='padding:2px' onmouseover='document.getElementById(\"mainpic\").src=this.src' onmouseout='document.getElementById(\"mainpic\").src=\""+image+"\"'/>";
        }
        	
        //if (parseInt(piccount) > 4){
        if (piccount > 4){
        var imgurl3 = "http://mediac.rapmls.com/norcalmls/listingpics/bigphoto/"+imgurl+"/"+mlsnum+"_04.jpg";
		imagethree = "<img src='"+imgurl3+"' width='40px' height='40px' style='padding:2px' onmouseover='document.getElementById(\"mainpic\").src=this.src' onmouseout='document.getElementById(\"mainpic\").src=\""+image+"\"'/>";
        }
        
        //marker.tooltip = "<div style='background-color:#0c293e;color:#fff;padding:2px;text-align:center;width:179px;'><strong>Click For More Information</strong></div><br /><div style='height: 150px;width:175px;text-align:center;font-size:11px;'><div style='width:160px; height: 135px; '><img width='160' height='125' style='padding:5px;margin:3px;' src='"+image+"' onError='swapimage(this);'></div><strong>$"+ price +"</strong><br /></div>";
	    
        var infoTabs = [
        new GInfoWindowTab("Details","<div style='width: 310px;color:#000;'><div style='width:160px; float:left; height: 125px;padding:8px;'><a href='javascript:void(0);' onclick='w = window.open(\"/mlssearch/"+ mlsnum +"/details.html\"); openmedude();'><img border='0' width='160' height='120' src='"+image+"' onError='swapimage(this);' id='mainpic'></a></div><span style='font-weight:bold;font-size:22px;'>$"+ price +"</span><br /><span style='font-weight:bold;font-size:16px;'>"+city.toProperCase()+", CA</span><br /><span style='font-size:14px;'>MLS# "+mlsnum+"</span><br /><br />"+ beds +" Beds | "+ baths +" Baths<br />"+sqft+" sqft  <div style='float:left;width:160px;padding:5px;z-index:500'>"+imageone + imagetwo + imagethree+"</div><div><a href='javascript:void(0);' style='float:left;font-weight:bold;font-size:18px;margin-left:5px;' onclick='w = window.open(\"/mlssearch/"+ mlsnum +"/details.html\"); openmedude();'>&gt; View Details!</a></div>")
		//new GInfoWindowTab("Photos", "<div style='width: 280px;color:#000;'><div style='width:200px; float:left; height: 150px;padding:5px;'><a href='/mlssearch/"+ mlsnum +"/details.html'><img border='0' width='200' height='150' src='"+image+"' onError='swapimage(this);' id='mainpic'></a></div><div style='float:left;width:180px;padding:5px;'><img src='"+imgurl1+"' width='40px' height='40px' style='padding:2px' onmouseover='document.getElementById(\"mainpic\").src=this.src' onmouseout='document.getElementById(\"mainpic\").src=\""+image+"\"'/><img src='"+imgurl2+"' width='40px' height='40px' style='padding:2px' onmouseover='document.getElementById(\"mainpic\").src=this.src' onmouseout='document.getElementById(\"mainpic\").src=\""+image+"\"'/><img src='"+imgurl3+"' width='40px' height='40px' style='padding:2px' onmouseover='document.getElementById(\"mainpic\").src=this.src' onmouseout='document.getElementById(\"mainpic\").src=\""+image+"\"'/><img src='"+imgurl4+"' width='40px' height='40px' style='padding:2px' onmouseover='document.getElementById(\"mainpic\").src=this.src' onmouseout='document.getElementById(\"mainpic\").src=\""+image+"\"'/></div></div>")
        ];
        
	    gmarkers[i] = marker;
	    side_bar_html += '<div id="map-listing"><a href="javascript:void(0);" onmouseover="myclick(' + i + ')"><img border="0" width="94px" height="88px" src="'+image+'" /></a><h5>$'+price+'</h5><p style="font-size:13px;">'+city+', CA</p><p>'+beds+' Beds | '+baths+' Bath</p><p style="font-size:13px;"><a href="javascript:void(0);" onmouseover="myclick(' + i + ')" onclick="w = window.open(\'/mlssearch/'+ mlsnum +'/details.html\');" class="showdialog">More Info</a></p></div>';

	
	    
	    //hidden_list += '<div id="fav-listings"><div id="fav-listings-header"><p>For Sale | Days on Market: N/a</p></div><div id="fav-listings-details"><div id="fav-listings-deatils-inner"><h3>'+price+'</h3><h4>'+city+', CA</h4><p>MLS #'+mlsnum+'</p><p>'+beds+' bedrooms<br>'+baths+' baths<br>'+sqft+' sqft</p></div></div><a href="/"><img src="'+image+'" alt="Listing Image" width="217" height="171" /></a><p><a href="/mlssearch/'+ mlsnum +'/details.html"><strong>View Listing</strong></a></p><div class="clear"></div></div>';
	    
	    htmls[i] = infoTabs;
        
	    i++;
        
       
        GEvent.addListener(marker, "mouseover", function() {
          marker.openInfoWindowTabsHtml(infoTabs);
        }); 
         
        
       
     /*  GEvent.addListener(marker,"mouseover", function() {
          showTooltip(marker);
        });      
        GEvent.addListener(marker,"mouseout", function() {
		tooltip.style.visibility="hidden"
        });
        */  

  
        
       return marker;        
      }

   // ====== This function displays the tooltip ======
   // it can be called from an icon mousover or a sidebar mouseover
    function showTooltip(marker) {
    tooltip.innerHTML = marker.tooltip;
	var point=map.getCurrentMapType().getProjection().fromLatLngToPixel(map.getBounds().getSouthWest(),map.getZoom());
	var offset=map.getCurrentMapType().getProjection().fromLatLngToPixel(marker.point,map.getZoom());
	var anchor=marker.icon.iconAnchor;
	var width=marker.icon.iconSize.width;
	var pos = new GControlPosition(G_ANCHOR_BOTTOM_LEFT, new GSize(offset.x - point.x - anchor.x + width,- offset.y + point.y +anchor.y)); 
	pos.apply(tooltip);
	tooltip.style.visibility="visible";
      }

    
      
           
	  function myclick(i) {
	    gmarkers[i].openInfoWindowTabsHtml(htmls[i]);
	    
	    // run the gigabyte accelator tron thingamcbob
        gigabyteaccellorator();
	    
      }      

	  
      function makemap(action,city,beds,baths,minprice,maxprice,proptype,subproptype,sqft,page,sortbuy){
        //map = new GMap(document.getElementById("map-container-inner"));
	   // map.addControl(new GLargeMapControl());
	    //map.addControl(new GMapTypeControl());
	    // move it to the map div
       // document.getElementById("map-container-inner").appendChild(tooltip);
        // hide it
        //tooltip.style.visibility="hidden"; 
        //xlmDocGet(action,city,beds,baths,minprice,maxprice,proptype,subproptype,sqft,page,sortbuy);
        
      }
        
      function xlmDocGet(action,city,beds,baths,minprice,maxprice,proptype,subproptype,sqft,page,sortbuy){
      	//document.getElementById('map-list-toggle').style.display = "none";
      	document.getElementById('wait').style.display = "block";
      	map.clearOverlays();
      	 // ===== Start with an empty GLatLngBounds object =====     
      	//var bounds = new GLatLngBounds();
      	
        var request = GXmlHttp.create();
		request.open('GET', '/rpc.php?action='+action+'&city='+city+'&beds='+beds+'&baths='+baths+'&minprice='+minprice+'&maxprice='+maxprice+'&proptype='+proptype+'&subproptype='+subproptype+'&page='+page+'&sqft='+sqft+'&sort='+sortbuy, true);
		
		
		request.onreadystatechange = function() {
		
			if (request.readyState == 4) {	
 			//var xmlDoc = request.responseXML;
 			//alert(request.responseText);
 			var jsonData = eval('(' + request.responseText + ')');
 			
 			
 			//markers = xmlDoc.documentElement.getElementsByTagName("Result");
          	
          	//if (markers.length > 2){
            //var center = parseInt(Math.floor(markers.length / 2));
            //}else{
          	var center = 0;
           // }
          	
          	
           
            //map.setCenter(new GLatLng(markers[center].getAttribute("Latitude"),markers[center].getAttribute("Longitude")), 14);
            map.setCenter(new GLatLng(jsonData.markers[center].lat, jsonData.markers[center].lng), 14);
            //map.setCenter(new GLatLng(jsonData.markers[center].point), 14);
            map.setMapType( G_HYBRID_MAP );
   	        //for (var i = 0; i < markers.length; i++) {
            for (var i=0; i<jsonData.markers.length; i++) {
               
            if (jsonData.markers[i].agent == "A317062" ){
            	var baseIcon = makeyellowmarker();
            }else{
            	var baseIcon = makeredmarker();            
            } 
   	        var icon = new GIcon(baseIcon);	
   	        var point = new GLatLng(jsonData.markers[i].lat, jsonData.markers[i].lng);
   	         // === Plot the markers ===
            var marker = createMarker(point, 
            						  icon,
            						  jsonData.markers[i].price, 
            						  jsonData.markers[i].beds,
            						  jsonData.markers[i].baths,
            						  jsonData.markers[i].mlsnum,
            						  jsonData.markers[i].city,
            						  jsonData.markers[i].type,
            						  jsonData.markers[i].pic,
            						  jsonData.markers[i].subproptype,
            						  jsonData.markers[i].sqft,
            						  jsonData.markers[i].piccount
            						  );
       
   	        	
   	        	
            //var point = new GLatLng(parseFloat(markers[i].getAttribute("Latitude")),
                                  // parseFloat(markers[i].getAttribute("Longitude")));
		    
         /*   var address = markers[i].getAttribute("address");
            var mlsnum = markers[i].getAttribute("mlsnum");
            var price = markers[i].getAttribute("price");
            var beds = markers[i].getAttribute("beds");
            var baths = markers[i].getAttribute("baths");
            var city = markers[i].getAttribute("city");
            var type = markers[i].getAttribute("type");
            var address = markers[i].getAttribute("address");
            var image = markers[i].getAttribute("pic");
            var remarks = markers[i].getAttribute("remarks");
            var piccount = markers[i].getAttribute("piccount");
            var sqft = markers[i].getAttribute("sqft");
           
            var subproptype = markers[i].getAttribute("subtype");
            var agent = markers[i].getAttribute("agent");
           
            if (agent == "A317062" ){
            var baseIcon = makeyellowmarker();
            }else{
            var baseIcon = makeredmarker();            
            }
            var icon = new GIcon(baseIcon);
            var marker = createMarker(point,icon,price,beds,baths,mlsnum,city,type,address,image,remarks,subproptype,sqft,piccount);
            */
			map.addOverlay(marker);
			//bounds.extend(point);
   	        }
       //map.setZoom(map.getBoundsZoomLevel(bounds));
       //document.getElementById('map-list-toggle').style.display = "block";
       document.getElementById("map-listings-container-inner").innerHTML = side_bar_html;
       //document.getElementById("hidden_list").innerHTML = hidden_list;
       document.getElementById('wait').style.display = "none";
        
       /* GEvent.addListener(map, "click", function(overlay,point) {
		map.showMapBlowup(point);
		});*/
  		 }
		}
		request.send(null);
		
	  }