var filter_letter='';
var filter_sponsor='';
var filter_design='';
var filter_borough='';
var sponsors_list=new Array();
var design_list=new Array();
var boroughs_list=["Bronx","Brooklyn","Manhattan","Queens","Staten Island"];
var spans=new Array();

function swapImage(letter){
	if(letter!=filter_letter){
		var theId="filter_"+letter;
		document.getElementById(theId).src="../../includes/site_images/panyc/"+letter+"_1.gif";
	}
}

function restoreImage(letter){
	if(letter!=filter_letter){
		var theId="filter_"+letter;
		document.getElementById(theId).src="../../includes/site_images/panyc/"+letter+"_0.gif";
	}
}

function filterThumbs(letter){
	var old_letter=filter_letter;
	filter_letter=letter;
	if(old_letter.length>0){
		restoreImage(old_letter);
	}
	writeThumbs();
}

function writeLinks(){
	var fileName=getFileName();
	for(var i=0;i<panyc_list.length;i++){
		if(panyc_list[i][0]==fileName){
			var previousLink='';
			var nextLink='';
			if(i==0){
				previousLink='<a href="../../html/panyc/'+panyc_list[panyc_list.length-1][0]+'">&laquo;&nbsp;PREVIOUS&nbsp;ARTIST</a>';
			}else{
				previousLink='<a href="../../html/panyc/'+panyc_list[i-1][0]+'">&laquo;&nbsp;PREVIOUS&nbsp;ARTIST</a>';
			}
			if(i==panyc_list.length){
				nextLink='<a href="../../html/panyc/'+panyc_list[0][0]+'">NEXT&nbsp;ARTIST&nbsp;&raquo;</a>';
			}else{
				nextLink='<a href="../../html/panyc/'+panyc_list[i+1][0]+'">NEXT&nbsp;ARTIST&nbsp;&raquo;</a>';
			}
			document.write('<table cellspacing="0" cellpadding="0" border="0" width="100%"><tr><td align="left" width="50%">'+previousLink+'</td><td align="right" width="50%">'+nextLink+'</td></tr></table>');
			break;
		}
	}
}

function getFileName(){
	var lastSlash = location.href.lastIndexOf("/")+1;
	return(location.href.substr(lastSlash));
}

function sponsorsContains(newEntry){
	for(var j=0;j<sponsors_list.length;j++){
		if(sponsors_list[j]==newEntry){
			return(true);
		}
	}
	return(false);
}

function designContains(newEntry){
	for(var j=0;j<design_list.length;j++){
		if(design_list[j]==newEntry){
			return(true);
		}
	}
	return(false);
}

function filterSponsor(theSponsor){
	filter_sponsor=theSponsor;
	writeThumbs();
}

function filterDesign(theDesign){
	filter_design=theDesign;
	writeThumbs();
}

function filterBorough(theBorough){
	filter_borough=theBorough;
	writeThumbs();
}

function getLetter(fullName){
	var names=fullName.split(" ");
	return(names[names.length-1].substring(0,1).toLowerCase());
}

function panycReset(){
	var oldLetter=filter_letter;
	filter_letter='';
	filter_sponsor='';
	filter_design='';
	filter_borough='';
	document.getElementById("sponsors").selectedIndex=0;
	document.getElementById("design").selectedIndex=0;
	document.getElementById("boroughs").selectedIndex=0;
	restoreImage(oldLetter);
	writeThumbs();
}

function writeDropdowns(){
	//create dropdown list
	for(var i=0;i<panyc_list.length;i++){
		if(!sponsorsContains(panyc_list[i][5])){
			sponsors_list[sponsors_list.length]=panyc_list[i][5];
		}
	}
	for(var i=0;i<panyc_list.length;i++){
		if(!designContains(panyc_list[i][6])){
			design_list[design_list.length]=panyc_list[i][6];
		}
	}
	sponsors_list=sponsors_list.sort();
	desgin_list=design_list.sort();
	document.write('<table cellspacing="0" cellpadding="0" border="0" width="499">');
	//start design agency dropdown
	document.write('<tr class="grey_bg"><td align="left" class="grey_bg1" width="134">Sponsor Agency:</td>');
	document.write('<td class="grey_bg2">');
	document.write('<select name="sponsors" id="sponsors" style="width:343px;" onchange=filterSponsor(this.options[this.selectedIndex].value)>');
	document.write('<option value="">All</option>');
	for(var i=0;i<sponsors_list.length;i++){
		document.write('<option value="'+sponsors_list[i]+'">'+sponsors_list[i]+'</option>');
	}
	document.write('</select>');
	document.write('</td></tr>');
	document.write('<tr><td colspan="2"><img src="../../includes/site_images/spacers/spacer.gif" width="499" height="5" alt=""></td></tr>');
	//start design sponsor dropdown
	document.write('<tr class="grey_bg"><td align="left" class="grey_bg1" width="134">Design Agency:</td>');
	document.write('<td class="grey_bg2">');
	document.write('<select name="design" id="design" style="width:343px;" onchange=filterDesign(this.options[this.selectedIndex].value)>');
	document.write('<option value="">All</option>');
	for(var i=0;i<desgin_list.length;i++){
		document.write('<option value="'+desgin_list[i]+'">'+desgin_list[i]+'</option>');
	}
	document.write('</select>');
	document.write('</td></tr>');
	document.write('<tr><td colspan="2"><img src="../../includes/site_images/spacers/spacer.gif" width="499" height="5" alt=""></td></tr>');
	//start design borough dropdown
	document.write('<tr><td align="left" class="grey_bg1">Borough:</td><td class="grey_bg2">');
	document.write('<select name="boroughs" id="boroughs" style="width:343px;" onchange=filterBorough(this.options[this.selectedIndex].value)>');
	document.write('<option value="">All</option>');
	for(var i=0;i<boroughs_list.length;i++){
		document.write('<option value="'+boroughs_list[i]+'">'+boroughs_list[i]+'</option>');
	}
	document.write('</select>');
	document.write('</td></tr>');
	document.write('<tr><td colspan="2"><img src="../../includes/site_images/spacers/spacer.gif" width="499" height="26" alt=""></td></tr></table>');
	document.write('<span id="thumbSpan"></span>');
}

