$(document).ready(function(){
	
	//home page page specific
	if($("body").hasClass("home")) {
		
		// Drop down course types
		  var toggle_link = $('<a>View</a>').attr({ className: 'toggle-link'});
		  $(".course-types").hover(
		    function() {
		      $(this).find('ul').slideDown('fast');
		      $(this).find('toggle-link').text('Hide');
		      $(this).find('div').addClass('down');
		    },
		    function() {
		      $(this).find('ul').slideUp('fast', function() {
		        // Remove the border after complete
		        $(this).parent().removeClass('down');
		      });
		      $(this).find('toggle-link').text('View');

		    }
		  );
		  $(".course-types ul").wrap('<div style="position:absolute; width: 288px; z-index: 999">','</div>').hide().after(toggle_link);

		  
	}
  	
	//course page specific
	if($("body").hasClass("course")) {
		//in page navigation setup
		$("#in-page-nav").accordion({
			active: true,
			alwaysOpen: false,
			autoheight: false,
			header: 'a.head',
			clearStyle: true
		});
		$("#undergraduate").accordion({
			active: true,
			alwaysOpen: false,
			autoheight: false,
			header: 'a.sub-head',
			clearStyle: true
		});
		$("#postgraduate").accordion({
			active: true,
			alwaysOpen: false,
			autoheight: false,
			header: 'a.sub-head',
			clearStyle: true
		});
		
	}
 
		// Drop down content links
	  	$("body .block").each(function (i) {
		  	$(this).find("div > p:gt(0)").wrapAll('<div id="inner-tray" style="border: 0; padding: 0;"></div>');
		  });
		  $("body .block a.drop-down").each(function (i) {
				$(this).parent().find("#inner-tray").hide();
				this.onclick = function () {
					if ($(this).parent().find("#inner-tray").is(":hidden")) {
						$(this).parent().find("#inner-tray").slideDown("fast");
						$(this).text("Close");
					} else {
					  	$(this).parent().find("#inner-tray").slideUp("fast");
						$(this).text("Read more...");
					}
					return false;
				};

		  });



 
	// Next and previous dates
	var prev_link = $('<a>Prev</a>').attr({ className: 'prev-link disabled'});
	var next_link = $('<a>Next</a>').attr({ className: 'next-link'});
 
	$(".open-days ul").after(next_link).after(prev_link).css({height: '13em'});

	$('#undergraduate-cal .open-days ul').cycle({
	    fx: 'scrollHorz',
	    speed:  'fast', 
	    timeout: 0, 
	    next:   '#undergraduate-cal a.next-link', 
	    prev:   '#undergraduate-cal a.prev-link',
	    after: check_link_status,
	    nowrap: 1
	});

	$('#postgraduate-cal .open-days ul').cycle({ 
	    fx: 'scrollHorz',
	    speed:  'fast', 
	    timeout: 0, 
	    next:   '#postgraduate-cal a.next-link', 
	    prev:   '#postgraduate-cal a.prev-link',
	    after: check_link_status,
	    nowrap: 1
	});
	
	var is_msie = $.browser.msie;
	
  	//adds the pseudo classes needed for ie6,7
	if(is_msie) {
		$('#main-nav li').each(function (i) {
			this.onmouseover = function() {
				if($(this).hasClass("has-sub-menu") && $(this).hasClass("selected-adjacent")) {
			            $(this).addClass("sfhover-has-sub-menu-a");
			          } else if($(this).hasClass("has-sub-menu")) {
			            $(this).addClass("sfhover-has-sub-menu");
			          } else {
			            $(this).addClass("sfhover");
			          }
			}
			
			this.onmouseout = function() {
			       if($(this).hasClass("has-sub-menu") && $(this).hasClass("selected-adjacent")) {
			         $(this).removeClass("sfhover-has-sub-menu-a");
			       } else if($(this).hasClass("has-sub-menu")) {
			         $(this).removeClass("sfhover-has-sub-menu");
			       } else {
			         $(this).removeClass("sfhover");
			       }
			}
		});
	}
  
  //function for clearing and instating the default search text
  var initial_value;
  
  $(".search-field").focus( function() {
    initial_value = this.value;
    this.value = '';
  });
  
  $(".search-field").blur( function() {
    this.value = initial_value;
  });

});

check_link_status = function(curr, next, opts) {
  var index = $(this).parent().children().index(this);
  var open_days_box = $(this).parent().parent();
  var prev_link = open_days_box.find('.prev-link');
  var next_link = open_days_box.find('.next-link');
  if (index == 0) {
    prev_link.addClass('disabled');
  } else {
    prev_link.removeClass('disabled');
  }
  if (index == opts.slideCount - 1) {
    next_link.addClass('disabled');
    // Fix for IE6s woeful CSS support
    if( jQuery.browser.msie && (jQuery.browser.version < 7) ) {
      next_link.addClass('disabled-next');
    }
  } else {
    next_link.removeClass('disabled');
  }
};
