/**

 * @namespace WPGMZA

 * @module Marker

 * @requires WPGMZA

 */

(function($) {

	/**

	 * Constructor

	 * @param json to load (optional)

	 */

	WPGMZA.Marker = function(row)

	{

		var self = this;

		

		WPGMZA.assertInstanceOf(this, "Marker");

		

		this.lat = "36.778261";

		this.lng = "-119.4179323999";

		this.address = "California";

		this.title = null;

		this.description = "";

		this.link = "";

		this.icon = "";

		this.approved = 1;

		this.pic = null;

		

		WPGMZA.MapObject.apply(this, arguments);

		

		if(row && row.heatmap)

			return; // Don't listen for these events on heatmap markers.

		

		this.on("init", function(event) {

			if(row.position)

				this.setPosition(row.position);

			

			if(row.map)

				row.map.addMarker(this);

		});

		

		this.addEventListener("added", function(event) {

			self.onAdded(event);

		});

	}

	

	WPGMZA.Marker.prototype = Object.create(WPGMZA.MapObject.prototype);

	WPGMZA.Marker.prototype.constructor = WPGMZA.Marker;

	

	/**

	 * Gets the constructor. You can use this instead of hard coding the parent class when inheriting,

	 * which is helpful for making subclasses that work with Basic only, Pro, Google, OL or a 

	 * combination of the four.

	 * @return function

	 */

	WPGMZA.Marker.getConstructor = function()

	{

		switch(WPGMZA.settings.engine)

		{

			case "open-layers":

				if(WPGMZA.isProVersion())

					return WPGMZA.OLProMarker;

				return WPGMZA.OLMarker;

				break;

				

			default:

				if(WPGMZA.isProVersion())

					return WPGMZA.GoogleProMarker;

				return WPGMZA.GoogleMarker;

				break;

		}

	}

	

	WPGMZA.Marker.createInstance = function(row)

	{

		var constructor = WPGMZA.Marker.getConstructor();

		return new constructor(row);

	}

	

	WPGMZA.Marker.ANIMATION_NONE			= "0";

	WPGMZA.Marker.ANIMATION_BOUNCE			= "1";

	WPGMZA.Marker.ANIMATION_DROP			= "2";

	

	WPGMZA.Marker.prototype.onAdded = function(event)

	{

		var self = this;

		

		// this.infoWindow = WPGMZA.InfoWindow.createInstance(this);

		

		this.addEventListener("click", function(event) {

			self.onClick(event);

		});

		

		this.addEventListener("mouseover", function(event) {

			self.onMouseOver(event);

		});

		

		this.addEventListener("select", function(event) {

			self.onSelect(event);

		});

		

		if(this.map.settings.marker == this.id)

			self.trigger("select");

	}

	

	/**

	 * This function will hide the last info the user interacted with

	 * @return void

	 */

	WPGMZA.Marker.prototype.hidePreviousInteractedInfoWindow = function()

	{

		if(!this.map.lastInteractedMarker)

			return;

		

		this.map.lastInteractedMarker.infoWindow.close();

	}

	

	WPGMZA.Marker.prototype.openInfoWindow = function()

	{

		//this.hidePreviousInteractedInfoWindow();

		//this.infoWindow.open(this.map, this);

		//this.map.lastInteractedMarker = this;

	}

	

	WPGMZA.Marker.prototype.onClick = function(event)

	{

		

	}

	

	WPGMZA.Marker.prototype.onSelect = function(event)

	{

		this.openInfoWindow();

	}

	

	WPGMZA.Marker.prototype.onMouseOver = function(event)

	{

		if(this.map.settings.info_window_open_by == WPGMZA.InfoWindow.OPEN_BY_HOVER)

			this.openInfoWindow();

	}

	

	WPGMZA.Marker.prototype.getIcon = function()

	{

		function stripProtocol(url)

		{

			if(typeof url != "string")

				return url;

			

			return url.replace(/^http(s?):/, "");

		}

		

		return stripProtocol(WPGMZA.settings.default_marker_icon);

	}

	

	/**

	 * Gets the position of the marker

	 * @return object

	 */

	WPGMZA.Marker.prototype.getPosition = function()

	{

		return {

			lat: parseFloat(this.lat),

			lng: parseFloat(this.lng)

		};

	}

	

	/**

	 * Sets the position of the marker

	 * @return void

	 */

	WPGMZA.Marker.prototype.setPosition = function(latLng)

	{

		if(latLng instanceof WPGMZA.LatLng)

		{

			this.lat = latLng.lat;

			this.lng = latLng.lng;

		}

		else

		{

			this.lat = parseFloat(latLng.lat);

			this.lng = parseFloat(latLng.lng);

		}

	}

	

	/**

	 * Set the marker animation

	 * @return void

	 */

	WPGMZA.Marker.prototype.getAnimation = function(animation)

	{

		return this.settings.animation;

	}

	

	/**

	 * Set the marker animation

	 * @return void

	 */

	WPGMZA.Marker.prototype.setAnimation = function(animation)

	{

		this.settings.animation = animation;

	}

	

	/**

	 * Get the marker visibility

	 * @return void

	 */

	WPGMZA.Marker.prototype.getVisible = function(visible)

	{

		

	}

	

	/**

	 * Set the marker visibility. This is used by the store locator etc. and is not a setting

	 * @return void

	 */

	WPGMZA.Marker.prototype.setVisible = function(visible)

	{

		if(!visible && this.infoWindow)

			this.infoWindow.close();

	}

	

	WPGMZA.Marker.prototype.setMap = function(map)

	{

		if(!map)

		{

			if(this.map)

				this.map.removeMarker(this);

			

			return;

		}

		

		map.addMarker(this);

	}

	

	WPGMZA.Marker.prototype.getDraggable = function()

	{

		

	}

	

	WPGMZA.Marker.prototype.setDraggable = function(draggable)

	{

		

	}

	

	WPGMZA.Marker.prototype.setOptions = function()

	{

		

	}

	

	WPGMZA.Marker.prototype.panIntoView = function()

	{

		if(!this.map)

			throw new Error("Marker hasn't been added to a map");

		

		this.map.setCenter(this.getPosition());

	}

	

	/**

	 * Returns the marker as a JSON object

	 * @return object

	 */

	WPGMZA.Marker.prototype.toJSON = function()

	{

		var result = WPGMZA.MapObject.prototype.toJSON.call(this);

		var position = this.getPosition();

		

		$.extend(result, {

			lat: position.lat,

			lng: position.lng,

			address: this.address,

			title: this.title,

			description: this.description,

			link: this.link,

			icon: this.icon,

			pic: this.pic,

			approved: this.approved

		});

		

		return result;

	}

	

	

})(jQuery);