$(document).ready(function() {
	$('.note').each(function() {

		var note = this;
		var noteLink = $('#' + this.id + '_link');
		var noteHide = null;

		var fnNoteShow = function() {
			if (noteHide) {
				clearTimeout(noteHide);
				noteHide = null;
			} 
			if (!$(note).is(':visible')) {
				note.style.top  = (noteLink.position().top - $(note).height()) + "px";
				$(note).fadeIn('fast');
				$(note).fadeTo('fast', 1);
			} else {
				$(note).stop().fadeTo('fast', 1);
			}
		};
		var fnNoteHide = function() {
			// If we are not already trying to hide the note
			if (!noteHide) {
				$(note).stop().fadeTo('fast', 0.8);
				noteHide = setTimeout((function() {
					noteHide = null;
					$(note).fadeOut('fast');
				}), 1000);
			}
		};
		
		if (noteLink != null) {
			$(note).addClass('positioned');
			$(note).hide();
			
			$(note).bind('mouseenter', fnNoteShow);
			$(note).bind('mouseleave', fnNoteHide);

			noteLink.bind('focus', fnNoteShow);
			noteLink.bind('blur', fnNoteHide);
			noteLink.bind('mouseenter', fnNoteShow);
			noteLink.bind('mouseleave', fnNoteHide);

			noteLink.bind('click', function(event) {
				event.preventDefault();
			});
		}
	});
	
	$('input[rel=toggle], input[rel=inv_toggle]').each(function() {
		var currentState = null;
		var eventFunction = function(event) {
			var toggle = options.toggle;
			var enable = $(toggle).attr('rel') == 'toggle' ? $(toggle).is(':checked') : !$(toggle).is(':checked');

			// Prevent the function from needlessly running twice
			if (enable === currentState) {
				return;
			}
			currentState = enable;

			$('.' + $(toggle).attr('class')).each(function() {

				if (this != toggle) {
					if (enable) {
						$(this).removeClass('disabled');
						$('div.mask', this).remove();
						$('input', this).each(function() {
							$(this).removeAttr('disabled');
						})

					} else {
						$(this).addClass('disabled');

						var mask = document.createElement('div');
						$(mask).addClass('mask');
						this.appendChild(mask);

						$('input', this).each(function() {
							$(this).attr('disabled', 'disabled');
						});
					}
				}
			});
		};

		var options = {toggle: this};
		eventFunction({data: options});
		$(this).bind('change', options, eventFunction);
		$(this).bind('click', options, eventFunction);
	});
	
	/*
	$('#home_image_rotate a:first').fadeIn(1000, function() {
		$('#home_image_rotate').cycle({
			fx: 'fade',
			timeout: 6103
		});
	});
	*/

	$('#small_banner_rotate a:first').fadeIn(1000, function() {
		$('#small_banner_rotate').cycle({
			fx: 'fade',
			timeout: 7147
		});
	});

	// Add placeholder text for all elements with placeholder attributes.
	var supportPlaceholder = function() {
		var i = document.createElement('input');
		return 'placeholder' in i;
	}
	if (!supportPlaceholder()) {
		$('input[placeholder], textarea[placeholder]').each(function() {
			var $this = $(this);
			var $form = $this.parents('form');
			$this.bind('blur.placeholder', function() {
				if ($this.val() == '') {
					$this.val($this.attr('placeholder'));
					$this.addClass('placeholder');
				}
			});
			$this.bind('focus.placeholder', function() {
				if ($this.val() == $this.attr('placeholder') && $this.hasClass('placeholder')) {
					$this.removeClass('placeholder');
					$this.val('');
				}
			});
			$this.trigger('blur.placeholder');
			$form.bind('submit', function(event) {
				$this.trigger('focus.placeholder');
			});
		});
	}

	var $timetables = $('#timetable-tabs');
	if ($timetables.length) {
		var currentTab = null;

		$timetables.addClass('tabbed');
		$timetables.find('li').each(function(i, timetable) {
			var $timetable = $(timetable);
			var $header = $timetable.find('h3');
			var $content = $timetable.find('form');

			$header.click(function() {
				currentTab && currentTab.removeClass('active');
				currentTab = $timetable;
				currentTab.addClass('active');
			});

			i || $header.click();
		});
	}

});

