//**  Plan custom javascript **//
jq(function() 
{

	// fonts replacement rules
  jq('body').addClass('hasjs');
	Cufon('h1.documentFirstHeading')("div.planPortlet h1")("#homeright h3")("#news h3")("#campaigns h3")("#blogs h3")("div.countrylist h2")("div.resources div.story h3")("div.countrylinks h3")("div.countryintro h3")("div#donateprocess h3")(".blogListingEntry h2");
    Cufon("body.no div#mainimage h2")("body.no div#mainintro h2")("body.no div#news h2")("body.no div.feature h2")("body.no div#campaigns h2")("body.no div.countrylist h2")("h4.priorities")("div.appeal-block h3");
  
  // global tabs - due to limited support for li:hover in older IE versions  
	jq("#portal-globalnav li").hover(function(){jq(this).addClass("sfhover");},function(){jq(this).removeClass("sfhover");});
  // navigation tree - put in the 'first' and 'last' class on the ul. 
  jq("div#subnav ul:first>li:last").addClass("last");
   // campaigns portlet - put 'first' class to cut off top margin 
  jq("#portal-column-two div.visualPadding div:first").addClass("first");
  
  // page trackers
  jq('a').not("[@onclick]").each(function() {
  
    var a = jq(this);
    var href = a.attr('href');
   
    if (href && (href.match(/^http/)) && (!href.match(document.domain))) 
    {
      a.click(function() {
        href = href.replace('http://',''); // cleanup for nice GA reports
        pageTracker._trackPageview('/outgoing/' + href);
      });
    }
   
  }); 
  
	// set old index high for initial comparison 
	oldindex = 100; 
	j = 0;
  
	// what menu onclick event 
	jq("#whatmenu").find("ul").find("li").hover(function() 
  {							
    clearTimeout(j);
    thisindex = jq("#whatmenu").find("ul").find("li").index(this); 
    if (thisindex!=oldindex) 
    { 
      runTransition2(thisindex+1, thisindex+2, "whatmenu");  
      clearTimeout(t);
    }
  //restart the scroll so long as not over the images of the links  
  },function() 
  {
    if (jq("#whatmenu").find("div.action").mouseover()) clearTimeout(t);
    else if (jq("#whatmenu").find("ul").mouseover()) clearTimeout(t);
    else setRotationwhat(thisindex+1, jq("#whatmenu").find("ul").find("li").length); 
	});

	//pause the on mouse over the image 
	jq("#whatmenu").find("div.action").hover(function() 
  {
		pauseindex = pauseTimer(); 
	},function()
  { 
		setRotationwhat(pauseindex+1, jq("#whatmenu").find("ul").find("li").length); 
	});
	
	//pause on mouse over the menu  
	jq("#whatmenu").find("ul").hover(function() 
  {
		pauseindex = pauseTimer(); 
	},function()
  {
		setRotationwhat(pauseindex+1, jq("#whatmenu").find("ul").find("li").length); 
	});
	
	//where menu onclick event 
	jq("#wheremenu").find("ul:first").find("li").click(function(event) 
  {
		event.preventDefault(); 
		var thisindex = jq("#wheremenu").find("ul:first").find("li").index(this); 
		runTransition(thisindex+1, thisindex+2, "wheremenu");  
	});

	//trigger what and where menu rotaion events 
	setRotationwhat(1, jq("#whatmenu").find("ul").find("li").length); 


	// DONATION
	// show stage text 
	jq("#donateprocess").find("div#donate1").find("div.donateblock").find("p").attr("style", "");
	// hide the submit button
	jq("#donateprocess").find("div#donate1").find("div.donateblock").find("input").attr("style", "display:none");
	// call function to get select value text and val.. 
	jq("#donateprocess").find("div#donate1").find("#selectlist").change(onSelectChange);
  
  // confirmation for deleting items
  jq("input[name=folder_delete:method]").addClass('allowMultiSubmit');
  jq("input[name=folder_delete:method]").click(function() {return confirm('Do you really want to remove selected items?');});
    
});

// function to handle donations.... 
function onSelectChange()
{  
  var selected = jq("#selectlist option:selected"); 
  var linksType = jq("#selectlist").attr("name");  
	if(selected.val())
  { 
		var selval = selected.val();
    var seltracker = selected.attr('tracker');
		var seltext = selected.text();
    var a = jq("#donateprocess div#donate2 h4 a")
    a.attr("href", selval);
    a.html("Plan "+seltext+ " website &raquo;"); 
    a.bind('click', function(event) {pageTracker._trackPageview('/clickouts/'+linksType+'/'+seltext);});
		jq("#donateprocess").find("div#donate2").attr("style", "");
		jq("#donateprocess").find("div#donate1").addClass("inactive");
	}
}  

//what menu rotation function - timing is set in here 
function setRotationwhat(index, length) 
{
    t=setTimeout("changeStylewhat("+index+", "+length+")", 12000);
}

//what change function... triggered by rotation function 
function changeStylewhat(index, length) 
{
	next = index+1; 
	if(next>length) next=1; 
	runTransition(next, next+1, "whatmenu");  
	clearTimeout(t);
	setRotationwhat(next, length); 
}	 


//what menu transition function.. with a pause  
function runTransition2(thisindex, blockindex, block) 
{	
	jq("#"+block).find("ul:first").find("li").removeClass("active"); 
	jq("#"+block).find("ul:first").find("li:nth-child("+thisindex+")").addClass("active"); 	
	j=setTimeout("changeblock("+ thisindex +", "+ blockindex +")", 300); 
}

//image change for whatmenu block 
function changeblock(thisindex, blockindex)
{ 	
	jq("#whatmenu").find("div.action:nth-child("+blockindex+")").addClass("ontwo"); 
	jq("#whatmenu").find("div.ontop").fadeOut("slow", function() 
  { 
		jq("#whatmenu").find("div").removeClass("ontop"); 
		jq("#whatmenu").find("div").removeClass("ontwo");
		jq("#whatmenu").find("div.action:nth-child("+blockindex+")").addClass("ontop");
		jq("#whatmenu").find("div").fadeIn("fast"); 
	}); 
	oldindex = thisindex-1; 
}

//pause timer and return the previous element index so we can then continue ticking along 
function pauseTimer() 
{	
	clearTimeout(t);
	jq("#whatmenu").find("ul").find("li").each(function() 
  {
    if(jq(this).hasClass('active'))
      theindex = jq("#whatmenu").find("ul").find("li").index(this); 
	});
	return theindex
}

 //what / where menu transition function used throughout  
function runTransition(thisindex, blockindex, block) 
{
	jq("#"+block).find("ul:first").find("li").removeClass("active"); 
	jq("#"+block).find("ul:first").find("li:nth-child("+thisindex+")").addClass("active"); 	
	jq("#"+block).find("div.action:nth-child("+blockindex+")").addClass("ontwo"); 
	jq("#"+block).find("div.ontop").fadeOut("slow", function() 
  { 
		jq("#"+block).find("div").removeClass("ontop"); 
		jq("#"+block).find("div").removeClass("ontwo");
		jq("#"+block).find("div.action:nth-child("+blockindex+")").addClass("ontop");
		jq("#"+block).find("div").fadeIn("fast"); 
	});  
}