/* Address Class */
function Address(street, city, state, zip, lat, lng)
{
    if(street != null)
    {
        this.street = street.replace(/^\s*|\s*$/g,"");
        if(this.street.length == 0)
        {
            this.street = null;
        }
    }
    else
    {
        this.street = null;
    }
    
    this.state = state;
    this.city = city;
    this.zip = zip;
    this.lat = lat;
    this.lng = lng;
}

Address.prototype.copy=function()
{
    return new Address(this.street, this.city, this.state, this.zip, this.lat, this.lng);
}

/* Agent class */
function Agent(id, agent_name, office_name, my_listing, my_brokerage)
{
    this.id = id;
    this.agent_name = agent_name;
    this.office_name = office_name;
    this.my_listing = my_listing;
    this.my_brokerage = my_brokerage;
}

Agent.prototype.copy=function()
{
    return new Agent(this.id, this.agent_name, this.office_name, this.my_listing, this.my_brokerage);
}

/* Home Class */
function Home(id, info, address, agent)
{
    this.id = id;
    this.info = info;
    this.address = address;
    this.realtor = 0;
    this.agent = agent;
    
    this.lth = this.info.listing_type + "_" + this.id;
    
    this.marker = null;
    this.compMarker = null;

/*    
    this.createMarker = createMarker;
    this.createCompMarker = createCompMarker;
    this.doMouseOver = doMouseOver;
    this.doMouseOut    = doMouseOut;
    this.doMouseClick = doMouseClick;
    
    this.doCompMouseOver = doCompMouseOver;
    this.doCompMouseOut    = doCompMouseOut;
    this.doCompMouseClick = doCompMouseClick;
    
    this.centerMap = centerMap;
*/    
}

Home.prototype.copy=function()
{
    return new Home(this.id, this.info.copy(), this.address.copy(), (this.agent==null)?null:this.agent.copy());
}

Home.prototype.createCompMarker=function(id, compIndex) 
{
    var icon = _mdc.iHomeSale;
    var iconSel = _mdc.iHomeSaleSel;
    var iconComp = _mdc.iHomeSaleComp;
    if(this.info.isFSBO())
    {
        if(this.info.realtor == "0" || this.info.realtor == "0.0")
        {
            icon = _mdc.iFSBO;
        }
        else
        {
            icon = _mdc.iFSBOAgent;
        }
        
        iconSel = _mdc.iFSBOSel;
        iconComp = _mdc.iFSBOComp;
    }
    
    var point = new GLatLng(this.address.lat, this.address.lng);
    
    var marker = new HomeMarker(icon, iconSel, iconComp, point, 0); 
    
    //when mapped
    GEvent.addListener(marker, "mouseover", function() {
        _mdc.findHomeById(id).doCompMouseOver(compIndex);
    });
    
    GEvent.addListener(marker, "mouseout", function() {
        _mdc.findHomeById(id).doCompMouseOut();
    });
    
    GEvent.addListener(marker, "click", function() {
        _mdc.findHomeById(id).doCompMouseClick(compIndex);
    });
    
    this.marker = marker;
    
    return marker;
}

Home.prototype.createMarker=function(index) 
{
    var icon = _mdc.iHomeSale;
    var iconSel = _mdc.iHomeSaleSel;
    var iconComp = _mdc.iHomeSaleComp;
    var zindex = 0;
    
    if (this.agent!= null && (this.agent.my_listing || this.agent.my_brokerage))
    {
        icon = _mdc.iHomeSaleMine;
        iconSel = _mdc.iHomeSaleMineSel;
        zindex = 1000;
    }
    if(this.info.isFSBO())
    {
        if(this.info.realtor == "0" || this.info.realtor == "0.0")
        {
            icon = _mdc.iFSBO;
        }
        else
        {
            icon = _mdc.iFSBOAgent;
        }
        
        iconSel = _mdc.iFSBOSel;
        iconComp = _mdc.iFSBOComp;
    }
	if (this.info.open_house == "1")//document.getElementById("search_open_house").checked)
	{
		icon = _mdc.iHomeSaleOpenHouse;
		iconSel = _mdc.iHomeSaleOpenHouseSel;
	}
    
    //debugger;
    var point = new GLatLng(this.address.lat, this.address.lng);
    
    var marker = new HomeMarker(icon, iconSel, iconComp, point, zindex); 
    
    //when mapped
    GEvent.addListener(marker, "mouseover", function() {
        _mdc.mapHomes[index].doMouseOver();
    });
    
    GEvent.addListener(marker, "mouseout", function() {
        _mdc.mapHomes[index].doMouseOut();
    });
    
    GEvent.addListener(marker, "click", function() {
        _mdc.mapHomes[index].doMouseClick();
    });
    
    this.marker = marker;
    
    return this.marker;
}

