
spcGMap.prototype.updateMinimap = function()
{
	var bounds = gmap.getBounds();
	if (typeof bounds == "undefined")
		return;

	// Google Maps V2
	//var bottomleft = this.gminimap.getProjection().fromLatLngToContainerPixel( bounds.getSouthWest() );
	//var topright = this.gminimap.getProjection().fromLatLngToContainerPixel( bounds.getNorthEast() );

	// Google Maps V3
	var bottomleft = helpermini.getProjection().fromLatLngToContainerPixel(bounds.getSouthWest());
	var topright = helpermini.getProjection().fromLatLngToContainerPixel(bounds.getNorthEast());

	//bounds.getCenter;
//	var frmWindow = document.getElementById('gmapwindow');
	this.frmWindow.style.cssText	=	'left: '+bottomleft.x+'px; '+
										'top: '+topright.y+'px; '+
										'width: '+(topright.x-bottomleft.x)+'px; '+
										'height: '+(bottomleft.y-topright.y)+'px;'; 
}

spcGMap.prototype.dragMinimapStart = function(e)
{
	// disable an active streetview
	if (this.gmap.getStreetView().getVisible())
	{
		if (product.sidebar)
			product.sidebar.show();

		this.gmap.getStreetView().setVisible(false);
	}

	//console.log(_this.gmap.getStreetView().getVisible());
	//console.log(_this.gmap.getStreetView().getVisible());

	if (this.onmapdrag)
		this.onmapdrag();

	var _this = this;

	this.cntMinimap.onmousemove = function(e) { _this.dragMinimap(e) };

	// stop dragging when the user stops clicking, also outside of the minimap
//	window.onmouseup = map.dragMinimapEnd; <-- doesn't fire in IE
	document.documentElement.onmouseup = function() { _this.dragMinimapEnd() };

	// in Firefox we don't get the onmouseup if it happens outside of the window, so stop the dragging when the user moved outside
	// of the window to prevent the dragging from not stopping.
	window.onmouseout = function(e) { if (!e.relatedTarget) _this.dragMinimapEnd(e) }	// fix for Firefox

//	also move the map of the user only clicked on the minimap once
	this.dragMinimap(e);

	// prevent the browser from dragging the frmMinimap itself
	if (useragent.isIE)
		return true;
	else
		e.preventDefault();

}

spcGMap.prototype.dragMinimapEnd = function()
{
	if (this.onmapdragend)
		this.onmapdragend();

	this.cntMinimap.onmousemove = null;

//	window.onmouseup = null;
	document.documentElement.onmouseup = null;

//	map.frmWindow.onmousedown	= map.dragMinimapStart; // restart
}




spcGMap.prototype.dragMinimap = function(e)
{
	if (!e) var e = window.event;		// For IE

/*	Get position of the mouse relative to the minimap
	for Fixed positioning of the minimap we have to subtract the scrollpos */
//	var pos = getElementPos(this.cntMinimap);
	var pos = getElementPos( document.getElementById('miniGMap') );
	
	var tempX = getPointerX(e) - pos.left;// - map.borderLeft;
	var tempY = getPointerY(e) - pos.top;//  - map.borderTop;

	// Google Maps V2 (almost)
	//var center = this.gminimap.getProjection().fromContainerPixelToLatLng( new google.maps.Point(tempX,tempY) );
	//gmap.setCenter(center);

	// Google Maps V3
	var center = helpermini.getProjection().fromContainerPixelToLatLng( new google.maps.Point(tempX,tempY) );
	gmap.setCenter(center);

	
	this.updateMinimap();
}

