$(document).ready( function(){

/* Set up Magic Classes/IDs
-------------------------------------------------------------------------------------- */ 
  $('.select_all').click(function(){
    $('input.delete_this').each(function(i){
      //console.log('id: ' + i);
      $(this).attr('checked', 'checked');
    });
  });
  
  $('.deselect_all').click(function(){
    $.each('input.delete_this', function(){
      //console.log('user id: ' + $(this).attr('id').split('delete_')[1]);
      $(this).removeAttr('checked');
    });
  });
  
  $('#search-field').click(function(){
    $('#search-field').val('').unbind('click');
  });
    
  $('#delete_selected').click(function(){
		  var msg_ids = new Array();		  
  		$('.delete_this').each(function(){
        if ($(this).attr('checked')) {
          msg_ids.push($(this).attr('id').split('delete_')[1]);
        }
        
      });
      $('#recipient_ids').val(msg_ids);
      
      //alert('submit?');
      $('#multi_delete').submit();
    
	});
	
  $('#edit_status').click(function(){
    $('#status').fadeOut('fast', function(){
      $('#update_status').fadeIn('slow');
    });
  });
  
  $('#show_log_in').click(function(){
    phx.modals.show_modal('nada', 0, 'login');
  
  });
  
  $('#confirm_all').click(phx.pages.notifications.confirm_friends);

/* Log In Validation
-------------------------------------------------------------------------------------- */    
	var login_validator = $("#login_form").validate(phx.forms.validate.log_in_form);
	
/* Forgot Password Validation
-------------------------------------------------------------------------------------- */    
  var forgot_validator = $("#login_form").validate(phx.forms.validate.forgot_form);


/* Join Validation
-------------------------------------------------------------------------------------- */    
	var join_validator = $("#big_join_form").validate(phx.forms.validate.big_join_form);
	

/* Buy Votes Validation
-------------------------------------------------------------------------------------- */
	var votes_validator = $("#buy_votes_cc_form").validate(phx.forms.validate.buy_votes_cc_form);
	

/* Reply Validation
-------------------------------------------------------------------------------------- */
	var reply_validator = $("#reply_form").validate(phx.forms.validate.reply_form);
	
  $('#submit_reply').mouseup(function(){
		if ($('.error').length === 0 && $('#message_subject').val().length !== 0 && $('#message_body').val().length !== 0) {
	    setTimeout('$("#submit_reply").attr({src: "/images/phoenix/buttons/btn_please_wait.png", disabled: true})', 150);
	  }
	});
	
/* To Fans Validation
-------------------------------------------------------------------------------------- */
	var fans_validator = $("#fans_form").validate(phx.forms.validate.fans_form);
	
  $('#fans_message').mouseup(function(){
		if ($('.error').length === 0 && $('#message_subject').val().length !== 0 && $('#message_body').val().length !== 0) {
	    setTimeout('$("#fans_message").attr({src: "/images/phoenix/buttons/btn_please_wait.png", disabled: true})', 150);
	  }
	});
	
/* To Friends Validation
-------------------------------------------------------------------------------------- */
	var friends_validator = $("#friends_form").validate(phx.forms.validate.friends_form);
	
  $('#friends_submit').mouseup(function(){
		if ($('.error').length === 0 && $('#message_subject').val().length !== 0 && $('#message_body').val().length !== 0) {
	    setTimeout('$("#friends_submit").attr({src: "/images/phoenix/buttons/btn_please_wait.png", disabled: true})', 150);
	  }
	});
	
/* Status Validation
-------------------------------------------------------------------------------------- */    
	var status_validator = $("#status_form").validate(phx.forms.validate.status_form);
	
	$('input#username').click(function(){
	  $(this).val('').unbind('click');
	});

/* Buy Votes Actions
-------------------------------------------------------------------------------------- */    

  $('.show_buy_votes').click(phx.buy_votes.show_buy_more_votes); // show the buy votes menu
  $('#btn_up').click(phx.buy_votes.votes_up); // incriment the votes up on the buy votes modal
  $('#btn_down').click(phx.buy_votes.votes_down); // incriment the votes down on the buy votes modal
  $('#votes_cancel_btn').click(phx.buy_votes.votes_cancel_btn); // close buy votes modal
  
  $('#paypal_btn').click( function() {
    track_page_view('PURCH_VOTES_CLICK_PAYPAL'); // was: PURCH_VOTES_CLICK
  });
  
  $('#processor').change(function(){
    var processor = $(this).val();
    if (processor == 'credit') {
      $('#subscribe_by_credit').slideDown();
    } else if (processor == 'paypal') {
      $('#subscribe_by_credit').slideUp();
    }
  });
  
  $('#use_credit_card').click(function(){
    if ($.browser.msie) {
      $('#buy_votes_quantity').fadeOut('fast', function(){ // fade this out so it doesn't hang off the edge on slideUp
        $('#buy_votes_quantity_form').slideUp('slow', function(){
          $('#buy_votes_card_form').slideDown('slow');
          $('#buy_votes_footer').fadeOut('fast');
        });
      });
    } else {
      $('#buy_votes_quantity_form').slideUp('slow', function(){
        $('#buy_votes_card_form').slideDown('slow');
        $('#buy_votes_footer').fadeOut('fast');
      }); 
    }
    track_page_view('PURCH_VOTES_CLICK_CC');
  });
  
  $('#roll_back_form').click(function(){
    if ($.browser.msie) {
      $('#order_total').fadeOut('slow'); // fade this out so it doesn't hang off the edge on slideUp
      $('#buy_votes_card_form').slideUp('slow', function(){
        $('#buy_votes_quantity_form').slideDown('slow', function(){
          $('#buy_votes_quantity').fadeIn('fast'); //fade this in AFTER the slideDown so it doesn't hang
          $('#buy_votes_footer').fadeIn('fast');
          $('#order_total').fadeIn('fast'); //reset this as it doesn't look bad on slideDown just up
        });
      });
    } else {
      $('#buy_votes_card_form').slideUp('slow', function(){
        $('#buy_votes_quantity_form').slideDown('slow');
        $('#buy_votes_footer').fadeIn('fast');
      });
    }
  });
  
  $('#buy_votes_close').click(phx.buy_votes.votes_cancel_btn);
  $('#thanks_close').click(phx.buy_votes.thanks_cancel_btn);
  	
	$('#buy_now').mouseup(function(){
	  if ($('.error').length === 0 && $('#billing_name').val().length !== 0 && $('#credit_card_number').val().length !== 0  && $('#exp_month').val().length !== 0 && $('#exp_year').val().length !== 0 && $('#ccv').val().length !== 0 && $('#address_1').val().length !== 0 && $('#city').val().length !== 0 && $('#state').val().length !== 0 && $('#country').val().length !== 0) {
	    setTimeout('$("#buy_now").attr({src: "/images/buttons/btn_submitting.jpg", disabled: true})', 150);
	  }
	});
	
	$('ul#credit_card_form li input').focus(function(){
	  if ($(this).attr('id') == 'buy_now') {
	    return;
	  } else {
	    $(this).addClass('current_field');
	  }
	});
	
	$('ul#credit_card_form li input').blur(function(){
	  if ($(this).attr('id') == 'buy_now') {
	    return;
	  } else {
  	  $(this).removeClass('current_field');
	  }
	});
	
	$('ul#credit_card_form li select').focus(function(){
	  $(this).addClass('current_field');
	});
	
	$('ul#credit_card_form li select').blur(function(){
	  $(this).removeClass('current_field');
	});
	
	$('p#whats_this').click(function(e) { // ccv hover-menu			
		$('#ccv_drop').css("left", (e.pageX - 250) +'px');
		$('#ccv_drop').css("top", (e.pageY - 50) +'px');
		$('#ccv_drop').fadeIn('fast');
  });
	
	$('#ccv_drop').hover(function() {	
		$('#ccv_drop').addClass("imOver"); // stamp it	
	},function(){
		$('#ccv_drop').fadeOut('fast'); // make the hover menu go away!	
		$('#ccv_drop').removeClass("imOver"); // stamp it	
	
	});

});