jQuery(
  function() {
    // Version 1.0.5

    // retrieve the list via AJAX
    jQuery.getJSON(
      'http://www.newsobserver.com/cgi-bin/top_homes_data2.php?jsoncallback=?',
      function(json) {

	// MIN_HOMES is min number of homes to populate the widget.
	// if there are fewer than MIN_HOMES avail, we fill with
	// homes from FILL_PUB
	var MIN_HOMES = 3;
	var FILL_PUB  = 'NO';

	function randOrd() { return ( Math.round( Math.random() ) - 0.5 ); }

	function pubcode() {
	  var pubs = {
	    'carynews': 'CN',
	    'chapelhillnews': 'CH',
	    'claytonnewsstar': 'CS',
	    'easternwakenews': 'GL',
	    'garnerclayton': 'GC',
	    'newsobserver': 'NO',
	    'northraleighnews': 'NR',
	    'midtownraleighnews': 'MR',
	    'thedurhamnews': 'DU',
	    'theherald': 'SH',
	    'southwestwakenews': 'SW'
	  };
	  var href = window.location.href.toLowerCase();
	  for (var u in pubs) {
	    if (pubs.hasOwnProperty(u) && href.indexOf(u) >= 0) return pubs[u];
	  }
	  return undefined;
	}

	// which pub? if pub == undefined then we'll use ALL homes
	var pub = pubcode();

	// find homes for this pub and another "fill" pub
	// if pub is undefined, use all of the homes.
	var myhomes=[], fillhomes=[], home;
	for (var i = json.length; i-- ; ) {
	  home = json[i];
	  if (!pub || home.pubs.indexOf(pub) >= 0) {
	    myhomes.push(home);
	  }
	  else if (home.pubs.indexOf(FILL_PUB) >= 0) {
	    fillhomes.push(home);
	  }
	}

	var myhome_count = myhomes.length;
	// ***NOTE***
	//
	// filling code is disabled so that widget ONLY displays homes for this pub
	//
	// var fill_count   = myhome_count >= MIN_HOMES ? 0 : MIN_HOMES - myhome_count;
	// if (fill_count > 0) {
	//   fillhomes.sort(randOrd);
	//   myhomes = myhomes.concat( fillhomes.splice(0, fill_count) );
	// }

	if (!myhomes.length) return;

	myhomes.sort(randOrd);

	var htm = '', url,
	divider = '<tr class="divider"><td colspan="2"><hr width="80%" size="1" /></td></tr>';

	for (i = 0, myhome_count = Math.min(MIN_HOMES, myhomes.length); i < myhome_count; i++) {
	  if (i != 0)  htm += divider;

	  home = myhomes[i]; url = home.url;
	  htm += '<tr>'
	    +   '<td class="top-home-image" onclick="window.location=\'' + url + '\'"><img src="http://media2.newsobserver.com/static/images/featured_homes/' + home.thm + '" /></td>'
	    +   '<td class="top-home-info" onclick="window.location=\'' + url + '\'">'
	    +     '<p><a href="' + url + '">' + home.c + ', ' + home.st + '</a></p>'
	    +     '<p>' + home.br + ' Bedrooms, ' + home.ba + ' Bathrooms</p>'
	    +     '<p>' + home.sqft + ' Square Feet</p>'
	    +     '<p>$' + home.p + '</p>'
	    +   '</td>'
	    + '</tr>';
	}

	jQuery('#top-homes-content').append('<tbody>' + htm + '</tbody>');

	jQuery('#top-homes').css('display', 'block');

	jQuery('.top-home-image')
	  .mouseover(function() {
		       jQuery(this).addClass('highlight');
		       jQuery(this).next().addClass('highlight');
		     })
	  .mouseout(function() {
		      jQuery(this).removeClass('highlight');
		      jQuery(this).next().removeClass('highlight');
		    });

	jQuery('.top-home-info')
	  .mouseover(function() {
		       jQuery(this).addClass('highlight');
		       jQuery(this).prev().addClass('highlight');
		     }).mouseout(function() {
				   jQuery(this).removeClass('highlight');
				   jQuery(this).prev().removeClass('highlight');
				 });
      });
});

