/* Simple form validation */
$(document).ready(function() {
	$('form').submit(function() {
		var is_valid = true;
		$(this).find(':input.required').each(function() {
			if ($(this).val() == '') {
				is_valid = false;
				$(this).addClass('error');
			}
		});
		
		if (is_valid) {
			return (true);
		}
		else {
			alert("One or more required fields were not completed.  Please check the highlighted fields and try again.");
			return (false);
		}
	});

	$(".delete_confirmation").click(function() {
		return confirm("Are you sure you want to delete this item?  This action can not be undone!");
	});
});




/* Apply some classes for styling purposes */
$(document).ready(function () {
	$("input:submit").addClass('submit');
	$("input:text").addClass('text');
	$("input:password").addClass('password');
	
	$(".track_hover").hover(function() {
		$(this).addClass('hover');
	}, function() {
		$(this).removeClass('hover');
	});
});



/* IMAGE REPLACEMENT FUNCTIONALITY
 *
 * Automates the swapping of two images of any images found of the form:
 *     <img src="example.gif" swap_on_hover="rollover.png">
 *	   <img src="example.gif" swap_on_click="example2.gif">
 *	   <img src="thumbnail.gif" swap_on_click="fullsize.gif" swap_on_click_target="fullsize_div_id">
 *
 * Parameters:
 *     - The 'swap_on_hover' attaches the image swap to the hover event.  Expects the URL of the image to
 *		 be used to swap to.
 *	   - The 'swap_on_click' attaches the image swap to the click event
 *	   - The 'swap_on_click_target' or 'swap_on_hover_target' expects the ID of a second element and
 *       will replace the image in that element with the image specified by the corresponding 'swap_on_hover'
 *	     or 'swap_on_click' attributes.
 *	   - These tags can be combined.
 */
$(document).ready(function() {
	$('img[swap_on_hover]').each(function() {
	    /* Attach the existing image src to the element so we can switch back */
		var cur_img = $(this).attr('src');
		
		$(this).attr('old_src', cur_img);

        /* Attach our event handler */
        $(this).hover(function() {
    		var new_img = $(this).attr('swap_on_hover');
    		var target = $(this).attr('swap_on_hover_target');
    		
            if(target) {
                $("#" + target).attr('src', new_img);
                alert(new_img);
            }
            else {
                $(this).attr('src', new_img);
            }
        }, function() {
            var old_img = $(this).attr('old_src');
            var target = $(this).attr('swap_on_hover_target');
            
            $(this).attr('src', old_img);
        });
	});
});






/* IMAGE ROTATION FUNCTIONALITY
 *
 * Automatically rotates any images found of the form:
 *     <img src="example.gif" rotate="first.gif,second.jpg,third.png" delay="4">
 *
 * Parameters:
 *     - The 'rotate' tag is a list of 1 or more comma separated images to rotate through.  The src
 *		 image will also be rotated through.
 *     - The 'delay' tag is an optional number of seconds to wait between rotations.  Defaults to the value below
 */
		
		
		
/* Fancy disappearing status messages */
setTimeout('clear_messages()', 5000);
function clear_messages()
{
	$('.success_message').slideUp();
	$('.error_message').slideUp();
}
