
/** FlatIcon is a GIcon extension that doesn't set the shadow information or 
	extra features such as separate print images or browser dependant fields
	(add a separate implementation file if we want to support mozprintimage, 
	transparent, or imagemap functionality)
	FlatIcon
	-	image				string	#href to the image
	-	iconSize			GSize	#image displayed size
	-	iconAnchor			GPoint	#pixel location in image to anchor onto GMap
	-	infoWindowAnchor	GPoint	#pixel location to anchor infowindow off of
	
@constructor FlatIcon(imageref, sizex, sizey)
@constructor FlatIcon(imageref, sizex, sizey, anchorx, anchory)
@constructor FlatIcon(imageref, sizex, sizey, anchorx, anchory, infoanchx, infoanchy)
*/
function FlatIcon(imageref,sizex,sizey,anchorx,anchory,infoanchx,infoanchy)
{	this.image		= imageref;
	this.iconSize	= new GSize(sizex,sizey);
	if (typeof anchorx!='number' || typeof anchory!='number')
		this.iconAnchor = new GPoint(Math.floor(sizex/2),Math.floor(sizey/2));
	else
		this.iconAnchor	= new GPoint(anchorx,anchory);
	if (typeof infoanchx!='number' || typeof infoanchy!='number')
		this.infoWindowAnchor = new GPoint(infoanchx,infoanchy);
	return this;
}
FlatIcon.prototype = new GIcon;
FlatIcon.prototype.constructor = FlatIcon;
FlatIcon.prototype.baseClass = GIcon;
/**	Set up a Dom Image element with the settings for this Icon.
	@param {Node} domimg	Optional -- a pre-existant DOM Image HTML Element or (if not passed)
							one will be created via document.createElement() (and returned).
	@param {String} title	Optional -- a title string to be applied to the <img> element.
	@return {Node}	The adjusted (or created) HTML Image Element
*/
FlatIcon.prototype.setDomImg = function(domimg,title)
{	if (typeof domimg!='object')
	{	domimg = document.createElement('img');
		if (typeof domimg=='string' && typeof title=='undefined')
			title = domimg;
	}
	domimg.src		= this.image;
	domimg.width	= this.iconSize.width;
	domimg.height	= this.iconSize.height;
	if (typeof title=='string')
		domimg.title = title;
	return domimg;
}


/**	FIconDistance Markers extend FlatIcon
	@constructor
	@param {Number} number		The number of the miles or kilometers to display on the Icon
	@param {Boolean} inMiles	An optional variable that decides whether this marks miles or
								kilometers. If it is not passed, we default to the current 
								measure setting (and if that is not set, we default to miles)
*/
function FIconDistance( number, inMiles)
{	if (typeof inMiles!='boolean')
		inMiles = !CheckBRdCSetting('measure',metric);
	var iconHeight	= 17+Math.ceil(Math.log(milecount+0.0001)/Math.log(10))*11+((milecount%1>0)?11:0);
	this.image		= 'img/'+(inMiles ? 'mile/':'km/')+number+'.png';
	this.iconSize	= new GSize( (inMiles ? 26 : 23), iconHeight );
	this.iconAnchor	= new GPoint((inMiles ? 13 : 11), iconHeight-2 );
	this.measure	= inMiles ? 'imperial' : 'metric';
	this.number		= number;
	this.zoomlvl	= (inMiles) ?
						(	(number%1000==0)? 3 :	//Imperial mile units
							(number%200==0)	? 5 :
							(number%100==0)	? 7 :
							(number%20==0)	? 8 :
							(number%10==0)	? 10:
							(number%5==0)	? 11:	12)
						:	
						(	(number%1000==0)? 4 :	//Metric kilometer units
							(number%200==0)	? 6 :
							(number%100==0)	? 8 :
							(number%20==0)	? 9 :
							(number%10==0)	? 11:
							(number%5==0)	? 12:	13);
}
FIconDistance.prototype = new FlatIcon;
FIconDistance.prototype.contructor = FIconDistance;


