//global variables
var extended_news_art='';

///////////////////////////////////////////////
// general functions
//////////////////////////////////////////////

// used to extract all GET variables ('?' and '&') and any anchors('#') in the URL
function parseURL(url_text)
{
	var ret={};
	var value=url_text.match(/\w+=\w+(?=&)|\w+=\w+|#\w+$/g);
	for (var val in value){
		if(/\d/.test(val)){ //IE fix to remove the 'input', 'index' and 'lastIndex' array key values
			ret[value[val].match(/\w+(?==)|#/).toString()]=value[val].match(/\w+$/).toString();
		}
	}
	return ret;
}

///////////////////////////////////////////////
// news functions
//////////////////////////////////////////////
function news_display(url_text)
{
	var article;
	var url_parameters = parseURL(url_text);
	if(url_parameters.art=='undefined'){
		article='';
	}else{
		article=url_parameters.art;
	}

	// leave only the introduction parts of the news items displayed
	$('.news_extended').hide();
	// display the ('...more)' links for all news items in this section
	$('.newslinks').show();
	$('.news_open img').show();

	// grab the article number which is stored in the URL anchor from the URL and display the rest of that article
	var id='#art'+article;
	var extended=id+'_extended';
	var close_button=id+'_close';

	// hide the extended article
	if(extended_news_art==article){
		$(extended_news_art).hide();
		$(close_button).hide();
		// show the '...more' link at the bottom of this specfic article
		var id_more='#art'+article+'_more';
		$(id_more).show();
		var id_hellip='#art'+article+'_hellip';
		$(id_hellip).show();
		// for monitoring purposes 
		extended_news_art='';
	}else{ // show the extended article
		$(extended).css('display','inline');
		$(close_button).show();
		// hide the '...more' link at the bottom of this specfic article
		var id_more='#art'+article+'_more';
		$(id_more).hide();
		var id_hellip='#art'+article+'_hellip';
		$(id_hellip).hide();
		// for monitoring purposes 
		extended_news_art=article;
	}
	
	// stop default behaviour
	return false;
}
///////////////////////////////////////////////
// managment functions
//////////////////////////////////////////////
function management_display()
{
	// leave only the introduction parts displayed
	$('.mgnt_extended').hide();
	// display the ('...more)' & close button links for all items in this section
	$('.mgntlinks').show();
	$('.mgnt_close').show();
	
	// stop default behaviour
	return false;
}


// wait till the DOM is loaded
$(document).ready(function(){

	///////////////////////////////////////////////
	// external links
	//////////////////////////////////////////////
	$('.external_link').click(
		function(){
			return !window.open(this.href);
	});


	///////////////////////////////////////////////
	// news
	//////////////////////////////////////////////
	if($('input[name:js_location]').val()==='news'){

		// set the default display
		news_display(''+window.location);
		
		// add a click handler to capture any further clicks on the '...more' links so we can just show/hide without reloading the page
		$('.newslinks').click(
			function(){
				// hide all close buttons
				$('.news_close').hide();
				news_display(''+this.href);
				
				// stop default behaviour
				return false;
		});

		$('.news_close').click(
			function(){
				// get the article id to identify which section we need to hide
				var id=$(this).attr('id');
				var article=id.split('_close');
				// hide the extended content
				var extended='#'+article[0]+'_extended';
				$(extended).hide();
				// show this articles ...more link
				var more='#'+article[0]+'_more';
				$(more).show();
				var id_hellip='#'+article[0]+'_hellip';
				$(id_hellip).show();
				//hide the close button
				var close='#'+id;
				$(close).hide();
				// for monitoring purposes 
				extended_news_art='';

				// stop default behaviour
				return false;
		});
		
		$('.news_open').click(
			function(){
				// hide all close buttons
				$('.news_close').hide();
				news_display(''+this.href);
				
				// stop default behaviour
				return false;
			});
	}

	///////////////////////////////////////////////
	// management
	//////////////////////////////////////////////
	if($('input[name:js_location]').val()==='management'){
		
		// set the default display
		management_display();

		$('.mgntlinks').click(
			function(){
				// hide all extended content
				$('.mgnt_extended').hide();
				// show all mgnt_more links
				$('.mgntlinks').show();
				// get the persons name to identify which section we need to display
				var id=$(this).attr('id');
				var name=id.split('_more');
				// hide the more link
				var more='#'+id;
				$(more).hide();
				// leave only the introduction parts displayed
				var extended='#'+name[0]+'_extended';
				$(extended).show();
				// stop default behaviour
				return false;
		});

		$('.mgnt_close').click(
			function(){
				// get the persons name to identify which section we need to hide
				var id=$(this).attr('id');
				var name=id.split('_close');
				// hide the extended content for this person
				var extended='#'+name[0]+'_extended';
				$(extended).hide();
				// show all mgnt_more links
				$('.mgntlinks').show();
				
				// stop default behaviour
				return false;
		});
	}
	
	// stop default behaviour
	return false;
});

