// JavaScript Document

function get_cookie ( cookie_name )
{
  var results = document.cookie.match ( '(^|;) ?' + cookie_name + '=([^;]*)(;|$)' );

  if ( results )
    return ( unescape ( results[2] ) );
  else
    return null;
}

function set_cookie ( name, value, exp_y, exp_m, exp_d, path, domain, secure )
{
  var cookie_string = name + "=" + escape ( value );

  if ( exp_y )
  {
    var expires = new Date ( exp_y, exp_m, exp_d );
    cookie_string += "; expires=" + expires.toGMTString();
  }

  if ( path )
        cookie_string += "; path=" + escape ( path );

  if ( domain )
        cookie_string += "; domain=" + escape ( domain );
  
  if ( secure )
        cookie_string += "; secure";
  
  document.cookie = cookie_string;
}


function init(centerLat,centerLong,startZoom) {
	
	if (GBrowserIsCompatible()) {
		var mymap = new GMap2(document.getElementById("mymap"));
		mymap.addControl(new GSmallMapControl());
		mymap.addControl (new GMapTypeControl());
		
		//Check the cookie to see what maptype we should use	JCM
		var saved_maptype = get_cookie ("maptype");
		//Set the maptype appropriately  JCM
		switch (saved_maptype) {
   			case "Hybrid" : 
      			mymap.setMapType(G_HYBRID_MAP);
      		break; 
   			case "Satellite" : 
      			mymap.setMapType(G_SATELLITE_MAP);
      		break; 
   			case "Map" : 
      			mymap.setMapType(G_NORMAL_MAP);
      		break; 
   			default : 
      			mymap.setMapType(G_HYBRID_MAP); 
		} 
		
		//Create a listener to update the saved maptype JCM
		GEvent.addListener(mymap, "maptypechanged", function(){
		var current_type=mymap.getCurrentMapType().getName();
		//Set the cookie with the correct maptype
		set_cookie ( "maptype", current_type);})
		
	    var location = new GLatLng(centerLat,centerLong);
    	mymap.setCenter(location, startZoom);

	}
}	//function init


//show_result map
function load_one(mystatus){
	if (GBrowserIsCompatible()){
    	var minMapScale = 1;
		var maxMapScale = 13;
		var result_map = new GMap2(document.getElementById("mymap"));	
		if (mystatus = 0){
        	var result_mapTypes = result_map.getMapTypes();
			for (var i=0; i < result_mapTypes.length; i++){
				result_mapTypes[i].getMinimumResolution = function() {return minMapScale;}
				result_mapTypes[i].getMaximumResolution = function() {return maxMapScale;}
			}
		}
		result_map.addControl (new GMapTypeControl());
            
        //Check the cookie to see what maptype we should use	JCM
		var saved_maptype = get_cookie ("maptype");
		//Set the maptype appropriately  JCM
		switch (saved_maptype) {
   			case "Hybrid" : 
      			result_map.setMapType(G_HYBRID_MAP);
	    	break; 
   			case "Satellite" : 
      			result_map.setMapType(G_SATELLITE_MAP);
      		break; 
	   		case "Map" : 
    			result_map.setMapType(G_NORMAL_MAP);
      		break; 
   			default : 
      			result_map.setMapType(G_HYBRID_MAP); 
		} 
		
		//Create a listener to update the saved maptype JCM
		GEvent.addListener(result_map, "maptypechanged", function(){
		var current_type=result_map.getCurrentMapType().getName();
		//Set the cookie with the correct maptype
		set_cookie ( "maptype", current_type);})
                    
	    //set center to US Map KL
	    result_map.setCenter(new GLatLng(38.00000,-97.00000),3);
	}
}

