//this are values that need to be intialized by main search script
var _search = null; //search.js
var _saved_search = null; //saved_search.js
var _mdc = null; //map.js MapOwner

var MapDisplayController = Class.create();

MapDisplayController.prototype = 
{
	initialize: function(web_template_path)
	{
		this.currentZoom = -1;
		this.prevViewType = "street";
		this.web_template_path = web_template_path;
		
		//house icons
		this.iHomeSale = new GIcon();
		this.iHomeSale.image = this.web_template_path+"/images/map/map_icon.php";
		this.iHomeSale.iconSize = new GSize(22, 22);
		this.iHomeSale.iconAnchor = new GPoint(6, 17);

		
		this.iHomeSaleOpenHouse = new GIcon(this.iHomeSale, this.web_template_path+"/images/map/map_icon.php?type=openhouse");
		this.iHomeSaleOpenHouse.iconSize = new GSize(26, 26);
		this.iHomeSaleOpenHouseSel = new GIcon(this.iHomeSale, this.web_template_path+"/images/map/map_icon.php?type=openhouse_sel");
		this.iHomeSaleOpenHouseSel.iconSize = new GSize(26, 26);
		this.iHomeSaleMine = new GIcon(this.iHomeSale, this.web_template_path+"/images/map/map_icon.php?type=mine");
		this.iHomeSaleMine.iconSize = new GSize(31, 31);
		this.iHomeSaleMineSel = new GIcon(this.iHomeSale, this.web_template_path+"/images/map/map_icon.php?type=mine_sel");
		this.iHomeSaleMineSel.iconSize = new GSize(31, 31);
		this.iHomeSaleSel = new GIcon(this.iHomeSale, this.web_template_path+"/images/map/map_icon.php?type=sel");
		this.iHomeSaleSel.iconSize = new GSize(22, 22);
		this.iHomeSaleComp = new GIcon(this.iHomeSale, this.web_template_path+"/images/map/map_icon.php?type=comp");
		this.iHomeSaleComp.iconSize = new GSize(22, 22);
		
		this.iFSBO = new GIcon(this.iHomeSale);
        this.iFSBOAgent = new GIcon(this.iHomeSale);
		this.iFSBOSel = new GIcon(this.iHomeSaleSel);
		this.iFSBOComp = new GIcon(this.iHomeSaleComp);
			
		//global variables for all js files
		this.mapDiv = $('map');
		this.map = new GMap2(this.mapDiv);
		this.map.addControl(new GOverviewMapControl());
		
		this.tooltip = null;
		this.tooltip = $('home_tooltip');
		this.tooltip.id = 'home_tooltip';
		
		this.mapDiv.appendChild(this.tooltip);
		
		this.mapHomes = new Array();
		this.mapHomesCount = 0;
		this.currentHome = null;
		
		this.selectedZoomColor = 'borange';
		this.deselectedZoomColor = 'bgrey';
		
//        var mypan = new GStreetviewPanorama(document.getElementById("sv_div"));
//        GEvent.addListener(this.map, "click", this.checkStreetView);
//        GEvent.addListener(this.map, "click", function(overlay,latlng) 
//        {
//            $('sv_div').style.display="block";
//            mypan.setLocationAndPOV(latlng);
//        });
//        GEvent.addListener(mypan, "error", handleNoFlash);

        initTabs(this.web_template_path);
	},
	clearHomes:function()
	{
		this.map.clearOverlays();
		//clear homes
		var size = this.mapHomes.length;
		for(var i = 0; i < size; i++)
		{
			if(this.mapHomes[i] != null)
			{
				delete this.mapHomes[i];
			}
			
			this.mapHomes[i] = null;
		}
		this.mapHomes = new Array();
		this.mapHomesCount = 0;
		this.currentHome = null;
		
	},
	zoomMap:function(level)
	{
		if(level == this.currentZoom)return;
	
		this.currentZoom = level;
		this.updateZoomBar();
		this.map.setZoom(level);
	},
	updateZoomBar:function()
	{
		for(var i = 3; i <= 17; i++)
		{
			if(i >= this.currentZoom)
			{
				$('zoom_' + i).className = this.selectedZoomColor;
			}
			else
			{
				$('zoom_' + i).className = this.deselectedZoomColor;
			}
		}
	},
	zoomIn:function()
	{
		if(this.currentZoom == 17) return;
		
		this.zoomMap(this.currentZoom+1);
	},
	zoomOut:function()
	{
		if(this.currentZoom == 3) return;
		
		this.zoomMap(this.currentZoom-1);
	},
	changeMapType:function(type)
	{
		if(this.prevViewType == type)
		{
			return;
		}
		
		$(this.prevViewType + "_text").className = "cblack map_view";

		$(this.prevViewType + "_lt").className = "";
		$(this.prevViewType + "_rt").className = "";
		
		$(type + "_text").className = "cwhite borange map_view";
		$(type + "_lt").className = "borange";
		$(type + "_rt").className = "borange";
		
		if(type == "street")
		{
			this.map.setMapType(G_NORMAL_MAP);
		}
		else if(type == "satellite")
		{
			this.map.setMapType(G_SATELLITE_MAP);
		}
        else
		{	
			this.map.setMapType(G_HYBRID_MAP);
		}
		
		this.prevViewType = type;
	},
	sortHomes:function(h1, h2)
	{
		if((+h1.id) < (+h2.id))
		{
			return -1;
		}
		else if((+h1.id) > (+h2.id))
		{
			return 1;
		}
		
		return 0;
	},
	findHomeById:function(id)
	{
		var size = this.mapHomes.length;
		if(size == 0)
		{
			return null;
		}
		
		if(this.mapHomes[0].id == id)
		{
			return this.mapHomes[0];
		}
		
		if(size == 1)
		{
			return null;
		}
		
		if(this.mapHomes[size-1].id == id)
		{
			return this.mapHomes[size-1];
		}
		
		//did end cases now binary search
		var lt = 0;
		var md = 0;
		var rt = size-1;
		
		while(lt <= rt)
		{
			md = parseInt((lt + rt)/2, 10);
			if(this.mapHomes[md].id == id)
			{
				return this.mapHomes[md];
			}
			else if((+this.mapHomes[md].id) < (+id))
			{
				lt = md+1;
			}
			else if((+this.mapHomes[md].id) > (+id))
			{
				rt = md-1;
			}
		}
		
		//see if it is in the comps section
		if(this.mapHomes.length > this.mapHomesCount)
		{
			for(var i = this.mapHomesCount; i < this.mapHomes.length; i++)
			{
				if(this.mapHomes[i].id == id)
				{
					return this.mapHomes[i];
				}
			}
		}
		
		return null;
	},
	findFirstHome:function()
	{
		if(this.mapHomes.length == 0)
		{
			return null;
		}
		
		return this.mapHomes[0];
	},
	hideDisclaimers:function()
	{
		//bch36: corrected "diclaimer" to "disclaimer"
		var td = $('disclaimer_table');
		if (!td)
			return;
		
		var ds = td.getElementsByTagName("div");
		
		for(var i = 0; i < ds.length; i++)
		{
			ds[i].style.display = "none";
		}
	},

    blueroofControls: function()
    {
        this.updateZoomBar = this.blueroofUpdateZoomBar;
        this.changeMapType = this.blueroofChangeMapType;
		this.iHomeSale.image = this.web_template_path+"/images/map/br_for_sale.gif";
		this.iHomeSale.iconSize = new GSize(30, 22);
//		this.iHomeSale.iconAnchor = new GPoint(6, 17);

		
//		this.iHomeSaleMine = new GIcon(this.iHomeSale, this.web_template_path+"/images/map/map_icon.php?type=mine");
        this.iHomeSaleMine.image= this.web_template_path+"/images/map/br_blueroof_home.png";
		this.iHomeSaleMine.iconSize = new GSize(33, 33);

//		this.iHomeSaleMineSel = new GIcon(this.iHomeSale, this.web_template_path+"/images/map/map_icon.php?type=mine_sel");
        this.iHomeSaleMineSel.image= this.web_template_path+"/images/map/br_for_sale_sel.gif";
		this.iHomeSaleMineSel.iconSize = new GSize(33, 33);
//		this.iHomeSaleSel = new GIcon(this.iHomeSale, this.web_template_path+"/images/map/map_icon.php?type=sel");
        this.iHomeSaleSel.image= this.web_template_path+"/images/map/br_for_sale_sel.gif";
		this.iHomeSaleSel.iconSize = new GSize(33, 33);
//		this.iHomeSaleComp = new GIcon(this.iHomeSale, this.web_template_path+"/images/map/map_icon.php?type=comp");
        this.iHomeSaleComp.image= this.web_template_path+"/images/map/br_for_sale_comp.gif";
		this.iHomeSaleComp.iconSize = new GSize(33, 33);
		
		this.iFSBO.image = this.web_template_path+"/images/map/br_fsbo.gif";
        this.iFSBO.iconSize = new GSize(30, 22);
		this.iFSBOSel = new GIcon(this.iHomeSaleSel);
		this.iFSBOComp = new GIcon(this.iHomeSaleComp);
        this.iFSBOAgent.image= this.web_template_path+"/images/map/br_fsbo_agent.gif";
        this.iFSBOAgent.iconSize = new GSize(30, 22);
    },

    blueroofUpdateZoomBar: function()
    {
		for(var i = 3; i <= 17; i++)
		{
			if(i == this.currentZoom && $('zoom_img_' + i))
			{
				$('zoom_img_' + i).src = "/shared/images/zoom_tick_selected.gif";
			}
			else if ($('zoom_img_' + i))
			{
				$('zoom_img_' + i).src = "/shared/images/zoom_tick.gif";
			}
		}
    },
    blueroofChangeMapType: function(type)
	{
		if(this.prevViewType == type)
		{
			return;
		}
		
		$(this.prevViewType + "_text").className = "map_type_unselected map_type";
		
		$(type + "_text").className = "map_type_selected map_type";
		
        if (this.prevViewType == "street_view")
        {
            this.map.removeOverlay(this.street_view);
            $('sv_div').style.display='none';
        }
		if(type == "street")
		{
			this.map.setMapType(G_NORMAL_MAP);
		}
		else if(type == "satellite")
		{
			this.map.setMapType(G_SATELLITE_MAP);
		}
        else if (type=="hybrid")
		{	
			this.map.setMapType(G_HYBRID_MAP);
		}
        else
        {
            this.map.setMapType(G_NORMAL_MAP);
            if (this.street_view == null)
            {
                this.street_view = new GStreetviewOverlay();
            }
            this.map.addOverlay(this.street_view);
        }
		
		this.prevViewType = type;
    },
    
    
    checkStreetView: function(overlay, latlng, overlaylatlng)
    {
        if (overlay != null)
        {
            if (overlay == this.street_view)
            {
                $('sv_div').style.display = 'block';
                /* start a street view here */
                this.sv_pan = new GStreetviewPanorama($('sv_div'));
                this.sv_pan.setLocationAndPOV(latlng);
            }
            else
            {
                $('sv_div').style.display = 'block';
                this.sv_pan.setLocationAndPOV(overlaylatlng);
            }
        }
    }

    
} //MapDisplayController

