$(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(e) {
		e.preventDefault();

		location.href = document.referrer;
	});

	// 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 );

		});
	}

	// Back links
	$('a.back').click(function(e) {
		e.preventDefault();

		history.go(-1);
	});

	// Use title to populate blank form fields
	$('form:has(input[title])').each(function() {
		var form = $(this);

		// Check for inputs with title
		$(this).find('input[title]').each(function() {
			var title = $(this).attr('title');

			// Function to copy title to non-empty inputs
			function copyTitle(el) {
				if (el.val().length < 1) {
					el.val(title);
				}
			}

			// Clear on focus only if value is the same as title
			$(this).focus(function() {
				if ($(this).val() == title) {
					$(this).val('');
				}
			});

			// Re-add on blur
			$(this).blur(function() {
				copyTitle($(this));
			});

			// If empty, populate from title
			copyTitle($(this));
		});

		// Clear title inputs on submit to allow for form validation to work
		$(this).submit(function() {
			$(this).find('input[title]').each(function() {
				if ($(this).attr('title') == $(this).val()) {
					$(this).val('');
				}
			});
		});
	});

	// Button fading
	if ($('div.fade').length > 0) {
		$('div.fade').hover(function(){
			var fade = $('> div', this);

			if (fade.is(':animated')) {
				fade.stop().fadeTo(200, 1);
			}
			else {
				fade.fadeIn(200);
			}
		}, function(){
			var fade = $('> div', this);
			if (fade.is(':animated')) {
				fade.stop().fadeTo(200, 0);
			}
			else {
				fade.fadeOut(200);
			}
		});
	}

	// google map
	if ($('#map').length > 0) load();


	var tabsopen	=	false;
	//read-more tab content
	$('.side-tabs .tab a').click(function(){
		defwidth			=	200;
		readmorewidth	=	700;
		inwidth		=	defwidth;
		outwidth	=	defwidth;
		readtabopen	=	readtabclicked	=	false;
		//slide in more if the readmore tab is being called
		if(this.hash == '#readmore-tab-content')	{
			inwidth	=	readmorewidth;
			readtabclicked	=	true;
		}
		//slide out some more if the readmore tab is already open
		if(tabsopen && $('.side-tabs .active-tab').attr('id')== 'readmore-tab-content')	{
			outwidth			=	readmorewidth;
			readtabopen		=	true;
		}

		//tab already open - close it
		if(this.hash == '#'+$('.side-tabs .active-tab').attr('id')) {
			$('.side-tabs').animate({'left':'-='+outwidth},function(){
				$('.side-tabs .active-tab').removeClass('active-tab').hide();
			});

			tabsopen	=	false;
			return false;
		}

		$('.side-tabs .active-tab').removeClass('active-tab').hide();
		//there's an open tab. close it first'
		if(!tabsopen) {
			$('.side-tabs').animate({'left':'+='+inwidth});
		}

		if(readtabclicked && tabsopen)
			$('.side-tabs').animate({'left':'+='+(readmorewidth-defwidth)});
		if(readtabopen)
			$('.side-tabs').animate({'left':'-='+(readmorewidth-defwidth)});

		//if the readmore tab is open then slide out a bit
		$(this.hash).show().addClass('active-tab');

		tabsopen	=	true;
		return false;
	});

	$('#bodyContent .person-profile .right div:last').addClass('last');

});