function load_listing(mystatus, myproperties) {
	if (GBrowserIsCompatible()) {
    	var minMapScale = 1;
		var maxMapScale = 13;
		var result_map = new GMap2(document.getElementById("mymap"));
		result_map.addControl(new GLargeMapControl());
		if (mystatus = 0){
        	var result_mapTypes = result_map.getMapTypes();
			for (var i=0; i < result_mapTypes.length; i++){
				result_mapTypes[i].getMinimumResolution = function() {return minMapScale;}
				result_mapTypes[i].getMaximumResolution = function() {return maxMapScale;}
			}
        }
		result_map.addControl (new GMapTypeControl());
            
        //Check the cookie to see what maptype we should use	JCM
		var saved_maptype = get_cookie ("maptype");
		//Set the maptype appropriately  JCM
		switch (saved_maptype) {
   			case "Hybrid" : 
      			result_map.setMapType(G_HYBRID_MAP);
	    	break; 
   			case "Satellite" : 
      			result_map.setMapType(G_SATELLITE_MAP);
      		break; 
	   		case "Map" : 
    	  		result_map.setMapType(G_NORMAL_MAP);
      		break; 
   			default : 
      			result_map.setMapType(G_HYBRID_MAP); 
		} 
		
		//Create a listener to update the saved maptype JCM
		GEvent.addListener(result_map, "maptypechanged", function(){
		var current_type=result_map.getCurrentMapType().getName();
		//Set the cookie with the correct maptype
		set_cookie ( "maptype", current_type);})
            
        var bounds = new GLatLngBounds;
        var marker;
        var n = 0;
        var myurl = window.location;
		for ( m = 0; m < myproperties.length; m++){
        	var point = new GLatLng(myproperties[n][0],myproperties[n][1]);
            result_map.setCenter(point,12);
            var new_icon = new GIcon();
            n++;
            //new_icon.image = "http://crnet-dev.launchpad.local/images/eric/house"+n+".png";
            //new_icon.image = "http://crnet-test.launchpad.local/images/eric/house"+n+".png";
            //new_icon.image ="http://pre.consumerresourcenet.com/images/eric/house"+n+".png";
            new_icon.image ="http://www.consumerresourcenet.com/images/eric/house"+n+".png";
		    new_icon.size = new GSize(12,12);
		    new_icon.iconAnchor = new GPoint(14,25);
            var marker = new GMarker(point,new_icon); 
            //GEvent.addListener(marker[n], "dblclick", function(){
            //result_map.panTo(marker[n].point);
	        //});
            result_map.addOverlay(marker);  
            bounds.extend(marker.getPoint());  
		}
        result_map.setZoom(result_map.getBoundsZoomLevel(bounds));
		result_map.setCenter(bounds.getCenter());
        //zoom out when icon is clicked
        //var mgr = new MarkerManager(result_map,{trackMarkers:true});
        GEvent.addListener(result_map,"click",function(){
        	updateStatus(mgr.getMarkerCount(result_map.getZoom()));
        });
	}
 }

function updateState(html){
	document.getElementById("result_map").innerHTML = html;
}

function importanceOrder(marker,b){
    return GOverlay.getZIndex(Math.round(marker[m].latitude())) - marker[m].importance*320000;
}

var preEl ;
var orgBColor;
var orgTColor;
function HighLightTR(el, backColor,textColor,j, mystatus, myproperties){
	if(typeof(preEl)!='undefined') {
  		preEl.bgColor=orgBColor;
	    	try{ChangeTextColor(preEl,orgTColor);}catch(e){;}
	}
  	orgBColor = el.bgColor;
	orgTColor = el.style.color;
  	el.bgColor=backColor;

	try{ChangeTextColor(el,textColor);}catch(e){;}
  	preEl = el;
		
    if (GBrowserIsCompatible()) {
    	var minMapScale = 1;
		var maxMapScale = 13;
		var result_map = new GMap2(document.getElementById("mymap"));
		result_map.addControl(new GLargeMapControl());
        if (mystatus = 0){
	    	var result_mapTypes = result_map.getMapTypes();
			for (var i=0; i < result_mapTypes.length; i++){
				result_mapTypes[i].getMinimumResolution = function() {return minMapScale;}
				result_mapTypes[i].getMaximumResolution = function() {return maxMapScale;}
			}
        }
		result_map.addControl (new GMapTypeControl());
       	//Check the cookie to see what maptype we should use	JCM
		var saved_maptype = get_cookie ("maptype");
		//Set the maptype appropriately  JCM
		switch (saved_maptype) {
   			case "Hybrid" : 
      			result_map.setMapType(G_HYBRID_MAP);
	    	break; 
   			case "Satellite" : 
      			result_map.setMapType(G_SATELLITE_MAP);
      		break; 
	   		case "Map" : 
    			result_map.setMapType(G_NORMAL_MAP);
      		break; 
   			default : 
      			result_map.setMapType(G_HYBRID_MAP); 
		} 
		
		//Create a listener to update the saved maptype JCM
		GEvent.addListener(result_map, "maptypechanged", function(){
		var current_type=result_map.getCurrentMapType().getName();
		//Set the cookie with the correct maptype
		set_cookie ( "maptype", current_type);})
        var p = 0;
        var r = 0;
        var marker = [];
        for ( g = 0; g < myproperties.length; g++){
        	var point = new GLatLng(myproperties[p][0],myproperties[p][1]);
            result_map.setCenter(point,8);
            var icon = new GIcon(); 
            p++;
            if (p != j){
				//icon.image ="http://crnet-dev.launchpad.local/images/eric/house"+p+".png";
                //icon.image ="http://crnet-test.launchpad.local/images/eric/house"+p+".png";
                //icon.image ="http://pre.consumerresourcenet.com/images/eric/house"+p+".png";
                icon.image ="http://www.consumerresourcenet.com/images/eric/house"+p+".png";
			    icon.size = new GSize(12,12);
			    icon.iconAnchor = new GPoint(14,25);
                marker[p] = new GMarker(point,icon);
                //GEvent.addListener(marker[n], "dblclick", function(){
                //result_map.panTo(marker[n].point);
	            //});
                result_map.addOverlay(marker[p]);  
            } else {
				var point2 = new GLatLng(myproperties[p-1][0],myproperties[p-1][1]);
     		}                   
		}
		r = j-1;
        result_map.setCenter(point2,11);       
        var icon= new GIcon();
        //icon.image = "http://crnet-dev.launchpad.local/images/eric/hactive"+j+".png";
        //icon.image = "http://crnet-test.launchpad.local/images/eric/hactive"+j+".png";
        //icon.image = "http://pre.consumerresourcenet.com/images/eric/hactive"+j+".png";
        icon.image = "http://www.consumerresourcenet.com/images/eric/hactive"+j+".png";
		icon.size = new GSize(24,24);
		icon.iconAnchor = new GPoint(14,25);
        marker[r] = new GMarker(point2,{icon:icon,zIndexProcess:importanceOrder});
        marker[r].importance = 10;
        //GEvent.addListener(marker[m], "dblclick", function(){
        //	result_map.panTo(marker[m].point2);
		//});
		result_map.addOverlay(marker[r]); 
    }        
}
    
