/*--------------------------------------------------------

	File:			common.js
	Version:		1.0
	Contents:		Common javascript functions
	Last modified:	01.03.10
	TOC:
	
		0. Loader
		1. Global Variables
		2. DOM Ready Functions
		3. Window Ready Functions
		4. General Functions
		
	Note: Items marked EXPERIMENTAL are incomplete and 
	      may or may not work as expected.

--------------------------------------------------------*/

/* 	0. 		Loader
--------------------------------------------------------*/
/* Preventing Cross-Site Framing */
		if (top.location!= self.location) {top.location = self.location.href};

/* 	1. 		Global Variables
--------------------------------------------------------*/

		// Full path to document
		var pathName = window.location.pathname;
		
		// Array of URL paths (e.g. /publications/smp/2009/may/html/contents.html )
		var pathNamesArr = pathName.split('/');
		
		// Individual array pieces
		var firstPathName = pathNamesArr[1];
		var secondPathName = pathNamesArr[2];
		var thirdPathName = pathNamesArr[3];
		var fourthPathName = pathNamesArr[4];
		var fifthPathName = pathNamesArr[5];
		var sixthPathName = pathNamesArr[6];
		var seventhPathName = pathNamesArr[7];
		/* Feel free to keep adding pathnames for very long URLs ... */
		
		// Determine current file name
		var currPath = pathName.substring(pathName.lastIndexOf('\\')+1);
			currPath = currPath.substring(currPath.lastIndexOf('/')+1);
			
		// Determine full path to current file
		var fullPath = '';
		for(var i=1; i<pathNamesArr.length; i++)
		{
			fullPath += '/'+pathNamesArr[i];
		}
		

/* 	2. 		DOM Ready Functions
--------------------------------------------------------*/

$(document).ready(function() {			   

	// Show hide search text
	$("#search_text").focus(function() {
	
		if( $(this).val() == "Search" )
		{
		
			$(this).val("");
		
		}
									 
	}).blur(function() {
		
		if( $(this).val() == "" )
		{
		
			$(this).val("Search");
			
		}
		
	});
	
	// Avoid submission of blank search form
	$("form[name='gs']").submit(function() {
	
		val = $("#search_text").val();
		
		if(val == '' || val == 'Search')
		{
		
			window.location = "http://search.rba.gov.au/search";
			
			return false;
			
		}
										 
	});

	
	
	// Highlight Nav
	navHighlight();
	// Recurse and show menu
	/*$("ul.subnav a.selected").each(function() {
		$(this).parent().parents().show();
		
	}); */
	
	// Remove border from last utilities item
	$("#utilities li a:last").css("border", "none"); 
			

	//Insert [back to text] link after each .footnoteList li

	var totalLi = $('.footnoteList li').length; // Count the total number of li's
	for(var i = 0; i <= totalLi; i++)
	{
		var thisLi = '.footnoteList li:eq('+i+')'; // Hold the current LI in a variable
		if( $('.footnoteList').attr('start') != -1 ) // Check to see if the ordered list starts at a different number using the start tag
		{
			var iteration = i; // Starting number
			var beginning = $('.footnoteList').attr('start');
			var correctNo = iteration + beginning;
			$('<span class="backtoText">[<a href="#t'+(correctNo)+'" title="back to text">back to text</a>]</span>').appendTo(thisLi);	
		} else
		{
			// Need to add 1 to i for the anchor tag since no anchors start at 0 and insert back to text link
			$('<span class="backtoText">[<a href="#t'+(i+1)+'" title="back to text">back to text</a>]</span>').appendTo(thisLi);
		}
	}
	
	//Insert print style sheet to the Feedback form page only
	if($('#formMuseum') != -1 ) {
		$('<link rel="stylesheet" href="/Museum/_css/print.css" type="text/css" media="print" />')
		.appendTo('head');	
	}
	
	
});

