$(document).ready(function() {
    // Big spaghetti block of Javascript, don't feel like dealing with it now.
    if ($('.mu-admin-user-show').length) {
        return;
    }

    $('#user-meta label').hide();
    $('#user-meta label.optional').show();
    $('#login-form #username').inputFieldText('username', 'hint');
    $('#login-form #password').inputFieldText('password', 'hint');
    $("input#domain").after(' <em class="domain">.omeka.net</em>');
    
    // Adds a class of 'last' to a few elements, for styling purposes
    $('.plan').last().addClass('last');
    $('.plan').hover(
        function() {
            $(this).addClass('hover');
        },
        function() {
            $(this).removeClass('hover')
        }
    );
    
	$('li:last-child').addClass('last');
	
    // Makes tooltops for plan features
	$('.plan li p').hide().addClass('tooltip');
	$(".plan li.plan-themes, .plan li.plan-plugins").addClass('more-info').hover(
	    function() {
	        $(this).addClass('active');
            $(this).children("p").stop(true, true).fadeIn();
	    }, 
	    function() {
	        $(this).removeClass('active');
	        $(this).children("p").stop(true, true).fadeOut();
	    }
	).css({'cursor': 'pointer'});
	
	$('#plans #silver').addClass('highlight');
	
    // Add a choose button to each panel

    
    var planFormInput = $('select#plan'); 
    var planFormInputParent = planFormInput.parent().parent();
    
    var signUpContainer = $('#sign-up-container');
    
    var planName = planFormInput.val();
    
    $(this).updatePlanHeading(planName, planFormInputParent, signUpContainer);
        
    if (!$('#signup-form').find('ul.errors').length) {
        signUpContainer.hide();
    }
    
    $('#plans #plans-list').each(function(){
        $(this).children().each(function(i) {
            var planName = $(this).attr('id');
            var button = $('<button class="button">Choose</button>');
            $(this).append(button);
            button.click(function() {
                button.parent().siblings().removeClass('selected highlight');
                button.parent().addClass('selected');
                $(this).updatePlanHeading(planName, planFormInputParent, signUpContainer);
                signUpContainer.show('slow');
                planFormInput.val(planName);
                
            });
        });
    });
    
    $('p#basic').each(function(){
        var planName = $(this).attr('id');
        var button = $('<button class="button">Choose</button>');
        $(this).append(button);
        button.click(function() {
            $('#plans #plans-list').children().removeClass('selected highlight');
            $(this).updatePlanHeading(planName, planFormInputParent, signUpContainer);
            signUpContainer.show('slow');
            planFormInput.val(planName);
        });
    });
    
    $('#select-free-plan').click(function() {
        var planName = 'basic';
        $('#plans #plans-list div').removeClass('selected highlight');
        planFormInputParent.children().hide();
        signUpContainer.find('h2').remove();
        signUpContainer.prepend('<h2 id="chosen-plan-text">You have chosen the <em>'+planName+'</em> plan.</h2>');
        planFormInput.val(planName);
        signUpContainer.show("slow");
    });
    
    // Makes plans panels the same height, regardless of content.
    $('#plans #plans-list').each(function(){
		var currentTallest = 0;
		$(this).children().each(function(i){
			if ($(this).height() > currentTallest) { currentTallest = $(this).height(); }
		});
		// for ie6, set height since min-height isn't supported
		if ($.browser.msie && $.browser.version == 6.0) { 
		    $(this).children().css({'height': currentTallest}); 
		}
		$(this).children().css({'min-height': currentTallest}); 
	});
	
	$('#choose-plan .plan label').click(function(){
	    var planName = $(this).find('input').val();
	    $('#choose-plan-continue em').remove();
	    $('#choose-plan-continue').prepend('<em>Yes, I want to upgrade to the <span>'+ planName +'</span> plan.</em>');
	});

});

jQuery.fn.updatePlanHeading = function(planName, planFormInputParent, signUpContainer) {
    planFormInputParent.children().hide();
    signUpContainer.find('h2').remove();
    signUpContainer.prepend('<h2 id="chosen-plan-text">You have chosen the <em>'+planName+'</em> plan.</h2>');
}

// inputFieldText function. Let's you overlay hint text onto form elements.
jQuery.fn.inputFieldText = function(string, hintClass) {
  this.each(function() {
      $(this).addClass(hintClass).val(string);
      $(this).focus(function(){
        if ($(this).val() == string){
          $(this).removeClass(hintClass).val('');
        }
      });
      $(this).blur(function(){
        if ($(this).val() == '' ){
          $(this).addClass(hintClass).val(string);
        }
      }); 
  });
}

$(document).ready(function() {
    $('.mu-admin-sites .deactivate-button').submit(function() {
        return confirm("Are you sure you want to deactivate this site?");
    });
    $('.mu-admin-sites .activate-button').submit(function() {
        return confirm("Are you sure you want to activate this site?");
    });
    $('.mu-admin-users .deactivate-button').submit(function() {
        return confirm("Are you sure you want to deactivate this user account?");
    });
    $('.mu-admin-users .activate-button').submit(function() {
        return confirm("Are you sure you want to activate this user account?");
    });
});

$(document).ready(function() {
    var selectAll = $('#select-options .select-all');
    if (!selectAll.length) {
        return false;
    }
    var selectNone = $('#select-options .select-none');
    var boxes = $('input[type="checkbox"]');
    selectAll.click(function() {
        boxes.attr('checked', true);
    });
    selectNone.click(function() {
        boxes.attr('checked', false);
    });
});

$(document).ready(function() {
    var form = $('form#mail');
    if (!form.length) {
        return false;
    }
    form.submit(function() {
        return confirm("Are you sure? This cannot be undone.");
    });
});

