/*
* MapChartControl Class 
*  Copyright (c) 2005-2007, Andre Lewis, andre@earthcode.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* 
*       http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* This class lets you add a control to the map which will let the user
*  zoom by dragging a rectangle.
*  More info on original GZoom at http://earthcode.com
*/

/**
 * Constructor for MapChartControl, which takes 3 option hashes and
 *  uses them to customize the control.
 * @param {opts_boxStyle} Named optional arguments:
 *   opts_boxStyle.opacity {Number} Opacity from 0-1
 *   opts_boxStyle.fillColor {String} Hex value of fill color
 *   opts_boxStyle.border {String} CSS-style declaration of border
 * @param {opts_other} Named optional arguments:
 *   opts_other.buttonHTML {String} The zoom button HTML in non-activated state
 *   opts_other.buttonStartingStyle {Object} A hash of css styles for the 
 *     zoom button which are common to both un-activated and activated state
 *   opts_other.buttonStyle {Object} A hash of css styles for the zoom button 
 *     which will be applied when the button is in un-activated state.
 *   opts_other.buttonZoomingHTML {String} HTML which is placed in the 
 *     zoom button when the button is activated. 
 *   opts_other.buttonZoomingStyle {Object} A hash of css styles for the 
 *    zoom button which will be applied when the button is activated.
 *   opts_other.overlayRemoveTime {Number} The number of milliseconds to wait before
 *     removing the rectangle indicating the zoomed-in area after the zoom has happened.
 *   opts_other.stickyZoomEnabled {Boolean} Whether or not the control stays in 
 *     "zoom mode" until turned off. When true, the user can zoom repeatedly, 
 *     until clicking on the zoom button again to turn zoom mode off.
 * @param {opts_callbacks} Named optional arguments:
 *   opts_callbacks.buttonclick {Function} Called when the DragZoom is activated 
 *     by clicking on the "zoom" button. 
 *   opts_callbacks.dragstart {Function} Called when user starts to drag a rectangle.
 *     Callback args are x,y -- the PIXEL values, relative to the upper-left-hand 
 *     corner of the map, where the user began dragging.
 *   opts_callbacks.dragging {Function} Called repeatedly while the user is dragging.
 *     Callback args are startX,startY, currentX,currentY -- the PIXEL values of the 
 *     start of the drag, and the current drag point, respectively.
 *   opts_callbacks.dragend {Function} Called when the user releases the mouse button 
 *     after dragging the rectangle. Callback args are: NW {GLatLng}, NE {GLatLng}, 
 *     SE {GLatLng}, SW {GLatLng}, NW {GPoint}, NE {GPoint}, SE {GPoint}, SW {GPoint}.
 *     The first 4 are the latitudes/longitudes; the last 4 are the pixel coords on the map.
 */ 
function MapChartControl(opts_boxStyle, opts_other, opts_callbacks) {
 
}

MapChartControl.prototype = new GControl();


/**
 * Creates a new button to control gzoom and appends to map div.
 * @param {DOM Node} map The div returned by map.getContainer()
 */
MapChartControl.prototype.initButton_ = function(mapDiv) {

  var buttonDiv = document.createElement('div');
  buttonDiv.innerHTML ="<img src='pie.gif' title='click here to show the shoreline summary' alt='click here to show the shoreline summary'/>",
  buttonDiv.id = 'gpie-control';
 buttonDiv.style.cursor='pointer';
  buttonDiv.style.zIndex=200;
  buttonDiv.style.width='24px';
    buttonDiv.style.height='24px';
  buttonDiv.style.border='none';
  buttonDiv.style.background= '#FFF';
  mapDiv.appendChild(buttonDiv);
  return buttonDiv;
};



/**
 * Is called by GMap2's addOverlay method. Creates the zoom control
 * divs and appends to the map div.
 * @param {GMap2} map The map that has had this MapChartControl added to it.
 * @return {DOM Object} Div that holds the gzoomcontrol button
 */ 
MapChartControl.prototype.initialize = function(map) {
  var G = this.globals;
  var me = this;
  var mapDiv = map.getContainer();
  //DOM:button
  var buttonDiv = this.initButton_(mapDiv);

  //DOM:map covers


  // add event listeners
  GEvent.addDomListener(buttonDiv, 'click', function(e) {
    me.buttonclick_(e);
  });


  return buttonDiv;
};

/**
 * Required by GMaps API for controls. 
 * @return {GControlPosition} Default location for control
 */