Home.prototype.doMouseOver=function()
{

    var formatPrice = new NumberFormat(this.info.price);
    formatPrice.setCurrency(true);
    $('tt_price').innerHTML = formatPrice.toFormatted();
    $('tt_beds').innerHTML = this.info.bedrooms;
    $('tt_baths').innerHTML = this.info.baths;
    formatPrice = new NumberFormat(this.info.sq_feet);
    formatPrice.setCurrency(false);
    $('tt_sq_feet').innerHTML = formatPrice.toFormatted();
    
    if(this.info.photos.length > 0)
    {
        $('tt_image').src = this.info.photos[0].src;
    }
    else
    {
        $('tt_image').src = "/shared/images/nophoto.gif";
    }
    
    var tipHeight=128;
    var tt_broker;

    switch (this.info.listing_type)
    {
    case '23': // devel
        $('tt_price').innerHTML = this.info.price_range;
        $('tt_price').width = "100px";
        $('tt_broker').innerHTML = this.info.title;
        $('tt_bed_label').style.display="none";
        $('tt_bath_label').style.display="none";
        $('tt_sqft_label').style.display="none";
        break;

	case '68': // metrolist and IRES combined
    case '14': // metrolist
        if(this.agent != null)
        {
			if (this.agent.my_listing != true && (this.info.listing_type != 68 || this.id.charAt(0) == '1')) // the combined feed has all metrolist entries starting with the letter "1"
			{
        	    $('tt_broker').innerHTML = "<div style='float:right;right:5px;padding-top:3px;'><img src='/shared/images/idx_logo.gif' width='33px' height='17px'/></div>";
           	}
	        else
    	    {
        	    $('tt_broker').innerHTML = "";
			}
            $('tt_broker').innerHTML += "<div class='borange' style=\"border-top: 1px solid white;padding-top: 2px; font-size:9px;vertical-align: top\">"+this.agent.office_name+" MLS&nbsp;#"+this.info.mls_num+"</div>";
            $('tt_sqft_label').innerHTML = "<span id='tt_sq_feet'></span>";
            // $('tt_sq_feet').style.display="none";
        }
        break;
    case '15': // wpmls
        if(this.agent && (this.agent.my_listing || this.agent.my_brokerage))
        {
            $('tt_broker').innerHTML = "<span style='font-size:12px'>" + this.agent.office_name + "</span>";
        }
        else
        {
            $('tt_broker').innerHTML = "<div width='100%' align='center'><img class='png' src='/shared/images/wpmls_logo.png' width='54px' height='27px'/></div>";
        }
        break;

    case '18':// santafe
	tt_broker = $('tt_santafe') == null ? $('tt_broker') : $('tt_santafe');

        if(this.agent && (this.agent.my_listing || this.agent.my_brokerage))
        {
            tt_broker.innerHTML = "<span style='font-size:12px'>" + this.agent.office_name + "</span>";
        }
        else
        {
            tt_broker.innerHTML = "<div width='100%' align='center'><img class='png' src='/shared/images/idx_logo_sf.png'/></div>";
        }
        break;
    case '21': //armls
        if(this.agent && (this.agent.my_listing || this.agent.my_brokerage))
        {
            $('tt_broker').innerHTML = "<span style='font-size:12px'>" + this.agent.office_name + "</span>";
        }
        else
        {
            $('tt_broker').innerHTML = "<div width='100%' align='center'><img class='png' src='/shared/images/armls_logo.png'/></div>";
        }
        break;

    case '33': //act (austin)
        if(this.agent && (this.agent.my_listing || this.agent.my_brokerage))
        {
            $('tt_broker').innerHTML = "<span style='font-size:12px'>" + this.agent.office_name + "</span>";
        }
        else
        {
            $('tt_broker').innerHTML ="";
        }
        break;

    default:
        if(this.agent != null)
        {
            $('tt_broker').innerHTML = "<div style=\"border-top: 1px solid white; padding-top: 2px; height: 25px; vertical-align: top\">"+this.agent.office_name+"</div>";
        }
        else
        {
            tipHeight = 103;
            $('tt_broker').innerHTML = "";
        }
    }

    if (this.info.listing_type != '23') // development
    {
        if ($('tt_bed_label') != null)
        {
            $('tt_bed_label').style.display="";
            $('tt_bath_label').style.display="";
            $('tt_sqft_label').style.display=""; 
        }
    }

    var point = _mdc.map.getCurrentMapType().getProjection().fromLatLngToPixel(_mdc.map.getBounds().getNorthEast(),_mdc.map.getZoom());
    var offset = _mdc.map.getCurrentMapType().getProjection().fromLatLngToPixel(this.marker.getPoint(),_mdc.map.getZoom());
    
    var x = point.x-offset.x + 4;
    var y = offset.y - point.y -tipHeight-11;
    var pos = new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(x, y)); 
    
    _mdc.tooltip.style.top = null;
    _mdc.tooltip.style.right = null;
    _mdc.tooltip.style.left = null;
    
    pos.apply(_mdc.tooltip);
    _mdc.tooltip.style.display = "block";

}

