﻿(function($) {

	$.fn.droppie = function(options) {
        // override options
        var opts = $.extend({}, $.fn.droppie.defaults, options);

        // for each list
        return this.each(function() {
            var data = $.fn.droppie.parse($(this));
            //debug(data);
            var $droppie = $.fn.droppie.build($(this), data);
            //debug($droppie);
            $.fn.droppie.populate($droppie, data);
            $.fn.droppie.binders($droppie);

            // kill the old list
            $(this).remove();
        });
    }

    // binders
    $.fn.droppie.binders = function($droppie) {
        $d = $droppie.children(".droppie");
        $d.bind("click", function(event) {
            if (!$(this).data("optionsVisible")) {
                $.fn.droppie.showOptions($(this));
            } else {
                $.fn.droppie.hideOptions($(this));
            }
            $(this).focus();
            //return false;
        });
        $d.bind("blur", function(event) {debug("out");
            /*setTimeout(function() {
				debug("----");debug("focusout");
				var $f = $(document.activeElement);
				debug($f.hasClass("droppie-option-inner"));
				debug($f.hasClass("droppie"));
	            if(($f.hasClass("droppie-option-inner") == false) && ($f.hasClass("droppie") == false)) {*/
					$.fn.droppie.hideOptions($d);
					//debug("go away");
				/*}
            }, 10);*/
		});
    }

    // show & hide
    $.fn.droppie.hideOptions = function($droppie) {
        $droppie.data("optionsVisible", false)
				.find(".droppie-options")
					.fadeOut($.fn.droppie.defaults.fadeOutTime);
    }
    $.fn.droppie.showOptions = function($droppie) {
        $droppie.data("optionsVisible", true)
				.find(".droppie-options")
					.fadeIn($.fn.droppie.defaults.fadeInTime);
    }

    // insert values
    $.fn.droppie.populate = function($droppie, data) {
        var selected = [];
        // add the data
        $.each(data, function(index, value) {
            // is it selected?
            if (value.selected) {
				selected.push({
					index: index,
					title: value.title
				});
            }
            // add our values
            $droppie.find(".droppie-options li:eq(" + index + ") a")
					.text(value.title)
					.attr("href", value.href);
        });

        // check to see how many selected values we have
        if (selected.length != 0) {
            $droppie.find(".droppie-options li:eq(" + selected[0].index + ")")
						.addClass($.fn.droppie.defaults.selectedClass)
					.end()
					.find(".droppie-selected-value")
						.text(selected[0].title);
            // if we have too many
            if (selected.length > 1)
				debug($.fn.droppie.debugMessage + "Too many selected items found. Using the first.");
        }
    }

    // build the new html
    $.fn.droppie.build = function($list, data) {
        var $droppie = $("<div/>")
						.insertBefore($list)
							.addClass("droppie-wrap")
							.wrapInner("<a/>").children("a")
								.addClass("droppie")
								.attr("href", "#")
								.wrapInner("<span/>").children("span")
									.addClass("droppie-selected-outer")
									.wrapInner("<span/>").children("span")
										.addClass("droppie-selected-inner")
										.wrapInner("<span/>").children("span")
											.addClass("droppie-selected-value")
											.text("test")
										.end()
									.end()
								.end()
								.append("<ul/>").children("ul")
									.addClass("droppie-options")
								.end()
							.end()
							.data("optionsVisible", false);
		
        for (i = 0; i < data.length; i++) {
            $droppie.find("ul.droppie-options")
					.append("<li/>").children("li:last")
						.addClass("droppie-option")
						.append("<a/>").children("a")
							.addClass("droppie-option-inner")
							.attr("href", "#")
							.text("test");
        }
        
        return $droppie;
    }

    // parse the list to get the important bits
    $.fn.droppie.parse = function($list) {
        var data = [];

        $list.find("a").each(function() {
            data.push({
                title: $(this).text(),
                href: $(this).attr("href"),
                selected: $(this).parent($.fn.droppie.defaults.selectedElement).hasClass($.fn.droppie.defaults.selectedClass) == true ? true: false
            });
        });

        return data;
    }

    // default options
    $.fn.droppie.defaults = {
        selectedElement: "li",
        selectedClass: "selected",
        fadeInTime: 250,
        fadeOutTime: 250
    }

    $.fn.droppie.debugMessage = "[droppie error]: ";

    // debugger
    function debug(msg) {
        //if (window.console && window.console.log)
        //window.console.log(msg);
    }
	

})(jQuery);
