var currentSection = 1;
function evalSection(section){	
	switch(section){
		case "next":
			if (currentSection >= 10) { return 1; } else { return parseInt(currentSection)+1; }
			break;
		case "previous":
			if (currentSection <= 1) { return 10; } else { return parseInt(currentSection)-1; };
			break;
		default:
			return section;
	}
}

function sectionLinks(){
	$('#next-section').click(function(e){
		e.preventDefault();
		currentSection = evalSection('next');
		$('.section:not(#section'+currentSection+'), ul.martyrs:not(#section'+currentSection+'-list)').fadeOut(1000, function(i){
			$('#section'+currentSection+', #section'+currentSection+'-list').fadeIn(1000);
		});
		return false;
	});
	$('#previous-section').click(function(e){
		e.preventDefault();
		currentSection = evalSection('previous');
		$('.section:not(#section'+currentSection+'), ul.martyrs:not(#section'+currentSection+'-list)').fadeOut(1000, function(i){
			$('#section'+currentSection+', #section'+currentSection+'-list').fadeIn(1000);
		});	
		return false;
	});
}

function setResultsListeners(){
	$('#search-results a').click(function(e){
		e.preventDefault();
		myTarget = this.id.split('-')[1];
		currentSection = evalSection(myTarget);
		$('.section:not(#section'+currentSection+'), ul.martyrs:not(#section'+currentSection+'-list)').fadeOut(1000, function(i){
			$('#section'+currentSection+', #section'+currentSection+'-list').fadeIn(1000);
		});	
		return false;
	});
}

function clearResultsListeners(){
	$('#search-results a').unbind('click');
}

function martyrSearch(formname){
	clearResultsListeners();
	query = document.getElementById(formname).value;
	$('#search-results').load('/sponsor/martyrsearch.php',{search: query}, function(e){
		setResultsListeners();		
	});
}

$(document).ready(function(){
	if(Nifty){
		Nifty("div.column","same-height, none");
	}
	$('form:not(.search) input').each(function(e){
		if ((this.type=="text")&&($("#"+this.id+"-label").get(0))){
			$("#"+this.id+"-label").hide();
			if(this.className != 'stored') {
				this.value = $("#"+this.id+"-label").get(0).innerHTML;
				$(this).one('focus', function(e){
					this.className="stored";
					this.value="";
				})
			}
		}
	});
	$('#tile-text input').keyup(function(e){
		$('#'+this.id+'-prev').text(this.value);
	});
});
