/*
 *
 * placeHolder
 *
 * Enables placeholder texts in input fields
 * 
 ******************************************** */

$(document).ready(function() {

    function changeInput(input){
        var newInput

        var parent = $(input).wrap('<label></label>').parent('label')

        if ($(input).attr("type") == "password"){
            if (parent.html().indexOf('"password"') != -1) {
                newInput = parent.html().replace(/type=\"?password\"?/, 'type="text"')
            }
            else {
                newInput = parent.html().replace("<INPUT", '<INPUT type="text"')
            }
        }
        else{
            if (parent.html().indexOf('"text"') != -1) {
                newInput = parent.html().replace(/type=\"?text\"?/, 'type="password"')
            }
            else {
                newInput = parent.html().replace("<INPUT", '<INPUT type="password"')
            }
        }

        parent.html(newInput)

        var newInput = parent.children(":first")

        newInput.unwrap()

        if (newInput.attr("type") == "password"){
            newInput.focus()
        }

        return newInput
    }

    $.each($("form"), function(index, form) {

        var placeholders = $(form).find('input:text[placeholder],input:password[placeholder]')

        $.each(placeholders, function(index) {

            if(index == 0){

                // Add a handler for the form submit
               $(form).submit(function(e){

                    var placeholdersElements = $(form).find('input:text[placeholder],input:password[placeholder]')

                    $.each(placeholdersElements, function() {
                        if ($(this).val() == $(this).attr("placeholder")){
                            $(this).val("")
                            e.preventDefault();
                        }
                    });
                });
            }


            var placeholder;
            var input = this
            addHandlers(this,placeholder,isPasswordInput)
            var isPasswordInput = $(input).is("input:password")

            if ($(input).attr("value") && $(input).attr("value") != $(input).attr("placeholder")){
                placeholder = $(input).attr("placeholder");
                $(input).addClass("changed");
            }
            else {

                if (isPasswordInput){
                    input = changeInput(input)
                    addHandlers(input,placeholder,isPasswordInput)
                }
                $(input).attr("value",$(input).attr("placeholder"))
                $(input).addClass("placeholder")
            }

        });
    });

    function addHandlers(input,placeholder,isPasswordInput){

		$(input).focus(
			function(){
                if (isPasswordInput && $(input).attr("type") != "password"){
                    input = changeInput(input)
                    $(input).focus()
                }

				if(!$(input).hasClass("changed")){
					placeholder = $(input).attr("value");
					$(input).addClass("changed");
					$(input).attr("value","");
					$(input).removeClass("placeholder");
				}
			}
		);


		$(input).blur(
			function(){
				if($(input).attr("value").length<1){
					$(input).attr("value",placeholder);
					$(input).removeClass("changed");
					$(input).addClass("placeholder");
				}
			}
		);

    }
});