function ChangeTextColor(a_obj,a_color){  ;
	for (i=0;i < a_obj.cells.length;i++)
    	a_obj.cells(i).style.color=a_color;
} 

//show_listing detail_map
var myPano;
var mygeocoder = new GClientGeocoder();

function placeMarker(address_view){
    mygeocoder.getLocations(address_view, mapMarkerDetail);
}

function mapMarkerDetail(response){
	if (GBrowserIsCompatible()) {
            var detail_map = new GMap2(document.getElementById("detail_map"));
            detail_map.setMapType(G_HYBRID_MAP);
            detail_map.addControl(new GSmallMapControl());           
            var bounds = new GLatLngBounds;
    		place = response.Placemark[0];
        	viewPoint = new GLatLng(place.Point.coordinates[1],place.Point.coordinates[0]);
            detail_map.setCenter(viewPoint,9);
            detail_map.getCurrentMapType().getMaxZoomAtLatLng(viewPoint, function(resp){
            	if (resp && resp['status'] == G_GEO_SUCCESS) {
                	detail_map.setCenter(viewPoint, resp['zoom']);
                }
            });
            var new_icon = new GIcon();
            //new_icon.image = "http://crnet-dev.launchpad.local/images/eric/hactive.png";
            //new_icon.image = "http://crnet-test.launchpad.local/images/eric/house"+n+".png";
            //new_icon.image ="http://pre.consumerresourcenet.com/images/eric/house"+n+".png";
            new_icon.image ="http://www.consumerresourcenet.com/images/eric/house"+n+".png";
		    new_icon.size = new GSize(12,12);
		    new_icon.iconAnchor = new GPoint(14,25);
            var marker = new GMarker(viewPoint,new_icon); 
            detail_map.addOverlay(marker);  
            bounds.extend(marker.getPoint());  
            detail_map.setZoom(detail_map.getBoundsZoomLevel(bounds));
			detail_map.setCenter(bounds.getCenter());
    	}
}

function bearing(from,to){
	var lat1 = from.lat();
    var lng1 = from.lng();
    var lat2 = to.lat();
    var lng2 = to.lng();
	var dlat = lat2 - lat1;
	var dlng = lng2 - lng1;
    var angle = Math.atan2(dlng * Math.cos(lat2 * (Math.PI / 180.0)), dlat) * (180.0 / Math.PI);
    return wrapAngle(angle);
}

function wrapAngle(angle) {
    if (angle >= 360) {
      angle -= 360;
    } else if (angle < 0) {
     angle += 360;
    }
    return angle;
};

function findStViewLoc(address_view){
    var mypanoClient = new GStreetviewClient();
    myPano = new GStreetviewPanorama(document.getElementById("detail_street_view"));
	mygeocoder.getLatLng(address_view, function(point){
		if (point){
            mypanoClient.getNearestPanoramaLatLng(point, function(point2){
				myPano.setLocationAndPOV(point,{latlng:point,yaw: bearing(point2,point)});   
        	});
        } else {
        	alert("Can't found point");
        }
    });
	GEvent.addListener(myPano);
}

	





