﻿
/*!
* Pixelchild PX Form Plugin v1.0.x beta
* www.pixelchild.com.au/px/
*
* Copyright 2010, Byron Salau
* Released under the MIT and GPL Licenses.
* www.pixelchild.com/px/license/
*
* Stable-Requirement: jQuery V1.4.4
*
* Date: Sat 22 Jan 15:55:04 2011 +1100
*/
(function ($) {
    $.fn.pxForm = function (options) {
        var defaults = {
            showTextClearButton: false,
            debugMode: false
        };

        var options = $.extend(defaults, options);
        var navigator_is_ie7 = (navigator.appVersion.indexOf("MSIE 7.") == -1) ? false : true;

        $('input, select, label', this).each(function () {

            var $obj = $(this);

            if ($obj.attr('id').length == 0 && !$obj.is('label')) {
                if ($obj.attr('type') != "submit" || $obj.attr('type') != "button" && typeof window.console != 'undefined')
                    console.error("'" + $obj[0].tagName + "' is missing required attribute 'id' when using pxForm()");
            } else {
                if ($obj.is("input")) {
                    /*
                    * Checkbox Methods
                    */
                    if ($obj.attr('type').toLowerCase() == "checkbox" || $obj.attr('type').toLowerCase() == "radio") {
                        var isRadio = $obj.attr('type').toLowerCase() == "radio" ? true : false;
                        if (!options['debugMode']) { $obj.hide(); }
                        $obj.after('<a id="pxForm_' + $obj.attr('id') + '" href="#">' + $obj.val() + '</a>');
                        var target = $('#pxForm_' + $obj.attr('id'));
                        $obj.data('associatedId', target.attr('id'));

                        if ($obj.attr('checked')) {
                            target.addClass('checked');
                        }
                        if (isRadio) {
                            var family = 'pxFamily_' + $obj.attr('name').replace(/\$/g, "");
                            $obj.data('family', family);
                            target.addClass('pxRadio').addClass(family);
                        } else {
                            target.addClass('pxCheckbox');
                        }

                        target.bind('click', function () {
                            if (isRadio) {
                                var family = $('.' + $obj.data('family'));
                                family.removeClass('checked');
                                $(this).addClass('checked');
                                $obj.attr('checked', true);
                            } else {
                                if ($obj.attr('checked')) {
                                    $obj.attr('checked', false);
                                    target.removeClass('checked');
                                } else {
                                    $obj.attr('checked', true);
                                    target.addClass('checked');
                                }
                            }
                            return false;
                        });
                    }
                    else if ($obj.attr('type').toLowerCase() == "text") {
                        /*
                        * Text Box Methods
                        */
                        if (options['showTextClearButton']) {
                            $obj.wrap('<span class="pxForm-txtWrap" />');
                            $obj.after('<a tabIndex="-1" id="pxForm_' + $obj.attr('id') + '" href="#" class="pxForm-btnClear">Clear Text</a>');
                            var target = $('#pxForm_' + $obj.attr('id'));
                            var wrap = target.parent();

                            $obj.keyup(function () {
                                if ($(this).val().length > 0)
                                    target.show();
                            });

                            $obj.blur(function () {
                                if (!target.focus()) {
                                    $(this).hide();
                                }
                            });

                            target.bind('click', function (e) {
                                target.hide();
                                $obj.val('').trigger('focus');
                                return false;
                            });

                            wrap.bind('mouseenter mouseleave', function (e) {
                                if (e.type == "mouseenter" && $obj.val().length > 0)
                                    target.show();
                                if (e.type == "mouseleave")
                                    target.hide();
                            });
                        }
                    }
                    else if ($obj.attr('type').toLowerCase() == "file") {
                        /*
                        * File Field Methods
                        */
                       // $obj.wrap('<label class="pxFileLbl" />');
                        //$obj.before('<a href="#" id="pxForm_' + $obj.attr('id') + '" class="pxFileBtn">Find File</a>' +
                       //'<input type="text" class="pxFileFieldValue" />');
                       // $obj.addClass('pxFile');
                        //var target = $('#pxForm_' + $obj.attr('id'));
                        //if (!options['debugMode']) { $obj.css({'opacity': 0, 'filter': 0}); }

                    }
                }
                else if ($obj.is("select") && !navigator_is_ie7) {
                    /*
                    * Selectbox Methods
                    */
                    var selected = $('option:selected', $obj);

                    var items = $("option", $obj);
                    $obj.after('<dl id="pxForm' + $obj.attr('id') + '" class="pxDropdown"></dl>');
                    var target = $('#pxForm' + $obj.attr('id'));
                    target.append('<dt><a href="#">' + selected.text() + '</a></dt>');
                    target.append('<dd><ul></ul></dd>');
                    if (!options['debugMode']) { $obj.hide(); }

                    items.each(function (i) {
                        var lineClass = "";
                        if (selected.text() == $(this).text()) {
                            lineClass = "selected";
                        }

                        $("dd ul", target).append('<li class="' + lineClass + '"><a href="#"><span class="text">' +
                             $(this).text() + '</span><span class="value">' +
                             $(this).val() + '</span></a></li>');

                    });

                    $('dt a', target).bind('click', function () {
                        $('ul', target).slideDown('fast').trigger('focus');
                        return false;
                    });

                    $(target).bind('mouseleave', function () {
                        var timeoutTarget = $('ul', target);
                        var timeoutTargetSrc = $('dt a', this);
                        dropDowntimer = setTimeout(function () {
                            timeoutTarget.slideUp('fast');
                        }, 200);
                    });

                    $('ul', target).bind('mouseenter', function () {
                        if (typeof dropDowntimer != 'undefined')
                            clearTimeout(dropDowntimer);
                    });


                    $("dd ul li a", target).bind('click', function () {
                        var text = $('.text', this).html();
                        $("dt a", target).html(text);
                        $("dd ul", target).fadeOut(200);
                        $('dd ul li.selected', target).removeClass('selected');
                        $(this).parent().addClass('selected');
                        $obj.val($(this).find("span.value").html());
                        $obj.change();
                        return false;
                    });

                }
                else if ($obj.is('label')) {
                    /*
                    * Label Methods
                    */
                    if ($(this).attr('for').length > 0) {
                        $(this).bind('click', function () {
                            var $x = $('#' + $(this).attr('for'));
                            $('#' + $x.data('associatedId')).click();
                            return false;
                        });
                    }
                }
            }

            return this;

        });

    };
})(jQuery);
