/**
 * $Id: ipieca.js 2415 2010-04-21 10:31:27Z xlcdev $
 * 
 * Contains supporting functions for the IPIECA website.
 */

/**
 * Script to support the download of alternate language versions of a publication. 
 * The languages for a publication are offered for download in a select box.
 * The select value contains the url of the download. On selection this is passed
 * to this function which performs the download using the containing form.
 * The first option in the select invites the user to choose a download. The 
 * script will ignore this option when selected.
 * @param theFormId the element id of the containing form
 * @param theSelectId the element id of the select box
 */
function libraryDownloadFile(theFormId, theSelectId) {
    var theForm = document.getElementById(theFormId);
    var theLanguage = document.getElementById(theSelectId);
    if (theLanguage.value != "#") {
        theForm.action = theLanguage.value;
        theForm.submit();
        return true;
    }
    else { 
        return false;    
    }
}

/**
 * Script to support the dynamic display of the gallery of images for focus
 * areas on the home page. 
 * <p>
 * Depends on jQuery.
 */
$(document).ready(function(){
	// reset all tabs on start
	$('.tab-set').find('a.tab').each(function(){
		$jQueryTabId = getjQueryGalleryTabIdforAnchor(this);
		if($(this).is('.active')){
			$($jQueryTabId).show();	
		} else {
			$($jQueryTabId).hide();
		}
	});
	$('.tab-set').find('a.tab').mouseover(function(){				
		$(this).parent().parent().find('a.tab').each(function(){
			$(this).removeClass('active');
			$jQueryTabId = getjQueryGalleryTabIdforAnchor(this);
			$($jQueryTabId).hide();			
		});
		$(this).addClass('active'); 
		$jQueryTabId = getjQueryGalleryTabIdforAnchor(this);		
		$($jQueryTabId).show();					
		return false;
	});	
});

/** 
 * Derives the jQuery id of one of tabs that is used to implement the gallery of 
 * focus area images on the front page from it's associated supplied anchor 
 * element.     
 */
function getjQueryGalleryTabIdforAnchor(anchorElement){
	// The tab id is derived by stripping of a prefix that is expected to be 
	// present on all gallery anchor elements
	var tabId = anchorElement.id.replace('anchor-','');
	// The jQuery id is the DOM element id prefixed by '#'.
	return '#'+tabId;	
}

/**
 * Wrap a span tag around the text of the primary navigation links (anchors), 
 * identified by their style class. This is required in order to style the
 * text of the links appropriately.
 * <p>
 * This script based solution (workaround) has been adopted because in Drupal 
 * 6.x there isn't currently an easy or nice way of theming the primary menu 
 * links on which the site's primary navigation is based.
 * <p>
 * This solution has been chosen over the following non-script, alternatives:
 * <p>
 * 1. Override Drupal theme function theme_links() - This appears to be the most 
 * commonly adopted solution in Drupal 6.x. It entails copying the whole of 
 * Drupal's theme.inc::theme_links() method to template.php, and customising it 
 * to add the span tags. Not particularly nice.
 * <p>
 * 2. Overriding Drupal theme function theme_menu_item_link() - This theme
 * function allows you to theme (change the markup of) Drupal's menu items.
 * The Zen theme uses it to add span tags around local menu items. This looks
 * a good solution, however it doesn't work, because Drupal doesn't appear to 
 * use the function to theme all menu links and specifically not primary menu 
 * links, even after forcing the menus to be re-built.   
 * <p>
 * Refs. 
 * http://adaptivethemes.com/add-span-tags-to-primary-links-for-sliding-door-tabs
 * http://agaric.com/note/add-a-span-wrapper-inside-primary-and-secondary-menu-links-drupal-6
 * http://drupal.org/node/221382
 * <p>
 * Depends on jQuery.
 */
$(document).ready(function(){
  $('.nav li a').wrapInner("<span>" + "</span>");
});
