// $id $

 if (typeof(Drupal) == "undefined" || !Drupal.shoutbox) {    
    Drupal.shoutbox = {};
 }

/*
 * Submit shout with javascript.
 *
 */

Drupal.shoutbox.attachShoutAddForm = function () {
    $("input[@name='ajax']").val("1"); // tell server we are using ajax to submit
	$("input[@name='nextcolor']").val(Drupal.shoutbox.color);
		// tell server what color to use
    var options = {
	resetForm: true,
	success: Drupal.shoutbox.success
    };

    $("#shoutbox-add-form").ajaxForm(options);
}

/**
  * Display response text and update the color
  * field. Remove top message if we are over 
  * the max count. 
  */

Drupal.shoutbox.success = function (responseText) {
	
    if(Drupal.shoutbox.shownAmount >= Drupal.shoutbox.showAmount) {
		$('#shoutbox-posts').children().eq(0).remove();		
	}
	else {
		Drupal.shoutbox.shownAmount += 1;		
	}
		//update color
	Drupal.shoutbox.color = (Drupal.shoutbox.color) ? 0 : 1;

	$('#shoutbox-posts').append(responseText);
		// tell server what color to use
	$("input[@name='nextcolor']").val(Drupal.shoutbox.color);		
}

 
Drupal.shoutbox.attachLinks = function() {

}

Drupal.shoutbox.attachForm = function() {
    $('input#edit-nick').focus(
	function() {
	    if( $(this).val() == Drupal.shoutbox.defaultNick) {
		$(this).val("");
	    }
	}
    );
    $('input#edit-message').focus(
	function() {
	    if( $(this).val() == Drupal.shoutbox.defaultMsg) {
		$(this).val("");
	    }
	}
    );
    $('input#edit-url').focus(
	function() {
	    if( $(this).val() == Drupal.shoutbox.defaultUrl) {
		$(this).val("");
	    }
	}
    );
}

if (Drupal.jsEnabled) {
    $(document).ready(function() {
	Drupal.shoutbox.shownAmount = shoutboxSettings['shownAmount'];
	Drupal.shoutbox.showAmount = shoutboxSettings['showAmount'];
	Drupal.shoutbox.color = (shoutboxSettings['shownAmount'])%2;
	Drupal.shoutbox.defaultNick = (shoutboxSettings['defaultNick']);
	Drupal.shoutbox.defaultMsg = (shoutboxSettings['defaultMsg']);
	Drupal.shoutbox.defaultUrl = (shoutboxSettings['defaultUrl']);
	Drupal.shoutbox.attachForm();
	Drupal.shoutbox.attachShoutAddForm()
    });
};
