Presidential.Inherits(Map);
function Presidential(name){
	if(typeof(name) == "undefined"){
		name = "Presidential";
	}
	
	this.Inherits(Map);
	
	this.drawingCanvas = null;
	
	this.coordOrigin = 15;
	this.top = 0;
	this.left = 0;
	this.pfColor = "#e7dfca";
	this.psColor = "#ccc0a8";
	this.psSize = 1;
	this.cfColor = "#e69e6a";
	this.csColor = "#a15925";
	this.csSize = (getBrowser() == "Explorer")? 1 : 1.1;
	this.chColor = "#de5e01";
	this.chsSize = (getBrowser() == "Explorer")? 1.2 : 1.6;
	
}

Presidential.prototype.init = function(canvas){
	this.initMap(canvas);

	this.canvas.appendChild(this.draw());
	
	this.prepareData();
}

Presidential.prototype.draw = function(){
	var state = this.getData();
	
	var len = state.length;
	var self = this;
	switch(getBrowser()){
		case "Explorer":{
			var vmlGroup = document.createElement("v:group");
			vmlGroup.style.width = this.width;
			vmlGroup.style.height = this.height;
			vmlGroup.setAttribute("coordsize", this.width + " " + this.height);
			
			for(var i=0; i<len; i++){		
				var polygon = new Polygon();
				polygon.width = this.width;
				polygon.height = this.height;
				polygon.fillColor = this.pfColor;
				polygon.strokeColor = this.psColor;
				polygon.strokeSize = this.psSize;
				polygon.opacity = this.opacity;
				polygon.create(state[i].code, state[i].coordinate);
				
				var sPolygon = polygon.polygon;
				vmlGroup.appendChild(sPolygon);
			}
			this.drawingCanvas = vmlGroup;
			return vmlGroup;
			break;
		}default:{
			var svg_ns = 'http://www.w3.org/2000/svg'; 
			var xhtml_ns = 'http://www.w3.org/1999/xhtml'; 
			
			var embed = document.createElement("embed");
			embed.src = "svg/svg.svg";
			embed.type = "image/svg+xml";
			embed.width = this.width;
			embed.height = this.height;
			embed.style.position = "absolute";
			 
			embed.addEventListener('load', function (evt) {
				var svgDoc = this.getSVGDocument();
				svgDoc.height = this.height;
				svgDoc.width = this.width;

				for(i=0; i<len; i++){
					var polygon = new Polygon();
					polygon.width = self.width;
					polygon.height = self.height;
					polygon.fillColor = self.pfColor;
					polygon.strokeColor = self.psColor;
					polygon.strokeSize = self.psSize;
					polygon.opacity = self.opacity;
					polygon.create(state[i].code, state[i].coordinate);
					
					var sPolygon = polygon.polygon;
					svgDoc.documentElement.appendChild(sPolygon);
				}
				
				self.drawingCanvas = svgDoc;
				
			}, false);
			return embed;
			break;
		}
	}
}


Presidential.prototype.prepareData = function(){
	var dataAccess = new DataAccess();
	
	var request=dataAccess.getRequest();
	var progressBar = showProgressBar(this.canvas);
	progressBar.style.display = "block";
	this.canvas.appendChild(progressBar);
	
	var self = this;
	request.onreadystatechange = function(){
		if (request.readyState==4){
			if (request.status==200 || window.location.href.indexOf("http")==-1){	
				var responseRoot = request.responseXML.documentElement;
				dataAccess.prepareTree(responseRoot);
				var mapData = dataAccess.parseDocument(responseRoot);
				mapData = mapData["gov.fec.map"];
				
				self.drawCircles(mapData);
				hideProgressBar();
			}/*else{
				alert("An error has occured making the request");
			}*/
		}
	}

	var qsReg = new RegExp("[?][^#]*","i");
    hRef = unescape(window.location.href);
    var qsMatch = hRef.match(qsReg);
    
    var candId = "P00000001";
    if(qsMatch != null){
	    qsMatch = new String(qsMatch);
	    qsMatch = qsMatch.substr(1, qsMatch.length -1);        

	    var qsArray = qsMatch.split("&");
    
	    candId = qsArray[0].split("=")[1];
    }
    
	request.open("POST", "presidentialMapApp.do", true);
	request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	request.send("cand_id=" + candId);
}

