function display_stats(props)
{        if (props.length > 0) {
                var prices = sortPrices(props);                
		var av_p = average(prices);
                var min_p = prices[0];                
		var max_p = prices[prices.length - 1];
                var median_p = median(prices);

                document.getElementById('median_p').innerHTML = '$' + outputComma(median_p.toFixed(0));
                document.getElementById('av_p').innerHTML = '$' + outputComma(av_p.toFixed(0));
                document.getElementById('min_p').innerHTML = '$' + outputComma(min_p.toFixed(0));
                document.getElementById('max_p').innerHTML = '$' + outputComma(max_p.toFixed(0));
        } else {

                document.getElementById('median_p').innerHTML = 'Waiting for data...';
                document.getElementById('av_p').innerHTML = '';
                document.getElementById('min_p').innerHTML = '';
                document.getElementById('max_p').innerHTML = '';
        }

}


function sortPrices(props) {
        var prices = Array();

        for (var i = 0; i < props.length; i++) {
        prices[i] = parseInt(cleanNum(props[i].getAttribute('lp_Listing_Price')));
        }

        prices.sort(function(a,b) { return a - b });

        return prices;
}

function median(prices) {

        if (prices.length % 2 == 0) {
                var high = prices[(prices.length/2)];
                var low = prices[(prices.length/2) - 1];

                return (high + low) / 2;
        } else if (prices.length == 1) {
                return prices[0];
        } else {
                return prices[Math.ceil(prices.length/2)];
        }

}

function average(prices)
{
   var sum = 0;

   for (i = 0; i < prices.length;i++)
   {
      sum += prices[i];
   }

   return (sum/prices.length);
}

    
function showHideStats(min, max) {
        var timer = 2;  
        var obj = document.getElementById('showHide');
        var step = -1;  
        var cur_top = parseInt(obj.style.top.substring(0, obj.style.top.length-2));
        if (cur_top < max) {
                // Hiding (scrolling down)
                step = 1;
        }

        function f() {
                cur_top = parseInt(obj.style.top.substring(0, obj.style.top.length-2));
                if (step == 1) {
                        if (cur_top > max) { return; }
                } else {
                        if (cur_top < min) { return; }
                }
        
                obj.style.top = parseInt(cur_top + step) + 'px';
                
                setTimeout(f, timer);
                
        }       

        setTimeout(f, timer); 
}

function outputComma(number) {
    number = '' + number
    if (number.length > 3) {
        var mod = number.length%3;
        var output = (mod > 0 ? (number.substring(0,mod)) : '');
        for (i=0 ; i < Math.floor(number.length/3) ; i++) {
            if ((mod ==0) && (i ==0))
                output+= number.substring(mod+3*i,mod+3*i+3);
            else
                output+= ',' + number.substring(mod+3*i,mod+3*i+3);
        }
        return (output);
    }
    else return number;
}