function isValid(theIndex){
	var valid=true;
	if(filter_letter.length>0){
		valid=valid&&getLetter(panyc_list[theIndex][2])==filter_letter;
	}
	if(filter_sponsor.length>0){
		valid=valid&&panyc_list[theIndex][5]==filter_sponsor;
	}
	if(filter_design.length>0){
		valid=valid&&panyc_list[theIndex][6]==filter_design;
	}
	if(filter_borough.length>0){
		valid=valid&&panyc_list[theIndex][4]==filter_borough;
	}
	return(valid);
}

function getThumb(theIndex){
	var theFull=panyc_list[theIndex][1];
	if(theFull.length==0){
		return("none.gif");
	}
	var theFile=theFull.substring(0,theFull.length-4)+"_thumb"+theFull.substring(theFull.length-4);
	return(theFile);
}

function swapPage(thePage){
	for(var i=0;i<spans.length;i++){
		var theId="span_"+i;
		if(i+1==thePage){
			var theThumb="thumbs_"+i;
			document.getElementById(theThumb).innerHTML=spans[i];
			document.getElementById(theId).style.display="inline";
		}else{
			document.getElementById(theId).style.display="none";
		}
	}
}

function writeThumbs(){
	spans=new Array();
	var itemCount=0;
	var theString='<table cellspacing="0" cellpadding="0" border="0" width="499"><tr valign="top">';
	for(var i=0;i<panyc_list.length;i++){
		if(isValid(i)){
			itemCount++;
			theString=theString+'<td width="88"><a href="../../html/panyc/'+panyc_list[i][0]+'"><img src="../../images/panyc/'+getThumb(i)+'" width="88" height="87" alt="'+panyc_list[i][3]+'" border="0"><br>'+panyc_list[i][2]+'</a></td>';
			if(itemCount%5==0){
				theString=theString+'</tr>';
				if(itemCount%25==0){
					theString=theString+'</table>';
					spans[spans.length]=theString;
					theString='<table cellspacing="0" cellpadding="0" border="0" width="499"><tr valign="top">';
				}else{
					theString=theString+'<tr valign="top"><td colspan="9"><img src="../../includes/site_images/spacers/spacer.gif" width="499" height="20" alt=""></td></tr><tr valign="top">';
				}
			}else{
				theString=theString+'<td width="7"><img src="../../includes/site_images/spacers/spacer.gif" width="7" height="87" alt=""></td>';
			}
		}
	}
	if(theString.length>79&&theString.substring(theString.length-8)!="</table>"){
		theString=theString+'</table>';
		spans[spans.length]=theString;
	}
	theString="";
	if(spans.length==0){
		document.getElementById("thumbSpan").innerHTML='<span class="grey_text">No results found</span>';
	}else{
		for(var i=0;i<spans.length;i++){
			theString=theString+'<span id="span_'+i+'"><p align="right" class="grey_text">Page:';
			for(var j=0;j<spans.length;j++){
				if(j!=spans.length){
					theString=theString+'&nbsp;';
				}
				if(j==i){
					theString=theString+'<span class="red_text">'+(j+1)+'</span>';
				}else{
					theString=theString+'<a href=javascript:swapPage('+(j+1)+') class="grey_text">'+(j+1)+'</a>';
				}
			}
			theString=theString+'</p><span id="thumbs_'+i+'"></span></span>';
		}
		document.getElementById("thumbSpan").innerHTML=theString;
		swapPage(1);
	}
}