spcGMap.prototype.drawMinimapPoints = function spcGMap_drawMinimapPoints()
{
	// check if there's a projection (or maybe it's not initialized (display: none; ?)
	var projection = helpermini.getProjection();
	if (!projection)
		return;

	if (this.canvasPoints )
		this.canvasPoints.parentNode.removeChild(this.canvasPoints);

	//if ( !this.canvasPoints ) {
		canvasPing        = document.createElement('canvas');
		canvasPing.id     = 'mapPoints';

//this.minimapMapContainer.clientWidth
		canvasPing.width  = this.minimapW;
		canvasPing.height = this.minimapH;

//		canvasPing.style.left = map.borderLeft+'px';
//		canvasPing.style.top  = map.borderTop+'px';

		this.cntMinimapFull.appendChild( canvasPing );

		this.canvasPoints = canvasPing;
	//}
	var canvas = this.canvasPoints;

	if (! canvas.getContext)
		return;
    var context = canvas.getContext("2d");

	var iList = mapdata.items;
	var types = this.types;

	var FC = Math.PI*2; // full circle

	// draw the shadow for outer edge
	context.fillStyle = "rgba(0,0,0,0.75)";

//	var bounds = gmap.getBounds();
//	var bottomleft = this.gminimap.getProjection().fromLatLngToContainerPixel( bounds.getSouthWest() );
//	var topright = this.gminimap.getProjection().fromLatLngToContainerPixel( bounds.getNorthEast() );

	for(itemNR in iList)
	{
		if (iList[itemNR].lat == undefined || iList[itemNR].long == undefined)
			continue;

		if (!iList[itemNR].published && !product.config.showunpublished)
			continue;
			
		//var pixelpos = this.gminimap.getProjection().fromLatLngToContainerPixel( new google.maps.LatLng(iList[itemNR].lat, iList[itemNR].long) );
	// Google Maps V3
	var pixelpos = helpermini.getProjection().fromLatLngToContainerPixel(new google.maps.LatLng(iList[itemNR].lat, iList[itemNR].long));

		context.beginPath();
		context.arc( pixelpos.x  + 1,
					 pixelpos.y + 1,
					 4 , 0, FC ,0);
		context.fill();
		context.closePath();
	}

	// draw the edges outer edge
	context.fillStyle = "rgba(255,255,255,0.5)";

	for(itemNR in iList)
	{
		if (iList[itemNR].lat == undefined || iList[itemNR].long == undefined)
			continue;

		//var pixelpos = this.gminimap.getProjection().fromLatLngToContainerPixel( new google.maps.LatLng(iList[itemNR].lat, iList[itemNR].long) );
	// Google Maps V3
	var pixelpos = helpermini.getProjection().fromLatLngToContainerPixel(new google.maps.LatLng(iList[itemNR].lat, iList[itemNR].long));

		if (!iList[itemNR].published)
		{
			if (product.config.showunpublished)
			{
				context.fillStyle = "rgba(120,0,120,1)";
				context.beginPath();
				context.arc( pixelpos.x,
							 pixelpos.y,
							 8 , 0, FC ,0);
				context.fill();
				context.closePath();
				context.fillStyle = "rgba(255,255,255,0.5)";
			}
		}
		else
		{
			context.beginPath();
			context.arc( pixelpos.x,
						 pixelpos.y,
						 4 , 0, FC ,0);
			context.fill();
			context.closePath();
		}
	}

	// draw the color within
	for(itemNR in iList)
	{
//		if (types[ iList[itemNR].icon ].mmcolor == undefined)
//			continue;

		if (iList[itemNR].lat == undefined || iList[itemNR].long == undefined)
			continue;

		if (!iList[itemNR].published && !product.config.showunpublished)
			continue;
			
		//var pixelpos = this.gminimap.getProjection().fromLatLngToContainerPixel( new google.maps.LatLng(iList[itemNR].lat, iList[itemNR].long) );
	// Google Maps V3
	var pixelpos = helpermini.getProjection().fromLatLngToContainerPixel(new google.maps.LatLng(iList[itemNR].lat, iList[itemNR].long));

        if (typeof types[ iList[itemNR].icon ] == 'undefined')
		{
          console.error('Type '+iList[itemNR].icon+' does not exist!');
		  continue;
		}

		context.beginPath();
		context.fillStyle = "#"+types[ iList[itemNR].icon ].mmcolor;

		context.beginPath();
		context.arc( pixelpos.x + 1,
					 pixelpos.y + 1,
					 3 , 0, FC ,0);
		context.fill();
		context.closePath();
	}

}




//	ping all items of the same type
spcGMap.prototype.pingType = function( itype )
{
	if (itype == this.currentPingType)
		return;

	this.currentPingType = itype;

	if (itype == 0) {
//		alert(itype);
		this.pulsePingStop();
		var canvasPing = document.getElementById('mapPing');
		if ( canvasPing ) {
			if (! this.canvasPing.getContext) return;

		    var context = this.canvasPing.getContext("2d");
			//context.clearRect( 0 , 0 , this.mapdata.minimapW , this.mapdata.minimapH );
		}
		return;
	}

	var tList = mapmeta.getItemsByAttribute("type", true, itype);
//	console.log(tList);
	this.drawPing( tList );
}

