/**
 * Represents an address with the geocoded longitude and latitude.
 *
 * @author elam 
 */
var longitude;
var latitude;
var flatNumber;
var streetNumber;
var streetName;
var streetType;
var suburb;
var stateAbb;
var lonlat;
var singleHeight = 22; 
var singleWidth = 9;

/**
 * These values are expected to be the values from address helper.
 *
 * @param	lon			Longitude
 * @param	lat			Latitude
 * @param	flatNum		Flat Number
 * @param	stNum		Street Number
 * @param	stTyp		Street Type
 * @param	stName		Street Name
 * @param	sub			Suburb
 * @param	state 		State 
 */
function position(lon,lat,flatNum,stNum,stTyp,stName,sub,state) {
	this.longitude = lon;
	this.latitude = lat;
	this.flatNumber = flatNum;
	this.streetNumber = stNum;
	this.streetName = stName;
	this.streetType = stTyp;
	this.suburb = sub;
	this.stateAbb = state;
	this.lonlat = new OpenLayers.LonLat(this.longitude,this.latitude);
	this.lonlatShifted = new OpenLayers.LonLat(this.longitude,this.latitude-0.0005);
	
	/**
	 * Gets the OpenLayers.LonLat object of this geoceded position.
	 */
	this.getLonLat = function() {
		return this.lonlat;
	}
	
	/**
	 * Idea is to shift the Longitutde value down so the mouse does.
	 * not get in the way ... needs some work.
	 */
	this.getLonLatShifted = function() {
		return this.lonlatShifted;
	}
	
	/**
	 * Gets the address in the following format.
	 *
	 * 1/12 Greenpit Terrace <br>
	 * Bondi Beach <br>
	 * NSW
	 */
	this.getAddress = function () {
		var line1 = new String("");
		if (this.flatNumber)
			line1 = line1 + this.flatNumber + "/";
		if (this.streetNumber)
			line1 = line1 + this.streetNumber + " ";
		if (this.streetName)
			line1 = line1 + this.streetName + " ";
		if (this.streetType)
			line1 = line1 + this.streetType + " ";
		
		if (line1.length != 0) 
			line1 = "&nbsp;" + firstUpper(line1) + "<br>";
			
		var line2 = "&nbsp;" + firstUpper(this.suburb) + "<br>";
		var line3 = "&nbsp;" + this.stateAbb;
	
		return "<font color='white'><b>" + line1 + line2 + line3 + "</b></font>";
	}
	
	/**
	 * Converts a string to lowercase with uppercase first letter
	 * e.g. BONDI BEACH or bondi beach -> Bondi Beach
	 */
	function firstUpper(line) {
	
		var newArray = line.split(' ');
		
		for (i = 0 ; i < newArray.length ; i ++ ) {
			var firstLetter = newArray[i].substring(0,1).toUpperCase();
			var restOfWord = newArray[i].substring(1, newArray[i].length).toLowerCase();
			
			newArray[i] = firstLetter + restOfWord; 
		}

		return newArray.join(' ');
	}
	
	/**
	 * Gets the size of the tooltip/popup required.
	 */
	this.getSize = function() {
		var width;
		var height;
		
		// works out the height required.
		// Minimum of state and suburb. ie 2 lines
		if (this.streetName)
			height = singleHeight * 3;
		else
			height = singleHeight * 2;
			
		// works out how wide the tooltip needs to be.
		// + 1 at the ends to add some padding in case.
		var line2Length = this.suburb.length;
		var line1Length = this.flatNumber.length + this.streetNumber.length + 
							this.streetName.length + this.streetType.length + 2;
		
		if (line1Length > line2Length)
			width = line1Length * singleWidth;
		else 
			width = line2Length * singleWidth;
		
		return new OpenLayers.Size(width,height); 
	}
}	
	
