
// Show hide fct for POLL section
        function unhide(divID) {
  var item = document.getElementById(divID);
  if (item) {
    item.className=(item.className=='hidden')?'unhidden':'hidden';
  }
}
 
/*
	breadcrumbs.js
	
	Writes out a BreadCrumb trail. To use this script, add the following code
	to your HTML file at the position, you want to have the code for the breadcrumbs tail:
	
	<script type="text/javascript">doBCEndFwd();</script>
	
	  or
	
	<script type="text/javascript">doBCEndRev();</script>

	Version 1.2

	1. The links "European Commission > CORDIS" will be fixed, and the rest of the breadcrumbs will be
	   generated using this JavaScript.
	2. First we try to create the breadcrumbs based on the title of the page and the "path"
	   metadata tag. The title has to follow this format: "European Commission : CORDIS : service name
	   [: sub service name ]: page name"
	3. If the title does not follow the above format, the URL of the page will be used to
	   create the breadcrumbs by parsing the folder names and page base name. This URL can
	   be retrieved either from the page's URL (window.location) or the "path" + "basename"
	   metadata tags, or the "URL" metadata tag.
	4. Pages with title tag not following the format could be gradually corrected manually
	   by the person in charge of the service. New and revamped services will have the
	   correct title entered during development.
	5. 2 additional functions provide a feature to just render the breadcrumb tail (actually the page name).
	   use doBCEndFwd() for the old style page titles and doBCEndRev() for pages that use
	   the new title structure (' - '- separator and reversed order)
*/

// Returns a part of the path depending on the level parameter
//
// Arguments	path: 	String containing the path (must start and end with a /)
//              level: 	the path level to return

function getPathLevel(path,level) {
	var localPath = path.split('/');		// create an array with the path bits
	var pathLevel = localPath.slice(0,level);	// create a new array with elements from 0 to level

	return pathLevel.join('/');			// create a new string from the new array separated by '/'
}

// Returns information from the documents meta tags
//
// Arguments	metaName:	name of the meta tag to return the content from

function getMetaContent(metaName) {
	var metaInfo = document.getElementsByTagName('meta'); 	// create an array of all available meta tags
	var rc = '';		// default return value when meta name is not found

	for (var x = 0; x < metaInfo.length; x++) {
			if (metaInfo[x].getAttribute('name') == metaName) {
				rc = metaInfo[x].getAttribute('content');  // read the content attribute
				break; 		// found it, so return immediately
			}
	}
	return rc;
}

// String method to capitalize only the 1st characters of words - used when
// creating the breadcrumbs from the URL path only

String.prototype.properCase = function(){
	var strRet = '';
	var iTemp = this.length;

	if (iTemp == 0) return strRet;

	var UcaseNext = false;
	strRet += this.charAt(0).toUpperCase();

	for (var i = 1; i < iTemp ; i++){
		s = this.charAt(i);
		strRet += (UcaseNext==true?s.toUpperCase():s.toLowerCase());
		var iChar = this.charCodeAt(i);
		UcaseNext = (iChar==32||iChar==45||iChar==46);

		if (iChar == 99 || iChar == 67){
			if( this.charCodeAt(i-1) == 77 || this.charCodeAt(i-1) == 109 ){
				UcaseNext = true;
			}
		}
	}	return strRet;
}

// Return HTML code for the breadcrumb line.
//
// Arguments
//		elementId:  id of the container to add nodes
//		pageTitle:	Array of titles for each breadcrumb link
//                  has to start with elements 'European Commission' and 'CORDIS'
//		pagePath:   String containing the path to the current document
//                  starting (end ending) with a '/'
//

function composeBreadCrumbs(elementId, pageTitle , pagePath) {
	var rc = false;

	if (pageTitle.length > 2){

		for (i = 2; i < pageTitle.length - 1; i++) {

			// create new nodes
			var aNode = document.createElement('a');
			var liNode = document.createElement('li');

			// set src attribute for the hyperlink and add the Text
			aNode.href = getPathLevel(pagePath,i);
			aNode.appendChild(document.createTextNode(pageTitle[i]));

			// place link inside new li node and add the li node to the ul element
			liNode.appendChild(aNode);
			document.getElementById(elementId).appendChild(liNode);
		}

		// create final li element for the page name...
		var liNode = document.createElement('li');
		liNode.appendChild(document.createTextNode(pageTitle[pageTitle.length - 1]));
		// ... and add everything to the document
		document.getElementById(elementId).appendChild(liNode);
		return true;
	} else {
		return false;
	}
}