Home.prototype.doCompMouseClick=function(compIndex)
{
    var comp = this.info.comps[compIndex];
    var home = _mdc.findHomeById(comp.id);
    if(home != null)
    {
        home.doMouseClick();
    }
    else
    {
        var newMarker = comp.copy();
        var index = _mdc.mapHomes.length;
        _mdc.mapHomes[index] = newMarker;
        _mdc.map.removeOverlay(comp.marker);
        _mdc.map.addOverlay(newMarker.createMarker(index));
        newMarker.doMouseClick();        
    }
}

Home.prototype.doMouseClick=function()
{    
    if(_mdc.currentHome != null && _mdc.currentHome != this)
    {
        _mdc.currentHome.info.hideCompIcons();
        if(_mdc.currentHome.marker != null)
        {
            _mdc.currentHome.marker.setInactive();
        }
    }
    
    if(this.marker != null && _mdc.currentHome != this)
    {
        this.marker.setActive();
    }
    
    
    _search.getHomeDetails(this.id);
}

Home.prototype.doCompMouseOver=function(compIndex)
{
    _mdc.currentHome.info.comps[compIndex].doMouseOver();
}

Home.prototype.doCompMouseOut=function()
{
    _mdc.tooltip.style.display = "none";
}

Home.prototype.doMouseOut=function()
{    
    _mdc.tooltip.style.display = "none";
}

Home.prototype.centerMap=function(marker)
{
    _mdc.map.setCenter(marker.getPoint());
    _mdc.zoomMap(15);
}


