// $('#worldmap').attr('src','/wp-content/themes/internationalaid/images/map/transparent.gif');
// $('area').mouseover(function() {
// 	region = this.id;
// 	$('#worldmap').attr('class','transparent');
// 	$('#worldmap').addClass(region);
// 	return false;
// });

function fancySearchLabel(){
	$('#s').each(function(e){
		if (this.type=="text"){
			this.value = $("#"+this.id+"-label").get(0).innerHTML;
			$("#"+this.id+"-label").hide();
			$(this).focus(function(e){
				this.value="";
			})
		}
	});
}

function enableStoryNav(){
	$('#storynav a.next').click(function(e){
		e.preventDefault();
		switchStory('next');
	});
	$('#storynav a.previous').click(function(e){
		e.preventDefault();
		switchStory('prev');
	});
}

function switchStory(direction){
	$('#stories .active').fadeOut('slow');
	if (direction == 'next'){
		$('#stories .next').fadeIn('slow', function(e){
		});
		findNextAndPrevious($('#stories .next').get(0).id);
	} else {
		$('#stories .previous').fadeIn('slow', function(e){
		});
		findNextAndPrevious($('#stories .previous').get(0).id);
		
	}
}

function findNextAndPrevious(currentId){
	$('#stories .story').removeClass('current').removeClass('next').removeClass('previous');
	var activeStories = $('#stories .story');
	var lastStory = activeStories.length-1;
	activeStories.each(function(e){
		if(this.id == currentId){
			switch(e){
				case 0:
				nextStory = activeStories.get(1);
				previousStory = activeStories.get(lastStory);
				break;
				
				case lastStory:
				nextStory = activeStories.get(0);
				previousStory = activeStories.get(e-1);
				break;
				
				default:
				nextStory = activeStories.get(e+1);
				previousStory = activeStories.get(e-1);
				break;
			}
		}
	});
	$('#'+currentId).addClass('current');
	$(nextStory).addClass('next');
	$(previousStory).addClass('previous');
}

fancySearchLabel();

if ($('#stories .story').size() > 0) {
	$('#stories .active').addClass('current');
	$('#stories .story:not(.active)').attr('style','display: none').addClass('active');
	findNextAndPrevious($('#stories .current').get(0).id);
	enableStoryNav();
}