$(document).ready(function(){
  // Hide forms
  $('form').hide().end();
  
  // Processing
  /*if(!$.browser.opera && !$.browser.msie){
    $('form').find('li > label').each( function( i ){
      var labelContent = this.innerHTML;
      var labelWidth = document.defaultView.getComputedStyle( this, '' ).getPropertyValue( 'width' );
      var labelSpan = document.createElement( 'span' );
          labelSpan.style.display = 'block';
          labelSpan.style.width = labelWidth;
          labelSpan.innerHTML = labelContent
      this.style.display = '-moz-inline-box';
      this.innerHTML = null;
      this.appendChild( labelSpan );
    } ).end();
  }*/
  
  // We should be fine to remove this with the new CSS rewrite -tjt
  /*if (navigator.userAgent.indexOf("Firefox")!=-1){
    headers = function(){
      element = $(this)
      $(this)
        .after('<h3 style="margin-top: -11px"></h3>').next()
        .html(element.html())
        .prev().remove()
    }
    $('div#body form > fieldset > legend').each(headers)
    $('div#summary form > fieldset > legend').each(headers)
    $('form').find('fieldset fieldset legend + label').each(function(i){
      this.style.marginTop = '-21px'
    })
  }*/
  
  $('form fieldset div.fieldWithErrors').each(function(){
    $(this).prev().css('color', 'red')
  })
  
  $("#advanced-link p a").click(function() {
    var $this = $(this);
    if( $this.is('.closed') ) {
      $this.addClass("open");
      $this.removeClass("closed");
    } else {
      $this.addClass("closed");
      $this.removeClass("open");
    }
    $("#advanced-options").toggle();
  });
  
  $("#user_delete_avatar").click(function() {
    if ($(this).attr('checked')){
      $('#user_uploaded_avatar').hide();
    } else {
      $('#user_uploaded_avatar').show();
    }
  });

  $("#select_all").click(function(){
    var checked_status = this.checked;
    $("input[@type=checkbox]").each(function(){
      this.checked = checked_status;
    });
  });
  
  $('#read').click(function(){
    $('input[name=do]').attr('value', 'read')
  });
  
  $('#delete').click(function(){
    $('input[name=do]').attr('value', 'delete')
  });
  
  // Show forms
  $( 'form' ).show().end();

  var email_val = $('#sidebar .login .email label').text();
  var pass_val = $('#sidebar .login .password label').text();
  $('#sidebar .login .email input').val(email_val);
  $('#sidebar .login .password input').val(pass_val);


  $('#sidebar .login .email input').focus(function(){
	var val = $('#sidebar .login .email label').text();
	if ($(this).val() == val) { 
	  $(this).val("");
	}
  });

  $('#sidebar .login .email input').blur(function(){
	var val = $('#sidebar .login .email label').text();
	if ($(this).val() == "") { 
	  $(this).val(val);
    }
  });

  $('#sidebar .login .password input').focus(function(){
 	var val = $('#sidebar .login .password label').text();
 	if ($(this).val() == val) { 
	  $(this).val("");
	}
  });

  $('#sidebar .login .password input').blur(function(){
	var val = $('#sidebar .login .password label').text();
	if ($(this).val() == "") { 
	  $(this).val(val);
    }
  });

})