/* togglesearch function for search box */

jQuery.fn.extend({
  toggleSearch: function() {
    return this.each(function() {
      var el = jQuery(this);
      if(el.css('width') != '80px') {
        el.animate({width: '80'});
        el.removeClass('search_on');
      }
      else {
        el.animate({width: '260'});
        el.addClass('search_on');
      }
    });
  }
});

jQuery(document).ready(function($){

  /* Search box */

  $('#tn_search a').click(function(){
    $('#tn_search').toggleSearch();
  });

  /* Main navigation/lightboxy type effect */

  $('#mainnav_interior').hover(function(){
      $('.mainnav_lb').fadeTo(200, 0.5);
      $('#mainnav_interior').addClass('active');
      $('#mainnav_container').css('background-color','#151515').css('background-color','rgba(0,0,0,0.8)');
    }, function(){
      $('.mainnav_lb').fadeTo(200, 0).hide();
      $('#mainnav_interior').removeClass('active');
      $('#mainnav_container').css('background-color','transparent');
    }
  );

  /* Homepage carousel with pager */

  $('#home_carousel').cycle({
    fx: 'fade',
    speed: 'slow',
    timeout: 10000,
    pager: '#home_carousel_pager',
    pagerAnchorBuilder: function(idx, slide) {
      return '<a href="#"><img src="/images/' + $(slide).attr('id') + '_pager.png" /></a>';
    }
  });

  /* Stop the carousel when the pager is clicked */
  $('#home_carousel_pager').click(function(){
    $('#home_carousel').cycle('pause');
    $('#home_carousel_pager').unbind('click');
  });

  /* Smaller carousels, no pager */

  $('#int_carousel').cycle({
    fx: 'fade',
    speed: 'slow',
    timeout: 5000
  });

  /* Popup for popup class links */

	$(".popup").fancybox({
	  'padding': 5,
    'autoScale': false,
    'overlayColor': '#000000',
    'overlayOpacity': 0.5,
    'scrolling': 'no'
	});

  /* Add active class to side nav links if necessary */

 
 var longpath = '/' + location.pathname.substring(1);
 var sPath = window.location.pathname;
 var shortpath = sPath.substring(sPath.lastIndexOf('/') + 1);
 if (shortpath == '') {
   var longpath = longpath + 'Default.aspx';
   var shortpath = 'Default.aspx';
 }
 if ( shortpath || longpath ) {
		  $('.Menu_Rt a[href$="' + shortpath + '"], .Menu_Rt a[href$="' + longpath + '"]').attr('class', 'active');
 }
 if(window.location.hash == '#showme') {
	   alert("Full path: " + longpath + "\nPage name: " + shortpath);
 }

  /* Regional map dropdown */
  $('#DropDown1').click(function(){
    if($('#DropDown1Content').css('display') == 'none') {
      $('#DropDown1Content').show();
      $(this).attr('src',"/images/visitregionaldd2.png");
    }
    else {
      $('#DropDown1Content').hide();
      $(this).attr('src',"/images/visitregionaldd.png");
    }
  });

})
