var TYPES   = new Array("newhomes", "addoni", "schools", "hschools", "hospitals", "zoomed", "partner");
var WIDTHS  = new Array(29		  , 21		, 18	   , 18		   , 18			, 29	  , 29);
var HEIGHTS = new Array(29		  , 21		, 18	   , 18		   , 18			, 29	  , 29);

function getImageStr(str, fav) {
	var favstr = "";
	if(fav) favstr = "_fav";
	return "images/icons/"+str+favstr+".png";
}

//========================LOCATION BEGIN====================================
function Location(id, latlon, m_type, m_subtype, name, address, phone, summary, vlink, showdir, ilink, builder) {
	this.id = id;
	this.marker;
	this.latlon = latlon;
	m_type = parseInt(m_type);
	this.type = m_type;
	this.type_num = m_type;
	this.subtype = parseInt(m_subtype);
	
	if(this.type_num == 0) {
		this.type_num = this.subtype;
	} else {
		if(this.type_num == 1) {
			this.type_num = this.type_num + this.subtype;
		} else
			this.type_num = this.type_num + 2;
	}
	
	this.m_type = TYPES[this.type_num];
	this.builderid = builder;

	this.name = name;
	if(this.type == 1) {
		if(this.subtype == 0)
			this.name += " Elementary School";
		else if(this.subtype == 1)
			this.name += " Middle School";
		else
			this.name += " High School";
	}
	this.address = address;
	this.phone = phone;
	this.summary = summary;
	this.vlink = vlink;
	this.showdir = showdir;
	this.ilink = ilink;
	if(this.ilink == "" || this.ilink == "images/community/") this.ilink = "images/community/nopic.png";
	
	this.initMarker();
}

Location.prototype.initMarker = function() {
	this.marker = createNewMarker(this.latlon, getImageStr(this.m_type, false), WIDTHS[this.type_num], HEIGHTS[this.type_num]);
	var me = this;
	
	//Masterplan click behavior.
	if(this.type == 3) {
		GEvent.addListener(me.marker, "click", function() {
			setMapCenter(me.latlon.lat(), me.latlon.lng());
		});
		
		GEvent.addListener(me.marker, "mouseover", function() {
			me.marker.openInfoWindowHtml(me.getSummaryHTML(0, true));
			loglink("blurb:"+me.id);
		});
	} else {
		if(this.type == 0 && this.subtype == 0) {
			GEvent.addListener(me.marker, "click", function() {
			//*
				if(checkMapSize()) {
					me.marker.openInfoWindowHtml("Loading details for location, please wait.");
				}
				showDetailsCheckHeight(me.id);
			});
			GEvent.addListener(me.marker, "mouseover", function() {
			//*/
				me.marker.openInfoWindowHtml(me.getSummaryHTML(0, true));
				loglink("blurb:"+me.id);
			});			
		} else {
			GEvent.addListener(me.marker, "mouseover", function() {
				/*
				if(checkMapSize()) {
					me.marker.openInfoWindowHtml("Loading details for location, please wait.");
				}
				showDetailsCheckHeight(me.id);
				*/
				me.marker.openInfoWindowHtml(me.getSummaryHTML(0, true));
				loglink("blurb:"+me.id);
			});
		}
	}
	
	/*
	//Add On property click behavior.
	if(this.type == 0 && this.subtype == 1) {
		GEvent.addListener(me.marker, "click", function() {
			if(me.vlink != "") {
				logopenlink(me.vlink);
			}
		});
	}
	*/
	
	if(this.address.substr(0,2) == ", ") this.address = this.address.substring(2, this.address.length);
}

Location.prototype.checkIfMasterPlan = function() {
	return (this.type == 3);
}

Location.prototype.getBuilderName = function() {
	var b = getBuilderForID(this.builderid);
	if(b != null) {
		return "<font class='Saddress'> by "+b.name+"</font>";
	}
	
	return "";
}

