HouseSenate.Inherits(Map);
function HouseSenate(name){
	if(typeof(name) == "undefined"){
		name = "House and Senate";
	}
	
	this.Inherits(Map, name);
	
	this.coordOrigin = 15;
	this.top = 0;
	this.left = 0;
	this.pfColor = "#fcfcd9";
	this.psColor = "#a9cb96";
	this.phColor = "#fadc5e";
	this.psSize = 1;
	this.activefColor = "#c0dbb2";
	this.inactivefColor = "#fcfcd9";
	this.ftColor = "#6d6d6d";
	this.htColor = "#454545";
}

HouseSenate.prototype.setElectionYear = function(year){
	(year==undefined)? this.electionYear = getElectionYear() : this.electionYear = year;
}

HouseSenate.prototype.getElectionYear = function(){
	return this.electionYear;
}

HouseSenate.prototype.getFile = function(year){
	var file = "_xml/houseAndSenateMap";
	(year==undefined)? file = file + getElectionYear() : file + year;
	file = file + ".xml";
	return file;
}


HouseSenate.prototype.setSenateRaceColor = function(activeSenateColor){
	switch(activeSenateColor){
		case "#0c6a8a":{
			this.activeSenateRace = "#0c6a8a";
			this.inactiveSenateRace = "#e6e6d5";
			this.stateCodeColor = "#fefefe";
			this.hilightColor = "#26415f";
			break;
		}case "#aadaaa":{
			this.activeSenateRace = "#aadaaa";
			this.inactiveSenateRace = "#e6e6d5";
			this.stateCodeColor = "#004477";
			this.hilightColor = "#26415f";
			break;
		}case "#ba500c":{
			this.activeSenateRace = "#ba500c";
			this.inactiveSenateRace = "#e6e6d5";
			this.stateCodeColor = "#004477";
			this.hilightColor = "#26415f";
			break;
		}case "#840259":{
			this.activeSenateRace = "#840259";
			this.inactiveSenateRace = "#e6e6d5";
			this.stateCodeColor = "#004477";
			this.hilightColor = "#26415f";
			break;
		}default:{
			this.activeSenateRace = "#aadaaa";
			this.inactiveSenateRace = "#e6e6d5";
			this.stateCodeColor = "#004477";
			this.hilightColor = "#26415f";
			break;
		}
	}
}

HouseSenate.prototype.init = function(canvas){
	this.initMap(canvas);

	var progressBar = showProgressBar(this.canvas);
	progressBar.style.display = "block";
	this.canvas.appendChild(progressBar);
	
	this.canvas.appendChild(this.draw());
	
	hideProgressBar();
}

HouseSenate.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);
			
			var tooltip = new Tooltip().create();
			
			for(var i=0; i<len; i++){		
				var polygon = new Polygon();
				polygon.width = self.width;
				polygon.height = self.height;
				polygon.fillColor = state[i].fillColor;
				polygon.strokeColor = self.psColor;
				polygon.strokeSize = self.psSize;
				polygon.opacity = self.opacity;
				polygon.create(state[i].code, state[i].coordinate);
				
				var sPolygon = polygon.polygon;
				sPolygon.defaultFillColor = state[i].fillColor;
				sPolygon.title = state[i].title;
				sPolygon.bodyText = state[i].tooltip;
				sPolygon.tooltip = tooltip;
				
				var stateCodeLeft = state[i].left + state[i].centerX - 14;
				var stateCodeTop = state[i].top + state[i].centerY - 10;
				
				var text = new Text();
				text.fColor = self.ftColor;
				sPolygon.appendChild(text.draw(state[i].code, stateCodeLeft, stateCodeTop));
				
				sPolygon.onmouseover = function(event){self.MouseOver(event);};
				sPolygon.onmouseout = function(event){self.MouseOut(event);};
				sPolygon.onmousemove = function(event){self.MouseMove(event);};
				sPolygon.onclick = function(event){self.MouseClick(event);};
				
				vmlGroup.appendChild(sPolygon);
			}
			vmlGroup.appendChild(tooltip.vmlTooltip);
			
			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 + "px"; 
			embed.height = this.height + "px";
			embed.style.position = "relative";
			 
			embed.addEventListener('load', function (evt) {
				var svgDoc = this.getSVGDocument();
				svgDoc.height = this.height;
				svgDoc.width = this.width;

				var tooltip = new Tooltip().create(svgDoc);
				
				for(i=0; i<len; i++){
					var polygon = new Polygon();
					polygon.width = self.width;
					polygon.height = self.height;
					polygon.fillColor = state[i].fillColor;
					polygon.strokeColor = self.psColor;
					polygon.strokeSize = self.psSize;
					polygon.opacity = self.opacity;
					polygon.create(state[i].code, state[i].coordinate);
					
					sPolygon = polygon.polygon;
					sPolygon.defaultFillColor = state[i].fillColor;
					sPolygon.title = state[i].title;
					sPolygon.bodyText = state[i].tooltip;
					sPolygon.tooltip = tooltip;
					
					stateCodeLeft = state[i].left + state[i].centerX - 5;
					stateCodeTop = state[i].top + state[i].centerY + 5;
					
					text = new Text();
					text.fColor = self.ftColor;
					sPolygon.appendChild(text.draw(state[i].code, stateCodeLeft, stateCodeTop));
					
					sPolygon.addEventListener("mouseover", function(event){self.MouseOver(event);}, false);
					sPolygon.addEventListener("mouseout", function(event){(self.MouseOut(event));}, false);
					sPolygon.addEventListener("click", function(event){(self.MouseClick(event));}, false);
					sPolygon.addEventListener("mousemove", function(event){(self.MouseMove(event));}, false);
					
					
					svgDoc.documentElement.appendChild(sPolygon);
				}
				svgDoc.documentElement.appendChild(tooltip.svgTooltip);	
				self.drawingCanvas = svgDoc;
				
			}, false);
			return embed;
			break;
		}
	}
}