/* HomeInfo class */
function HomeInfo(listing_type, mls_num, price, realtor, bedrooms, baths, sq_feet, sold, open_house, relevence)
{
    this.listing_type = listing_type;
    this.mls_num = mls_num;
    this.price = price;
    this.realtor = realtor;
    this.bedrooms = bedrooms;
    this.baths = baths;
    this.sq_feet = sq_feet;
    this.open_house = open_house;
    this.relevence = relevence;
    
    if(sold == 'n')
    {
        this.sold = false;
    }
    else
    {
        this.sold = true;
    }
    
    //details extra
    this.home_type = "";
    this.home_style = "";
    this.acres = 0;
    this.year = 0;
    this.photos = new Array();
    this.photo_index = 0
    
    this.comps = new Array();
    
    
    /*
    this.addPhotos = addPhotos;
    this.getLotFootage = getLotFootage;
    this.showInfo = showInfo;
    this.showDetails = showDetails;
    this.addComps = addComps;
    this.showComps = showComps;
    this.nextPhoto = nextPhoto;
    this.prevPhoto = prevPhoto;
    this.isFSBO = isFSBO;
    */
}

HomeInfo.prototype.copy=function()
{
    var home = new HomeInfo(this.listing_type, this.mls_num, this.price, this.realtor, this.bedrooms, this.baths, this.sq_feet, (this.sold)?'y':'n', this.open_house);
    home.home_type = this.home_type;
    home.home_style = this.home_style;
    home.acres = this.acres;
    home.year = this.year;
    home.photos = this.photos;
    home.photo_index = this.photo_index;
    home.comps = this.comps;
    
    return home;
}
HomeInfo.prototype.showCompIcons=function()
{
    for(var i = 0; i < this.comps.length; i++)
    {
        //map.addOverlay(this.comps[i]);
        var comp = this.comps[i];
        var home = _mdc.findHomeById(comp.id);
        if(home != null)
        {
            home.marker.setComp();
        }
        else
        {
            var homeMarker = comp.marker;
            if(homeMarker == null)
            {
                homeMarker = comp.createCompMarker(_mdc.currentHome.id, i);
            }
            
            _mdc.map.addOverlay(homeMarker);
            homeMarker.setComp();    
        }
    }
}

HomeInfo.prototype.hideCompIcons=function()
{
    for(var i = 0; i < this.comps.length; i++)
    {
        var comp = this.comps[i];
        var home = _mdc.findHomeById(comp.id);
        if(home == null)
        {
            if(comp.marker != null)
            {
                _mdc.map.removeOverlay(comp.marker);
            }
        }
        else
        {
            if(_mdc.currentHome != home)
            {
                home.marker.setInactive();
            }
        }
    }
}

HomeInfo.prototype.addComps=function(comps)
{
    this.comps = comps;
}

