/* Image preloading functionality */
$.extend({
	preloadImages: function() {
		for(var i = 0; i<arguments.length; i++)
		{
			$("<img>").attr("src", arguments[i]);
		}
	}
});

/* Custom vertical tabs function. */
$.fn.extend({
	verticalTabs: function() {
		$(this).tabs();
		$(this).parent().find(".ui-tabs-nav").removeClass('ui-tabs-nav').removeClass('ui-tabs').addClass('ui-tabs-vertical');
		$(this).parent().find(".ui-tabs-panel").removeClass('ui-tabs-panel').addClass('ui-tabs-vertical-panel');
		$(this).parent().find("div, ul, li").removeClass('ui-widget ui-widget-content ui-corner-all ui-widget-header ui-helper-clearfix ui-helper-reset ui-corner-bottom ui-corner-top');
	}
});  

/* Rotator show/hide functionality */
$(document).ready(function() {
						   
	if ($.minmax) {
		$('#page').minmax();
	}
	
	/* Add event handlers to rotating header image show/hide link. */
	$('#btn-rotator-show').click(function() {
		$('#rotator').slideDown();
		$('#rotator_show').fadeOut();
		setRotatorStatus(1);
		return false;
	});
	$('#btn-rotator-hide').click(function() {
		$('#rotator').slideUp();
		$('#rotator_show').fadeIn();
		setRotatorStatus(0);
		return false;
	});	
	
	/* Rollover support for header menu. */
	$('div#navigation ul li').mouseover(function() {
		$(this).addClass('selected');
	}).mouseout(function() {
		$(this).removeClass('selected');
	});	
});


/* AJAX call to handle permanent storage of rotator status. */
function setRotatorStatus(new_rotator_status) {
	$.post("/online/rotator", { new_status: new_rotator_status }, function(data){ }, "json");
}

/* Function to swap two divs */
function swapDivs(first_div_id, second_div_id) {
	$(first_div_id).toggle();
	$(second_div_id).toggle();
}

/* Limit number of characters in a textarea. */
function limitChars(textid, limit, infodiv) {
	var text = $(textid).val();
	var textlength = text.length;

	$(infodiv).html('You are currently using '+textlength+' of '+limit+' characters.');

	if (textlength > limit) {
		$(textid).val(text.substr(0, limit));
	}
}

/* Limit number of words in a textarea. */
function limitWords(textid, limit, infodiv) {
	var total_words = checkWordCount(textid);
	
	$(infodiv).html('You are currently using '+total_words+' of '+limit+' words.');
}
function checkWordCount(textid) {
	var text = $(textid).val();
	return text.split(/[\s\.\?]+/).length;
}

/* General Functions */
function confirmAction() {
	return confirm("Are you sure?");
}
function confirmDelete() {
	return confirm("Are you sure you want to delete this item?");
}
function confirmModifyStatus() {
	return confirm("Are you sure you want to modify this information? Doing so may alter your recognition status.");
}
function closeWindow() {
	alert("This window cannot be closed from within a presentation.");	
}

/* Recognition Functions */
function confirmReplaceMember() {
	return confirm("Another user is already filling this position in the roster. Are you sure you want to approve this request, replacing the current user?");
}
function confirmDeclineRequest() {
	return confirm("Are you sure you want to decline this request?");
}
function confirmRemoveMemberRec() {
	return confirm("Are you sure you want to remove this member? Because this member is listed on the Agency Account Form, you will be required to print and sign a new one.");	
}
function confirmRemoveMemberNoRec() {
	return confirm("Are you sure you want to remove this member?");
}