// Comments will go here before release (if released)
// 
// 
// 
// 
// hopefully

;(function($) {

	apply_styles_actions = function(this_select,select_options,opts,updateInput,hiddenInputID,zIndexNumber) {
		
		// Tweak some styles initilally
		select_options.hide().css('position','absolute');

		// Set the "active" link as the visible text (emulating the "selected" attribute)
		var active = this_select.find('a.active').eq(0);
		
		// If the anchor tag has a title attribute we use that, otherwise we just use the anchor text
		var replacement_text = (active.attr('title') != '') ? active.attr('title') : active.text();
		
		if (active.text() != '') this_select.find('li.current > a:eq(0)').text(replacement_text);


		if (opts.action == 'hover') {
			this_select.find('li.current').hover(
				function(){
					select_options.show();
				},
				function(){
					select_options.hide();
				}
			);
		} else if (opts.action == 'click') {
			alert('The "click" action needs refinging and currently doesn\'t work');
			return false;
			// this_select.find('li.current a').click(function(){
			//		alert('trigger');
			//		select_options.show();
			// });
		};
		// Here we do the "replace" text to mimic newly "selected options"
		this_select.find('a').click(function(){
			// Return false if the user clicks the the .current li to keep the select visible
			if ($(this).parent('li').hasClass('current')) { return false; }
			// Return false if the user is clicking an active list element 
			if ($(this).hasClass('active')) { return false; }
			// Remove the 'active' class and add it to the newly active anchor
			$(this).closest('ul').find('.active').removeClass('active');
			// Replace text then hide the select options
			var replacement_text = ($(this).attr('title') != '') ? $(this).attr('title') : $(this).text();
			
			$(this).addClass('active').closest('.dropdown').find('li.current > a:eq(0)').text(replacement_text);
			select_options.hide();
			// If it's an actual form box we've replaced we need to update the DOM inserted
			// hidden form input with the value
			if(updateInput) {
				new_value = (opts.value_to_rel) ? $(this).attr('rel') : $(this).val ;
				$(hiddenInputID).attr('value',new_value);
			}
			// If the link is a # then return false
			// Otherwise go to the actual link
			if ($(this).attr('href') == '#') { return false; }
		});

	};


	$.fn.pseudoSelect = function(options) {
		
		var opts = $.extend({}, $.fn.pseudoSelect.defaults, options), select_to_ul;
		
		var zIndexNumber = 1000;
		
		return this.each(function(e) {

			// Set the "select" node we're working with & the first UL which hold the "options"
			var this_select = $(this);
			var select_options = $(this).find('ul');

			if (this_select.is('select')) {
				
				// create a hidden form element to hold the value
				this_select.after('<input type="hidden" name="'+$(this_select).attr('name')+'" value="" id="pseudoSelectInput-'+e+'" />');
				
				array_of_options = $(this_select).find('option[value!=], optgroup');
				
				select_to_ul = '<div id="pseudoSelect-'+e+'" class="pseudoSelectWrapper" style="z-index:'+zIndexNumber+'"><ul ';
				select_to_ul += (opts.replacement_element.charAt(0) == '#') ? 'id' : 'class';
				select_to_ul += '="'+opts.replacement_element.substring(1)+'">';
				select_to_ul += '<li class="current"><a href="#">'+opts.prefix+'</a><ul>';
				array_of_options.each(function(){
					if(this.tagName == 'OPTGROUP') {
						select_to_ul += '<li class="optgroup">'+$(this).attr('label')+'</li>';
					} else if (this.tagName == 'OPTION') {
						select_to_ul += '<li class="option"><a href="';
						select_to_ul += (opts.value_to_href) ? $(this).attr('value') : '#' ;
						select_to_ul += (opts.add_slash) ? '/' : '' ;
						select_to_ul += '"';
						select_to_ul += (opts.value_to_rel) ? ' rel="'+$(this).attr('value')+'"' : '' ;
						// Check for a currently selected option and make the li "active"
						// Also assign the hidden input the value of the selected option
						if($(this).is(':selected')) {
							select_to_ul += ' class="active"';
							$('#pseudoSelectInput-'+e).attr('value',$(this).attr('value'));
						};
						select_to_ul += '>'+$(this).text()+'</a></li>';
					};
				});
				select_to_ul += '</ul></li></ul></div>';
				
				// Replace select with newly built unordered list
				this_select.replaceWith(select_to_ul);
				
				// Reset our element ojbects now that they've been created
				this_select = $('#pseudoSelect-'+e+' ul');
				select_options = this_select.find('ul');
				
				apply_styles_actions(this_select,select_options,opts,true,'#pseudoSelectInput-'+e,zIndexNumber);
				
				zIndexNumber -= 10;
				
				return;
			};

			apply_styles_actions(this_select,select_options,opts,null,null,zIndexNumber);	 
			
			zIndexNumber -= 10;

		});
		
	};

	// default options
	$.fn.pseudoSelect.defaults = {
		replacement_element: '.dropdown',
		action: 'hover',
		prefix: 'Choose from list:',
		value_to_rel: true,
		value_to_href: false,
		add_slash: false
	};

})(jQuery);
