$(document).ready(function() {
	// delete confirmation
	$('form:has(input.delete:not(.no-warn), button.delete:not(.no-warn))').each(function() {
		// form reference
		var form = $(this);

		// hook onto form submit
		form.find('input.delete, button.delete').each(function() {
			$(this).click(function() {
				// create element to hold the dialog
				$('<div>Are you sure you want to delete?</div>').dialog({
					bgiframe: true,
					title: "Please Confirm",
					resizable: false,
					modal: true,
					buttons: {
						Yes: function() {
							form.submit();
							$(this).dialog('close');
						},
						No: function() {
							$(this).dialog('close');
						}
					}
				});

				// prevent natural form submission
				return false;
			});
		});
	});

	// slimbox
	if ($('a[rev^="lightbox"]').length > 0) {
		$('a[rev^="lightbox"]').colorbox();
	}

	// external links
	$('a.external').click(function() {
		window.open($(this).attr('href'), 'blank');

		return false;
	});

	// back links
	$('a.back').click(function() {
		history.go(-1);

		return false;
	});

	// clean junk on form submit
	$('form').submit(function() {
		$(this).find('input[title], textarea[title]').each(function() {
			if ($(this).attr('title') == $(this).attr('value')) $(this).attr('value', '');
		});
	});

	// link click confirm
	if ($('a.confirm').length > 0) {
		$('a.confirm').click(function() {
			var url = $(this).attr('href');

			$('<div>You are about to open a link to ' + url + ', continue?</div>').dialog({
				title: 'External link',
				modal: true,
				buttons: {
					Yes: function(){
						$(this).dialog('close');
						window.open(url, 'blank');
					},
					No: function(){
						$(this).dialog('close');
					}
				}
			});

			return false;
		});
	}

	// table sort
	if ($('table.sortable').length > 0) {
		$('table.sortable').tablesorter({
			widgets: ['zebra']
		});
	}

	// tooltips
	if ($('.tooltip').length > 0) {
		$('.tooltip').each(function() {
			// get content for tooltip
			var title = $(this).attr('title');

			// clear title
			$(this).attr('title', '');

			$(this).qtip({
				style: {
					name: 'cream',
					tip: true,
					width: 250
				},
				position: {
					corner: {
						target: 'rightMiddle',
						tooltip: 'leftMiddle'
					}
				},
				content: title,
				show: 'mouseover',
				hide: 'mouseout'
			});
		});
	}

	// home page slider
	if ($('#slider').length > 0) {
		$("#slider").easySlider({
			auto: true,
			continuous: true
		});

		$(".UpdateButton, .SaveButton, .HelpButton").mouseenter(function(){
			$(this).find("img").animate({
				top:"-20px"
			}, 100 );

		});

		$(".UpdateButton, .SaveButton, .HelpButton").mouseleave(function(){
			$(this).find("img").animate({
				top:"-5px"
			}, 300 );

		});
	}
	
	// black background 100% height
	function resizeWrapper() {
		var wrapperHeight = $('#bodyWrapper').height();
		var windowHeight = $(window).height();
		
		if (wrapperHeight < windowHeight) {
			$('#bodyWrapper').height(windowHeight);
		}
	}
	
	resizeWrapper();
	
	$(window).bind('resize', function() {
		resizeWrapper();
	});
});
