/**
 * jNice select modifier
 *
 * Based on jNice by Sean Mooney (sean@whitespace-creative.com) 
 *
 */

(function ($) {
	
    $.fn.jNice = function (options) {
        $(this).each(function (index) {
            var $select = this;
            
            /* First thing we do is Wrap it */
            $($select).addClass('jNiceHidden').wrap('<div class="jNiceSelectWrapper"></div>');
            var $wrapper = $(this).parent().css("zIndex", (options) ? options : 100 - index);
            
            /* Now add the html for the select */
            $wrapper.prepend('<div class="jNiceSelectOpen"><span></span></div><ul></ul>');
            var $ul = $('ul', $wrapper);
            
            /* Now we add the options */
            $('option', this).each(function (i) {
                $ul.append('<li><a href="#" index="' + i + '">' + this.text + '</a></li>');
            });
            
            /* Hide the ul and add click handler to the a */
            $ul.hide().find('a').click(function () {
                $('a.selected', $wrapper).removeClass('selected');
                $(this).addClass('selected');
                
                /* Fire the onchange event */
                if ($select.selectedIndex != $(this).attr('index')) {
                    $select.selectedIndex = $(this).attr('index');
                    $($select).trigger('change');
                } else {
                    $select.selectedIndex = $(this).attr('index');
                }
                
                $('div.jNiceSelectOpen span', $wrapper).html($(this).html());
                $ul.hide();
                
                return false;
            });
            
            /* Set the defalut */
            $('a:eq(' + $select.selectedIndex + ')', $ul).click();
        });
        	
        /* Apply the click handler to the Open */
        $(this).parents(".jNiceSelectWrapper").click(function () {
            var $ul = $('ul', this);
            
            /* Check if box is already open to still allow toggle, but close all other selects */
            if ($ul.css('display') === 'none') {
                hideSelect();
            }
            
            $ul.css('width', $ul.parent().width() - 2);
            $ul.css("display", "block");
            var offSet = ($('a.selected', $ul).offset().top - $ul.offset().top);
            $ul.animate({scrollTop: offSet});
            
            return false;
        });
        
        /* Hide all open selects */
        var hideSelect = function () {
            $('.jNiceSelectWrapper ul:visible').hide();
        };

        /* Check for an external click */
        var checkExternalClick = function (event) {
            if ($(event.target).parents('.jNiceSelectWrapper').length === 0) {
                hideSelect();
            }
        };
        
        /* Apply document listener */
        $(document).mousedown(checkExternalClick);
    };
})(jQuery);