/**********************************************************************
* GCylindricalProjection - An implementation of the GProjection 
* interface for the cylindrical projections data sets
* - Based on the original Mercator Projection of the Google Maps
*
* Author: Betim Deva betim@asu.edu
* Date: August 2006
* Mars Space Flight Facility at Arizona State University
**********************************************************************/

function GCylindricalProjection(zoom){
	this.pixelsPerLonDegree=[];
	this.pixelsPerLonRadian=[];
        this.tileBounds=[];
	this.pixelOrigo=[];

	var c = 256;
	//Assume that the max zoomlevel is 17
	for(var d=0; d < 17; d++){
		var e = c/2;
		this.pixelsPerLonDegree.push(2*c/(360));
		this.pixelsPerLonRadian.push(2*c/(2*Math.PI));

		this.pixelOrigo.push(new GPoint(2*e,e));
		this.tileBounds.push(2*c);
		c *= 2;
	}	
} 
GCylindricalProjection.prototype=new GProjection(); 



GCylindricalProjection.prototype.fromLatLngToPixel=function(point,zoom){
	var x = Math.round(this.pixelOrigo[zoom].x+point.lng()*this.pixelsPerLonDegree[zoom]);
        var y = Math.round(this.pixelOrigo[zoom].y-point.lat()*this.pixelsPerLonDegree[zoom]);

	return new GPoint(x,y);
}


GCylindricalProjection.prototype.fromPixelToLatLng=function(point,zoom,nofix){
 var lng=(point.x-this.pixelOrigo[zoom].x)/this.pixelsPerLonDegree[zoom];
 var lat=(-point.y+this.pixelOrigo[zoom].y)/(this.pixelsPerLonDegree[zoom]);

  return new GLatLng(lat,lng,nofix); 
}


GCylindricalProjection.prototype.tileCheckRange=function(a,b,c){
	var d=this.tileBounds[b]

	if(a.y<0||a.y*c>=d/2){
		return false
	}
	if(a.x<0||a.x*c>=d){
		var e=Math.floor(d/c);
		a.x=a.x%e;
		if(a.x<0){
			a.x+=e
		}
	}
	return true
}


GCylindricalProjection.prototype.getWrapWidth = function(zoom){
	return this.tileBounds[zoom];
}