//tab controls
var lt_off_img = new Image();
var lt_on_img = new Image();
var rt_off_img = new Image();
var rt_on_img = new Image();
var on_off_img = new Image();
var off_on_img = new Image();
var off_off_img = new Image();
var bg_on_img = new Image();
var bg_off_img = new Image();

function initTabs(web_template_path)
{
	lt_off_img.src = web_template_path+"/images/map/tab_lt_off.png";
	lt_on_img.src = web_template_path+"/images/map/tab_lt_on.png";
	rt_off_img.src = web_template_path+"/images/map/tab_rt_off.png";
	rt_on_img.src = web_template_path+"/images/map/tab_rt_on.png";
	on_off_img.src = web_template_path+"/images/map/tab_on_off.png";
	off_on_img.src = web_template_path+"/images/map/tab_off_on.png";
	off_off_img.src = web_template_path+"/images/map/tab_off_off.png";
	bg_on_img.src = web_template_path+"/images/map/tab_bg_on.png";
	bg_off_img.src = web_template_path+"/images/map/tab_bg_off.png";
}

var currentTab = "search";
var currentNeighbor = "results";
function switchTab(tab, neighbor)
{
    if ($('search_lt') == null)
    {
        return(switchTab2(tab, neighbor));
    }
	$(currentTab + "_panel").style.display = "none";
	$('no_home_panel').style.display = "none";
    $('save_search_panel').style.display = "none";
	
	if(_mdc.currentHome || tab == "search" || (tab == "results" && _search.homeQuery != null))
	{
		$(tab + "_panel").style.display = "block";
	}
	else
	{
		if(tab == "results")
		{
			$('no_home_text').innerHTML = "no results found";
		}
		else
		{
			$('no_home_text').innerHTML = "no home selected";
		}
		$('no_home_panel').style.display = "block";
	}
	
	
	lt_img = $(currentTab + "_lt");
	$(currentTab + "_text").className = "cwhite tab_off png";
	$(currentTab + "_text").style.backgroundImage = "";
	
	
	if(currentTab == "search")
	{
		$('search_lt').src = lt_off_img.src;
		$('results_lt').src = off_off_img.src;
	}
	else if(currentTab == "info")                                                   
	{                    
		$('info_lt').src = off_off_img.src;
		$("info_rt").src = rt_off_img.src;
	}
	else
	{
		$(currentTab + '_lt').src = off_off_img.src;
		$(currentNeighbor + '_lt').src = off_off_img.src;
	}
	
	$(tab + "_lt").src = off_on_img.src;
	$(tab + "_text").className = "cwhite tab_on png";
	$(tab + "_text").style.backgroundImage = "";
	
	if(tab == "search")
	{
		$("search_lt").src = lt_on_img.src;
		$("results_lt").src = on_off_img.src;
	}
	else if(tab == "results")
	{
		$("results_lt").src = off_on_img.src; 
		$("info_lt").src = on_off_img.src;
	}
	else if(tab == "info")
	{
		$("info_lt").src = off_on_img.src;
		$("info_rt").src = rt_on_img.src;
		
		if(_mdc.currentHome)
		{
			_mdc.currentHome.info.showInfo();
		}
		
	}
	
	currentTab = tab;
	currentNeighbor = neighbor;
}