/* 	3. 		Window Ready Functions
--------------------------------------------------------*/

/* 	4. 		General Functions
--------------------------------------------------------*/

	navHighlight = function(){
		
		// Concatenate pathnames
		var firstPathName=false;
		if (typeof pathNamesArr[1] == 'string' && pathNamesArr[1]!='') {
			firstPathName='/'+pathNamesArr[1];
		}
		
		var secondPathName=false;
		if (typeof pathNamesArr[2] == 'string' && pathNamesArr[2]!='') {
			secondPathName=firstPathName+'/'+pathNamesArr[2];
		}
		
		var thirdPathName=false;
		if (typeof pathNamesArr[3] == 'string' && pathNamesArr[3]!='') {
			thirdPathName=secondPathName+'/'+pathNamesArr[3];
		}
		
		var fourthPathName=false;
		if (typeof pathNamesArr[4] == 'string' && pathNamesArr[4]!='') {
			fourthPathName=thirdPathName+'/'+pathNamesArr[4];
		}
		
		
		/*
			 Highlight Global Nav
		*/
		if(fullPath != '/index.html') // Ensures we are not on the homepage, which would make all links highlighted.
		{
	
			 $("ul#nav li a[href*='"+secondPathName+"']").each(function(){   
				$(this).addClass("selected");
			});
			 
			 
			 
		} 
		
		/*
			Highlight subnav
		*/
	/*	$("ul.subnav li a[href='"+fourthPathName+"']").each(function(){   
			$(this).addClass("selected");
			$(this).parent().parent().css("display", "block");
		}); */
		
/*		
		$("ul.subnav li a[href='"+thirdPathName+"']").each(function(){   
			$(this).addClass("selected");
		});
		
		
		$("ul.subnav li a[href='"+fourthPathName+"']").each(function(){   
			$(this).addClass("selected");
		//	$(this).parent().parent().css("display", "block");
		});
		
*/
		
		/* Highlight navigation from address bar based on parent of current pages */
		
		var baseURL = window.location.protocol + '//' + window.location.host;

		var simplifyURL = function(url, removeHash)
		{
			if(removeHash) url = url.replace(/#.*$/,'');
			return url.replace(baseURL,'').replace(/index.html/, '').replace(/\/$/,'');
		};

		var currentPath = simplifyURL(window.location.pathname);

		$('#content .subnav a').each(function(){
											  
			var url = simplifyURL(this.href);
			
			var lnk = $(this);
			
			checkIfParent = (new RegExp('^'+url+'/[^\/]+(\.html)?$','i').exec(currentPath));			
			
			var submenu = lnk.next('ul').hide();
			
			if(submenu.length)
			{
				lnk
				.addClass('open')
				.click(function(e){
					e.preventDefault();
					submenu.toggle();
					lnk.toggleClass('close');
					lnk.toggleClass('open');
				});
			}			
				
			if((url == currentPath) || checkIfParent)		
			{
				var parent = lnk.addClass("selected").parent('li').parent('ul').show();
				var prevA = parent.prev('a');
				if(prevA.length)
				{
					prevA.removeClass('open').addClass('close selected');
				}
			}
/*		
	// Drop down menus	
	// Hide menu
	$(".subnav li ul").hide().each(function() {
		$(this).prev("a").each(function() {
			$(this).click(function() {
				$(this).next("ul").toggle(0,function(){					
					$(this).parent().toggleClass('selected');
				});
	
				return false;
			});
		});
	});	
	*/		
			
		});
		
		


		/*
			Highlight visitors menu on the Feedback form page
		*/
		if(fullPath == '/forms/museum-feedback-form/')
		{
		
			$("#nav-visitors").addClass("selected");
		}
		
	
/*		
if(fullPath.match('/Museum/Exhibitions/Previous/'))
		{
		
			$("ul.subnav li:nth-child(2) a").addClass("selected");
		
		}
	*/
	}
