/*
Site:          partnersworldwide.org
File:          /assets/js/pww.js
Last Modified: Aug 02 2010
===========================================================
masugadesign.com | Copyright 2010
=========================================================== */
$(window).load(function() {
	
	// Front Page
	// Feature
	$('#slides').cycle({speed: 2500,fx:'fade',timeout:10000,pager:'div.switcher'});
	
	// // Equal Height Columns
	// // Variables
	var maxHeight = 0;
	var featureHeight = 368;
	var leftColHeight = $('#content.index #newsAndEvents').height();
	var rightColHeight = $('#content.index #makeADifference').height();
	var sidebarHeight = $('#content.index #sidebar').height();
	
	// Calculate max height
	maxHeight = sidebarHeight;
	if(leftColHeight + featureHeight > maxHeight){
		maxHeight = leftColHeight + featureHeight;
	}
	if(rightColHeight + featureHeight > maxHeight){
		maxHeight = rightColHeight + featureHeight;
	}
	//alert(maxHeight);
	
	// Set heights
	// 18px comes from the feature box.  52px comes from the header height of newsAndEvents.
	$('#content.index #sidebar').height(maxHeight);
	$('#content.index #newsAndEvents ul.rows').height(maxHeight - featureHeight + 18 - 52);
	$('#content.index #makeADifference').height(maxHeight - featureHeight + 18);
});

$(document).ready(function() {	
// Where We Work JS
	// Initial Setup
	$("img.overlay").hide();
	$('.region-info').hide();
	
	// temp
	//$('#west-africa-div').show();
	
	$('#main #flatback').hide();
	

	// Affiiate Map
	// ==============================================================================================
	// The behavior here is tricky, because we have a mouseout and a click on a single
	// element doing two different things (showing or hiding an element). We need to bind/unbind here.
	// See: http://stackoverflow.com/questions/805133/how-do-i-unbind-hover-in-jquery/805150#805150

	function mOver()
	{
		var element = $(this).attr("href");
		$(element).show();
	}
	function mOut()
	{
		var element = $(this).attr("href");
		$(element).hide();
	}

	// explicitly bind so we can unbind
	$("a.map-button").bind('mouseover', mOver).bind('mouseout', mOut);

	$("a.map-button").click(function(e) {
		e.preventDefault();

		var relImg = $(this).attr("href");
		var relDiv = $(this).attr("href")+"-div";

		$(relImg).show();
		$(relDiv).show();
		
		var theheight = $(relDiv).height();
		var topPad = Math.floor((620 - theheight)/2);
		$(relDiv).css("marginTop", topPad);
		
		$("#main #flatback").show();
		// unbind (mouseout) here so we can keep the country lit up
		$(this).unbind('mouseover', mOver).unbind('mouseout', mOut);
	});

	$(".region-info a.close").click(function() {

		var relImg = $(this).attr("href");

		$('#main #flatback').hide();
		$(this).parent().parent().hide();
		$("img.overlay").hide();

		// re-bind the initial behaviors now that the pop-up is gone
		$("a.map-button").bind('mouseover', mOver).bind('mouseout', mOut);

		return false;
	});


	// Who We Are / Meet The Staff
	// ==============================================================================================
	$(".item-full").hide();
	$('a.readmore').each(function() {
		var lHref = $(this).attr('href');
		$(this).click(function(){
			var lText = $(this).text();
			$(this).text(lText == "READ MORE" ? "READ LESS" : "READ MORE");
				$(lHref).toggle();
				return false;
		}); 
	});

	
	// FAQ
	// Initial Setup
	$("div.faq-answer").hide();
	$("div.faq-question").hide();
	var visible = "";
	
	
	// Actions
	// Clicking category shows questions
	$("div.faq h2 a").click(function(event){
		event.preventDefault();
		if($(this).parent().nextUntil("h2").is(":visible")){
			$(this).parent().nextUntil("h2").hide();
		}
		else{
			$(this).parent().nextUntil("h2").show();
			$(this).parent().nextUntil("h2").children("div").hide();
		}
	});
	
	// Clicking question shows answers
	$("div.faq-question h3 a").click(function(event){
		event.preventDefault();
		if($(this).parent().next("div").is(":visible")){
			$(this).parent().next("div").hide();
		}
		else{
			$(this).parent().next("div").show();
		}
	});


	// Email sign up field
	var emailtxt = 'Sign-up for newsletter';
	var emailField = $("#newsletterSignup #email");
	emailField.attr("value", emailtxt); 
	emailField.focus( function(){if(emailField.val() == emailtxt){emailField.attr("value", '');}});
	emailField.blur( function(){if(emailField.val() == ''){emailField.attr("value", emailtxt); }});
	
	// Stories search box
	var searchText = 'Search (Optional)';
	var searchField = $("#story-search #keywords");
	if(searchField.val() == ''){searchField.attr("value", searchText);}
	searchField.focus( function(){if(searchField.val() == searchText){searchField.attr("value", '');}});
	searchField.blur( function(){if(searchField.val() == ''){searchField.attr("value", searchText); }});
	$("#story-search #submit").click(function(){if(searchField.val() == searchText){searchField.attr("value", '');}});

	// Affiliates Gallery Images
	$("a.gallery").fancybox({
			
		});

});