HomeInfo.prototype.showInfo=function()
{

    if (typeof custom_show_info != 'undefined' && (custom_show_info == 'start' || custom_show_info == 'both'))
    {
        var finished = customShowInfo(this, "start");
        if (finished)
        {
            return;
        }
    }

    $('info_photo').src = this.photos[0].src;
    this.photo_index = 0;
    $('current_photo_text').innerHTML = "1";
    $('total_photos_text').innerHTML = this.photos.length;
    var address_el = $('info_address');
    if (address_el != null) {
        address_el.innerHTML = this.home_address;
    }

    if(this.virtual_url == null)
    {
        $('virtual_tour_lnk').onclick = new Function("return false");
        if ($('virtual_tour_lnk').className.indexOf("br_search_btn") != -1)
        {
            $('virtual_tour_lnk').className = "br_search_btn_disabled";
        }
        else
        {
            roundedBackgroundColor($('virtual_tour_lnk'), "span", "#cecece");  
            $('virtual_tour_lnk').style.backgroundColor = "#cecece";
        }
    }
    else
    {
        $('virtual_tour_lnk').onclick = new Function("launchTour('"+ this.virtual_url +"');");
        if ($('virtual_tour_lnk').className.indexOf("br_search_btn") != -1)
        {
            $('virtual_tour_lnk').className = "br_search_btn";
        }
        else
        {
            roundedBackgroundColor($('virtual_tour_lnk'), "span", ""); 
            roundedClassName($('virtual_tour_lnk'), "span", "borange");  
            $('virtual_tour_lnk').style.backgroundColor = "";
        }
    }

    
    if (this.listing_type == '23') // New home developments
    {
        $('info_summary').style.display='none';
        $('info_summary2').style.display='';
        if (this.feature != null)
        {
             if (this.feature.length > 195)
             {
                 $('info_summary_text').innerHTML = this.feature.substring(0, 195)+"... <span class='corange' style='cursor:pointer' onclick='launchDetails(_mdc.currentHome.lth);'>MORE</span>";
             }
             else $('info_summary_text').innerHTML = this.feature;
        }
        else
        {
              $('info_summary_text').innerHTML = "";
        }
        $('mls_price_text').innerHTML = this.price_range;
    }
    else
    {
        if ($('info_summary'))
        {
            $('info_summary').style.display='';
            $('info_summary2').style.display='none';
        }
        var format = new NumberFormat(this.price);
        format.setCurrency(true);
        $('mls_price_text').innerHTML = format.toFormatted();
        if(this.isFSBO()) //fsbo
        {
            $('mls_num_label').innerHTML = "Home&nbsp;#:";
            $('mls_num_text').innerHTML = _mdc.currentHome.id;
        }
        else
        {
            $('mls_num_label').innerHTML = "MLS&nbsp;#:";
            $('mls_num_text').innerHTML = this.mls_num;
        }
        $('year_text').innerHTML = this.year;
        $('beds_text').innerHTML = this.bedrooms;
        format.setNumber(this.sq_feet)
        format.setCurrency(false);
        $('sq_ft_text').innerHTML = format.toFormatted();
        $('baths_text').innerHTML = this.baths;
        format.setNumber(this.getLotFootage())
        if($('lot_sq_ft_text'))
        {
            $('lot_sq_ft_text').innerHTML = format.toFormatted();
        }
        if ($('acres_text'))
        {
            $('acres_text').innerHTML = this.acres;
        }

        format.setNumber(this.price/this.sq_feet);
        format.setCurrency(true);
        if ($('per_sq_ft_text')) 
        {
            $('per_sq_ft_text').innerHTML = format.toFormatted();
        }
    }
    
    //show status
    if ($('status_text')) 
    {
        if (_mdc.currentHome.info.listing_type=='14' || _mdc.currentHome.info.listing_type=='68') // metrolist
        {
            if ($('per_sq_ft_text'))
            {
                if ($('per_sq_ft_label'))
                    $('per_sq_ft_label').style.display="none";
                $('per_sq_ft_text').innerHTML = "";
            }
			var is_metrolist = !(_mdc.currentHome.info.listing_type == '68' && _mdc.currentHome.id.charAt(0) != '1');
            if(_mdc.currentHome.agent)
            {
                if (_mdc.currentHome.agent.my_listing || _mdc.currentHome.agent.my_brokerage)
                {
                    $('after_address_text').innerHTML = "<table cellpadding='0' cellspacing='0'><tr><td class='corange' style='font-size:12px;padding-top:8px;'><strong>Listed With: " + _mdc.currentHome.agent.office_name + "</strong></td></tr></table>";            
                }
                else if (is_metrolist)
                {
                    $('after_address_text').innerHTML = "<table cellpadding='0' cellspacing='0'><tr><td style='padding-left:6px; padding-right:6px;padding-top:8px;'><img src='/shared/images/idx_logo.gif' width='33px' height='17px'/></td><td class='corange' style='font-size:12px;padding-top:8px;'><strong>Listed With: " + _mdc.currentHome.agent.office_name + "</strong></td></tr></table>";
                }
            }
            else if (is_metrolist)
            {
                $('after_address_text').innerHTML += "<div width='100%' align='right'><img src='/shared/images/idx_logo.gif' width='33px' height='17px'/></div>";
            }
            if(this.sold == true)
            {
                $('status_text').innerHTML = "Sold";
            }
            else
            {
                $('status_text').innerHTML = "Active on Market";
            }
        }
        else if (_mdc.currentHome.info.listing_type=='15' || _mdc.currentHome.info.listing_type=='19') // wpmls and tristate
        {
            if(_mdc.currentHome.info.listing_type == '19')
            {
                $('status_text').innerHTML = "<span class='cdgrey' style='font-size:12px'>"+_mdc.currentHome.agent.agent_name.replace(',','<br/>')+"<br/>"+ _mdc.currentHome.agent.office_name + "</span>"
            }
            else if(_mdc.currentHome.agent.my_listing || _mdc.currentHome.agent.my_brokerage)
            {
                $('status_text').innerHTML = "<span class='cdgrey' style='font-size:12px'>" + _mdc.currentHome.agent.office_name + "</span>";
            }
            else
            {
                $('status_text').innerHTML = "<div width='100%' align='center'><img src='/shared/images/wpmls_logo.gif' width='54px' height='27px'/></div>";
            }
        }
        else
        {
            if(this.sold == true)
            {
                $('status_text').innerHTML = "Sold";
            }
            else
            {
                $('status_text').innerHTML = "Active on Market";
            }
            if(_mdc.currentHome.agent)
            {
                $('status_text').innerHTML += "<br><strong>Listed With:</strong> " + _mdc.currentHome.agent.office_name;    
            }
            else
            {
                $('status_text').innerHTML += "&nbsp;";
            }
            if (this.listing_type == '4') // NNRMLS
            {
                $('status_text').innerHTML += "<img src='/shared/images/nnrmls_reciprocity-thumb.gif'/>";
            }
        }
    }
    else if ($('listing_office_text')) 
    {
        if(_mdc.currentHome.agent)
        {
            $('listing_office_text').innerHTML = "<strong>Listed With:</strong> " + _mdc.currentHome.agent.office_name;    
        }
        else
        {
            $('listing_office_text').innerHTML += "&nbsp;";
        }
    }
    else if ($('metrolist_text'))
    {
        if(_mdc.currentHome.agent)
        {
            $('metrolist_text').innerHTML = "<table cellpadding='0' cellspacing='0'><tr><td style='padding-left:6px; padding-right:6px'><img src='/shared/images/idx_logo.gif' width='33px' height='17px'/></td><td class='corange' style='font-size:12px'><strong>Listed With: " + _mdc.currentHome.agent.office_name + "</strong></td></tr></table>";
        }
        else
        {
            $('metrolist_text').innerHTML += "<div width='100%' align='right'><img src='/shared/images/idx_logo.gif' width='33px' height='17px'/></div>";
        }
    }
    else if ($('santafe_text'))
    {
        if(_mdc.currentHome.agent && ((_mdc.currentHome.agent.office_name == "Prudential North Valley") || (_mdc.currentHome.agent.office_name.search("Prudential Santa Fe") != -1)))
        {
            $('santafe_text').innerHTML = "<span class='cdgrey' style='font-size:12px'>" + _mdc.currentHome.agent.office_name + "</span>";
        }
        else
        {
            $('santafe_text').innerHTML = "<div width='100%' align='center'><img class='png' src='/shared/images/idx_logo_sf.png'/></div>";
        }
    }
    if ($('open_house_text') != null && (_mdc.currentHome.info.open_house_start != null))
    {
        var ohtext = "<br/>Open House: "+_mdc.currentHome.info.open_house_start;
        if (_mdc.currentHome.info.open_house_start != _mdc.currentHome.info.open_house_end)
            ohtext += " - "+_mdc.currentHome.info.open_house_end;
	if (_mdc.currentHome.info.open_house_comment != "")
		ohtext += "<br />"+_mdc.currentHome.info.open_house_comment;
        $('open_house_text').innerHTML = ohtext;
    }

    if ($('list_class_text') != null && (_mdc.currentHome.info.list_class != null))
    {
        $('list_class_text').innerHTML = _mdc.currentHome.info.list_class;
    }

    if ($('walk_score_panel') != null)
    {
        ws_lat = _mdc.currentHome.address.lat;
        ws_lng = _mdc.currentHome.address.lng;
    }
        
    if(_tab_accordian != null)
    {
//        _tab_accordian.showTabByIndex(1, false); // rico 1.0 syntax
//      Rico.animate(_tab_accordian.transition(_tab_accordian.contents[1])); // rico 2.0 syntax
        _tab_accordian.selectionSet.select($('detailsHeader'));
    }

    if (typeof custom_show_info != 'undefined' && (custom_show_info == 'end' || custom_show_info == 'both'))
    {
        customShowInfo(this, 'end');
    }

    var ajax = new SimpleAjax('increment_visited', '/ajax/increment_visited.php', null);
    ajax.sendPost(new Array("lth="+this.listing_type+"_"+_mdc.currentHome.id));
}

