;(function($){
	$(document).ready(function(){
		init_menu();
		if($('body.home-page').length > 0)
			init_slideshow();
		if($('#searchform input[name="keywords"]').length > 0)
			init_searchbox();
		if($('a.lightbox').length > 0)
			$('a.lightbox').fancybox();
		if($('#dif-details').length > 0){
			var elem = $('#dif-details');
			elem.change(function(){set_invoice(this)});
			set_invoice(elem);
		}
		if($('ul.lecture-list').length > 0)
			init_lecturers();
		
	});
	function init_lecturers(){
		$('ul.lecture-list a').click(function(){
			var name = $(this).attr('href').split('#');
			name = name[1];
			if($('.'+name).length > 0){
				var elem = $('.lecturer-item:visible:not(.'+name+')');
				if(elem.length > 0)
					elem.slideUp('fast',function(){$('.'+name).slideDown('fast')});
				else
					$('.'+name).slideDown('fast');
				return false;
			}
		});
	};
	function set_invoice(elem){
		$('input, textarea','.invoice-form').attr('disabled',$(elem).attr('checked'));	
	};
	function init_searchbox(){
		$('#searchform input[name="keywords"]').focus(function(){
			if($(this).val() == 'e.g. course name or skill')
				$(this).val('');
		}).blur(function(){
			if($(this).val() == '')
				$(this).val('e.g. course name or skill');
		});
		$('#searchform').submit(function(){
			var elem = $('#searchform input[name="keywords"]');
			if(elem.val() == '' || elem.val() == 'e.g. course name or skill'){
				elem.focus();
				return false;
			}
		});
	};
	function init_menu(){
		$(".dropdown-tag").mouseenter(function(){
			$(this).find('.dropdown-menu').css({top: $(this).offset().top + 21, left: $(this).offset().left -1 });
			$(this).find('.dropdown-menu').css({ minWidth: $(this).outerWidth() });
			$(this).find('.dropdown-menu').show();
		});
		$(".dropdown-tag").mouseleave(function(){
			$(this).find('.dropdown-menu').hide();
		});
	};
	function init_slideshow(){
		$('.main .sub-slideshow .the-fyvie-project .lightbox a span').hide();
		$('.main .sub-slideshow .the-fyvie-project .lightbox a').hover(function() {
			$(this).find('span').fadeIn('200');
		},function () {
			$(this).find('span').fadeOut('200');		
		});
		$('.slideshow').jcarousel({
			auto: 3,
			scroll: 1,
			wrap: 'last',
			initCallback: mycarousel_initCallback,
			itemVisibleInCallback: {
				onBeforeAnimation:  mycarousel_itemVisibleInCallbackBeforeAnimation
			}	
		});
	};
	function mycarousel_initCallback(carousel){
		// Disable autoscrolling if the user clicks the prev or next button.
		carousel.buttonNext.bind('click', function() {
			carousel.startAuto(0);
		});
		carousel.buttonPrev.bind('click', function() {
			carousel.startAuto(0);
		});
		// Pause autoscrolling if the user moves with the cursor over the clip.
		carousel.clip.hover(function() {
			carousel.stopAuto();
		}, function() {
			carousel.startAuto();
		});
	};
	function mycarousel_itemVisibleInCallbackBeforeAnimation(carousel, item, idx, state) {
		$('.dots').removeClass('dots-1 dots-2 dots-3 dots-4').addClass('dots-' + idx);
	};
})(jQuery);
