(function($) {

// Some enhancements to make this more rebust, as well as removal of z-index code were made.
// Also added multiselect box hooks
/* var z = 9; -- Removed z-index code as harmful */

checkExternalClick = function(event)
{
	if ($(event.target).parents('.activedropdown').length === 0)
	{
		$('.activedropdown').removeClass('activedropdown');
		$('.options').hide();
	}
};

$.fn.enhanceSelect = function() 
{
    $(this).each(function() {
    
        if(!$(this).parent().hasClass('enhanced'))
        {
	        targetselect = $(this);
	        targetselect.hide();

	        // set our target as the parent and mark as such
	        var target = targetselect.parent();
	        target.addClass('enhanced');

	        // prep the target for our new markup
	        target.append('<dl class="dropdown"><dt><a class="dropdown_toggle" href="#"></a></dt><dd><div class="options"><ul></ul></div></dd></dl>');
	        /*target.find('.dropdown').css('zIndex',z);
	        z--;*/

	        // we don't want to see it yet
	        target.find('.options').hide();

	        // parse all options within the select and set indices
	        var i = 0;
	        targetselect.find('option').each(function() 
	        {
		        // add the option
		        target.find('.options ul').append('<li><a href="#"><span class="value">' + $(this).text() + '</span><span class="hidden index">' + i + '</span></a></li>');

		        // check to see if this is what the default should be
		        if($(this).attr('selected') == true)
		        {
			        targetselect.parent().find('a.dropdown_toggle').append('<span></span>').find('span').text($(this).text());
		        }
		        i++;
	        });
        }
    });
}

$(document).ready(function()
{
	$(document).mousedown(checkExternalClick);

	$('select.enhanced').enhanceSelect();


	// let's hook our links, ya?
	$('a.dropdown_toggle').live('click', function() 
	{
		var theseOptions = $(this).parent().parent().find('.options');
		if(theseOptions.css('display')=='block')
		{
			$('.activedropdown').removeClass('activedropdown');
			theseOptions.hide();
		}
		else
		{
			theseOptions.parent().parent().addClass('activedropdown');
			theseOptions.show();
		}
		return false;
	});

	// bind to clicking a new option value
	$('.options a').live('click', function(e)
	{
		$('.options').hide();

		var enhanced = $(this).parent().parent().parent().parent().parent().parent();
		var realselect = enhanced.find('select');

		// set the proper index
		realselect[0].selectedIndex = $(this).find('span.index').text();
		realselect.trigger("change");

		// update the pseudo selected element
		enhanced.find('.dropdown_toggle').empty().append('<span></span>').find('span').text($(this).find('span.value').text());

		return false;
	});


	$("a.dropdown_toggle").click(function() {
		//$(this).closest('form')[0].reset();
		if( $(this).parent().parent('.activedropdown').length == 0){
			$(".multiSelect > fieldset.open").removeClass('open');
		}
	});
	
	$(".multiSelect .bttn_dropdown").click(function() {
		var fieldset = $(this).parent('.multiSelect > fieldset');
		//$(this).closest('form')[0].reset();
		if( fieldset.hasClass('open')) {
			fieldset.removeClass('open');
		} else {
			$(".multiSelect > fieldset.open").removeClass('open');
			$(".activedropdown a.dropdown_toggle").trigger("click");
			fieldset.addClass('open');
		}
		return false;
	});

  $(".multiSelect a.submit").click(function() {
    var fieldset = $(this).parent('.multiSelect > fieldset');
    //var form = $(this).parents('form');
    fieldset.removeClass('open');
    //if( form.length > 0 ) form[0].submit();
    return false;
  });

});

})(jQuery);

