var firstName=0;
var lastName=1;
var fullOffice=2;
var abbrOffice=3;
var party=4;
var english=5;
var spanish=6;
var chinese=7;
var korean=8;
var files=9;
var pdfLocation="../../downloads/pdf/";
var videoLocation="../../media/video/";

candidates=candidates.sort(sortCandidates);

var theOffice="";

document.write('<table cellspacing="0" cellpadding="0" border="0" width="369">');
for(var i=0;i<candidates.length;i++){
	if(candidates[i][fullOffice]!=theOffice){
		theOffice=candidates[i][fullOffice];
		writeOffice();
	}
	var candidateName=candidates[i][firstName]+' '+candidates[i][lastName];
	if(candidates[i][party].length>0){
		candidateName=candidateName+' ('+candidates[i][party]+')';
	}
	document.write('<tr><td class="candidate_name">'+candidateName+'</td></tr>');
	if(candidates[i][english]){
		document.write('<tr><td><img src="../../includes/site_images/misc/arrow.gif" width="12" height="9" border="0" alt="">Watch the video in <a class="candidate_link" href="'+videoLocation+candidates[i][files][1]+'">low</a> or <a class="candidate_link" href="'+videoLocation+candidates[i][files][2]+'">high</a> bandwidth</td></tr>');
		document.write('<tr><td><img src="../../includes/site_images/misc/arrow.gif" width="12" height="9" border="0" alt="">Transcription: <a class="candidate_link" href="'+pdfLocation+candidates[i][files][0]+'">English</a>');
		var fileIndex=3;
		if(candidates[i][spanish]){
			document.write(' | <a class="candidate_link" href="'+pdfLocation+candidates[i][files][fileIndex]+'">En espa&ntilde;ol</a>');
			fileIndex++;
		}
		if(candidates[i][chinese]){
			document.write(' | <a class="candidate_link" href="'+pdfLocation+candidates[i][files][fileIndex]+'">&#20013;&#25991;</a>');
			fileIndex++;
		}
		if(candidates[i][korean]){
			document.write(' | <a class="candidate_link" href="'+pdfLocation+candidates[i][files][fileIndex]+'">&#54620;&#44544;</a>');
			fileIndex++;
		}
	}else{
		document.write('<tr><td>Did not record a statement</td></tr>');
	}
	document.write('<tr><Td><img src="../../includes/site_images/spacers/spacer.gif" width="369" height="10" border="0" alt=""></Td></tr>');
	if(i<candidates.length-1){
		if(candidates[i+1][fullOffice]!=theOffice){
			document.write('<tr><Td><img src="../../includes/site_images/spacers/spacer.gif" width="369" height="20" border="0" alt=""></Td></tr>');
		}
	}
}
document.write('</table>');

function writeOffice(){
	document.write('<tr><td class="office">'+candidates[i][fullOffice]+'</td></tr>');
	document.write('<tr><Td class="dots"><img src="../../includes/site_images/spacers/spacer.gif" width="369" height="1" border="0" alt=""></Td></tr>');	
}

function sortCandidates(obj1,obj2){
	var first=0;
	var second=0;
	if(obj1[fullOffice].toLowerCase().indexOf("mayor")>-1){
		first=100;
	}
	if(obj2[fullOffice].toLowerCase().indexOf("mayor")>-1){
		second=100;
	}
	if(obj1[fullOffice].toLowerCase().indexOf("comptroller")>-1){
		first=90;
	}
	if(obj2[fullOffice].toLowerCase().indexOf("comptroller")>-1){
		second=90;
	}
	if(obj1[fullOffice].toLowerCase().indexOf("advocate")>-1){
		first=80;
	}
	if(obj2[fullOffice].toLowerCase().indexOf("advocate")>-1){
		second=80;
	}
	if(obj1[fullOffice].toLowerCase().indexOf("borough president")>-1){
		first=70;
	}
	if(obj2[fullOffice].toLowerCase().indexOf("borough president")>-1){
		second=70;
	}
	if(obj1[fullOffice].toLowerCase().indexOf("council")>-1){
		first=60;
	}
	if(obj2[fullOffice].toLowerCase().indexOf("council")>-1){
		second=60;
	}
	if(first>0&&second>0&&first!=second){
		return(second-first);
	}else{
		if(obj1[fullOffice].toLowerCase().indexOf("council")>-1&&obj1[fullOffice].toLowerCase().indexOf("council")>-1){
			var office1=parseInt(obj1[fullOffice].substring(22));
			var office2=parseInt(obj2[fullOffice].substring(22));
			var comp=office1-office2;
			if(comp!=0){
				return(comp)
			}else{
				if(obj1[party].toLowerCase()>obj2[party].toLowerCase()){
					return(1);
				}else if(obj1[party].toLowerCase()<obj2[party].toLowerCase()){
					return(-1);
				}else{
					if(obj1[lastName].toLowerCase()>obj2[lastName].toLowerCase()){
						return(1);
					}else if(obj1[lastName].toLowerCase()<obj2[lastName].toLowerCase()){
						return(-1);
					}else{
						return(0);
					}
				}
			}
		}else{
			if(obj1[fullOffice].toLowerCase()>obj2[fullOffice].toLowerCase()){
				return(1);
			}else if(obj1[fullOffice].toLowerCase()<obj2[fullOffice].toLowerCase()){
				return(-1)
			}else{
				if(obj1[party].toLowerCase()>obj2[party].toLowerCase()){
					return(1);
				}else if(obj1[party].toLowerCase()<obj2[party].toLowerCase()){
					return(-1);
				}else{
					if(obj1[lastName].toLowerCase()>obj2[lastName].toLowerCase()){
						return(1);
					}else if(obj1[lastName].toLowerCase()<obj2[lastName].toLowerCase()){
						return(-1);
					}else{
						return(0);
					}
				}
			}
		}
	}
}
