$(document).ready(function() {

$('html').removeClass('no-js');

// adds hide class to skip links
$('#access').addClass('hide');

applyAsterix();
clean_hr();
targetExternal();
addImageMargins();


 // shows skip links when accessed by keyboard focus
 $("#access a").focus(function () {
         $("#access").css({'position':'relative','left':'auto'});
  });
  // hides skip links when keyboard focus is lost
$('#access a').blur(function() {
     $("#access").removeAttr('style');
});

$('#tools .addthis_button_compact, #share_this_right .addthis_button').blur(function() {
     return _atw.clb();
});


$('.dropdown > li > a').focus(function () {
         $(this).next('ul').addClass('highlighted');
});

$('.dropdown ul li:last-child a').blur(function () {
         $('.dropdown .highlighted').removeClass('highlighted');
});

$('.dropdown').mouseleave(function() {
   $('.dropdown .highlighted').removeClass('highlighted');
});




 $('#matrix_content table').each(function(){
   var getBdr = $(this).attr('border');
   if(getBdr == '0' || getBdr == ''){
     $(this).addClass('no_bdr');
   }
 });

   // wraps first word in main navigation in span tag to allow wrapping
   $('li.first-level > a').each(function() {
      var html = $(this).html();
      var word = html.substr(0, html.indexOf(" "));
      var rest = html.substr(html.indexOf(" "));
      $(this).html(rest).prepend($("<span/>").html(word));
   });

   // Sets search field value
   //$('#queries_search_site_query').val('Search');
   //var searchID = $('#queries_search_site_query');
   //var searchVal = "Search";
   // search input actions - auto show/hide default values
  // searchID.focus(function(){
   	   //if($(this).attr("value") == searchVal) $(this).attr("value", "");
   //});
   //searchID.blur(function(){
   	   //if($(this).attr("value") == "") $(this).attr("value", searchVal);
   //});
   

        // add 'last' class
        $('#breadcrumb a:last').addClass('last');

	//Initialize main-menu - http://users.tpg.com.au/j_birch/plugins/superfish/#options
	$('ul.sf-menu').superfish({ }); 
	
	//Controls the actions of the users mouse action on the Infomation icon in the
	// the right-side main section - above the map.
	
        $('.information').mouseover(function () {
		$('#pop-up').addClass('pop-up-show').removeClass('hidden');
	});

        $('.information').mouseout(function () {
		$('#pop-up').removeClass('pop-up-show').addClass('hidden');
	});
	
	


	/* =TEXT RESIZING
	********************************************************/
	//Add text resize  button
	$('div#tools ul').prepend(
			'<li><a href="/home/site-map" id="sitemap">Site map</a></li>' +
			'<li class="text-decrease"><a href="#" class="text-resize" title="Decrease text size">a<span> decrease font size</span></a></li>' +
			'<li class="text-increase"><a href="#" class="text-resize" title="Increase text size">A<span> increase font size</span></a></li>'
	);
	//Add text resize  
	$("#main-content").textResize($("li.text-decrease"), $("li.text-increase"), $("li.reset"), {baseFontSize: "1em", maxZoom: .5, steps: 3});
	


   // check if item has a thumbnail, if not, expand row
   $('.item').each(function(){
     var getThumb = $(this).children('.thumb').has('img').length;
     if(getThumb < 1) {
      $(this).children('.page_info').css('width','100%');
      $(this).children('.thumb').hide();
     };
   });

   // if no left col, expand mid col
   if($('#inside_leftcol').length) {
      
   } else {
         // make sure homepage doesnt get effected
         if($('.home').length) {
           // leave homepage as is
         } else {
             $('#matrix_content').css('width','705px');
          }
   };
   
   // hides lvl2 ul if no child li's exist
   if($('#inside_leftcol ul.lvl-2 li').length) {
      
   } else {
      $('#inside_leftcol ul.lvl-2').hide();
   };


}); /*end ready function*/

function targetExternal(){
  $("a[href^='http://']").not("a[href^='http://gbrmpa.staging.squiz.net']").not("a[href^='http://gbrmpa.gov.au']").not("a[href^='http://www.gbrmpa.gov.au']").attr("target", "_blank");
};

       //Functions to put a span tag around asterix symbols in forms
	function replaceAsterix(origString){
	  var inChar = '*';
	  var outChar = '<span class="error">*</span>';
	  var newString = origString.split(inChar);
	  newString = newString.join(outChar);
	  return newString;
	};
	function applyAsterix(){
	   $("#matrix_content form td:contains('*')").each(function(){
	      var tempString = this.innerHTML;
	      this.innerHTML = replaceAsterix(tempString);
	   });
	};

// Map lightbox close function
function close_window() {
   $('#map_popup').remove();
   $('#bg_overlay').remove();
   return false;
};

// Matrix Content - remove excess p tags surrounding hr tags
function clean_hr() {
  $('#matrix_content hr').each(function() {
       if($(this).next().html() == "") {
         $(this).next('p').remove();
       }

       if($(this).prev().html() == "") {
         $(this).prev('p').remove();
       }
  });
}


//Converts hspace and vspace into margin attributes on images added via the wysiwyg
function addImageMargins(){
  $('#container.home #matrix_content img').addClass('default-margin');
  $('#matrix_content img').each(function(){
    if($(this).is('.default-margin')){
      //Then leave v and h space alone
    }else if($(this).attr('hspace') != 0 || $(this).attr('vspace') != 0){
      var hMargin = $(this).attr('hspace') + 'px ';
      var vMargin = $(this).attr('hspace') + 'px ';
      $(this).css('margin',hMargin + vMargin + hMargin + vMargin);
      $(this).attr('hspace','').attr('vspace','');
    }
  });
}






