/* ********************************************************* 
Filename: nps.js
This file appears in different places, as individual assets:
#38478
#113090
#116349
#113067
#132002
#119719 (sandbox only - but still needs to be updated)
********************************************************** */

// Change Date Formats
// run after all files are loaded
$(window).load(function(){
   $(".iso_date").each(function (i) {                // For each element with class iso_date
       if ($(this).text()!="") {                       // Check there is text to manipulate
           var isoDate = $(this).text().split(' ')[0]; // Grab the date only from the date/time
       } else {
           return true;                                // If no text move onto next .iso_date
       }
       isoDate = isoDate.split('-');                   // Split date into 3 tokens
       $(this).text(isoDate[2]+'/'+isoDate[1]+'/'+isoDate[0]); // Set text of current element to format: 28/10/2008
   });

   var isoDateMonthText = ["","January","February","March","April","May","June","July","August","September","October","November","December"];
   var isoDateMonthTextShort = ["","Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];

   $(".iso_date_month").each(function (i) {                // For each element with class iso_date_month
       if ($(this).text()!="") {                       // Check there is text to manipulate
           var isoDateMonth = $(this).text().split(' ')[0]; // Grab the date only from the date/time
       } else {
           return true;                                // If no text move onto next .iso_date_month
       }
       isoDateMonth = isoDateMonth.split('-');                   // Split date into 3 tokens
       if (!((isoDateMonth[1] == 10) ||(isoDateMonth[1] == 11) || (isoDateMonth[1] == 12))) {
	  isoDateMonth[1] = isoDateMonth[1].substring(1,isoDateMonth[1].length);
       }

       $(this).text(isoDateMonthText[isoDateMonth[1]]+' '+isoDateMonth[0]); // Set text of current element to format: October 2008
   });

   $(".iso_date_month_short").each(function (i) {                // For each element with class iso_date_month
       if ($(this).text()!="") {                       // Check there is text to manipulate
           var isoDateMonth = $(this).text().split(' ')[0]; // Grab the date only from the date/time
       } else {
           return true;                                // If no text move onto next .iso_date_month
       }
       isoDateMonth = isoDateMonth.split('-');                   // Split date into 3 tokens
       if (isoDateMonth[1]) {
           if (!((isoDateMonth[1] == 10) ||(isoDateMonth[1] == 11) || (isoDateMonth[1] == 12))) {
    	       isoDateMonth[1] = isoDateMonth[1].substring(1,isoDateMonth[1].length);
           }
       }

       $(this).text(isoDateMonthTextShort[isoDateMonth[1]]+' '+isoDateMonth[0]); // Set text of current element to format: October 2008
   });

   $(".iso_date_day_month").each(function (i) {                // For each element with class iso_date_day_month
       if ($(this).text()!="") {                       // Check there is text to manipulate
           var isoDateDayMonth = $(this).text().split(' ')[0]; // Grab the date only from the date/time
       } else {
           return true;                                // If no text move onto next .iso_date_day_month
       }
       isoDateDayMonth = isoDateDayMonth.split('-');                   // Split date into 3 tokens
       if (!((isoDateDayMonth[1] == 10) ||(isoDateDayMonth[1] == 11) || (isoDateDayMonth[1] == 12))) {
	  isoDateDayMonth[1] = isoDateDayMonth[1].substring(1,isoDateDayMonth[1].length);
       }

       var day = isoDateDayMonth[2];
	   if (day.indexOf("0")==0){
	   	day = day.substring(1);
	   }

       $(this).text(day+' '+isoDateMonthText[isoDateDayMonth[1]]+' '+isoDateDayMonth[0]); // Set text of current element to format: 28 October 2008
   });
	 // End Change Date Formats
	 
});   //end window.load



//do the following after the DOM has loaded
$(document).ready(function() {

	// Active Ingredient Search box
	// onclick
	function clearInput(el) {
		if(el.value == 'Type name here') {
			el.value = '';
			el.className = '';
		}
	}

	// onblur
	function checkInput(el) {
		if(el.value == '' || el.value == ' ') {
			el.value = 'Type name here';
			el.className = 'mnf_keywordBlank';
		} else if (el.value != 'Type name here') {
									el.className = '';
					}
	}
	// End Active Ingredient Search box

	// Set Metadata
	function cleanTitle() {
			if ($("#dc_alt_title").text()!="") {
					$("#dc_title").remove();
			} else {
					$("#dc_alt_title").remove();
			}
	}

	function cleanQL() {
			$("#quick_links ul li").each(function (){
					if ($(this).text()=="") {
							$(this).remove();
					}
			});

			if (!($("#quick_links ul").children("li").length>0)) {
					$("#quick_links").remove();
			}
	}

  cleanTitle();
  cleanQL();
	// End Set Metadata

// Change Content Type text
   var _correspondence = "Correspondence";
   var _complete = "Complete issue";
   var _review = "Review article";
   var _brief = "Brief item";
   var _web = "Web-only item";

   $(".content_type").each(function (i) {
       if ($(this).text().indexOf("Correspondence") != -1) {
           $(this).text(_correspondence);
       } else if ($(this).text().indexOf("Full") != -1) {
           $(this).text(_complete);
       } else if ($(this).text().indexOf("Review") != -1) {
           $(this).text(_review);
       } else if ($(this).text().indexOf("Brief") != -1) {
           $(this).text(_brief);
       } else if ($(this).text().indexOf("Web Only Item") != -1) {
           $(this).text(_web);
       }
   });
// End Content Type text

// PDF Gateway Cookie
	function ngw()    {
			if ($("#ngw:checked"))    {
					setCookie("npsngw",1);
			}
			else    {
					setCookie("npsngw",0);
			}
	}

	function setCookie(c_name,value)    {
			document.cookie=c_name+ "=" +escape(value);
	}

	function getCookie(c_name)    {
			if (document.cookie.length>0)    {
					c_start=document.cookie.indexOf(c_name + "=");
					if (c_start!=-1)    {
							c_start=c_start + c_name.length+1;
							c_end=document.cookie.indexOf(";",c_start);
							if (c_end==-1) c_end=document.cookie.length;
							return unescape(document.cookie.substring(c_start,c_end));
					}
			}
			return "";
	}

  $("#ngw").click(ngw);

  var ck = getCookie("npsngw");
  if (ck == "1")    {
    $("#ngw").attr("checked","checked");
    var newLoc = document.getElementById("pdfurl").href;
    window.location.replace(newLoc);
  }
// End PDF Gateway Cookie

// Text resizer
	// <a href="javascript:ts('body',1)">+ Larger Font</a>
	// | <a href="javascript:ts('body',-1)">+ Smaller Font</a>

	var tgs = new Array( 'div','td','tr');

	//Specify spectrum of different font sizes:
	var szs = new Array( 'xx-small','x-small','small','medium','large','x-large','xx-large' );
	var startSz = 2;

	function ts( trgt,inc ) {
		if (!document.getElementById) return
		var d = document,cEl = null,sz = startSz,i,j,cTags;

		sz += inc;
		if ( sz < 0 ) sz = 0;
		if ( sz > 6 ) sz = 6;
		startSz = sz;

		if ( !( cEl = d.getElementById( trgt ) ) ) cEl = d.getElementsByTagName( trgt )[ 0 ];

		cEl.style.fontSize = szs[ sz ];

		for ( i = 0 ; i < tgs.length ; i++ ) {
			cTags = cEl.getElementsByTagName( tgs[ i ] );
			for ( j = 0 ; j < cTags.length ; j++ ) cTags[ j ].style.fontSize = szs[ sz ];
		}
	}

	$("#text_decrease").click(function(){
			if (document.getElementById("content_container")) {
					ts("content_container",-1);
			}
			if (document.getElementById("column_container")) {
					ts("column_container",-1);
			}
	});
	$("#text_increase").click(function(){
			if (document.getElementById("content_container")) {
					ts("content_container",1);
			}
			if (document.getElementById("column_container")) {
					ts("column_container",1);
			}
	});
	$(".text_decrease").click(function(){
			ts("inside_body_wrapper",-1);
	});
	$(".text_increase").click(function(){
			ts("inside_body_wrapper",1);
	});

// End Text resizer

// Email Page
	function emailPage() {
		var messageText = 'I thought you\'d be interested in this article on the NPS website. ' + window.location;
		var subjectText = 'NPS website: '+ document.title;
		var email = prompt("Enter your friend's email address:", '');
			window.location = 'mailto:' + email +
				'?subject=' + encodeURIComponent(subjectText) +
				'&body=' + encodeURIComponent(messageText);
		}

	$("#email_page,.email_page").click(function(){
		emailPage();
	});
	// End Email Page

	// Remove blank end dates
	$(".end_date").each(function(){
			if ($(this).text()==" to ") {
					$(this).remove();
			}
	});
	// End remove blank end dates

	if ($("#mnf_keyword").length > 0 ) {
			checkInput($("#mnf_keyword").get(0));
	}

	if($("ul.boxList").length>0) {
		var liCount = 1;
		var rowCount = 1;
		var objLen = $(".boxList li").length;
		var liMaxHeight = 0;

		if($("body").hasClass("singleCol")) {
			var rowLen = 5;
		} else {
			var rowLen = 3;
		}
		//Prepare rows
		$(".boxList li").each(function() {
			$(this).addClass("row"+rowCount);
		liMaxHeight = ($(this).height()>liMaxHeight)?$(this).height():liMaxHeight;
			if(liCount>=rowLen) {
			//set the height for all li elems with same class
			$(".boxList li.row"+rowCount).height(liMaxHeight);
				rowCount+=1;
			liCount = 0;
			liMaxHeight = 0;
			}
			liCount+=1;
		});
		//$(".boxList li").height(liMax);
	}

	//AV Library Specific
	if($(".av_player_link").length>0) {
		$(".av_player_link").each(function() {
			if($(this).html().indexOf("<img")==-1)
			{
				if($(this).attr("class").indexOf("Video")>-1)
				{
					$(this).append('<img src="http://www.nps.org.au/__data/assets/image/0004/122179/result-thumb-vdo.gif"/>');
					$(this).attr("class","av_player_link no_play_overlay");
				} else {
					$(this).append('<img src="http://www.nps.org.au/__data/assets/image/0003/122178/result-thumb-sound.gif"/>');
					$(this).attr("class","av_player_link no_play_overlay");
				}
			}
		});
	}

  //Add spaces to audience list for AV library
  if($("td.audience").length>0) {
    $("td.audience").each(function() {
      var tempText = $(this).html();
      tempText = tempText.replace(",",",&nbsp;");
      $(this).html(tempText);
    });
  }


	//initialise the show/hide video transcript text on pages which contain it
	//added by MA - October 2011
	if($('.video_transcript').length > 0){
		// append show/hide link to the element directly preceding the element. 'hid' class used to toggle
		$('.video_transcript').prev().append('<a href="#" class="showTranscriptLink hid">[Show/Hide transcript]</a>');
		// hide all of the elements with a class of '.video_transcript'
		$('.video_transcript').css('display','none');
	}
	
	//and attach click handler to the anchor tag if exists
	$('a.showTranscriptLink').click(function(){
		var show='[Show transcript]';
		var hide='[Hide transcript]';
		
		if($('a.showTranscriptLink').hasClass('hid')){
			// show all of the elements with a class of '.video_transcript'
			$('.video_transcript').css('display','block');
			$('a.showTranscriptLink').removeClass('hid');
			$('a.showTranscriptLink').html('');
			$('a.showTranscriptLink').html(hide);
		}else{	
			// hide all of the elements with a class of '.video_transcript'
			$('.video_transcript').css('display','none');
			$('a.showTranscriptLink').addClass('hid');
			$('a.showTranscriptLink').html('');
			$('a.showTranscriptLink').html(show);
		}
	
		// return false so any link destination is not followed
		return false;
	});

	//similar technique for show/hide text of a more generic nature
	//initialise the show/hide text on pages which contain it
	//added by MA - October 2011
	if($('.hideable_text').length > 0){
		// append show/hide link to the element directly preceding the element. 'hid' class used to toggle
		$('.hideable_text').prev().append('<a href="#" class="showTextLink hid">[Show/Hide text]</a>');
		// hide all of the elements with a class of '.video_transcript'
		$('.hideable_text').css('display','none');
	}
	
	//and attach click handler to the anchor tag if exists
	$('a.showTextLink').click(function(){
		var show='[Show text]';
		var hide='[Hide text]';
		
		if($('a.showTextLink').hasClass('hid')){
			// show all of the elements with a class of '.showTextLink'
			$('.hideable_text').css('display','block');
			$('a.showTextLink').removeClass('hid');
			$('a.showTextLink').html('');
			$('a.showTextLink').html(hide);
		}else{	
			// hide all of the elements with a class of '.showTextLink'
			$('.hideable_text').css('display','none');
			$('a.showTextLink').addClass('hid');
			$('a.showTextLink').html('');
			$('a.showTextLink').html(show);
		}
	
		// return false so any link destination is not followed
		return false;
	});
	
	//show/hide functionality for cases where the user needs the 
	//text heading (title) to be the anchor link for the show/hide
	//do this for all heading styles <h2> to <h5>
	//added by MA - November 2011
	var theHeadingTag = 0;
	if('h2.toggleHeadingtext'){
		
		$('h2.toggleHeadingtext').append('&nbsp;&nbsp;<a class="showTextBelow hid">[show/hide text]</a>');
		$('.h2_hideable_text').css('display','none');  //hide by default
		theHeadingTag = 2;
	}
	if('h3.toggleHeadingtext'){
		
		$('h3.toggleHeadingtext').append('&nbsp;&nbsp;<a class="showTextBelow hid">[show/hide text]</a>');
		$('.h3_hideable_text').css('display','none');  //hide by default
		theHeadingTag = 3;
	}
	if('h4.toggleHeadingtext'){
		
		$('h4.toggleHeadingtext').append('&nbsp;&nbsp;<a class="showTextBelow hid">[show/hide text]</a>');
		$('.h4_hideable_text').css('display','none');  //hide by default
		theHeadingTag = 4;
	}
	if('h5.toggleHeadingtext'){
		
		$('h5.toggleHeadingtext').append('&nbsp;&nbsp;<a class="showTextBelow hid">[show/hide text]</a>');
		$('.h5_hideable_text').css('display','none');  //hide by default
		theHeadingTag = 5;
	}
	
	switch(theHeadingTag){
	
		case 2:
		
			$('a.showTextBelow').click(function(){
				
				if($('a.showTextBelow').hasClass('hid')){
					$('.h2_hideable_text').css('display','block');
					$('a.showTextBelow').removeClass('hid');
					//$('a.showTextBelow').html('');
					//$('a.showTextBelow').html(hide);
				}else{	
					$('.h2_hideable_text').css('display','none');
					$('a.showTextBelow').addClass('hid');
					//$('a.showTextBelow').html('');
					//$('a.showTextBelow').html(show);
				}
				return false;
			});
			break;
	
		case 3:
		
			$('a.showTextBelow').click(function(){
				
				if($('a.showTextBelow').hasClass('hid')){
					$('.h3_hideable_text').css('display','block');
					$('a.showTextBelow').removeClass('hid');
					//$('a.showTextBelow').html('');
					//$('a.showTextBelow').html(hide);
				}else{	
					$('.h3_hideable_text').css('display','none');
					$('a.showTextBelow').addClass('hid');
					//$('a.showTextBelow').html('');
					//$('a.showTextBelow').html(show);
				}
				return false;
			});
			break;
			
		case 4:
			
			$('a.showTextBelow').click(function(){
				
				if($('a.showTextBelow').hasClass('hid')){
					$('.h4_hideable_text').css('display','block');
					$('a.showTextBelow').removeClass('hid');
					//$('a.showTextBelow').html('');
					//$('a.showTextBelow').html(hide);
				}else{	
					$('.h4_hideable_text').css('display','none');
					$('a.showTextBelow').addClass('hid');
					//$('a.showTextBelow').html('');
					//$('a.showTextBelow').html(show);
				}
				return false;
			});
			break;
	
		case 5:
		
			$('a.showTextBelow').click(function(){
				
				if($('a.showTextBelow').hasClass('hid')){
					$('.h5_hideable_text').css('display','block');
					$('a.showTextBelow').removeClass('hid');
					//$('&nbsp;&nbsp;<a class="showTextBelow hid">[show/hide text]</a>').replaceWith('&nbsp;&nbsp;<a class="showTextBelow hid">[hide text]</a>');
					//$('h5.toggleHeadingtext').html('');
					//$('h5.toggleHeadingtext').html('&nbsp;&nbsp;<a class="showTextBelow hid">[Hide text]</a>');
					$('a.showTextBelow').html('');
					$('a.showTextBelow').html('[hide text]');
				}else{	
					$('.h5_hideable_text').css('display','none');
					$('a.showTextBelow').addClass('hid');
					//$('&nbsp;&nbsp;<a class="showTextBelow hid">[show/hide text]</a>').replaceWith('&nbsp;&nbsp;<a class="showTextBelow hid">[show text]</a>');
					$('a.showTextBelow').html('');
					$('a.showTextBelow').html('[show text]');
				}
				return false;
			});
			break;
			
	} //end switch case
	
	
	
});  //end document.ready
