//////////////////////////////////////////////////////////////////////////////
//	jaxinput
//////////////////////////////////////////////////////////////////////////////
Jax.Widgets.Input = function() {

    // Declare class and set instance.
    Jax.oop.declare(Jax.Widgets.Input, Jax.Widgets.Widget);
    this.setInstance();
    
    // Set properties.
    this.element = null;
    this.error = false;
    var m_handlers = [];
    var m_show = "inline";
    var m_this = this;

    //////////////////////////////////////////////////////////////////////////
    //	bind
    //////////////////////////////////////////////////////////////////////////
    this.bind = function(eventType, handler) {
        this.checkHandler(handler);
        var elEvent = Jax.EventTypes.getItem(eventType);
        this.element[elEvent] = onEvent;
        m_handlers[eventType] = handler;
    };

    //////////////////////////////////////////////////////////////////////////
    //  clearError
    //////////////////////////////////////////////////////////////////////////
    this.clearError = function() {
        this.error = false;
    };
    
    //////////////////////////////////////////////////////////////////////////
    // checkHandler
    //////////////////////////////////////////////////////////////////////////
    this.checkHandler = function(handler) {
        if (!Jax.js.isFunction(handler)) {
            throw new Error("handler is not a function type");
        }
        else {
            return true;
        }
    };
    
    //////////////////////////////////////////////////////////////////////////
    // blurHandler
    //////////////////////////////////////////////////////////////////////////
    this.blurHandler = function(handler) {
        this.bind("blur", handler);
    }
    
    //////////////////////////////////////////////////////////////////////////
    // clickHandler
    //////////////////////////////////////////////////////////////////////////
    this.clickHandler = function(handler) {
        this.bind("click", handler);
    }
    
    //////////////////////////////////////////////////////////////////////////
    // focusHandler
    //////////////////////////////////////////////////////////////////////////
    this.focusHandler = function(handler) {
        this.bind("focus", handler);
    }
    
    //////////////////////////////////////////////////////////////////////////
    // keypressHandler
    //////////////////////////////////////////////////////////////////////////
    this.keypressHandler = function(handler) {
        this.bind("keypress", handler);
    }
    
    //////////////////////////////////////////////////////////////////////////
    // hide()
    //////////////////////////////////////////////////////////////////////////
    this.hide = function() {
        if (this.isVisible()) {
            m_show = this.getStyle("display");
            this.element.style.display = "none";
        }
    }
	
    //////////////////////////////////////////////////////////////////////////
    // show()
    //////////////////////////////////////////////////////////////////////////
	this.show = function() {
        if (!this.isVisible()) {
            this.element.style.display = m_show;
        }
	}
	
    //////////////////////////////////////////////////////////////////////////
    //  errorState
    //////////////////////////////////////////////////////////////////////////
    this.setError = function(state) {
        this.error = state;
    };

    //////////////////////////////////////////////////////////////////////////
    //  enable
    //////////////////////////////////////////////////////////////////////////
    this.enable = function(state) {
        return this.element.disabled = !state;
    };

    //////////////////////////////////////////////////////////////////////////
    //  readonly
    //////////////////////////////////////////////////////////////////////////
    this.readOnly = function(state) {
        return this.element.readOnly = state;
    };

    //////////////////////////////////////////////////////////////////////////
    //	isEmpty
    //////////////////////////////////////////////////////////////////////////
    this.isEmpty = function() {
        return (this.element.value.toString().trim() === "");
    };

    //////////////////////////////////////////////////////////////////////////
    //	isEnabled
    //////////////////////////////////////////////////////////////////////////
    this.isEnabled = function() {
        return !this.element.disabled;
    };

    //////////////////////////////////////////////////////////////////////////
    //	isError
    //////////////////////////////////////////////////////////////////////////
    this.isError = function() {
        return this.error;
    };

    //////////////////////////////////////////////////////////////////////////
    // isVisible()
    //////////////////////////////////////////////////////////////////////////
    this.isVisible = function() {
        return this.getStyle().display !== "none";
    }
    
    //////////////////////////////////////////////////////////////////////////
    // onKeyPress()
    //////////////////////////////////////////////////////////////////////////
    this.onKeyPress = function(evnt) {
        return true;
    }

    //////////////////////////////////////////////////////////////////////////
    //	setFocus
    //////////////////////////////////////////////////////////////////////////
    this.setFocus = function() {
        this.element.focus();
        if (this.element.type == "text") {
            this.element.select();
        }
    };
    
    //////////////////////////////////////////////////////////////////////////
    //  Style
    //////////////////////////////////////////////////////////////////////////
    this.getStyle = function(property) {
        var style = (window.getComputedStyle)
            ? window.getComputedStyle(this.element, null)
            : (this.element.currentStyle)
                ? this.element.currentStyle
                : this.element.style;

        return (property) ? style[property] : style;
    }

    //////////////////////////////////////////////////////////////////////////
    // Value
    //////////////////////////////////////////////////////////////////////////
    this.getValue = function() {
        return this.element.value.toString().trim();
    }
    
    this.setValue = function() {
        if (arguments.length) {
            this.element.value = arguments[0];
        }
    }
    
    //////////////////////////////////////////////////////////////////////////
    //	validate : Validate input control.
    //////////////////////////////////////////////////////////////////////////
    this.validate = function() {};
    
    //////////////////////////////////////////////////////////////////////////
    //	Helpers
    //////////////////////////////////////////////////////////////////////////
    function onEvent(evnt) {
        var theEvent = new Jax.Event.create(evnt);
        return m_handlers[theEvent.getEventType()](m_this, theEvent);
    }
};