/*
 * Adapted/stolen from jNice by Sean Mooney (sean@whitespace-creative.com) 
 * Renamed to avoid naming conflicts and mix-ups.
 *
 ******************************************** */
(function($){
	$.fn.jSelect = function(options){
		var self = this;
		this.each(function(){
			$('select', this).each(function(index){
				var $select = $(this);
				/* First thing we do is Wrap it */
				$(this).addClass('jSelectHidden').wrap('<div class="jSelectSelectWrapper"></div>');
				var $wrapper = $(this).parent().css({zIndex: 100-index});
				/* Now add the html for the select */
				$wrapper.prepend('<div><a href="#" class="jSelectSelectOpen"><span></span></a></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>');
				//});
				$('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[0].selectedIndex != $(this).attr('index') && $select[0].onchange) {
							$select[0].selectedIndex = $(this).attr('index'); $select[0].onchange();
						}
						$select[0].selectedIndex = $(this).attr('index');
						$('span:eq(0)', $wrapper).html($(this).html());
						$ul.hide();
						return false;
				});
				/* Set the defalut */
				$('a:eq('+ this.selectedIndex +')', $ul).click();
			});
			/* End select each */
			/* Apply the click handler to the Open */
			$('a.jSelectSelectOpen', this).click(function(){
				var $ul = $(this).parent().siblings('ul');										
				/* Check if box is already open to still allow toggle, but close all other selects */
				if ($ul.css('display')=='none'){hideSelect();} 
				$ul.slideToggle();
				var offSet = ($('a.selected', $ul).offset().top - $ul.offset().top);
				$ul.animate({scrollTop: offSet});
				return false;
				});
		}); /* End Form each */
		/* Hide all open selects */
		var hideSelect = function(){
			// ORG
			//$('.jSelectSelectWrapper ul:visible').hide();
			// PA - slide out
			$('.jSelectSelectWrapper ul:visible').slideToggle();
		};
		/* Check for an external click */
		var checkExternalClick = function(event) {
			if ($(event.target).parents('.jSelectSelectWrapper').length === 0) { hideSelect(); }
		};
		/* Apply document listener */
		$(document).mousedown(checkExternalClick);
		/* Add a new handler for the reset action */
		var jReset = function(f){
			var sel;
			$('.jSelectSelectWrapper select', f).each(function(){
				sel = (this.selectedIndex<0) ? 0 : this.selectedIndex; $('ul', $(this).parent()).each(function(){$('a:eq('+ sel +')', this).click();
			});
			});
		};
		this.bind('reset',function(){var action = function(){jReset(this);}; window.setTimeout(action, 10);});
	};
})(jQuery);