HomeInfo.prototype.develInfo = function(price_range, title)
{
    this.price_range = price_range;
    this.title= title;
}

HomeInfo.prototype.showComps=function()
{
    //clear list view table;
    var tbody = $('comp_list_table');

    var rows = tbody.rows;
    var size = rows.length; //bch36: delete everything
    
    for(var i = 0; i < size; i++)
    {
        tbody.deleteRow(0);
    }
    
    
    var total_price = 0;
    var total_beds = 0;
    var total_baths = 0;
    var total_sq_feet = 0;
    for(var i = 0; i < this.comps.length; i++)
    {
        var comp = this.comps[i];
        var tr = tbody.insertRow(i);
        
        if(i%2 != 0)
        {
            //bch36 tr.className = "bllgrey";
        }
    
        //var cell = tr.insertCell(0);
        //cell.style.paddingLeft = "5px";
        //cell.innerHTML = "<nobr><a href=\"#\" onclick=\"_mdc.currentHome.doCompMouseClick("+i+"); return false;\">" + comp.address.street + "</a></nobr>";
        
        total_price += parseInt(comp.info.price, 10);;
        
        var format = new NumberFormat(comp.info.price);
        format.setCurrency(true);
        
        //cell = tr.insertCell(1);
        //cell.innerHTML = "<nobr>" + format.toFormatted() + "</nobr>";
        
        total_beds += parseInt(comp.info.bedrooms, 10);
        //cell = tr.insertCell(2);
        //cell.innerHTML = "<nobr>" + comp.info.bedrooms+ "</nobr>";
        
        total_baths += parseInt(comp.info.baths, 10);
        //cell = tr.insertCell(3);
        //cell.innerHTML = "<nobr>" + comp.info.baths+ "</nobr>";
        
        total_sq_feet += parseInt(comp.info.sq_feet, 10);
        var format2 = new NumberFormat(comp.info.price);
        format2.setNumber(comp.info.sq_feet)
        format2.setCurrency(false);
        
        //cell = tr.insertCell(4);
        //cell.innerHTML = "<nobr>" + format2.toFormatted() + "</nobr></td>";

        //bch36: new view
        var cell = tr.insertCell(0);
        cell.style.paddingLeft = "5px";
        cell.style.paddingRight = "5px";
        cell.style.width = "1%";
        cell.style.height = "1%";
        var img_url = "/shared/images/nophoto.gif";
        if (comp.info.photos != undefined && comp.info.photos[0] != undefined)
            img_url = comp.info.photos[0].src;

        cell.innerHTML = "<a href=\"#\" onclick=\"_mdc.currentHome.doCompMouseClick("+i+"); return false;\" style='text-decoration: none'><img src='"+img_url+"' width='80px' border='0' /></a>";

        cell = tr.insertCell(1);
        cell.innerHTML = "";

        cell = tr.insertCell(2);
        cell.innerHTML = "";

        cell = tr.insertCell(3);
        cell.innerHTML = "";


        cell = tr.insertCell(4);
        cell.innerHTML = "<nobr><a href=\"#\" class='body_contrast_color' onclick=\"_mdc.currentHome.doCompMouseClick("+i+"); return false;\" style='text-decoration: none;'><div class='body_contrast_color' style='font-size: 14px; font-weight: bold;'>"+format.toFormatted()+"</div>"+
                "<div class='body_contrast_color' style='font-weight: bold;'>Beds: "+comp.info.bedrooms+"&nbsp;&nbsp;&nbsp;&nbsp;Baths: "+comp.info.baths+"</div>"+
                "<div class='body_contrast_color' style='font-weight: bold;'>Total Sq Ft: "+format2.toFormatted()+"</div></a>";
        if(_mdc.currentHome.info.listing_type=='14') //metrolist
        {
            if (comp.agent.my_listing != true && comp.agent.my_brokerage != true)
            {
                cell.innerHTML += "<div style='float:right;padding-top:3px;'><img src='/shared/images/idx_logo.gif' width='33px' height='17px'/></div>";
            }
            cell.innerHTML += "<div style=\"padding-top: 2px;font-weight:bold;vertical-align: top\">"+comp.agent.office_name+" MLS&nbsp;#"+comp.info.mls_num+"</div>";
        }
    }
    
    if(this.comps.length == 0)
    {
        var tr = tbody.insertRow(0);
        var cell = tr.insertCell(0);
        cell.style.paddingLeft = "5px";
        cell.colSpan = 5;
        cell.innerHTML = "<nobr>No homes available.</nobr>";
    }
    else
    {
        this.showCompIcons();
    }
    
    var total_comps = this.comps.length;
    if(total_comps == 0)
    {
        total_comps = 1; //don't allow division by zero
    }
    
    var format = new NumberFormat(total_price/total_comps);
    format.setCurrency(true);
    $('comp_avg_price').innerHTML = "<nobr>" + format.toFormatted() + "</nobr>";
    
    
    format.setNumber(total_beds/total_comps)
    format.setCurrency(false);
    $('comp_avg_beds').innerHTML = "<nobr>" + format.toFormatted()+ "</nobr>";
    
    format.setNumber(total_baths/total_comps)
    $('comp_avg_baths').innerHTML = "<nobr>" + format.toFormatted()+ "</nobr>";
    
    format.setNumber(total_sq_feet/total_comps)
    $('comp_avg_sq_feet').innerHTML = "<nobr>" + format.toFormatted() + "</nobr></td>";
}

