/*
 * jQuery kdoForms plugin
 *
 * Copyright (c) 2008 Ralf Geschke
 * Dual licensed under the MIT (MIT-LICENSE.txt)
 * and GPL (GPL-LICENSE.txt) licenses.
 * 
 * http://kuerbis.org
 *
 * Depends:
 *	jQuery library
 */
 (function($) {
    $.kdoForms = {

        inputTextField: function(options) {
            var opts = $.extend({},
            $.kdoForms.defaults, options);

            if (opts.wgtName == undefined) {
                return false;
            }
            var wgtName = opts.wgtName;

            var wgtIndex = 0;
            $("#" + wgtName + "_add").click(function() {
                var formInput = $("#" + wgtName + "_input").val();
                if (formInput) {
                    wgtIndex += 1;
                    $("#" + wgtName + "_fields").append("<div id=\"" + wgtName + "_item_" + wgtIndex + "\" style=\"display: none\"><img src=\"" + opts.deleteImg + "\" border=\"0\" />" + formInput + "<input type=\"hidden\" name=\"" + wgtName + "[]\" value=\"" + formInput + "\"></div>");
                    $("#" + wgtName + "_input").val("");
                    $("#" + wgtName + "_item_" + wgtIndex).show("fast");
                    $("#" + wgtName + "_item_" + wgtIndex + " img ").click(function() {
                        $(this).parent().hide("fast",
                        function() {
                            $(this).remove();
                        });
                        return false;
                    });
                }
            });

            $("#" + wgtName + "_input").keypress(function(e) {
                if (e.which == 13)
                {
                    var formInput = $("#" + wgtName + "_input").val();
                    if (formInput) {
                        $("#" + wgtName + "_fields").append("<div><input type=\"hidden\" name=\"" + wgtName + "[]\" value=\"" + formInput + "\"></div>");
                    }
                    return true;
                }
            });
        },

        selectChooser: function(options) {
            var opts = $.extend({},
            $.kdoForms.defaults, options);
            if (opts.wgtName == undefined) {
                return false;
            }
            var wgtName = opts.wgtName;
            var wgtIndex = 0;
            var selectValues = [];
            $("#" + wgtName).children("option").each(function() {
                selectValues[wgtIndex++] = [$(this).val(), $(this).text(), true];
            });
            wgtIndex = 0;
            $("#" + wgtName).change(function() {
                $(this).children("option:selected").each(function() {
                    wgtIndex += 1;
                    var selectValue = $(this).val();
                    var selectText = $(this).text();
                    // set array item to false
                    for (i = 0, len = selectValues.length; i < len; i++) {
                        if (selectValues[i][0] == selectValue)
                        selectValues[i][2] = false;
                    }
                    $("#" + wgtName + "_fields").append("<div id=\"" + wgtName + "_item_" + wgtIndex + "\" style=\"display: none\"><input type=\"hidden\" name=\"" + wgtName + "[]\" value=\"" + selectValue + "\"><img src=\"" + opts.deleteImg + "\" border=\"0\" />" + selectText + "</div>");
                    $("#" + wgtName + "_item_" + wgtIndex).show("fast");
                    $("#" + wgtName + "_item_" + wgtIndex + " img ").click(function() {
                        $(this).parent().hide("fast",
                        function() {
                            for (i = 0, len = selectValues.length; i < len; i++) {
                                if (selectValues[i][0] == selectValue)
                                selectValues[i][2] = true;
                            }
                            var options = '';
                            for (i = 0, len = selectValues.length; i < len; i++) {
                                if (selectValues[i][2] == true) {
                                    options += "<option value=\"" + selectValues[i][0] + "\">" + selectValues[i][1] + "</option>";
                                }
                            }
                            $("#" + wgtName).children().remove();
                            $("#" + wgtName).append(options);
                            $(this).remove();
                        });
                        return false;
                    });
                    $(this).remove();
                });
            });
        },

        defaults: {
            deleteImg: "images/icons/cross.png"
        }

    };
})(jQuery);