if (!window.CandidateFilter)
	window.CandidateFilter = {};
CandidateFilter.Page = function() 
{
}
CandidateFilter.Page.prototype =
{
	handleLoad: function(control, userContext, rootElement) 
	{
		this.control = control;
		this.root = rootElement;
		
		this.BUSY = false;
		
		//Broser Compatibility Check
		if (document.implementation && document.implementation.createDocument) {
			this.BrowserFamily = "Mozilla";
		} else if (window.ActiveXObject) {
			this.BrowserFamily = "IE";
		} else {
			this.BrowserFamily = "Unknown";
		}
		
		this.xMouse = 0;
		this.yMouse = 0;
		
		this.numCandidateColumns = 3;
		this.numIssueColumns = 2;
		this.issueBottomPadding = 6;
		this.issueColumnWidth = 180;
		this.candidateColumnHeight = 295;
		this.candidateColumnWidth = 205;
		
		this.candidates = new Array();
		this.issues = new Array();

		this.CandidateGrid = this.root.findName("CandidateGrid");
		this.IssueGrid = this.root.findName("IssueGrid");
		this.ToolTip = this.root.findName("ToolTip");
		
		this.root.findName("ResetIssuesButton").addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.onResetIssuesButtonMouseDown));
		this.root.findName("ClickBlocker").addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.onClickBlockerMouseDown));
		this.root.findName("SB_ClickBlocker_Hide").addEventListener("Completed", Silverlight.createDelegate(this, this.onClickBlockerHideComplete));
		this.root.findName("SB_DetailPanel_Hide").addEventListener("Completed", Silverlight.createDelegate(this, this.onDetailPanelHideComplete));
		this.root.findName("DetailPanel_CloseButton").addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.onDetailPanelCloseButtonMouseDown));
		this.root.findName("DetailPanel_Link").addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.onDetailLinkMouseDown));
		this.root.findName("DetailPanel_Link").addEventListener("MouseEnter", Silverlight.createDelegate(this, this.onDetailLinkRollover));
		this.root.findName("DetailPanel_Link").addEventListener("MouseLeave", Silverlight.createDelegate(this, this.onDetailLinkRollout));
		this.root.findName("DescPanel_CloseButton").addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.onDescPanelCloseButtonMouseDown));
		this.root.findName("SB_DescPanel_Hide").addEventListener("Completed", Silverlight.createDelegate(this, this.onDescPanelHideComplete));
		
		this.root.findName("LogoHotspot").addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.onLogoHotspotMouseDown));
		this.root.findName("LogoHotspot").addEventListener("MouseEnter", Silverlight.createDelegate(this, this.onLogoHotspotRollover));
		this.root.findName("LogoHotspot").addEventListener("MouseLeave", Silverlight.createDelegate(this, this.onLogoHotspotRollout));

		this.loadingBar_maxBar = this.root.findName("loadingBar_maxBar");
		this.loadingBar_progressBar = this.root.findName("loadingBar_progressBar");

		this.root.addEventListener("MouseMove", Silverlight.createDelegate(this, this.onMouseMove));
		
		this.downloader = control.createObject("downloader");
		this.downloader.addEventListener("downloadProgressChanged", Silverlight.createDelegate(this, this.onDownloadProgressChanged));
		this.downloader.addEventListener("completed", Silverlight.createDelegate(this, this.onDownloadCompleted));
		this.downloader.open("GET", "DownloadPackage.zip");
		this.downloader.send();
	},
	handleError: function(msg) {
		
	},
	
	startApp: function() {
		this.root.findName("SB_LoadComplete").Begin();
	},
	
	onDownloadProgressChanged: function(sender, eventArgs)
	{
		var percentage = sender.downloadProgress * 100;
		// Update the Rectangle and TextBlock objects of the visual progress indicator.
		this.loadingBar_progressBar.Width = Math.floor(percentage * this.loadingBar_maxBar.Width/100);
	},

	onDownloadCompleted: function(sender, eventArgs)
	{
		this.root.findName("Image_"+"Logo").setSource(this.downloader, "images/"+"Logo"+".png");
		this.root.findName("Image_"+"Parchment").setSource(this.downloader, "images/"+"Parchment"+".png");
		this.root.findName("Image_"+"Parchment2").setSource(this.downloader, "images/"+"Parchment"+".png");
		this.root.findName("Image_"+"Parchment3").setSource(this.downloader, "images/"+"Parchment"+".png");
		this.root.findName("Image_"+"thinRedBar").setSource(this.downloader, "images/"+"thinRedBar"+".jpg");
		this.root.findName("Image_"+"2008Bar").setSource(this.downloader, "images/"+"2008Bar"+".png");
		this.root.findName("Image_"+"PresidentialCandidates").setSource(this.downloader, "images/"+"PresidentialCandidates"+".png");
		
		this.getXMLDoc("data.xml", this.onDataLoad, this);
	},
	
	onLinkRequest: function(url) {
		location.href = url;
	},
	
	onDataLoad: function(xmldoc, xthis) {
		xthis.storeDataFromXMLDoc(xmldoc);
	},
	
	storeDataFromXMLDoc: function(xmldoc) {
		
		//Store Candidate Data
		//IE Check
		if (this.BrowserFamily=="IE") {	
			var candidateNodes = xmldoc.selectNodes("/data/candidates/candidate");
			var candidateNodesLength = candidateNodes.length;
		} else if (this.BrowserFamily=="Mozilla") {
			var candidateNodes = document.evaluate('/data/candidates/candidate', xmldoc, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null );
			var candidateNodesLength = candidateNodes.snapshotLength;
		}
		
		for ( var i=0 ; i < candidateNodesLength; i++ )
		{
			//IE Check
			if (this.BrowserFamily=="IE") {	
				var myCandidateNode = candidateNodes[i];
			} else if (this.BrowserFamily=="Mozilla") {
				var myCandidateNode = candidateNodes.snapshotItem(i);
			}
			
			var myCandidate = new Object();
			myCandidate["name"] = getAttributeValue(myCandidateNode,"name");
			myCandidate["party"] =getAttributeValue(myCandidateNode,"party");
			myCandidate["image"] = getAttributeValue(myCandidateNode,"image");
			myCandidate["url"] = getAttributeValue(myCandidateNode,"url");
			var myIssues = new Object();
			
			//Store Candidate's Issue Data
			//IE Check
			if (this.BrowserFamily=="IE") {	
				var issueNodes = myCandidateNode.selectNodes('issues/issue');
				var issueNodesLength = issueNodes.length;
			} else if (this.BrowserFamily=="Mozilla") {
				var issueNodes = document.evaluate('issues/issue', myCandidateNode, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null );
				var issueNodesLength = issueNodes.snapshotLength;
			}
			
			for ( var j=0; j<issueNodesLength; j++ )
			{
				//IE Check
				if (this.BrowserFamily=="IE") {	
					var myIssueNode = issueNodes[j];
				} else if (this.BrowserFamily=="Mozilla") {
					var myIssueNode = issueNodes.snapshotItem(j);
				}
				myIssues[getAttributeValue(myIssueNode,"name")] = getAttributeValue(myIssueNode,"stance");
			}
			myCandidate["issues"] = myIssues;
			myCandidate["hidden"] = false;
			this.candidates.push(myCandidate);
		}
		
		//Store Issue Data
		//IE Check
		if (this.BrowserFamily=="IE") {	
			var issueNodes = myCandidateNode.selectNodes('/data/issues/issue');
			var issueNodesLength = issueNodes.length;
		} else if (this.BrowserFamily=="Mozilla") {
			var issueNodes = document.evaluate('/data/issues/issue', xmldoc, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null );
			var issueNodesLength = issueNodes.snapshotLength;
		}
		
		for ( var i=0 ; i < issueNodesLength; i++ )
		{
			//IE Check
			if (this.BrowserFamily=="IE") {	
				var myIssueNode = issueNodes[i];
			} else if (this.BrowserFamily=="Mozilla") {
				var myIssueNode = issueNodes.snapshotItem(i);
			}
			var myIssue = new Object();
			myIssue["name"] = getAttributeValue(myIssueNode,"name");
			//myIssue["desc"] = getAttributeValue(myIssueNode,"desc");
			var myStances = new Array();
			
			//"ALL" option front-loaded
			var myStance = new Object();
			myStance["name"] = "All opinions";
			myStance["image"] = "all.jpg";
			myStances.push(myStance);
			
			//Store Issue's Stance Data
			//IE Check
			if (this.BrowserFamily=="IE") {	
				var stanceNodes = myIssueNode.selectNodes('stances/stance');
				var stanceNodesLength = stanceNodes.length;
			} else if (this.BrowserFamily=="Mozilla") {
				var stanceNodes = document.evaluate('stances/stance', myIssueNode, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null );
				var stanceNodesLength = stanceNodes.snapshotLength;
			}
						
			for ( var j=0; j<stanceNodesLength; j++ )
			{
				//IE Check
				if (this.BrowserFamily=="IE") {	
					var myStanceNode = stanceNodes[j];
				} else if (this.BrowserFamily=="Mozilla") {
					var myStanceNode = stanceNodes.snapshotItem(j);
				}
				
				var myStance = new Object();
				myStance["name"] = getAttributeValue(myStanceNode,"name");
				myStance["image"] = getAttributeValue(myStanceNode,"image");
				myStances.push(myStance);
			}
			myIssue["stances"] = myStances;
			myIssue["selectedStance"] = myIssue["stances"][0];
			myIssue["selectedStanceIndex"] = 0;
			
			//Store Issue's Description Data
			//IE Check
			if (this.BrowserFamily=="IE") {	
				var descriptionNodes = myIssueNode.selectNodes('description');
			} else if (this.BrowserFamily=="Mozilla") {
				var descriptionNodes = document.evaluate('description', myIssueNode, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null );
			}
			
			//IE Check
			if (this.BrowserFamily=="IE") {	
				var myDescriptionNode = descriptionNodes[0];
			} else if (this.BrowserFamily=="Mozilla") {
				var myDescriptionNode = descriptionNodes.snapshotItem(0);
			}
			if(myDescriptionNode!= null) {
				myIssue["description"] = myDescriptionNode.firstChild.nodeValue;
			}

			this.issues.push(myIssue);
		}
		this.loadStanceTemplate();	
	},

	//Stance Template
	loadStanceTemplate: function() {
		this.getXMLDocSource("CandidateFilter/Stance.xaml", this.onStanceTemplateLoad, this);
	},
	onStanceTemplateLoad: function(xmldocsource, xthis) {
		xthis.StanceTemplateXAML = xmldocsource;
		xthis.loadIssueTemplate();	
	},

	//Issue Template
	loadIssueTemplate: function() {
		this.getXMLDocSource("CandidateFilter/Issue.xaml", this.onIssueTemplateLoad, this);
	},
	onIssueTemplateLoad: function(xmldocsource, xthis) {
		xthis.IssueTemplateXAML = xmldocsource;
		xthis.createIssueGrid();
		xthis.loadCandidateTemplate();	
	},
	
	createIssueGrid: function() {
		var topOffset = 0;
		var leftOffset = 0;
		var numCols = this.numIssueColumns;
		var bottomPadding = this.issueBottomPadding;
		for(var j in this.issues) {
			var iteration = "_"+String(j)+"_";
			
			//Template XAML string customized...
			var xamlFragment = this.IssueTemplateXAML.replace(/_N_/gi,iteration);
			//... and turned into Silverlight Object
			var myIssue = this.control.content.createFromXaml(xamlFragment);
			
			//Resources pulled out of template
			var tmpResources = new Array();
			for (var i=myIssue.resources.count-1;i>=0;i--) {
				var myResource = myIssue.resources.getItem(i);
				tmpResources.push(myResource)
				myIssue.resources.remove(myIssue.resources.getItem(i));
			}
			
			//Create Stance Grid
			this.createStanceGrid(j, myIssue);

			//Silverlight Object added to main document
			var myName = myIssue.findName("Issue"+iteration+"Name");
				myName["Text"] = this.issues[j]["name"];
			var myStanceGrid = myIssue.findName("Issue"+iteration+"StanceGrid");
				myStanceGrid["Canvas.Top"] = Math.ceil(myName.ActualHeight)+1;
			myIssue.Height = myStanceGrid["Canvas.Top"]+myStanceGrid.Height
			
			if(topOffset+myIssue.Height+bottomPadding>this.IssueGrid["Height"]) {
				topOffset = 0;
				leftOffset += this.issueColumnWidth;
			}			
			myIssue["Canvas.Top"] = topOffset;
			myIssue["Canvas.Left"] = leftOffset;
			topOffset += myIssue.Height+bottomPadding;
			
			this.IssueGrid.children.add(myIssue);
			
			//Resources added back in to main document
			for (var i=0;i<tmpResources.length;i++) {
				this.root.resources.add(tmpResources[i]);
			}
			
			//Interaction Event hookup
			this["Issue"+iteration] = this.root.findName("Issue"+iteration);
			this.root.findName("Issue"+iteration+"Name").addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.onIssueNameMouseDown));
			
			//Enable defaults
			this.root.findName("SB_Issue_"+j+"_Stance_0_Enable").Begin();
		}
	},
	
	createStanceGrid: function(issueIndex, myIssue) {
		var myStances = this.issues[issueIndex]["stances"];
		var issueIndexString = "_"+String(issueIndex)+"_";
		var myStanceGrid = myIssue.findName("Issue"+issueIndexString+"StanceGrid");
		for(var j in myStances) {
			var iteration = "_"+String(j)+"_";
			
			//Template XAML string customized...
			var xamlFragment = this.StanceTemplateXAML.replace(/_N_/gi,issueIndexString);
				xamlFragment = xamlFragment.replace(/_O_/gi,iteration);
			//... and turned into Silverlight Object
			var myStance = this.control.content.createFromXaml(xamlFragment);
			
			//Resources pulled out of template
			var tmpResources = new Array();
			for (var i=myStance.resources.count-1;i>=0;i--) {
				var myResource = myStance.resources.getItem(i);
				tmpResources.push(myResource)
				myStance.resources.remove(myStance.resources.getItem(i));
			}

			//Silverlight Object added to main document
			myStance["Canvas.Left"] = j*(myStance.Width+5);
			myStance.findName("Issue"+issueIndexString+"Stance"+iteration+"Name").Text = myStances[j]["name"];
			//myStance.findName("Issue"+issueIndexString+"Stance"+iteration+"Icon")["Fill"]["ImageSource"] = "CandidateFilter/images/"+myStances[j]["image"];
			myStance.findName("Issue"+issueIndexString+"Stance"+iteration+"ImageBrush").setSource(this.downloader, "images/"+myStances[j]["image"]);
			
			myStanceGrid.Height = myStance.Height;
			myStanceGrid.children.add(myStance);
			
			//Resources added back in to main document
			for (var i=0;i<tmpResources.length;i++) {
				this.root.resources.add(tmpResources[i]);
			}
			
			//Interaction Event hookup
			this["Issue"+issueIndexString+"Stance"+iteration] = myStanceGrid.findName("Issue"+issueIndexString+"Stance"+iteration);
			this["Issue"+issueIndexString+"Stance"+iteration].addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.onStanceMouseDown));
			this["Issue"+issueIndexString+"Stance"+iteration].addEventListener("MouseEnter", Silverlight.createDelegate(this, this.onStanceMouseEnter));
			this["Issue"+issueIndexString+"Stance"+iteration].addEventListener("MouseLeave", Silverlight.createDelegate(this, this.onStanceMouseLeave));
		}
	},

	//Candidate Template
	loadCandidateTemplate: function() {
		this.getXMLDocSource("CandidateFilter/Candidate.xaml", this.onCandidateTemplateLoad, this);
	},
	onCandidateTemplateLoad: function(xmldocsource, xthis) {
		xthis.CandidateTemplateXAML = xmldocsource;
		xthis.createCandidateGrid();
		xthis.startApp();
	},
	
	createCandidateGrid: function() {
		var numCols = this.numCandidateColumns;
		for(var j in this.candidates) {
			var iteration = "_"+String(j)+"_";
			
			//Template XAML string customized...
			var xamlFragment = this.CandidateTemplateXAML.replace(/_N_/gi,iteration);
			//... and turned into Silverlight Object
			var myCandidate = this.control.content.createFromXaml(xamlFragment);
			
			//Resources pulled out of template
			var tmpResources = new Array();
			for (var i=myCandidate.resources.count-1;i>=0;i--) {
				var myResource = myCandidate.resources.getItem(i);
				tmpResources.push(myResource)
				myCandidate.resources.remove(myCandidate.resources.getItem(i));
			}
			
			//Silverlight Object added to main document
			myCandidate["Canvas.Top"] = (j-(j%numCols))/numCols*this.candidateColumnHeight;
			myCandidate["Canvas.Left"] = (j%numCols)*this.candidateColumnWidth;
			
			//myCandidate.findName("Candidate"+iteration+"Image")["Fill"]["ImageSource"] = "CandidateFilter/images/"+this.candidates[j]["image"];
			myCandidate.findName("Candidate"+iteration+"ImageBrush").setSource(this.downloader, "images/"+this.candidates[j]["image"]);
		
		
			var myFullName = myCandidate.findName("Candidate"+iteration+"FullName");
				myFullName["Text"] = this.candidates[j]["name"];
			var myName = myCandidate.findName("Candidate"+iteration+"Name");
				var nameparts = this.candidates[j]["name"].split(" ");
				myName["Text"] = nameparts[nameparts.length-1];
				this.centerText(myName);
			var myParty = myCandidate.findName("Candidate"+iteration+"Party");
			var myParty2 = myCandidate.findName("Candidate"+iteration+"Party_Sharpen");
				myParty["Text"] = 
				myParty2["Text"] = this.candidates[j]["party"];
				this.centerText(myParty);			
				this.centerText(myParty2);			
			var partyColor = "#FF009900";
			if (this.candidates[j]["party"] == "Republican") {
				partyColor = "#FFFF0000"
			} else if (this.candidates[j]["party"] == "Democrat") {
				partyColor = "#FF6666FF"
			}
			myCandidate.findName("Candidate"+iteration+"Party")["Foreground"] = 
			myCandidate.findName("Candidate"+iteration+"Party_Sharpen")["Foreground"] = partyColor;
			this.CandidateGrid.children.add(myCandidate);
			
			//Resources added back in to main document
			for (var i=0;i<tmpResources.length;i++) {
				this.root.resources.add(tmpResources[i]);
			}
			
			//Interaction Event hookup
			this["Candidate"+iteration] = this.root.findName("Candidate"+iteration);
			this["Candidate"+iteration].addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.onCandidateMouseDown));
			this["Candidate"+iteration].addEventListener("MouseEnter", Silverlight.createDelegate(this, this.onCandidateMouseEnter));
			this["Candidate"+iteration].addEventListener("MouseLeave", Silverlight.createDelegate(this, this.onCandidateMouseLeave));
		}
	},	

	selectIssue: function(myIssueIndex) {
		var myIssue = this.issues[myIssueIndex];
		
		if(myIssue["name"]=="Subsidized Internet Access") {
			window.open("http://www.pcworld.com/article/id,142121/article.html", "PCWORLDWIN", "");
		} else if(myIssue["description"]!=null) {
			this.root.findName("DescPanel_name").Text = myIssue["name"];
			this.root.findName("DescPanel_desc").Text = myIssue["description"];
			
			this.root.findName("Image_Parchment3").Height = this.root.findName("DescPanel_desc").ActualHeight + 80;
			
			this.root.findName("ClickBlocker").Visibility = "Visible";
			this.root.findName("SB_ClickBlocker_Show").Begin();
			this.root.findName("DescPanel").Visibility = "Visible";
			this.root.findName("SB_DescPanel_Show").Begin();
		}
	},

	selectIssueStance: function(myIssueIndex, myStanceIndex) {
		var myIssue = this.issues[myIssueIndex];
		var myStance = this.issues[myIssueIndex]["stances"][myStanceIndex]
		
		if(myIssue["selectedStance"] != myStance && !this.BUSY) {
			this.root.findName("SB_Issue_"+myIssueIndex+"_Stance_"+myIssue["selectedStanceIndex"]+"_Disable").Begin();
			myIssue["selectedStance"] = myStance;
			myIssue["selectedStanceIndex"] = myStanceIndex;
			this.root.findName("SB_Issue_"+myIssueIndex+"_Stance_"+myIssue["selectedStanceIndex"]+"_Enable").Begin();	
			
			this.filterCandidates();
		}
	},
	
	selectCandidate: function(myCandidate) {
		this.SelectedCandidate = myCandidate;
		
		
		this.root.findName("DetailPanel_ImageBrush").setSource(this.downloader, "images/"+myCandidate["image"]);
		//this.root.findName("DetailPanel_Image")["Fill"]["ImageSource"] = "CandidateFilter/images/FredThompson.jpg";
			
		this.root.findName("DetailPanel_Title").Text = myCandidate["name"];
		var partyColor = "#FF009900";
		if (myCandidate["party"] == "Republican") {
			partyColor = "#FFCC0000"
		} else if (myCandidate["party"] == "Democrat") {
			partyColor = "#FF0000CC"
		}
		this.root.findName("DetailPanel_Party")["Foreground"] = partyColor;
		this.root.findName("DetailPanel_Party").Text = myCandidate["party"];
		this.root.findName("DetailPanel_Link").Text = "Visit "+myCandidate["name"]+"'s official Web site at "+myCandidate["url"]+".";
		this.root.findName("DetailPanel_Link").Width = 396;
		this.root.findName("DetailPanel_Link")["Canvas.Left"] = -200;
		this.centerText(this.root.findName("DetailPanel_Link"));
		
		this.root.findName("DetailPanel_Content").Text ="";
		for(i in myCandidate["issues"]) {
			var myRun = this.control.content.createFromXaml("<Run/>");
			myRun.Text = i;			
			switch (myCandidate["issues"][i])
			{
			   case "Supports" :
			   case "Supports immediate withdrawal" :
				  myRun.Foreground = "#FF006600";
				  break;
			   case "Opposes" :
				  myRun.Foreground = "#FFCC0000";
				  break;
			   case "Supports phased withdrawal" :
				  myRun.Foreground = "#FFCC6600";
				  break;
			   case "Believes the issue should be left up to the states" :
				  myRun.Foreground = "#FF003399";
				  break;
			   case "Unknown":
				  myRun.Foreground = "#AA000000";
				  break;
			   default :
				  myRun.Foreground = "#FF000000";
			} 
			this.root.findName("DetailPanel_Content").Inlines.add(myRun);
			
			var myRun = this.control.content.createFromXaml("<LineBreak/>");
			this.root.findName("DetailPanel_Content").Inlines.add(myRun);
		}

		this.root.findName("ClickBlocker").Visibility = "Visible";

		this.root.findName("SB_ClickBlocker_Show").Begin();

		this.root.findName("DetailPanel").Visibility = "Visible";

		this.root.findName("SB_DetailPanel_Show").Begin();
	},
	
	/**************************************************
		RESET ISSUES
	**************************************************/
	resetIssues: function() {
		if(!this.BUSY) {
			for(var i in this.issues) {
				var myIssue = this.issues[i];
				
				if(myIssue["selectedStance"] !=  myIssue["stances"][0]) {
					this.root.findName("SB_Issue_"+i+"_Stance_"+myIssue["selectedStanceIndex"]+"_Disable").Begin();
					myIssue["selectedStance"] =  myIssue["stances"][0];
					myIssue["selectedStanceIndex"] = 0;
					this.root.findName("SB_Issue_"+i+"_Stance_0_Enable").Begin();	
				}
			}
			this.filterCandidates();
		}
	},
		
	/**************************************************
		FILTERING CHAIN
	**************************************************/
	filterCandidates: function() {
		this.BUSY = true;
		this.root.findName("SB_Hourglass_Show").Begin();
		
		this.currentCandidateIndex = -1;
		this.filterNextCandidate();
	},
	filterNextCandidate: function() {
		this.currentCandidateIndex++;
		if(this.currentCandidateIndex<this.candidates.length) {
			this.filterCandidate(this.currentCandidateIndex);
		} else {
			this.root.findName("SB_Hourglass_Hide").Begin();
			this.BUSY = false;
		}
	},
	filterCandidate: function(myCandidateIndex) {
		var myCandidate = this.candidates[myCandidateIndex];
		
		var foundProblem = false;
		
		for(var i in this.issues) {
			if(this.issues[i]["selectedStance"]!=this.issues[i]["stances"][0]) {
				if(myCandidate["issues"][this.issues[i]["name"]] != this.issues[i]["selectedStance"]["name"]) {
					foundProblem = true;
					break;
				}
			}
		}
		
		if(foundProblem) {
			if(!myCandidate["hidden"]) {
				myCandidate["hidden"] = true;
				this.root.findName("SB_Candidate_"+String(myCandidateIndex)+"__Hide").Begin();
			}
		} else {
			if(myCandidate["hidden"]) {
				myCandidate["hidden"] = false;
				this.root.findName("SB_Candidate_"+String(myCandidateIndex)+"__Show").Begin();
			}
		}
		
		setTimeout("CandidateFilterObject.filterNextCandidate();");
		//this.filterNextCandidate();
	},

	/**************************************************
		TOOLTIP
	**************************************************/
	displayToolTip: function(msg) {
		this.root.findName("ToolTipText").Text = msg;
		this.ToolTip.findName("ToolTipFrame").Height = Math.ceil(this.ToolTip.findName("ToolTipText").ActualHeight)+10;
		this.ToolTip.findName("ToolTipFrame").Width = Math.ceil(this.ToolTip.findName("ToolTipText").ActualWidth)+10;
		this.root.findName("SB_ToolTip_Hide").Stop();
		this.root.findName("SB_ToolTip_Show").Begin();
		this.correctToolTip();
	},
	hideToolTip: function() {
		this.root.findName("SB_ToolTip_Show").Stop();
		this.root.findName("SB_ToolTip_Hide").Begin();
	},
	correctToolTip: function() {
		var xpos = this.xMouse+15;
		if(xpos+this.root.findName("ToolTipFrame").Width>980) {
			xpos = this.xMouse-this.root.findName("ToolTipFrame").Width-5;
		}
		this.ToolTip["Canvas.Left"] = xpos;
		this.ToolTip["Canvas.Top"] = this.yMouse+15;
	}, 	
	

	/**************************************************
		MOUSE INTERACTION
	**************************************************/

	onResetIssuesButtonMouseDown: function(sender, eventArgs) 
	{
		this.resetIssues();
	},
	onClickBlockerMouseDown: function(sender, eventArgs) 
	{
		this.root.findName("SB_ClickBlocker_Hide").Begin();	
		this.root.findName("SB_DetailPanel_Hide").Begin();	
	},
	onDetailPanelCloseButtonMouseDown: function(sender, eventArgs) 
	{
		this.root.findName("SB_ClickBlocker_Hide").Begin();	
		this.root.findName("SB_DetailPanel_Hide").Begin();	
	},
	onDescPanelCloseButtonMouseDown: function(sender, eventArgs) 
	{
		this.root.findName("SB_ClickBlocker_Hide").Begin();	
		this.root.findName("SB_DescPanel_Hide").Begin();	
	},
	
	onLogoHotspotMouseDown: function(sender, eventArgs) {
		this.onLinkRequest("http://www.352media.com/");
	},
	onLogoHotspotRollover: function(sender, eventArgs) {
		this.root.findName("Image_Logo").Opacity = 1;	
	},
	onLogoHotspotRollout: function(sender, eventArgs) {
		this.root.findName("Image_Logo").Opacity = 0.7;	
	},
	
	onDetailLinkMouseDown: function(sender, eventArgs)
	{
		this.onLinkRequest("http://www."+this.SelectedCandidate["url"]+"/");
	},
	onDetailLinkRollover: function(sender, eventArgs)
	{
		this.root.findName("SB_DetailPanel_Link_Rollover").Begin();
	},
	onDetailLinkRollout: function(sender, eventArgs)
	{
		this.root.findName("SB_DetailPanel_Link_Rollout").Begin();
	},
	
	onClickBlockerHideComplete: function(sender, eventArgs) 
	{
		this.root.findName("ClickBlocker").Visibility = "Collapsed";
	},
	
	onDetailPanelHideComplete: function(sender, eventArgs) 
	{
		this.root.findName("DetailPanel").Visibility = "Collapsed";
	},
	
	onDescPanelHideComplete: function(sender, eventArgs) 
	{
		this.root.findName("DescPanel").Visibility = "Collapsed";
	},
	
	//CANDIDATE INTERACTION
	onCandidateMouseEnter: function(sender, eventArgs) 
	{
		this.root.findName("SB_"+sender.Name+"_Rollover").Begin();
		this.displayToolTip("More info on "+this.root.findName(sender.Name+"FullName").Text);
	},
	onCandidateMouseLeave: function(sender, eventArgs) 
	{
		this.root.findName("SB_"+sender.Name+"_Rollout").Begin();
		this.hideToolTip();
	},
	onCandidateMouseDown: function(sender, eventArgs) 
	{
		
		var myCandidateIndex = Number(sender.Name.substr(10,sender.Name.length-11));
		
		var myCandidate = this.candidates[myCandidateIndex];

		this.selectCandidate(myCandidate);
	},	
	
	//ISSUE INTERACTION
	onIssueNameMouseDown: function(sender, eventArgs) 
	{
		var myIssueIndex = sender.Name.split("_")[1];
		this.selectIssue(myIssueIndex);
	},

	//STANCE INTERACTION
	onStanceMouseDown: function(sender, eventArgs) 
	{
		var myIssueIndex = sender.Name.split("_")[1];
		var myStanceIndex = sender.Name.split("_")[3];
		this.selectIssueStance(myIssueIndex, myStanceIndex);	
	},
	onStanceMouseEnter: function(sender, eventArgs) 
	{
		this.displayToolTip(this.root.findName(sender.Name+"Name").Text);
	},
	onStanceMouseLeave: function(sender, eventArgs) 
	{
		this.hideToolTip();
	},
	
	//TOOLTIP FOLLOW
	onMouseMove: function(sender, eventArgs) 
	{
		this.xMouse = eventArgs.getPosition(null).x;
		this.yMouse = eventArgs.getPosition(null).y;
		this.correctToolTip();
	},



	/**************************************************
		UTILITIES
	**************************************************/
	getXMLDoc: function(url, callBack, xthis) {
		if (document.implementation && document.implementation.createDocument)
		{
			var xmldoc = document.implementation.createDocument("", "", null);
			xmldoc.onload = function() {
					callBack(this, xthis);
			}
		}
		else if (window.ActiveXObject)
		{
			var xmldoc = new ActiveXObject("Microsoft.XMLDOM");
			xmldoc.onreadystatechange = function () {
				if (xmldoc.readyState == 4) {
					callBack(xmldoc, xthis);
				}
			};
	 	}
		else
		{
			alert('Your browser can\'t handle this script.');
			return;
		}
		xmldoc.load(url);
	},

	getXMLDocSource: function(url, callBack, xthis) {
		if (this.BrowserFamily=="Mozilla")
		{
			var xmldoc = document.implementation.createDocument("", "", null);
			xmldoc.onload = function() {
					callBack(xthis.getSourceFromXMLDoc(this), xthis);
			}
		}
		else if (this.BrowserFamily=="IE")
		{
			var xmldoc = new ActiveXObject("Microsoft.XMLDOM");
			xmldoc.onreadystatechange = function () {
				if (xmldoc.readyState == 4) {
					callBack(xthis.getSourceFromXMLDoc(xmldoc), xthis);
				}
			};
	 	}
		else
		{
			alert('Your browser can\'t handle this script.');
			return;
		}
		xmldoc.load(url);
	},
	
	getSourceFromXMLDoc: function(xmlDoc) {
		if (xmlDoc.xml == null)
		{
			var serializer = new XMLSerializer();
			var xml = serializer.serializeToString(xmlDoc);
		}
		else
		{
			var xml = xmlDoc.xml;
		}
		return xml;
	}, 
	
	centerText: function(textObject) {
		textObject["Canvas.Left"] += Math.round((textObject.Width - textObject.ActualWidth)/2);
		textObject.Width = Math.ceil(textObject.ActualWidth);
	},
	
	rightAlignText: function(textObject) {
		textObject["Canvas.Left"] += Math.round(textObject.Width - textObject.ActualWidth);
		textObject.Width = Math.ceil(textObject.ActualWidth);
	}
}