function switchTab2(tab, neighbor)
{
/*    if (currentTab == 'search')
    {
        $('search_panel_view').style.display='none';
        $('search_tabs').style.display='block';
    }
    if (tab == 'search')
    {
        $('search_tabs').style.display='none';
	$('search_panel_view').style.display='block';
    }
*/
	if (typeof search_form_require_login != "undefined" && search_form_require_login == 'home_details' && tab == 'info')
	{
		if (!isUserLoggedIn())
		{
			requiredLogin("switchTab2('info','results');");
			return;
		}
        }
	$(currentTab + "_panel").style.display = "none";
	$('no_home_panel').style.display = "none";
    $('save_search_panel').style.display = "none";
	
	if(_mdc.currentHome || tab == "search" || (tab == "results" && _search.homeQuery != null))
	{
		$(tab + "_panel").style.display = "block";
	}
	else
	{
		if(tab == "results")
		{
			$('no_home_text').innerHTML = "no results found";
		}
		else
		{
			$('no_home_text').innerHTML = "no home selected";
		}
		$('no_home_panel').style.display = "block";
	}
	
	
    $(currentTab+"_tab").className = "search_tab_off";
    $(tab+"_tab").className = "search_tab_on";

    if(_mdc.currentHome)
    {
    	if(tab == "info")
    	{
    	    _mdc.currentHome.info.showInfo();
		}
		
        if (tab == "comp")
        {
            _mdc.currentHome.info.showComps();
        }
        else
        {
            _mdc.currentHome.info.hideCompIcons();
        }
	}
	
	currentTab = tab;
	currentNeighbor = neighbor;
}

var MapBounds = Class.create();

MapBounds.prototype = 
{
    initialize : function(latlng)
    {
        this.minLat = this.maxLat = latlng.lat();
        this.minLng = this.maxLng = latlng.lng();
    },

    extend : function(latlng)
    {
        var lat = latlng.lat();
        var lng = latlng.lng();
        if (lat < this.minLat)
            this.minLat = lat;
        if (lng < this.minLng)
            this.minLng = lng;
        if (lat > this.maxLat)
            this.maxLat = lat;
        if (lng > this.maxLng)
            this.maxLng = lng;
    },

    centerLat : function(skew)
    {
        return(this.maxLat-((this.maxLat-this.minLat)*skew));
    },

    centerLng : function()
    {
        return((this.minLng+this.maxLng)/2);
    }
}