HomeInfo.prototype.addPhotos=function(photos)
{
    //find the main photo
    var more_count = 1;
    for(var i = 0; i < photos.length; i++)
    {
        var photo = photos[i];
        
        if(photo.getAttribute == null)
        {
            this.virtual_url = photo;
            continue;
        }
        
        
        if(photo.getAttribute("type") == 'main')
        {
            this.photos[0] = photo;
        }
        else if(this.photos[0] != null && this.photos[0].src != photo.src)
        {
            this.photos[more_count++] = photo;
        }
    }
        
    if(this.photos.length == 0)
    {
        this.photos[0] = new Image();
        this.photos[0].src = "/shared/images/nophoto.gif";
    }
}

HomeInfo.prototype.isFSBO=function()
{
    return (this.listing_type == 1);
}

HomeInfo.prototype.nextPhoto=function()
{
    var size = this.photos.length;
    if(size <= parseInt(this.photo_index) + 1)
    {
        this.photo_index = 0;
    }
    else
    {
        this.photo_index++;
    }
    
    $('info_photo').src = this.photos[this.photo_index].src;
    $('current_photo_text').innerHTML = this.photo_index+1;
}

HomeInfo.prototype.prevPhoto=function()
{
    if(this.photo_index - 1 < 0)
    {
        this.photo_index = this.photos.length-1;
    }
    else
    {
        this.photo_index--;
    }
    
    $('info_photo').src = this.photos[this.photo_index].src;
    $('current_photo_text').innerHTML = this.photo_index+1;
}

HomeInfo.prototype.getLotFootage=function()
{
    return this.acres*43560;
}
