/*! NV Forms v2.0 <http://nvinteractive.co.nz>
	Copyright (c) NV Interactive
	
	References:
		utilities.2.1.js
		swfobject.js
		jquery-1.2.6.js
		
	Release Notes:
		2.0
		-- rewrote for jQuery
		
*/

nv_forms = function() {

    var TYPES = "input, textarea";

    var init = function() {

        $(TYPES)
			.bind("focus", _focus)
			.bind("blur", _blur);
        //			.filter(":password").each(initPasswordField);
        /*
        for(var t=0; t<types.length; t++){
        var fields = getElement
        sBySelector(types[t]);
	
			for (i=0; i<fields.length; i++) {
        var node = fields[i];
        if (node.type == "text" || node.type == "password" || node.type == "textarea" || node.type.indexOf("select") >= 0) {
        if(node.type == "password"){
        initPasswordField(node);
        initField(node);
        }else{
        initField(node);
        }
        }
				
			}
        }*/
    }


    function initPasswordField() {

        var iv = $(this).attr("initialvalue");

        var openField = document.createElement("input");

        with (openField) {
            type = "text";
            value = iv;
            id = "open_" + this.id;
        }

        openField.obscuredField = this;
        this.openField = openField;

        $(this).replaceWith(openField);
        $(openField)
			.bind("focus", _focus)
			.bind("blur", _blur);

        this.ispassword = true;
        openField.ispassword = true;

    }


    function _focus(evt) {

        var field = this;

        if (this.ispassword) {
            if (this.obscuredField) {
                $(this).replaceWith(this.obscuredField);
                field = this.obscuredField;
                $(field)
					.bind("focus", _focus)
					.bind("blur", _blur);
            }
        }

        if (field.value == $(field).attr("initialvalue")) field.value = "";
        field.select();
        $(field).addClass("focus");

    }

    function _blur() {

        var field = this;
        if (this.ispassword) {
            if (this.openField && this.value == "") {
                $(this).replaceWith(this.openField);
                field = this.openField;
                $(field)
					.bind("focus", _focus)
					.bind("blur", _blur);
            }
        }

        if (field.value == "" && $(field).attr("initialvalue")) field.value = $(field).attr("initialvalue");
        $(field).removeClass("focus");

    }

    return {
        /* Public API
        */
        init: init
    }

} ();

$(document).ready(nv_forms.init);
