/** MapItem
* A container for a general Map Item (a GMarker, GPolygon, or GPolyline) 
* that holds its name, address, point (GLatLng), and if the Map Item can be hidden. 
* (The GPolygon's that outline the buildings should not be hidden)
* 
* @class MapItem
*	@constructor
* @param {int} mapItemID
* @param {String} name
* @param {String} address
* @param {String} img
* @param {String} url
* @returns {void}
*/
var MapItem = function( mapItemID, name, address, img, url, wirelessLocation ) {
	this.mapItemID = mapItemID;
	this.name = name;
	this.address = address;
	this.img = img;
	this.url = url;
	this.wirelessLocation = wirelessLocation;
	
	this.gItems = new Array();		// Array to hold sub-items, such as multiple lines for one bus route mapItem, or both a marker and polygon for buildings.
	this.overlayAdded = false;

}

	
MapItem.prototype = {
	
	/** Accessor Methods 
	*
	*/
	getMapItemID : function() { return this.mapItemID; },
	getName : function() { return this.name; },
	getAddress : function() { return this.address; },
	getImg : function() { return this.img; },
	getUrl : function() { return this.url; },
	getWirelessLocation : function() { return this.wirelessLocation; },
	
	/** addItem
	* Adds a gItem (marker/line/polygon) to this map item
	*
	* @method addItem
	* @param {GMarker | GPolygon | GPolyline} gItem
	*/
	addGItem : function( gItem ) {
		this.gItems.push( gItem );
	},
	
	
	/** show
	* Shows all gItems under this MapItem
	*
	* @method show
	* @return {void}
	*/
	show : function() {
		for( var i = 0, numItems = this.gItems.length; i < numItems; i++ ) {
			if( !this.overlayAdded ) Map.map.addOverlay( this.gItems[ i ] );
				this.gItems[ i ].show();
		}
		this.overlayAdded = true;
	},
	
	
	/** hide
	* Hides all gItems under this MapItem, but does not hide GPolygon's
	*
	* @method hide
	* @return {void}
	*/
	hide : function() {
		for( var i = 0, numItems = this.gItems.length; i < numItems; i++ ) {
			if( !( this.gItems[ i ] instanceof GPolygon ) ) {		// Do not hide GPolygons
				this.gItems[ i ].hide();
			}
		}
	},
	
	
	/** getMainGItem
	* Gets the "Main" gItem in this map item.  Returns a marker if there is one,
	* otherwise just returns the the first gItem in the array
	* 
	* @method getMainGItem
	* @returns {GMarker | GPolygon | GPolyline}
	*/
	getMainGItem : function() {
		var gItem = this.gItems[ 0 ];
		
		// Find a GMarker in this mapItem's gItems array to use as the info window spawner.
		// If no GMarker exists for this mapItem, this method will use the first "gItem" in the array (which was set above).
		for( var i = 0, numItems = this.gItems.length; i < numItems; i++ ) {
			if( this.gItems[ i ] instanceof GMarker ) {
				gItem = this.gItems[ i ];
				break;
			}
		}
		
		return gItem;
	},
	
	
	/** getCenterPoint
	* Gets the center point of this map item.  If the map item has a marker, it gets the center
	* point from that.  If the map item does not have a marker, it gets it from a polygon or polyline.
	*
	* @method getCenterPoint
	* @param {GMarker | GPolygon | GPolyline} gItem (optional) : The gItem to get the center point of. If not sent, function
	*																														 gets the center point of the main gItem in this MapItem.
	* @return {GLatLng}
	*/
	getCenterPoint : function( gItem ) {
		if( YAHOO.lang.isUndefined( gItem ) )	
			var gItem = this.getMainGItem();
		
		var centerPoint;
				
		if( gItem instanceof GMarker ) {
			centerPoint = gItem.getLatLng();
		} else {
			centerPoint = gItem.getBounds().getCenter();
		}
		return centerPoint;
	},
	
	
	/** openWindow
	*	A private method to handle opening an "info" window from a marker (if one exists), 
	* or otherwise from the center of a polygon.  Also accepts both an HTML Element and
	* a string to use as the HTML in the "info window"
	*
	* @method openWindow
	* @private
	* @param {HTMLElement} htmlEl
	* @return {void}
	*/
	openWindow : function( htmlEl ) {
		var gItem = this.getMainGItem(),
				centerPoint = this.getCenterPoint( gItem );
				
		// Show the info window with the map item's address
		// If the gItem is a Marker, spawn the info window from the marker itself.
		// If the gItem is a GPolygon, create the info window from the map (as GPolygon doesn't have an openInfoWindowHtml method)
		if( gItem instanceof GMarker ) {
			gItem.openInfoWindow( htmlEl );
		} else {
			Map.map.openInfoWindow( centerPoint, htmlEl );
		}
	},
	
	
	/** openInfoWindow
	* Opens a map item's information window (i.e. its address and such)
	*
	* @method openInfoWindow
	* @param {String} html : The HTML to put into the info window
	* @returns {void}
	*/
	openInfoWindow : function() {
		var gItem = this.getMainGItem(),
				centerPoint = this.getCenterPoint( gItem );
		
		// Pan the map to the point where this map item lies on the map
		Map.map.panTo( centerPoint );
		
		// If this map item is a GPolyline, return (as no address information is available)
		if( gItem instanceof GPolyline ) return;
		
	/*	
		// Create the "Address Div" using direct HTML, 
		// but then use DOM methods to add the "directions" links so that we can assign event handlers to them
		var addressDiv = document.createElement( "DIV" ); addressDiv.className = "infomain";
		addressDiv.innerHTML = '<div class="infoContainer"><div class="infoleft"><img src="' + this.img + ' " class="popup" /></div>' +
													 '<div class="inforight"><h4>' + this.name.replace( /&lt;/gmi, '<' ).replace( /&gt;/gmi, '>' ) + '</h4><br>' +
													 '<a target="_blank" class="placeLink" href="' + this.url + '">More Information</a>' +
													 //'<br>' + this.address + '<br><br>' +
													 '<br>' + this.address.replace( /&lt;/gmi, '<' ).replace( /&gt;/gmi, '>' ) + '<br><br>' +
													 'Directions: <span></span> - <span></span>' + 
													 '</div><div class="clear"></div>' + 
													 '</div>';
		
		 */                              
		//Builds an array for the infowindow
		// but then use DOM methods to add the "directions" links so that we can assign event handlers to them
		var infoWindowDiv = document.createElement( "DIV" );
		infoWindowDiv.className = "infomain";

		var html = new Array();
		//html[ html.length ] = '<div class="infoContainer">';
		html[ html.length ] = '<table border="0" align="center" cellspacing="0">';
		
		//mapItem name
		html[ html.length ] = '<tr>';
		html[ html.length ] = '<td colspan="2" class="infoWindowText" >';
		html[ html.length ] = '<h4>' + this.name.replace( /&lt;/gmi, '<' ).replace( /&gt;/gmi, '>' ) + '</h4>';
		html[ html.length ] = '</td>';
		html[ html.length ] = '</tr>';
		
		//mapItem image
		html[ html.length ] = '<tr>'
		html[ html.length ] = '<td colspan="2">';
		html[ html.length ] = '<img src="' + this.img + '" class="infoWindowImage" />';
		html[ html.length ] = '</td>';
		html[ html.length ] = '</tr>';
		
		//mapItem URL -- has a column for the possibility of adding handicap spaces
		html[ html.length ] = '<tr>';
		html[ html.length ] = '<td class="infoWindowText">';
		html[ html.length ] = '<a target="_blank" class="placeLink" href="' + this.url + '">More Information</a>';
		html[ html.length ] = '</td>';
		html[ html.length ] = '<td></td>';
		html[ html.length ] = '</tr>';
		
		//mapItem address (physical or details about the mapItem) -- has a column to add metered parking
		html[ html.length ] = '<tr>';
		html[ html.length ] = '<td class="infoWindowText">' + this.address.replace( /&lt;/gmi, '<' ).replace( /&gt;/gmi, '>' ) + '</td>';
		html[ html.length ] = '<td></td>';
		html[ html.length ] = '</tr>';
		
		// mapItem wirelessLocation
		html[ html.length ] = '<tr>';
		if (this.wirelessLocation == "") {
			html[ html.length ] = '<td></td>';
		}else {
			html[ html.length ] = '<td class="infoWindowText">' + 'Wireless Locations<br />' + this.wirelessLocation	+ '</td>';
		}
		html[ html.length ] = '<td></td>';
		html[ html.length ] = '</tr>';
		
		
		//This is set to have a link for the expandable infowindow
		html[ html.length ] = '<tr>';
		html[ html.length ] = '<td colspan="2" align="left">';
		html[ html.length ] = '</td>';
		html[ html.length ] = '</tr>';
		
		//Sets up the directions for the infowindow
		html[ html.length ] = '<tr>';
		html[ html.length ] = '<td colspan="2" align="center">';
		html[ html.length ] = 'Directions: <span></span> - <span></span>';
		html[ html.length ] = '</td>';
		html[ html.length ] = '</tr>';
		
		html[ html.length ] = '</table>';
		//html[ html.length ] = '</div>';

		infoWindowDiv.innerHTML = html.join( "" );  //combine all of the array elements into one string with no separator
		
		
		var directionsTo_span = infoWindowDiv.getElementsByTagName( 'SPAN' )[ 0 ];
		var directionsFrom_span = infoWindowDiv.getElementsByTagName( 'SPAN' )[ 1 ];
		
		var link_to = document.createElement( "A" ); link_to.href = "javascript:void(0);";
		link_to.appendChild( document.createTextNode( "To here" ) );
		YAHOO.util.Event.addListener( link_to, 'click', this.openDirectionsWindow, 'to', this );
		directionsTo_span.appendChild( link_to );
		
		var link_from = document.createElement( "A" ); link_from.href = "javascript:void(0);";
		link_from.appendChild( document.createTextNode( "From here" ) );
		YAHOO.util.Event.addListener( link_from, 'click', this.openDirectionsWindow, 'from', this );
		directionsFrom_span.appendChild( link_from ); 
		
		this.openWindow( infoWindowDiv );
	},
	
	
	/** openDirectionsWindow
	* Opens the map item's directions window
	*
	* @method openDirectionsWindow
	* @param {String} direction : Either 'to' or 'from'.  Defaults to 'to'
	* @returns {void}
	*/
	openDirectionsWindow : function( evt, direction ) {
		var centerPoint = this.getCenterPoint();
		
		if( YAHOO.lang.isUndefined( direction ) ) 
			var direction = 'to';
			
		// Pan the map to the point where this map item lies on the map
		Map.map.panTo( centerPoint );
		
		// Set some variables based on the direction
		if( direction == 'to' ) {
			var fieldName1 = "saddr";
			var fieldName2 = "daddr";
			var directionText = "Start";
		} else {
			var fieldName1 = "daddr";
			var fieldName2 = "saddr";
			var directionText = "End";
		}
			
		// The info window version with the "to here" form open
		var directionsDiv = document.createElement( "DIV" ); directionsDiv.className = "directionsInfoWindowDiv";
/*		directionsDiv.innerHTML = '<h4>' + this.name + '</h4><br>Directions: <span></span> - <span></span><br>' +
															directionText + " Address:<br>" +
															'<input type="text" size="40" maxlength="40" id="' + fieldName1 + '" value="" /><br>' +
															'<input type="submit" value="Get Directions" onclick="Map.Directions.getDirections();"><br>' +
															'Walk <input type="checkbox" name="walk" id="walk" /> &nbsp; Avoid Highways <input type="checkbox" name="highways" id="highways" />' +
															'<input type="hidden" id="' + fieldName2 + '" value="' + this.name + '@' + centerPoint.lat() + ',' + centerPoint.lng() + '"/>';
	*/
		directionsDiv.innerHTML = '<form action="" onsubmit="Map.Directions.getDirections(); return false;"><h4>' + this.name + '</h4><br>Directions: <span></span> - <span></span><br>' +
															directionText + " Address:<br>" +
															'<input type="text" size="40" maxlength="40" id="' + fieldName1 + '" value="" /><br>' +
															'<input type="submit" value="Get Directions" ><br>' +
															'Walk <input type="checkbox" name="walk" id="walk" /> &nbsp; Avoid Highways <input type="checkbox" name="highways" id="highways" />' +
															'<input type="hidden" id="' + fieldName2 + '" value="' + this.name + '@' + centerPoint.lat() + ',' + centerPoint.lng() + '"/></form>';

		var directionsTo_span = directionsDiv.getElementsByTagName( 'SPAN' )[ 0 ];
		var directionsFrom_span = directionsDiv.getElementsByTagName( 'SPAN' )[ 1 ];
		
		var link = document.createElement( "A" ); link.href = "javascript:void(0);";
		link.appendChild( document.createTextNode( ( ( direction == 'to' ) ? "From" : "To" ) + " here" ) );
		YAHOO.util.Event.addListener( link, 'click', this.openDirectionsWindow, ( ( direction == 'to' ) ? "from" : "to" ), this );
		
		if( direction == "to" ) {
			directionsTo_span.appendChild( document.createTextNode( "To here" ) );
			directionsFrom_span.appendChild( link );
		} else {
			directionsTo_span.appendChild( link );
			directionsFrom_span.appendChild( document.createTextNode( "From here" ) );
		}
		
		// Show Info Window
		this.openWindow( directionsDiv );
		
		// Watermark the directions field
		new WatermarkField( fieldName1, "Street address + city/state and/or zip" );
	}
	
}