$(document).ready(function() {
	
	$('div#site_navigation ul li').hover(function() {
		$(this).addClass('over');
	}, function() {
		$(this).removeClass('over');
	});
	
	$(".hover-tip").tooltip({
		position: 'center center',
		effect: 'slide',
		position: false
	});
	
	$('.toggle_closed').each(function() {
		var target = $(this).attr('id').substr(7);
		$('#'+target).hide();
	});	
	
	$('.toggle').click(function() {
		var link = $(this);
		var target = $(this).attr('id').substr(7);
		$('#'+target).slideToggle('fast', function() {
			if($(this).css('display') == 'none') {
				$(link).addClass('toggle_closed');
				$(link).removeClass('toggle_open');
			} else {
				$(link).addClass('toggle_open');
				$(link).removeClass('toggle_closed');
			}
		});
	});
	
	$('.tip-close').click(function() {
		$(this).parent().fadeOut('fast');
	});
	
	/*
	$('a#AdvancedSearchLink').click(function() {
		if($('div#CourseSearch').hasClass('open')) {
			$('div#CourseSearch').animate( { left : 480 }).removeClass('open');
			$('a#AdvancedSearchLink').removeClass('open');
		} else {
			$('div#CourseSearch').animate( { left : 0 }).addClass('open');
			$('a#AdvancedSearchLink').addClass('open');
		}			
	});
	
	$('.dialogLink').click(function() {
			
		var windowHeight = $(window).height();
		var windowWidth = $(window).width();
		var documentHeight = $(document).height();
		
		var target = $(this).attr('id').split('Link')[0];
		var dialog = $('div#' + target + 'Dialog');
		
		if(!$('#dialogOverlay').length) {
			var overlay = $('<div id="dialogOverlay" />').css({'height': documentHeight, 'width': windowWidth });
			$('body').append(overlay);
		}
				
		if($(this).hasClass('dialogGalleryLink')) {
			
			function updateGalleryDialog() {
				var i = new Image();
				console.log(currentlinkidx);
				i.src = $(gallerylist).eq(currentlinkidx).attr('href');
				console.log(i.src);
				i.title = $(gallerylist).eq(currentlinkidx).attr('title');
				i.onload = function() {
					dialog.animate({
						'width': i.width,
						'height': i.height
					});
					dialog.find('.dialogGalleryMain').html(i);
					dialog.find('.dialogGalleryCaption').html(i.title);
				}
				return true;
			}
			
			$('.dialogGalleryMain').empty();
			$('.dialogGalleryControls a').unbind('click');
			
			// Get list of all images in the gallery
			var gallerylist = $(this).parent().parent().find('li a');
			var currentlinkidx = $(this).parent().prevAll().length;
			
			updateGalleryDialog();
							
			$('a#dialogGalleryControlsPrev').click(function() {
				currentlinkidx--;
				if(currentlinkidx < 0) {
					currentlinkidx = gallerylist.length - 1;
				} 					
				console.log('prev');
				updateGalleryDialog();
				return false;
			});
			
			$('a#dialogGalleryControlsNext').click(function() {
				currentlinkidx++;
				if(currentlinkidx == gallerylist.length) {
					currentlinkidx = 0;
				} 
				console.log('next');
				updateGalleryDialog();
				return false;
			});
			
		}
		
		dialog.css({
			'left': (windowWidth / 2) - (dialog.width() / 2),
			'top' : (windowHeight / 2) - (dialog.height() / 2)
		}).show().bgiframe();
		
		$('.dialogX').click(function() {
			$('div#dialogOverlay').remove();
			$('.dialog').fadeOut('fast');
		});
		
		$(document).keydown(function(e) {
			switch(e.which) {
				case 27:
					$('div#dialogOverlay').remove();
					$('.dialog').fadeOut('fast');
					break;
			}
		});
		
		return false;
	});*/
	
	$('input#SchoolName').autocomplete('/ajax/schools/schools/autocomplete', {
		minChars: 2,
		onItemSelect: selectAutoCompleteItem,
		onFindValue: autoCompleteFindValue,
		formatItem: autoCompleteFormatItem,
		autoFill: false
	});

	function selectAutoCompleteItem(li) {
		if(li != null) {
			if(li.extra) {
				location.href = '/' + li.extra[1];
			} 
		}
	}

	function autoCompleteFindValue(li) {
		if(li != null) {
			if(!!li.extra) {
				var sValue = li.extra[0];
			} else {
				var sValue = li.selectValue;
			}
		}
	}

	function autoCompleteFormatItem(row) {
		if(row[1] == undefined) {
			return row[0];
		} else {
			return '<a href="/' + row[2] + '">' + row[0] + '</a>';
		}
	}
	
	$('form#SchoolSearch label').labelOver('over-apply');
	
});