Location.prototype.getLinkHTML = function(unique) {
		var myHtml = "";
		if(this.vlink != "") {
			myHtml += "&nbsp;<a href='javascript:logopenlink(\""+this.vlink+"\");'><img src='images/community.png' border=0 title='Community link.'></a>";
		}
		if(this.showdir) {
			myHtml += "&nbsp;<a href='javascript:loglink(\"street:"+this.id+"\"); show_street("+this.id+");'><img src='images/street.png' border=0 title='View in street view.'></a>";
			myHtml += "&nbsp;<a href='javascript:loglink(\"directions:"+this.id+"\"); getDirections("+this.id+", 0);'><img src='images/direction.png' border=0 title='Get driving directions.'></a>";
			myHtml += "&nbsp;<a href='javascript:loglink(\"commute:"+this.id+"\"); getDirections("+this.id+", 1);'><img src='images/gas.png' border=0 title='Get commute cost and driving directions.'></a>";
		}
		if(this.type == 0) {
			if(isInFav(this.id)) {
				myHtml += "&nbsp;<a id='favA"+this.id+"u"+unique+"' href='javascript:loglink(\"remfav:"+this.id+"\"); removeFav("+this.id+");'><img id='favI"+this.id+"u"+unique+"' src='images/remfav.png' border=0 title='Remove from favorties.'></a>";
			} else {
				myHtml += "&nbsp;<a id='favA"+this.id+"u"+unique+"' href='javascript:loglink(\"addfav:"+this.id+"\"); addToFav("+this.id+");'><img id='favI"+this.id+"u"+unique+"' src='images/addtofav.png' border=0 title='Add to favorties.'></a>";
			}
		}
		if(this.checkIfMasterPlan()) {
			myHtml += ' <a href="javascript:setMapCenter('+this.latlon.lat()+', '+this.latlon.lng()+')"><img src="images/zoomin.png" border=0 title="Zoom in."></a>';
		}

		//this.marker.closeInfoWindow();
		
		return myHtml;
}

Location.prototype.getSummaryHTML = function (unique, withBubble) {
	var myHtml = "";
	if(withBubble) {
		myHtml += '<table cellspacing=0 cellpadding=0 border=0 width=400><tr><td class="brtl">&nbsp;</td><td class="brtm">&nbsp;</td><td class="brtr">&nbsp;</td></tr>';
		myHtml += '<tr><td class="brlm">&nbsp;</td><td class="brm">';
	}
	
	if(unique != 0) {
		myHtml += '<div style="cursor: pointer;" onmousedown="triggerOverFor('+this.id+'); setMapCenter'+this.latlon.toString()+';">';
	}
	
	if(this.type == 0 && this.ilink != "images/community/nopic.png")
		myHtml += '<img src="'+this.ilink+'" style="margin-right: 5px;" width=75 height=50 align=left>';
	myHtml += '<b class="Sname">'+this.name+'</b>'+this.getBuilderName();
	
	if(this.type != 3) {
		myHtml += '<br><font class="Saddress">'+this.address+'</font>';
		myHtml += '<br><i class="Sphone">'+this.phone+'</i>';
	}
		
//	if(unique != 3) {
		if(this.summary != null && this.summary != "") {
			myHtml += '<br><br><font class="Ssummary">'+this.summary+'</font>';
		} else {
			var ld = getDetailsForID(this.id);
			if(ld != null)
				myHtml += '<br><br><font class="Ssummary">'+ld.getLDHtml()+'</font>';
		}
//	}

	myHtml += "<br><i class='Sphone'>*Information/pricing provided is subject to change without notice.</i>";
	if(unique != 0) {
		myHtml += '</div>';
	}
	
	myHtml += "<br><br>";
	if(this.type == 0 && this.subtype == 0)
		myHtml += '<a href="javascript:showDetailsCheckHeight('+this.id+');"><img src="images/tvico.gif" border=0 title="More information and video tour."></a>';
	myHtml += this.getLinkHTML(unique);
	
	if(withBubble) {
		myHtml += '</td><td class="brrm">&nbsp;</td></tr>';
		myHtml += '<tr><td class="brbl">&nbsp;</td><td class="brbm">&nbsp;</td><td class="brbr">&nbsp;</td></tr></table>';
	}
	
	return myHtml;
}

/*
Location.prototype.getDetailsHTML = function() {
	if(this.details == "" || this.details == null) {
		this.details = "No details for this location. Sorry.&nbsp;&nbsp;&nbsp;";
	}
	
	var html = "<b>"+this.name+"<b><br>";
	html += "<i>"+this.address+" "+this.phone+"</i><br>";
	html += "Summary: "+this.summary+"<br><br>";
	html += this.details;
	return html+"<br><br>&nbsp;";
}
*/
//=========================LOCATION END=====================================

function getTextBoxesFor(frmNum) {
	if(frmNum == 0) {
		t1 = document.addressForm.saddr;
		t2 = document.addressForm.daddr;
	} else {
		t1 = document.commuteForm.saddr;
		t2 = document.commuteForm.daddr;
	}
	var tb = new Array(t1, t2);
	return 	tb;
}

