// Smooth scrolling anchors
// Add 'no-scroll' class to anchor elements that want to avoid smooth scrolling
$(function(){
    $('a[href*=#]').click(function() {
    if ((!$(this).hasClass('no-scroll')) && (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'')) 
        && location.hostname == this.hostname) {
            var $target = $(this.hash);
            $target = $target.length && $target || $('[name=' + this.hash.slice(1) +']');
            if ($target.length) {
                var targetOffset = $target.offset().top;
                $('html,body').animate({scrollTop: targetOffset}, 500);
                return false;
            } 
        }
    });
});

// Search
$(function() {
	// deal with search box
	var defaultText = "Enter search term";
	$('input.search').val(defaultText).focus(function() {
		if (this.value == defaultText) {
			$(this).val('');
		}
	}).blur(function() {
		if (this.value == '') {
			$('input.search').val(defaultText);
		}
	});
	
// Search types
$('form#search .search-type a').addClass('no-scroll').click(function(event){
  event.preventDefault();
  var selected = $(this).attr('href');
  var thisli = $(this).parent();
  $('form#search .selected').removeClass('selected');
  $(thisli).addClass('selected');
  $(selected).addClass('selected');
});	
});

//  Expert dir
$(function() {
	// deal with search box
	var defaultText = "Search by name";
	$('#expert_name').val(defaultText).focus(function() {
		if (this.value == defaultText) {
			$(this).val('');
		}
	}).blur(function() {
		if (this.value == '') {
			$('#expert_name').val(defaultText);
		}
	});
});

// Audience links
$(function() {
  $('#audience-links .audience').addClass('no-scroll').click(function(event){
    event.preventDefault();
    var selected = $(this).attr('href');
    if ($(selected).is(":hidden")){
      $("#audience-links").addClass("selected");
      $('#audience-links .audience').removeClass("selected");
      $('#audience-links .audience-links-expanded ul').hide();
      $(this).addClass("selected");
      $(selected).slideToggle("fast");
    }
    else {
      $(this).removeClass("selected");
      $("#audience-links").removeClass("selected");
      $('#audience-links .audience').removeClass("selected");
      $('#audience-links .audience-links-expanded ul').hide();
    }
  });
});
  
// Social links
$(function() {
   $("#social .social-link a").hover(function(event){
     var thisli = $(this).parent();
     if (!$(thisli).hasClass('selected')) {
       var thisinner = $(this).find('h3');
       $('#social .selected').removeClass('selected');
       $('#social .social-link h3').hide();
       $(thisinner).fadeIn('fast');
       $(thisli).addClass('selected');
     }
   });
});

// Low bandwidth version switching
$(function() {
  if ($.cookie('hide-lowbandwidth-box')) {
    $('div#low-bandwidth').hide();
  }
  
  $('div#low-bandwidth a.close').click(function(event){
    $.cookie('hide-lowbandwidth-box', 'true'); // set a cookie so that we don't show the box again
    $('div#low-bandwidth').hide();
  });
  
  if ($.cookie('lowbandwidth')) { // if the low bandwidth cookie exists
    // change the text of the links
    $('div#low-bandwidth a.switch').text('Switch to normal version'); 
    $('li.bandwidth-switch a').text('Normal version'); 
    
    $('head').append('<link rel="stylesheet" href="/assets/css/low-bandwidth.css" type="text/css" />'); // add the extra stylesheet
    $('div#low-bandwidth a.switch, li.bandwidth-switch a').click(function(event){
      $.cookie('lowbandwidth', null); // delete the cookie which declares a user is on the low bandwidth version
      event.preventDefault();
      window.scrollTo(0,0); // we're reloading the page for the new version, so scroll to the top of the page
      window.location.reload();
    });
  }
  else { // on normal version
    $('div#low-bandwidth a.switch, li.bandwidth-switch a').click(function(event){
      $.cookie('lowbandwidth', 'true'); // set a cookie so that we know they're on the low bandwidth version
      $.cookie('hide-lowbandwidth-box', 'true'); // set a cookie so that we don't show the box any more
      event.preventDefault();
      window.scrollTo(0,0);
      window.location.reload();
    });
  }
});

//Introduction toggle
$(function() {
  $('#introduction-extra-toggle').click(function() {
    $('.introduction-extra').toggle('slow', function() {
      // Animation complete.
    });
  });
});

// Tabs
$(function() {
	if ($("ul#tabs").length) {
		$("ul#tabs").tabs("div.listing > ul", {effect: 'fade', fadeOutSpeed: 100});
	}
});
$(function() {
	if ($("ul.course").length) {
		$("ul.course").tabs("div#primary-column > div", {effect: 'fade', fadeOutSpeed: 100});
	}
});

//Print function
$(function() {
	 $('li.print a').click(function() {
		window.print();
		return false;
	});
});