HouseSenate.prototype.MouseOver = function(e){
	var workElement = (e==undefined)? event.srcElement : e.currentTarget;
	
	switch(getBrowser()){
		case "Explorer":{
			var vmlGroup = workElement.parentNode;
			while(vmlGroup.nodeName != "group"){
				vmlGroup = vmlGroup.parentNode;
			}
			vmlGroup.style.cursor = "hand";
			for(var i=0; i<vmlGroup.childNodes.length; i++){
				if(vmlGroup.childNodes[i].nodeName == "textbox"){
					vmlGroup.childNodes[i].fillColor = this.ftColor;
				}else if(vmlGroup.childNodes[i].nodeName == "shape"){
					vmlGroup.childNodes[i].fillColor = this.phColor;
				}
			}
			vmlGroup.tooltip.setTitle(vmlGroup.title);
			vmlGroup.tooltip.setBodyText(vmlGroup.bodyText);
			vmlGroup.tooltip.show();
			
			
			break;
		}
		default:{
			for(i=0; i<workElement.childNodes.length; i++){
				if(workElement.childNodes[i].nodeName == "text"){
					workElement.childNodes[i].setAttributeNS(null, "fill", this.ftColor);
				}else{
					workElement.childNodes[i].setAttributeNS(null, "fill", this.phColor);
				}
			}
			
			workElement.style.cursor = "pointer";
			workElement.tooltip.setTitle(workElement.title);
			workElement.tooltip.setBodyText(workElement.bodyText);
			workElement.tooltip.show();
			
			break;
		}
	}
}

HouseSenate.prototype.MouseOut = function(e){
	var workElement = (e==undefined)? event.srcElement : e.currentTarget;
	
	switch(getBrowser()){
		case "Explorer":{
			var vmlGroup = workElement.parentNode;
			while(vmlGroup.nodeName != "group"){
				vmlGroup = vmlGroup.parentNode;
			}
			
			for(var i=0; i<vmlGroup.childNodes.length; i++){
				if(vmlGroup.childNodes[i].nodeName == "textbox"){
					vmlGroup.childNodes[i].fillColor = this.ftColor;
				}else if(vmlGroup.childNodes[i].nodeName == "shape"){
					vmlGroup.childNodes[i].fillColor = vmlGroup.defaultFillColor;
				}
			}
			
			vmlGroup.tooltip.hide();
			
			break;
		}default:{
			for(var i=0; i<workElement.childNodes.length; i++){
				if(workElement.childNodes[i].nodeName == "text"){
					workElement.childNodes[i].setAttributeNS(null, "fill", this.ftColor);
				}else{
					workElement.childNodes[i].setAttributeNS(null, "fill", workElement.defaultFillColor);
				}
			}
			
			workElement.tooltip.hide();
			break;
		}
	}
	
	
}

HouseSenate.prototype.MouseClick = function(e){
	var workElement = (e == undefined)? event.srcElement : e.currentTarget;
    
	switch(getBrowser()){
		case "Explorer":{
			var vmlGroup = workElement.parentNode;
			while(vmlGroup.nodeName != "group"){
				vmlGroup = vmlGroup.parentNode;
			}
			
			var link = getContextRoot() + "mapHSApp.do?drillLevel=state&stateName="+vmlGroup.id + "&election_yr="+getElectionYear();
		    
		    if(vmlGroup.id == "VI" || vmlGroup.id == "GU" || vmlGroup.id == "AS" || vmlGroup.id == "MP"){
		    	link = getContextRoot() + "HSRefreshCandList.do?category=allState&stateName="+vmlGroup.id + "&election_yr="+getElectionYear();
		    }

			
			break;
		}default:{
			link = getContextRoot() + "mapHSApp.do?drillLevel=state&stateName="+workElement.id + "&election_yr="+getElectionYear();
		    
		    if(workElement.id == "VI" || workElement.id == "GU" || workElement.id == "AS" || workElement.id == "MP"){
		    	link = getContextRoot() + "HSRefreshCandList.do?category=allState&stateName="+workElement.id + "&election_yr="+getElectionYear();
		    }
			break;
		}
	}
	window.location.replace(link);
}


HouseSenate.prototype.MouseMove = function(e){
	var workElement = (e==undefined)? event.srcElement : e.currentTarget;
	
	switch(getBrowser()){
		case "Explorer":{
			var vmlGroup = workElement.parentNode;
			while(vmlGroup.nodeName != "group"){
				vmlGroup = vmlGroup.parentNode;
			}
			
			vmlGroup.tooltip.resetPosition(event.x, event.y);
			
			break;
		}default:{
			workElement.tooltip.resetPosition(e.clientX, e.clientY);
			break;
		}
	}
}

HouseSenate.prototype.getData = function(){
	var dataAccess = new DataAccess();
	var file = this.getFile();
	var mapData = dataAccess.getXmlData(file);
	
	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);
			state[i].fillColor = (mapData.state[i].activeSenate=="1") ? this.activefColor : this.inactivefColor; 
			state[i].tooltip = mapData.state[i].tooltip.split("<br/>");
			
			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);
		state[0].fillColor = (mapData.state[i].activeSenate=="1") ? this.activefColor : this.inactivefColor;
		state[0].tooltip = mapData.state.tooltip.split("<br/>");
		
		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;
}