function swapAddresses(frmNum) {
	var t1, t2, tb;
	tb = getTextBoxesFor(frmNum);
	t1 = tb[0];
	t2 = tb[1];
	var txt = t1.value;
	t1.value = t2.value;
	t2.value = txt;
	defDirectionIsFrom[frmNum] = !defDirectionIsFrom[frmNum];
}

defDirectionIsFrom = new Array(false, false);
function getDirections(id, frmNum) {
	clearGDir();
	var loc = getLocationWithID(id);
	
	if(loc != null) {
		var t1, t2, tb;
		tb = getTextBoxesFor(frmNum);
		t1 = tb[0];
		t2 = tb[1];
		var txtBox = t2;
		if(defDirectionIsFrom[frmNum]) {
			txtBox = t1;
		}
		
		txtBox.value = loc.address;
		
		if(frmNum == 0) {
			loadPopup('directions');
		} else {
			loadPopup('commute');
		}
	}
}

function changeIdToFavIcon(id, fav) {
	var loc = getLocationWithID(id);
	if(loc != null) {
		if(loc.marker != null) {
			try {
				var iconstr = getImageStr(loc.m_type, fav);
				if(!loc.marker.isHidden()) {
					loc.marker.setImage(iconstr);
					loc.marker.getIcon().image = iconstr;
				} else {
					loc.marker.getIcon().image = iconstr;
				}
			} catch(err) { }
		}
	}
}

function getLocationWithID(id) {
	if(locations != null && id != null) {
		for(var i=0 ; i<locations.length ; i++) {
			if(locations[i].id == id)
				return locations[i];
		}
	}
	
	return null;
}

function triggerOverFor(id) {
	if(map.getZoom() <= 10) {
		map.setZoom(11);
		setTimeout("triggerOverFor("+id+")", 500);
		return;
	}
	
	var loc = getLocationWithID(id);
	
	if(loc != null && loc.marker != null) {
		if(!loc.marker.isHidden()) {
			GEvent.trigger(loc.marker, "click");
		} else {
			map.setCenter(loc.marker.getPoint());
			GEvent.trigger(loc.marker, "click");
		}
	} else
		alert("Your favorite is not recognized.");
}

//========================LOCATION DETAIL BEGIN====================================
function LocationDetail(pid, minp, maxp, mins, maxs, minr, maxr) {
	this.pid = parseInt(pid);
	this.minprice = parseInt(minp);
	this.maxprice = parseInt(maxp);
	this.minsf = parseInt(mins);
	this.maxsf = parseInt(maxs);
	this.minroom = parseInt(minr);
	this.maxroom = parseInt(maxr);
}

LocationDetail.prototype.withinPriceRange = function(low, high) {
	//alert(this.minprice + " " + low + " - " + high);
	return (withinrange(this.minprice, low, high) || withinrange(this.maxprice, low, high));
}

LocationDetail.prototype.withinSFRange = function(sf) {
	if(sf == "" || sf == "0")
		return true;
	return withinrange(sf, this.minsf, this.maxsf);
}

LocationDetail.prototype.withinRoomRange = function(room) {
	if(room == "" || room == "0")
		return true;
	return withinrange(room, this.minroom, this.maxroom);
}

function addCommas(nStr) {
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

LocationDetail.prototype.getLDHtml = function() {
	var myHtml = "";
	
	if(this.minprice != null && this.maxprice != null)
		myHtml = myHtml + "$"+addCommas(this.minprice)+" to $"+addCommas(this.maxprice)+" ... ";
	if(this.minsf != null && this.maxsf != null)
		myHtml = myHtml + addCommas(this.minsf)+"-"+addCommas(this.maxsf)+"sf";
	if(this.minroom != null && this.maxroom != null)
		myHtml = myHtml + " ... "+this.minroom+"-"+this.maxroom+" Bedrooms";	
	
	return myHtml;
}

function withinrange(val, low, high) {
	var ival = parseInt(val);
	var ilow = parseInt(low);
	var ihigh = parseInt(high);
	return (ilow <= ival && ival <= ihigh)	
}

function getDetailsForID(id) {
	for(var i=0 ; i<locdetails.length ; i++) {
		var ld = locdetails[i];
		if(ld != null && ld.pid == id)
			return ld;
	}
	
	return null;
}
//========================LOCATION DETAIL END====================================

//========================LOCATION DETAIL BEGIN====================================
function Builder(bid, name) {
	this.bid = bid;
	this.name = name;
}

function getBuilderForID(id) {
	for(var i=0 ; i<builderarr.length ; i++) {
		var b = builderarr[i];
		if(b != null && b.bid == id)
			return b;
	}
	
	return null;
}
//========================LOCATION DETAIL END====================================