Presidential.prototype.drawCircles = function(mapData){
	var state = mapData.state;
	var len = state.length;
	
	var self = this;
	
	var tooltip = new Tooltip();
	tooltip.bodyFontSize = 11;
	tooltip.width = 180;
	
	switch(getBrowser()){
		case "Explorer":{
			tooltip.create();
			
			for(var i=0; i<len; i++){
				var ellipse = new Ellipse();
				ellipse.fillColor = this.cfColor;
				ellipse.strokeColor = this.csColor;
				ellipse.strokeSize = this.csSize;
				ellipse.opacity = 0.7;
				
				var radius = state[i].size;
				var left = Math.ceil(state[i].left * this.zoomFactor) + this.coordOrigin;
				var top = Math.ceil(state[i].top * this.zoomFactor) + this.coordOrigin;
				
			
				ellipse.create(state[i].code, radius, left, top);
				var pEllipse = ellipse.ellipse;
				pEllipse.stateName = state[i].code;
				pEllipse.title = state[i].title;
				pEllipse.bodyText = state[i].tooltip;
				pEllipse.tooltip = tooltip;
				
				pEllipse.onmouseover = function(event){self.MouseOver(event);};
				pEllipse.onmouseout = function(event){self.MouseOut(event);};
				pEllipse.onmousemove = function(event){self.MouseMove(event);};
				pEllipse.onclick = function(event){self.MouseClick(event);};
				
				this.drawingCanvas.appendChild(pEllipse);
			}
			this.drawingCanvas.appendChild(tooltip.vmlTooltip);
			break;
		}default:{
			tooltip.create(this.drawingCanvas);
			for(i=0; i<len; i++){
				ellipse = new Ellipse();
				ellipse.fillColor = this.cfColor;
				ellipse.strokeColor = this.csColor;
				ellipse.strokeSize = this.csSize;
				ellipse.opacity = 0.7;
				
				radius = state[i].size;
				left = Math.ceil(state[i].left * this.zoomFactor) + this.coordOrigin;
				top = Math.ceil(state[i].top * this.zoomFactor) + this.coordOrigin;
				
			
				ellipse.create(state[i].code, radius, left, top);
				pEllipse = ellipse.ellipse;
				pEllipse.stateName = state[i].code;
				pEllipse.title = state[i].title;
				pEllipse.bodyText = state[i].tooltip;
				pEllipse.tooltip = tooltip;
				
				pEllipse.addEventListener("mouseover", function(event){self.MouseOver(event);}, false);
				pEllipse.addEventListener("mouseout", function(event){(self.MouseOut(event));}, false);
				pEllipse.addEventListener("click", function(event){(self.MouseClick(event));}, false);
				pEllipse.addEventListener("mousemove", function(event){(self.MouseMove(event));}, false);
				
				this.drawingCanvas.documentElement.appendChild(pEllipse);
			}
			this.drawingCanvas.documentElement.appendChild(tooltip.svgTooltip);
			break;
		}
	}
}

Presidential.prototype.MouseOver = function(e){
	var workElement = (e==undefined)? event.srcElement : e.currentTarget;
	
	switch(getBrowser()){
		case "Explorer":{
			workElement.style.cursor = "hand";
			workElement.fillColor = this.chColor;
			workElement.strokeWeight = this.chsSize;
			break;
		}default:{
			workElement.style.cursor = "pointer";
			workElement.setAttributeNS(null, "fill", this.chColor);
			workElement.setAttributeNS(null, "stroke-width", this.chsSize);
			break;
		}
	}
	
	workElement.tooltip.setTitle(workElement.title);
	workElement.tooltip.setBodyText(workElement.bodyText);
	workElement.tooltip.show();
	
}

Presidential.prototype.MouseOut = function(e){
	var workElement = (e==undefined)? event.srcElement : e.currentTarget;
	
	switch(getBrowser()){
		case "Explorer":{
			workElement.style.cursor = "hand";
			workElement.fillColor = this.cfColor;
			workElement.strokeWeight = this.csSize;
			break;
		}default:{
			workElement.style.cursor = "pointer";
			workElement.setAttributeNS(null, "fill", this.cfColor);
			workElement.setAttributeNS(null, "stroke-width", this.csSize);
			break;
		}
	}
	
	workElement.tooltip.hide();
}

Presidential.prototype.MouseMove = function(e){
	var workElement = (e==undefined)? event.srcElement : e.currentTarget;
	
	switch(getBrowser()){
		case "Explorer":{
			workElement.style.cursor = "hand";
			workElement.tooltip.resetPosition(event.x, event.y);
			break;
		}default:{
			workElement.style.cursor = "pointer";
			workElement.tooltip.resetPosition(e.clientX, e.clientY);
			break;
		}
	}
	
}

