/*!
Cross-browser placeholder handling
Author: Sebastian Krause, www.codeline.de
Requires jQuery

Description:
Checks for native placeholder attribute support. If support is missing labels are placed over input
fields (text, password, tel, email, url) using position: absolute (inside css file).

Required markup:
parentElement <- position: relative
label|for=myinput
input|id=myinput
/parentElement

Does not work with jQuery.placeholder plugin!

Change log:
1.0: 	initial version
1.1: 	updated check support function
		
*/
$(function() {
    (function($) {
        (function() {
            $.support.placeholder = false;
            var test = document.createElement('input');
            if ('placeholder' in test) $.support.placeholder = true;
        } ());

        var _inputs = $("input").filter(":text, :password, [type=tel], [type=email], [type=url]"),
		    _support = $.support.placeholder;

        if (_inputs.length === 0) {
            return false;
        }

        _inputs.each(function(i) {
            var _this = $(this),
			    _label = $("label[for=" + this.id + "]"),
			    _pos = [],
			    _width = 1;

            // browser has support, hide labels
            if (_support) {
                _label.addClass("visuallyhidden");
                return true;
            }

            _pos = _this.position();

            if (_this.attr('class') == 'fm-text-half last-child') {
                if (parseInt(_pos.left) == 0) {
                    _width = 267;
                }
            }
            if (_this.attr('class') == 'fm-text-1fourth last-child') {
                if (parseInt(_pos.left) == 0) {
                    _width = 397;
                }
            }
            if (_this.attr('class') == 'fm-text-3fourth last-child') {
                if (parseInt(_pos.left) == 0) {
                    _width = 137;
                }
            }

            // position label on input
            _label.addClass("placeholder-label").css({
                'left': _pos.left + parseInt(_this.css("paddingLeft"), 10) + _width
            });

            // onload check
            if (hasValue(_this)) {
                _label.hide();
            }

            _this
			.focus(function() {
			    _label.hide();
			})
			.blur(function() {
			    if (!hasValue(_this)) {
			        _label.show();
			    }
			});

            // trigger focus on label click
            _label.click(function() {
                _this.trigger('focus');
            });
        });

        // helper functions
        function hasValue($el) {
            return ($el.val().length === 0) ? false : true;
        }
    } (jQuery));
});

