/********************************
*  Default Text                 *
*********************************/
;(function( $ ){
	
	// Copy over the real jQuery val function
	$.fn.realVal = $.fn.val;
	
	// Replace the jquery val function with out val function
	$.fn.val = function( argument ) {
		var field = $(this);
		
		// Do not return a value if it is the default
		if ( typeof argument == 'undefined' && typeof field.data('defaultText') != 'undefined' && field.data('textChanged') == false){
			return '';
		} else {
			return field.realVal( argument );
		}
	}
	
	$.fn.default_text = function( defaultText, options ) {
	
		var settings = $.extend({
			cssClass: 		"default-text",
			defaultText:	defaultText,
			persistent:		false
		}, options);
		
		this.each( function( ) {
		
			var x = this.tagName.toLowerCase();
			
			if ( /^input|textarea$/.test(x) ){
			
				var field = $(this);
				var prev = field.prev();
			
				field.data('defaultText', settings.defaultText);
				
				field.data('textChanged', false);
				
				if (!field.realVal()) {
					field.addClass('default-text');
					if (field.attr('type') == 'password') {
						var second_field = $('<input type="text" id="test_me">').data('parentPassword', field).default_text(settings.defaultText, settings);
						field.data('childPassword', second_field).hide().after(second_field);
					}
					field.realVal( field.data('defaultText') );
				}
				
				// On focus, blank out the field if the text is still default
				field.bind( 'focus', function() {
						if (!settings.persistent && field.data('textChanged') == false) {
							field.val('');
						}
						field.removeClass(settings.cssClass);
						if (typeof field.data('parentPassword') == 'object') {
							field.hide();
							field.data('parentPassword').show().focus();
						}
				} );
				
				// On blur, see if the text has changed or not
				field.bind( 'blur change', function() {
					if ( !field.realVal().length ) {
						field.data('textChanged', false);
						field.addClass(settings.cssClass).realVal( field.data('defaultText') );
						if (typeof field.data('childPassword') == 'object') {
							field.hide();
							field.data('childPassword').show().blur();
						}
					} else {
						field.data('textChanged', true);
					}
				} );
				
				// When submitting, blank out fields that are still default
				field.parents('form').bind('submit', function(){
					if ( field.data('textChanged') == false ) {
						field.val('');
					}
				});
				
			}
			
		});
		
		return this;
	};
})( jQuery );

/********************************
*  AJAX                         *
*********************************/

// Set standard behavior for AJAX
$.ajaxSetup({

	// Default AJAX attributes
	cache: false,
	type: 'POST',
	dataType: 'json',

	// Success Event
	success: function(response) {
		ajax_notice(response);
	},

	// Error Event
	error: function() {
		ajax_error();
	}

});

// Handle AJAX response notices
function ajax_notice(response){

	// If the response came with an error
	if (response.error) {

		// There is an error, post notice
		if (response.error_message !== undefined) {
			display_notice(response.error_message, 'notice-error', false);
			return true;
		}

	// If the response came with a notice
	} else if (response.notice) {

		// There is a notice, post it
		if (response.notice !== undefined) {
			display_notice(response.notice, 'notice-info', true);
			return true;
		}

	}

	// No notice
	return false;

}

// Handle AJAX errors
function ajax_error(){
	display_notice('Connection Error', 'notice-error', true);
}



/********************************
*  NOTICE                       *
*********************************/

// Creates a notice
function display_notice(notice_message, notice_type, fadeout) {

	// If the error string has a value
	if (notice_message != "") {

		// Locate the notice div tag if available
		if ($('#notice').length > 0) {

			// Break apart error message
			parts = notice_message.split("\r\n");

			// Start error message string
			notice_messages = "";

			// Loop error messages and build error message string
			for (i = 0; i < parts.length; i++) {
				if (parts[i] != "") {
					notice_messages += "<span>" + parts[i] + "</span>";
				}
			}

			// Notice Messages
			notice_messages = $('<p>' + notice_messages + '</p>');

			// Add Notice HTML
			$('#notice').html(notice_messages);

			// Reset Notice Class
			$('#notice').removeClass();
			$('#notice').addClass('notice');

			// If set, add custom notice type
			if (notice_type !== undefined) {
				$('#notice').addClass(notice_type);
			}

			// Make the notice visible
			$('#notice').fadeIn(500);

			// Fade Out Notice
			if (fadeout) {
				setTimeout(function(){ $('#notice').fadeOut(1000)}, 3000);
			}

		} else {
			alert(notice_message);
		}

	}

}



/********************************
*  BROWSING                     *
*********************************/

// Submit a hidden form after asking question
function submit_form_request(form_id, question_id) {

	// Get the question from a hidden field in the form
	ask_question = $("#" + question_id).text();

	// Ask the question and force a YES response
	if (confirm(ask_question)) {
		$("#" + form_id).submit();
	}

}

// Submit a hidden form after asking question with checkbox selection
function submit_form_request_checkbox(form_id, source_form_id, question_id) {

	// Get the selected Checkboxes
	ids = get_selected_checkboxes(source_form_id);

	// If there were IDs selected
	if (ids != "") {

		// If no question was passed
		if (question_id == undefined) {

			// Submit the form
			$("#" + form_id + " [name='selected_ids']").val(ids);
			$("#" + form_id).submit();

		} else {

			// Get the question from a hidden field in the form
			ask_question = $("#" + question_id).text();

			// Ask the question and force a YES response
			if (confirm(ask_question)) {
				$("#" + form_id + " [name='selected_ids']").val(ids);
				$("#" + form_id).submit();
			}

		}

	} else {
		alert ("You did not select any checkboxes.");
	}

}

//Get selected check boxes of a form
function get_selected_checkboxes(container_id) {

	// Hold a comma seperated list of IDs
	ids = "";

	// Loop through each checkbox within our container
	$("#" + container_id + " :checked").not(".checkall").each( function () {
		ids += "," + $(this).val();
	});

	// Return list of IDs, remove beginning comma
	return ids.substr(1);

}

// Check or Uncheck checkboxes of a container
function check_uncheck_all(container_id, self_id) {
	$("#" + container_id + " :checkbox").attr('checked', $("#" + self_id).is(':checked'));
}


