/**
 * Call function populate map overlay
 */
//getSummary('Uganda');

jQuery.noConflict();
	
	/**
	 * Where we work - Country overlay
	 */
	function country_overlay(country) {
			
		// Write to console
		//console.debug('Flash has called: country_overlay('  + country + ')');
		// jQuery inside here
		(function($){
				  			
			/**
			 * Fade in the map-overlay
			 */
			//$('#map-overlay').fadeTo('slow','1').html('<h2>' + country + '</h2><a href="#" class="close">close</a>');
			
			if($.browser.msie){
				$('#map-overlay').show();
			}
			else{
				//$('#map-overlay').fadeIn('slow').html('<h2>' + country + '</h2><a href="#" class="close">close</a>');
			
				$('#map-overlay').fadeIn('slow');
			}
			
			/**
			 * On request close the map-overlay
			 */
			$('#map-overlay .close').click(function(e){
				e.preventDefault();
				$('#map-overlay').fadeOut('slow');
			});
			
		})(jQuery);
		
	}
	
	
jQuery(document).ready(function($){
	
	/**
	 * Get those tabs tabbing
	 */
	$("#tabs").tabs();
	
	
	/**
	 * Rounded corners in IE
	 */
/*	if($.browser.msie && $.browser.version < 9) {
		$('.more a, a.more').wrap('<div class="more-wrap" />').each(function(){
			$child = $(this);
			$(this).parent().css({
				width: $child.innerWidth(),
				height: $child.innerHeight(),
				float: 'right',
				background: 'url(templates/default/images/button-grad.png) repeat-x'
			}).corner('5px');
			$child.css({
				background: 'url(templates/default/images/button-arrow.png) no-repeat center right'
			});
		});
	}*/
	
	
	/**
	 * Replace <hr> with something styleable
	 */
	$('hr').replaceWith('<div class="hr"></div>');
	
	
	/**
	 * Lets play with the logo
	$("#return-home").hover(function(){
		$(this).clearQueue().animate({
			opacity: 1
		}, 1000);
	}, function(){
		$(this).animate({
			opacity: 0.8
		}, 500);
	});
	 */
	
	
	/**
	 * Input box pre fill
	 */
	(function(){
		// Set all input field default values according to alt attribute
		$("input[type='text']").each(function(){
			if($(this).attr("rel") == "prefill") {
				$(this).val($(this).attr("alt"));
			}
		});
		
		// Clear input field on focus, if still in default
		$("input[type='text']").focus(function() {
			if($(this).val() == $(this).attr("alt")) {
				$(this).val("");
			}
		});
		
		// If input field is empty afterward, add text again
		$("input[type='text']").blur(function() {
			if($(this).attr("rel") == "prefill" && $(this).val() == "") {
				$(this).val($(this).attr("alt"));
			}
		});
	})();
	
	
	
	/**
	 * Photo story fun..
	 */
	(function(){
		
		// Show only the first page
		$('.photo-story-page').hide().first().addClass('active-story').show();
		
		// Go to next page
		$('.photo-story-next').live('click', function(e) {
			e.preventDefault();
			current_page = $('.photo-story-page').index($('.active-story')) + 1;
			photo_story_go_to(current_page + 1);
		});
		
		// Go to prev page
		$('.photo-story-prev').live('click', function(e) {
			e.preventDefault();
			current_page = $('.photo-story-page').index($('.active-story')) + 1;
			photo_story_go_to(current_page - 1);
		});
		
		// Go to prev page
		$('.photo-story-image').live('click', function(e) {
			e.preventDefault();
			current_page = $('.photo-story-page').index($('.active-story')) + 1;
			if($('.active-story').next('.photo-story-page').length) {
				photo_story_go_to(current_page + 1);
			}
			else {
				photo_story_go_to(1);
			}
		});
		
	})();
	
	
	function photo_story_go_to(num) {
		//console.log('Going to page: ' + num);
		
		// Fade this story out
		$('.active-story').removeClass('active-story').fadeOut('normal', function() {
			// Fade in the new page
			$('.photo-story-page:nth-child(' + num + ')').addClass('active-story').fadeIn('normal');
			// What nav will we need?
			photo_story_controls(num);
			
		});
	}
	
	
	function photo_story_controls(num)
	{
		// If there is a previous page then show the prev button
		if($('.photo-story-page:nth-child(' + num + ')').prev('.photo-story-page').length) {
			$('.photo-story-prev').css('visibility', 'visible');
		}
		else {
			//console.log('First page');
			$('.photo-story-prev').css('visibility', 'hidden');
		}
		
		// If there is a next page then show the next button
		if($('.photo-story-page:nth-child(' + num + ')').next('.photo-story-page').length) {
			$('.photo-story-next').css('visibility', 'visible');
		}
		else {
			//console.log('Final page');
			$('.photo-story-next').css('visibility', 'hidden');
		}
	}
});
