﻿/******************************
  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');
			$('#content-wrap h2 a').removeAttr('style');

		},
		function() {
			createAccordion();
			$(this).empty();
			$(this).html(expand);
			$(this).removeClass('selected');
			$('#content-wrap h2').addClass('head');
		}
	);
}

$(document).ready(function(){

	/******************************
      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');

	/******************************
      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);
			}
		}
	);

	/*
	==============================================
		Fix the height in doormats
	==============================================
	*/	

	function fixDoormats()
	{
		$(".singledoormat").css("border-bottom","0");

		var max_height = 0;
		$(".singledoormat .middle").each
		(
			function (i)
			{
				if($(this).height() > max_height)
					max_height = $(this).height();
			}
		);
		$(".singledoormat .middle").height(max_height);
		max_height = 0;
		$(".doubledoormat .middle").each
		(
			function (i)
			{
				if($(this).height() > max_height)
					max_height = $(this).height();
			}
		);
		$(".doubledoormat .middle").height(max_height);
	}
	fixDoormats();


});

// 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');
	return true;
}

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