/**	A series of multiple sized copies of the same GIcon for use at different map zoom levels
	or for display in different parts of the html page.
	(While we talk about GIcons, this series actually generates FlatIcons specifically)
	@constructor
	@param {String}	imgbase	The start of the image's url (the ending is the size and '.png')
	@param {Array}	sizes	Array of size info, each entry is either a single Number or an
							Array of values to be passed to FlatIcon constructor in the form
							[ size, sizex, anchory, anchorx, infowindowanchory, infowindowanchorx ]
							(**IMPORTANT: Note that the order is reversed, i.e. y-coord, x-coord **)

*/
function FIconSeries(imgbase,sizes)
{	this.name = imgbase;
	this.icons= new Array( sizes.length );
	this.best = 0;
	for (var i in sizes)
	{	var imgsize;
		var imgsizex;
		var ianchx = null;
		var ianchy = null;
		var iwancx = null;
		var iwancy = null;
		if (typeof sizes[i]=='number')
		{	imgsizex = imgsize = sizes[i];
		}	else //Array
		{	var info	= sizes[i];
			imgsize = info[0];
			imgsizex= (info.length>1) ? info[1] : imgsize;
			if (info.length>3)
			{	ianchy = info[2];
				ianchx = info[3];
			}
			if (info.length>5)
			{	iwancy = info[4];
				iwancx = info[5];
			}
		}		
		var imgfile = imgbase + imgsize + '.png';
		this.icons[i] = new FlatIcon( imgfile, imgsizex, imgsize, ianchx, ianchy, iwancx, iwancy);
		this.icons[i].imgsize = imgsize;
		if (this.icons[this.best].imgsize<imgsize)
			this.best = i;
	}
}
/**	Finds the index of a flaticon with the requested size in the series. 
	@param {Number}	size	Icon size to find
	@return {Number}	Index of the found GIcon in the series, or -1 if not found
*/
FIconSeries.prototype.findSize = function(size)
{	for (var i in this.icons)
		if (this.icons[i].imgsize == size)
			return i;
	return -1;
}
/**	Returns the FlatIcon of the requested size or returns a new resized one if one of the
	requested size was not existant in series.
	@param {Number} size	Icon size to find (or to create via resizing)
	@return {GIcon}		The Icon from the series or a resized one created to that size
*/
FIconSeries.prototype.FIcon = function(size)
{	var i = this.findSize(size);
	if (i==-1)	//make a new entry, roughly resizing the best one
		return this.NewIconResize(size);
	return this.icons[i];
}
/**	Create and add a resized version of the largest icon to the series
	@param {Number}	size	the requested size for the new Icon (may already exist)
	@return {GIcon}	the resized version of "best" (read largest sized) icon in the series
*/
FIconSeries.prototype.NewIconResize = function(size)
{	var baseIcon = this.icons[this.best];
	var multi = size / baseIcon.imgsize;
	var nsizex = baseIcon.iconSize.width * multi;
	var anchx = baseIcon.iconAnchor.x * multi;
	var anchy = baseIcon.iconAnchor.y * multi;
	if (typeof baseIcon.infoWindowAnchor=='object')
	{	var iwanchx = baseIcon.infoWindowAnchor.x * multi;
		var iwanchy = baseIcon.infoWindowAnchor.y * multi;
		this.icons[this.icons.length] = new FlatIcon(baseIcon.image, nsizex, size, anchx, anchy, iwanchx, iwanchy);
	}	else
		this.icons[this.icons.length] = new FlatIcon(baseIcon.image, nsizex, size, anchx, anchy);
	return this.icons[ this.icons.length-1 ];
}
/** Get the largest-sized icon within the size range
	Will return the largest icon in the series, less than or equal to the requested size.
	@param {Number} size	the size to match (equal to or less than, not over)
	@param {Number} minsize	an optional parameter, a minimum size (defaults to 0)
	@return {GIcon}	the closest icon to the requested size, or null if no smaller sized icons exist
*/
FIconSeries.prototype.closest = function(size, minsize)
{	var nearest = false;
	if (typeof minsize!='number')
		minsize = 0;
	for (var i in this.icons)
	{	if (this.icons[i].imgsize==size)
			return this.icons[i];
		if (this.icons[i].imgsize<size&&this.icons[i].imgsize>=minsize)
		{	if ((nearest==false)||this.icons[i].imgsize>nearest.imgsize)
				nearest = this.icons[i];
		}
	}
	return nearest;
}


/** this stuff should go in a separate file like the edit route .js one	*/


var route_startmark	= new FlatIcon('img/markerStart24.png',24,24);
var route_endmark	= new FlatIcon('img/markerEnd24.png',24,24);
var route_blankmark	= new FlatIcon('img/dot17.png',17,17,8,8);

var routesml_start	= new FlatIcon('img/markerStart13.png',13,13);
var routesml_end	= new FlatIcon('img/markerEnd13.png',13,13);

var red_x			= new FlatIcon('img/X25_red.png',25,25,12,12);
var moveicon		= new FlatIcon('img/moveicon21.png',21,21,10,10);

var editimg_lineback= new FlatIcon('img/line13back.png',13,13);
var editimg_linedown= new FlatIcon('img/line13down.png',13,13);
var editimg_ptmarker= new FlatIcon('img/circle13marker.png',13,13);
var editimg_nomarker= new FlatIcon('img/circle13nomarker.png',13,13);
var editimg_move	= new FlatIcon('img/4arrows.png',13,13);
var editimg_delegray= new FlatIcon('img/delete_gray.png',13,13);
var editimg_delered	= new FlatIcon('img/delete_red.png',13,13);
var editimg_settext	= new FlatIcon('img/textBubble.png',18,13);

var routebox_open	= new FlatIcon('img/arrow_down.png',6,3);
var routebox_closed	= new FlatIcon('img/arrow_up.png',6,3);