spcGMap.prototype.drawPing = function( itemsToPing )
{
	//var iList = mapdata.items;

	this.pulsePingStop();

	if ( !this.canvasPing )
	{
		canvasPing        = document.createElement('canvas');

		canvasPing.id     = 'mapPing';

		canvasPing.width  = this.minimapW;
		canvasPing.height = this.minimapH;

		//canvasPing.style.left = this.borderLeft+'px';
		//canvasPing.style.top  = this.borderTop+'px';

		this.cntMinimapFull.appendChild( canvasPing );

		this.canvasPing = canvasPing;		
	}

	// stop if the browser doesn't support <CANVAS>
	if (! this.canvasPing.getContext)
		return;

	var context = this.canvasPing.getContext("2d");

	context.clearRect( 0 , 0 , this.minimapW , this.minimapH );

	context.fillStyle = "rgb(255,255,255)";

	var FC = Math.PI*2;
	
	

	for( tel=0 ; tel < itemsToPing.length; tel++ )
	{
		//console.log('itemsToPing[%i] = item #%i',tel, itemsToPing[tel]);
		
		var item = itemsToPing[tel];
		//console.log('drawing ping for '+itemID);
		if (typeof item == 'undefined')
		{
			console.error("given arrposition for point to ping does't exist.");
			continue;
		}
		
		if (item.lat == undefined || item.long == undefined)
			continue;

//		var pixelpos = this.gminimap.getProjection().fromLatLngToContainerPixel( new google.maps.LatLng(iList[itemID].lat, iList[itemID].long) );
	var latlng = new google.maps.LatLng(item.lat, item.long);
	pixelpos = helpermini.getProjection().fromLatLngToContainerPixel(latlng);


//		console.log('Point #'+itemID' has lat '+iList[itemID].lat+' / long '+iList[itemID].long+' which is %i, %i on the minimap', pixelpos.x, pixelpos.y);
		
		context.beginPath();
		context.arc( pixelpos.x,
					 pixelpos.y,
					 5 , 0, FC ,0);
		context.fill();
		context.closePath();

/*		if (useragent.isIE)
			document.getElementById('ds'+itemNR).className = "mapicon iconDanger pulsate";
		else
			document.getElementById('ds'+itemNR).className = "mapicon pulsate"; */
	}

	context.strokeStyle = "rgb(0,0,0)";

	for( tel=0 ; tel < itemsToPing.length ; tel++ ) {

		var item = itemsToPing[tel];
	
		if (item.lat == undefined || item.long == undefined)
		continue;

		//var pixelpos = this.gminimap.getProjection().fromLatLngToContainerPixel( new google.maps.LatLng(iList[itemID].lat, iList[itemID].long) );
		var latlng = new google.maps.LatLng(item.lat, item.long);
		var pixelpos = helpermini.getProjection().fromLatLngToContainerPixel(latlng);

		context.beginPath();
		context.arc( pixelpos.x,
					 pixelpos.y,
					 3 , 0, FC ,0);
		context.stroke();
		context.closePath();
	}

	this.pingPulse = 0;
	this.pulsePing();
//	document.getElementById('mapPing').style.display = 'block';
	this.canvasPing.style.display = 'block';

}
spcGMap.prototype.pulsePing = function()
{
	this.pingPulse += Math.PI / 20;

	var pStrength = Math.sin(this.pingPulse) * 0.5 + 0.5;

	this.canvasPing.style.opacity = pStrength;

//	var rule = document.styleSheets[1].cssRules[0];
//	rule.style.opacity = pStrength;
	
	var self = this;
	this.pingPulseTimer = setTimeout(function() { self.pulsePing(); }, 80);
}
spcGMap.prototype.pulsePingStop = function()
{
	if (this.pingPulseTimer == undefined || this.pingPulseTimer == null)
		return;

	//console.log('Stopping pulsing ping');
		
	clearTimeout( this.pingPulseTimer );
	this.pingPulseTimer = null;

	this.canvasPing.style.display = 'none';
}
