// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

$( function() {
	$('input[placeholder],textarea[placeholder]').placeholder();
	
	$( "#close-notice" ).click( function(e) {
		close_notice();
		e.preventDefault();
	})
})

jQuery.fn.placeholder = function() {
   function toggle( arr ) {
    for ( i = 0; i < arr.length; i++ ) {
      arr[i].toggle();
    }
  }

  return this.each(function(){
    var elem = $(this);
    var is_password = ( elem.attr( "type" ) == "password" ) ? true : false;

    if( elem.val() == "" || elem.val() == elem.attr( 'placeholder' ) ) {
      elem.val(elem.attr('placeholder'))
      elem.addClass('waiting');
    }

    elem.focusin(function() {

      if(elem.val() == elem.attr('placeholder'))
        elem.val('').removeClass('waiting');

    }).focusout(function() {

      if(elem.val() == '')
        elem.val(elem.attr('placeholder')).addClass('waiting');

    });

    elem.closest( "form" ).submit( function() {
      if( elem.val() == elem.attr( 'placeholder' ) ) {
        if( is_password ) {
          toggle( [ elem, elem_stub ] );
        }

        elem.val( "" );

      }

      return true;
    })

    if ( is_password ) {
      var elem_stub = $( '<input type="text" class="' + 'waiting' + '" value="' + elem.attr('placeholder') + '" />' );

      elem_stub.focus( function() {
        toggle( [elem, elem_stub] );
        elem.focus();
      });

      elem.hide().after( elem_stub );
    }
  });
};

var notice = function( text ) {
	$("#notice-content").html( text );
	$("#notice").css( "left", ($("body").width() - $("#notice").width())/2);
	$("#notice").fadeIn();
}

var close_notice = function() {
	$("#notice").fadeOut();
}
