/******************************
  Basic JS Functions for Eirgrid
 ******************************/

// Create Accordion
function createAccordion() {
	jQuery('#content-wrap').accordion ({ 
		header: 'h2', 
		alwaysOpen: false,
		autoHeight: false,
		active: '.selected',
		load: setSelectedContent()
	}).bind('accordionchange', function(event, ui) {
		setSelectedContent();
	});

	// Make the accordion keyboard accessible
	$("#content-wrap h2").keypress(
		function (e) {
			if (e.which == 13) {
				$('#content-wrap').accordion('activate', this);
			}
		}
	);
}

// Open and close the accordion
function accordionToggle() {
	// Set basic variables
	var expand   = 'Expand all sections';
	var contract = 'Collapse all sections';
	var toggle   = '<div id="accordion-toggle"><a href="#">'+expand+'</a></div>';
	
	// Add the button to the content wrap
	$('#content-wrap').prepend(toggle);
	
	// Set the toggle function on the button
	$('#accordion-toggle a').toggle(
		function() {
			enableAll();
			$(this).empty();
			$(this).html(contract);
			$(this).addClass('selected');
			$('#content-wrap h2').removeClass('head');
			$('#content-wrap h2').removeClass('selected');
		},
		function() {
			createAccordion();
			$(this).empty();
			$(this).html(expand);
			$(this).removeClass('selected');
			$('#content-wrap h2').addClass('head');
		}
	);
}

// Dynamically add a print page link
function printPage() {

	var printId = 'print-page';
	var printHTML = '<a href="#" id="'+printId+'"><img src="/images/common/print-icon.png" alt="Print this page" /> Print this page</a>';

	$('#controls').append(printHTML);
	$('#'+printId).click(
		function() {
			window.print();
			return false;
		}
	);
}

$(document).ready(function(){

	/******************************
      Create the expandable navigation
     ******************************/
  $('.expLink').each(function(i) 
    {
      var link = '#' + $(this).parent().attr('id');
      var label = $(this).html();
      $('#expNav').append('<li><a href="'+ link +'">'+ label +'</a></li>');
    });

	/******************************
      Accordion for Projects
     ******************************/
	jQuery('#accordion').accordion ({ 
		header: 'h2', 
		alwaysOpen: false,
		autoHeight: false
	});
	
	
	/******************************
      Add +/- to the H2 tags
     ******************************/
	$('#content-wrap h2').addClass('head');
	
	/******************************
      Make the accordion accessible
     ******************************/
	$("#accordion h2").keypress(
		function (e) {
			if (e.which == 13) {
				$('#accordion').accordion('activate', this);
			}
		}
	);
	
	/******************************
      Accordion for Content
     ******************************/
	createAccordion();
	
	/******************************
      Activate accordion based
	  on links clicked
     ******************************/
	 $('#section-nav li a').click(function () {
		// Activate the accordion as soon as the link is clicked
		// split the string from '#'
		url_array = $(this).attr("href").split('#');
		anchor = '#' + url_array[url_array.length -1];
		jQuery('#content-wrap').accordion ("activate", anchor);
		
		// Set the selected link
		setSelectedContent();
		
		// Make sure the screen doesn't jump when the link is clicked
		//return false;
	 });
	 
	 accordionToggle();
	
	/******************************
      Manages accorion appearances
     ******************************/
  $('#accordion li:last').addClass('last');
  $('#accordion li:first').addClass('first');
  $('#expNav li:last').css('background','none');
  $('#expNav li:last').css('padding-bottom','0');

	/******************************
      Searchbox Content
     ******************************/
	var searchboxContent = 'Search';
	var searchbox = '#search';
	
	$(searchbox).attr('value', searchboxContent);
	$(searchbox).focus(
		function() {
			if ($(searchbox).attr('value') == searchboxContent) {
				$(searchbox).attr('value', '');
			}
		}
	);
	$(searchbox).blur(
		function() {
			if ($(searchbox).attr('value') == '' || !$(searchbox).attr('value')) {
				$(searchbox).attr('value', searchboxContent);
			}
		}
	);
	
	/******************************
      Add a JavaScript pring page link
     ******************************/
	printPage()

	/******************************
     Highlight current project subsection
    ******************************/
	switch($('#self').attr('rel'))
	{
		case "Overview":
		case "Trosolwg":
		{
			$('#tb1 a').addClass('selected');
			break;
		}
		case "Health & Safety":
		case "Diogelwch":
		{
			$('#tb2 a').addClass('selected');
			break;
		}
		case "Environment":
		case "Amgylchedd":
		{
			$('#tb3 a').addClass('selected');
			break;
		}
		case "Benefits":
		case "Buddion":
		{
			$('#tb4 a').addClass('selected');
			break;
		}
		case "Project Activity":
		case "Digwyddiadau'r Prosiect":
		{
			$('#tb5 a').addClass('selected');
			break;
		}
		default:
		{
			$('#tb1 a').addClass('selected');
			break;
		}
	}
});

// Set selected sidenav
function setSelectedContent () {
	// Set the link to be selected when it's clicked
	var selectedId = $('#content-wrap h2.selected').attr('id');
	$('#section-nav li a').removeClass('selected');
	$("#section-nav li a[href$='#"+selectedId+"']").addClass('selected');
	$('#content-wrap div div a').removeAttr('style');
	return true;
}

// Destroy the accordion (enable all sections)
function enableAll() {
	$('#content-wrap').accordion("destroy");
}

