//Stop CSS Background Flickering in Internet Explorer 6
try {
  document.execCommand("BackgroundImageCache", false, true);
} catch(e) {}

//function to get random number from 1 to n (maxVal)
function randomToN(maxVal,floatVal) {
   var randVal = Math.random()*maxVal;
   return typeof floatVal=='undefined'?Math.round(randVal):randVal.toFixed(floatVal);
}
function destroyCommList() {
	//var fp = $.url.attr('directory');
	var fp = pageDirPath;
	$.cookie('MeritageHomes_Location', null, { expires: -1, path: fp });
	$('#commListZone').hide();
	$('.findItem').removeClass('current');
	$('#MeritageHomes_Location').fadeOut(300, function(){
		$('#MeritageHomes_Location strong').empty();
	});
}
function showCommList(x,z) {
	//var fp = $.url.attr('directory');
	var fp = pageDirPath;
	var p = new Array(x,z);
	$.cookie('MeritageHomes_Location', p, { expires: 99999, path: fp });
	curLocCheck ();
}
function commListAction() {
	$('.btn-close').click(function (){
		destroyCommList();
	});
	$('#commBody tr td a').click(function (){
		var targ = $(this).attr('href');
		getExtPage(targ);
		return false;
	});
	$('#commBody tr').hover(
		function () {
			$(this).addClass('current');
			var cell = $(this).find('td').get(0);
			var targ = $(cell).find('a').attr('href');
			var anch = $(cell).find('a');
			$(this).click(function (){
				getExtPage(targ);
			});
		}, 
		function () {
			$(this).removeClass('current');
			$(this).unbind('click');
		}
	);
}
function curLocCheck () {
	var curLoc = $.cookie('MeritageHomes_Location');
	if (curLoc !== null) {
		var p = curLoc.split(',');
		var theLoc = String(p[0]);
		var theState = String(p[1]);
		var rawLoc = theLoc;
		var rawLoc = rawLoc.replace("%20", " ");
		var theLoc = theLoc.replace("%20", " ");
		var theLoc = theLoc.replace("-", "/");
		var theLoc = theLoc.replace("_", ". ");
		//var curLocString = theLoc+'&ndash;'+theState;
		var curLocString = theLoc;
		$('#MeritageHomes_Location strong').html(curLocString);
		$('#MeritageHomes_Location').show();
		var findItems = $('.findItem').find('span');
		var findItemsL = findItems.length;
		for (i=0; findItemsL; i++) {
			var fI = $(findItems)[i];
			var I = String($(fI).html());
			if (I == theLoc) {
				//alert(I+' | '+p[0]+' | '+i);
				$('.findItem').eq(i).addClass('current');
				break;
			}
		}
		$.get("communities.php", { community:  rawLoc }, function(data){
   			//alert("Data Loaded: " + data);
   			$('#commBody').html(data);
			$('#commTitle').html(theLoc);
			$('#commListZone').slideDown(500);
			commListAction();			
 		});
	}
}
// JQUERY READY
$(function() {
$('#commListZoneALT').hide();

$('a.external').attr("target","_blank");

$('.findItem').click(function(){
	$('#commListZone').hide();
	$('.findItem').removeClass('current');
	var curLoc = $(this).find('a').attr('href');
	//var curLoc = curLoc.replace("%20", " ");
	var spans = $(this).parent().parent().find('span');
	var stateEl = $(spans)[0];
	var state = $(stateEl).html();
	showCommList(curLoc,state);
	return false;
});

if (window.commDirect) {
	showCommList(window.commDirect,null);
}
else {
	curLocCheck();
}

}); // JQUERY END