MapChartControl.prototype.getDefaultPosition = function() {
  return new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(3, 120));
};

/**
 * Function called when the zoom button's click event is captured.
 */
MapChartControl.prototype.setButtonMode_ = function(mode){

}





/**
 * Function called when the zoom button's click event is captured.
 */
MapChartControl.prototype.buttonclick_ = function(){
  
//	this.intitCover_();
	this.resetView_();
	this.showPies_();
	this.setButtonMode_("zooming");
  
};



MapChartControl.prototype.resetView_ = function(){
	 jg.clear();
	  map.setCenter(new GLatLng(58.077,-96.575),3,G_NORMAL_MAP);
	  
}

   function subGPoints(a,b) {
        return new GPoint(a.x-b.x, a.y-b.y);
      }      

     

MapChartControl.prototype.showPies_ = function(){
	map.setCenter(new GLatLng(58.077,-96.575),3,G_NORMAL_MAP);
	var cnv = document.getElementById("map");
	jgnew = new jsGraphics(cnv);
	var urlstr = "activityinfo.php?provinces=all";
	var request = GXmlHttp.create();
	request.open('GET', urlstr , true);	// request XML from PHP with AJAX call
	request.onreadystatechange = function () {
		if (request.readyState == 4) {
		
			var xmlDoc = request.responseXML;
		//	alert(xmlDoc.
			var activities = xmlDoc.documentElement.getElementsByTagName("location");
			
			for (var a = 0; a < activities.length; a++) {
				var color = activities[a].getAttribute('color');
				var	from = activities[a].getAttribute('from');
				var name = activities[a].getAttribute('name');
				var perc = Math.round((activities[a].getAttribute('percentage')*100),1);
				var to = activities[a].getAttribute('to');
				var advancedegrees = activities[a].getAttribute('advance');
				// set x and y based on polygon centroid.
				var centroidxy = (activities[a].getAttribute('centroid')).split(" ");
				
	
     
				
				var lat = centroidxy[0];
				var lng = centroidxy[1];
				var coord = new GLatLng(lat,lng);
				
							
				 var CDivPixel = map.fromLatLngToDivPixel(map.getCenter());
				var pointDivPixel = map.fromLatLngToDivPixel(coord);
				 var fromCenter = subGPoints(pointDivPixel, CDivPixel); 
				
				//var point = map.fromLatLngToDivPixel(coord);
				var x = fromCenter.x +198;
				var y = fromCenter.y +141;
				
				var coordsxy = (activities[a].getAttribute('coords')).split(" ");
				var xcoord = coordsxy[0];
				var ycoord = coordsxy[1];
			/*	
				if(Math.abs(ycoord-y) > 5){
					x = xcoord;
				}
				if(Math.abs(ycoord-y) > 5){
					y = ycoord;
				}
				*/
				if(is_ie){
					y=y;
				}
				if(perc > 0.1){
				//	var text1 =  perc+"% " +name;
					jgnew.setColor(color); // red
					jgnew.fillArc(x,y,50,50,from,to);
					
				}
				
			}
			jgnew.setFont("arial","11px",Font.PLAIN);  
				
			jgnew.setColor("#FFFFFF");
			jgnew.fillRect(50,33,200,70);//bar
			jgnew.setColor("#000000");
			jgnew.drawRect(50,33,200,70);//bar
			
			jgnew.setColor("#dc3232");
			jgnew.fillRect(53,36,10,10);//bar
			jgnew.drawString("Shoreline and Recreational Activities",65,35);
			
			jgnew.setColor("#9e32dc");
			jgnew.fillRect(53,48,10,10);//bar
			jgnew.drawString("Ocean/Waterway Activities",65,47);
			
			jgnew.setColor("#326adc");
			jgnew.fillRect(53,60,10,10);//bar
			jgnew.drawString("Smoking Related Activities",65,59);
			
			jgnew.setColor("#32dc42");
			jgnew.fillRect(53,72,10,10);//bar
			jgnew.drawString("Dumping Activities",65,71);
			
			jgnew.setColor("#d2dc32");
			jgnew.fillRect(53,84,10,10);//bar
			jgnew.drawString("Medical/Personal Hygiene",65,83);
			
			//document.getElementById("provdozen").style.background = "#ebebeb";
			//document.getElementById("provinfo").style.background = "#FFFFFF";
			
			jgnew.paint(); 
			
			
		}
	}
	request.send(null);	

}