Presidential.prototype.MouseClick = function(e){
	var workElement = (e==undefined)? event.srcElement : e.currentTarget;
	
	var qsReg = new RegExp("[?][^#]*","i");
    hRef = unescape(window.location.href);
    var qsMatch = hRef.match(qsReg);
    
    var candId = "P00000001";
    if(qsMatch != null){
	    qsMatch = new String(qsMatch);
	    qsMatch = qsMatch.substr(1, qsMatch.length -1);        

	    var qsArray = qsMatch.split("&");
    
	    candId = qsArray[0].split("=")[1];
    }
    
	var link = getContextRoot() + "MapAppState.do?stateName="+workElement.stateName+"&cand_id="+candId;
	window.location.replace(link);
}

Presidential.prototype.getData = function(){
	var dataAccess = new DataAccess();
	var mapData = dataAccess.getXmlData("_xml/map.xml");
	
	var state = new Array();
	if(mapData.state.length != undefined){
		for(var i=0; i<mapData.state.length; i++){
			state[i] = new Object();
			state[i].code = mapData.state[i].code;
			state[i].title = mapData.state[i].title;
			state[i].left = Math.ceil(mapData.state[i].x * this.zoomFactor) + this.coordOrigin;
			state[i].top = Math.ceil(mapData.state[i].y * this.zoomFactor) + this.coordOrigin;
			state[i].centerX = Math.ceil(mapData.state[i].center_x * this.zoomFactor);
			state[i].centerY = Math.ceil(mapData.state[i].center_y * this.zoomFactor);
			
			var coordLen = 1;
			if(typeof(mapData.state[i].coordinate) == "object"){
				coordLen = mapData.state[i].coordinate.length;
			}
			
			var coordinate = new Array(coordLen);
			if(coordLen != 1){
				for(var j=0; j<coordLen; j++){
					coord = mapData.state[i].coordinate[j].split(",");
					
					var pointArray = new Array();
					
					for(var k=0; k<coord.length; k+=2){
						var point = new Point((state[i].left + Math.ceil(coord[k] * this.zoomFactor)), (state[i].top + Math.ceil(coord[k+1] * this.zoomFactor)));
						
						pointArray.push(point);
						delete point;
					}
					
					coordinate[j] = new Object();
					coordinate[j] = pointArray;
				}
				
			}else{
				coord = mapData.state[i].coordinate.split(",");
				pointArray = new Array();
				
				for(k=0; k<coord.length; k+=2){
					point = new Point((state[i].left + Math.ceil(coord[k] * this.zoomFactor)), (state[i].top + Math.ceil(coord[k+1] * this.zoomFactor)));
					pointArray.push(point);
					delete point;
				}
				
				coordinate[0] = new Object();
				coordinate[0] = pointArray;
			}
			
			state[i].coordinate = coordinate;
		}
	}else{
		state[0] = new Object();
		state[0].code = mapData.state.code;
		state[0].title = mapData.state.title;
		state[0].left = Math.ceil(mapData.state.x * this.zoomFactor) + this.coordOrigin;
		state[0].top = Math.ceil(mapData.state.y * this.zoomFactor) + this.coordOrigin;
		state[0].centerX = Math.ceil(mapData.state.center_x * this.zoomFactor);
		state[0].centerY = Math.ceil(mapData.state.center_y * this.zoomFactor);
		
		coordLen = 1;
		if(typeof(mapData.state.coordinate) == "object"){
			coordLen = mapData.state.coordinate.length;
		}
		
		coordinate = new Array(coordLen);
		if(coordLen != 1){
			for(var j=0; j<coordLen; j++){
				coord = mapData.state.coordinate[j].split(",");
				
				var pointArray = new Array();
				
				for(var k=0; k<coord.length; k+=2){
					var point = new Point((state[0].left + Math.ceil(coord[k] * this.zoomFactor)), (state[0].top + Math.ceil(coord[k+1] * this.zoomFactor)));
					
					pointArray.push(point);
					delete point;
				}
				
				coordinate[j] = new Object();
				coordinate[j] = pointArray;
			}
			
		}else{
			coord = mapData.state.coordinate.split(",");
			pointArray = new Array();
			
			for(k=0; k<coord.length; k+=2){
				point = new Point((state[0].left + Math.ceil(coord[k] * this.zoomFactor)), (state[0].top + Math.ceil(coord[k+1] * this.zoomFactor)));
				pointArray.push(point);
				delete point;
			}
			
			coordinate[0] = new Object();
			coordinate[0] = pointArray;
		}
		
		state[0].coordinate = coordinate;
	}
	
	return state;
}






