/*
 * jQuery Placeholder Plug-in
 *
 * Copyright (c) 2010 Max Davis
 *
 * Dual licensed under the MIT and GPL licenses:
 *   http://www.opensource.org/licenses/mit-license.php
 *   http://www.gnu.org/licenses/gpl.html
 *
 * Revision: 1
 * Version: 0.1
 *
 * v0.1
 * - First public release
 *
*/

var first_focus = true;




(function($){
var verify_subdomain = function(place) {
    $.ajax({
       url: "/",
       data: {'test_subdomain': $(place).val().toLowerCase()},
       success: function(data) {
           if (!data['valid']) {
               $("#invalid_subdomain_alert").css("color", "red").html("!").show();
               $("#subdomain").css("border-color", "red");
               //$('#username').css("color","#CCCCCC");
           } else {
               $("#invalid_subdomain_alert").css("color", "green").html("+").show();
               $("#subdomain").css("border-color", "#CCCCCC");
           }
       },
       dataType: 'json'
    });
}
	$.fn.placeholder = function() {

		if($(this).attr("type") == "password") {

			var original_pass_field = $(this);

			if(original_pass_field.val() == "") {
				$(this).parent().append("<input type=\"text\" style=\"color: #CCCCCC;\" value=\""+ $(this).attr("placeholder") +"\" name=\"pass_placeholder\" class=\"form-text\" id=\"pass_placeholder\"  >");
				$(this).css("display","none");
			} else {return;}

			var original_pass_field = $(this);

			$("#pass_placeholder").focus(function(event) {
					$("#pass_placeholder").css("display","none");
					original_pass_field.css("display","");
					original_pass_field.focus();
                                        event.preventDefault();
			}).keydown(function(event) {
                            if (event.keyCode == '9') {
                                $("#login_submit_button").focus();
                            }
                        });

			original_pass_field.blur(function() {
				if(original_pass_field.val() == "") {
					$("#pass_placeholder").css("display","");
					original_pass_field.css("display","none");
				}
			});

		} else if ($(this).attr("id") == "username") {

			if($(this).val() === "" || $(this).val() == $(this).attr("placeholder")) {
				$(this).val($(this).attr("placeholder"));
				$(this).css("color","#CCCCCC");
			}

                        $(this).mouseup(function(event) {
                            event.preventDefault();
                        });
			$(this).focus(function(event) {
				if($(this).val() === $(this).attr("placeholder")) {
					$(this).css("color","#000000");
					$(this).select(); //val("");
                                        event.preventDefault();
				}


			}).blur(function(event) {
                                if ($(this).val() == $(this).attr("placeholder") || $(this).val() == "") {
                                    $(this).css("color","#CCCCCC");
                                    $(this).val($(this).attr("placeholder"));
                                }
			}).keydown(function(event) {
                            if (event.keyCode == '9') {
                                event.preventDefault();
                                if ($("#password").is(":visible")) {
                                    $("#password").focus();
                                } else {
                                    $("#pass_placeholder").focus();

                                }
                                if ($(this).val() == $(this).attr("placeholder") || $(this).val() == "") {
                                    $(this).css("color","#CCCCCC");
                                    $(this).val($(this).attr("placeholder"));
                                }
                            }
                        });
		} else {

			if($(this).val() === "") {
				$(this).val($(this).attr("placeholder"));
				$(this).css("color","#CCCCCC");
			}

                        $(this).mouseup(function(event) {
                            event.preventDefault();
                        });
			$(this).focus(function(event) {
				if($(this).val() === $(this).attr("placeholder")) {
                                    $(this).css("color","#000000");
                                    $(this).select(); //val("");
                                    event.preventDefault();
				}


			}).blur(function(event) {
                                if ($(this).val() == $(this).attr("placeholder") || $(this).val() == "") {
                                    $(this).css("color","#CCCCCC");
                                    $(this).val($(this).attr("placeholder"));
                                } else {
                                    verify_subdomain(this);
                                }

			}).keydown(function(event) {
                            if (event.keyCode == '9') {
                                event.preventDefault();
                                $('#username').focus();
                                if ($(this).val() == $(this).attr("placeholder") || $(this).val() == "") {
                                    $(this).css("color","#CCCCCC");
                                    $(this).val($(this).attr("placeholder"));
                                }
                                verify_subdomain(this);
                                $('#username').focus().select;
                            }
                        });

                }

	}

})(jQuery);