//	extract title and path infomration from title tag and meta
//	tag "path"
//
// 	Arguments
//		elementId:  id of the container to add nodes
//		mySep:      separator for splitting the page title
//		order:		in which order to parse the page title. Everything else than 'fwd' will reverse the order.

function createFromMeta(elementId,mySep,order){
	var pageTitle = document.title.split(mySep);
	var pagePath = getMetaContent('path');

	if ( (pageTitle.length > 2) && (pagePath != '')) {

		if(order != 'fwd') pageTitle.reverse(); 		// do we have to reverse the order of title elements?

		composeBreadCrumbs(elementId,pageTitle,pagePath);
		return true;
	} else {
		return false; 		// return false if something went wrong
	}
}

//	extract all information from the document URL path
//
// 	Arguments  elementId:  id of the container to add nodes

function createFromPath(elementId){
	var metaInfo = document.getElementsByTagName('meta');
	var baseName = getMetaContent('basename').properCase();
	var pagePath = window.location.pathname;
	var lastSlash = pagePath.lastIndexOf('/');

	if (lastSlash < (pagePath.length - 1)) {
		pagePath = pagePath.substr(0,lastSlash + 1);	// remove last slash from pagePath
	}

	var pageTitle = pagePath.split('/');

	pageTitle.splice(0,1,'European Commission','CORDIS'); 	// Add these to the title array for proper counting lateron

	for (var i in pageTitle){
		pageTitle[i] = pageTitle[i].properCase();	// Make words uppercase
	}

	// check if the last element of the title array is an empty element, then
	// either fill it or append a new one
	if (pageTitle[pageTitle.length-1] == ''){
		pageTitle[pageTitle.length-1] = baseName; 	// replace the last (empty) element with the basename
	} else {
		pageTitle.push(baseName);			// append basename
	}
 	composeBreadCrumbs(elementId,pageTitle,pagePath);
}

// small function to add the Page name to the end of the breadcrumb line.
// this function can be simply called like:
// 		doBreadCrumbsEnd();
// There are several optional arguments can be provided:
//  1. order:  		the order of the title elements (default: 'fwd' )
//  2. sep:			change the separator for title elements (default: ' : ')
//	3. elementId:	change the target id of the breadcrumb element (default: 'autobc')

function doBreadCrumbsEnd(){
	var order = (arguments[0])?arguments[0]:'fwd';
	var sep = (arguments[1])?arguments[1]:' : ';
	var elementId = (arguments[2])?arguments[2]:'autobc';

	var baseName = getMetaContent('basename').properCase();
	var pageTitleArr = document.title.split(sep);

	var pageTitle = '';

	if(pageTitleArr.length > 2){	// Can use the page title as information source?
		if(order == 'fwd'){			// First or last element to take?
			pageTitle = pageTitleArr[pageTitleArr.length - 1];
		} else {
			pageTitle = pageTitleArr[0];
		}
	} else {						// Use the basename information instead
		pageTitle = baseName;
	}

	// create final li element for the page name...
	var liNode = document.createElement('li');
	liNode.appendChild(document.createTextNode(pageTitle));
	// ... and add everything to the document
	document.getElementById(elementId).appendChild(liNode);
}

// main function for creating the breadcrumb line. Just place the function call
// at the location  where the HTML code for the breadcrumbs should go.
// One optional argument can be provided to alter the default separator (' : ') for
// the title tag
//
// Arguments  elementId:  id of the container to add nodes

function doBreadCrumbs(){
	var order = (arguments[0])?arguments[0]:'fwd';	// optional argument for title element order
	var sep = (arguments[1])?arguments[1]:' : ';	// optional argument for alternative separator
	var elementId = (arguments[2])?arguments[2]:'autobc';	// optional argument for alternative element ID
	var breadCrumbs = '';

	if (!createFromMeta(elementId,sep,order)) {		// 1st try to create from meta information
	//	alert('createFromMeta() failed!');
		createFromPath(elementId);			// otherwise, use window.location.pathname only
    }
}

// wrapper for doBreadCrumbsEnd('fwd')
function doBCEndFwd(){
	var sep = (arguments[0])?arguments[0]:' : ';	// optional argument for alternative separator
							// defaults to ' : ' (CORDIS old style titles)
	doBreadCrumbsEnd('fwd',sep);
}

// wrapper for doBreadCrumbsEnd('rev')
function doBCEndRev(){
	var sep = (arguments[0])?arguments[0]:' - ';	// optional argument for alternative separator
							// defaults to ' - ' (CORDIS new style titles)
	doBreadCrumbsEnd('rev',sep);
}
