;(function($) {	
	
	/************************************************************
						jQuery Plugins
	************************************************************/
	
	$.fn.az_decorate_form_items = function(){		
		
		var parentForm = this;
		
		$(this).find('select').each(function(){
			var initial_width = $(this).outerWidth(false) + 20;
			$(this).parents('form:first').submit(function(){
				$(this).remove('select');
			});
			
			//$(this).parent().children().css('display', 'none');
			$(this).css('display','none');
			var selectbox_markup = az_create_select_box(parentForm, this);			
			var selectbox_decoration = az_decorate_select_box(selectbox_markup, '');
			
			$(this).parent().append(selectbox_decoration);	
			
			$('.az_select_box:last').width(initial_width);
			
			$('.az_select_box:last').bind('mousedown', function(){
				$(this).css('z-index', '999999');											
				var ul_container = $(this).find('.az_option_container:first');				
				                
				ul_container.css('z-index', '999999');            
                
                                
				if(ul_container.css('display') == 'none'){
					ul_container.slideDown('fast',
                        function()
                        {   
							try{
                            	$(this).jScrollPane();
							}catch(err){};
						}
                    );
                }
                    //ul_container.css('display', 'block');
                
                    				
			}).bind('mouseleave', function(){
				$(this).css('z-index', '1');
				
				var ul_container = $(this).find('.az_option_container:first');	
				
				if(ul_container.css('display') == 'block')
					ul_container.css('display', 'none');
					
			}).find('a').bind('click',function(){
				/*
				$(this).parents('.az_select_box:first').find('input[type="hidden"]:first').val($(this).attr('href'));	
				$(this).parents('.az_select_box:first').find('.az_select_val:first').html($(this).html());
				$(this).parents('.az_select_box:first').find('.az_option_container:first').slideUp();
				*/
                var baseUrl = window.location.href.substring(0, window.location.href.lastIndexOf("/") + 1);
				var hidden_field = $(this).parents('.az_select_box:first').find('input[type="hidden"]:first');
				
				var prev_val = hidden_field.val();				
				hidden_field.val($(this).attr('href').replace(baseUrl, ""));	
				
				$(this).parents('.az_select_box:first').find('.az_select_val:first').html($(this).html());
				$(this).parents('.az_select_box:first').find('.az_option_container:first').slideUp();						
				$(this).parents('.az_select_box:first').siblings('select:first').trigger('change');
			});

		});
	};
	
	/************************************************************
						End - jQuery Plugins
	************************************************************/
	
	/************************************************************
						Helper Functions
	************************************************************/
	
	
	az_decorate_select_box = function(select_box_markup, additional_class){
		var pre = '<div class="az_select_box' + additional_class + '">';
		pre += '<div class="az_select_box_left">';
		pre += '<div class="az_select_box_right">';
		pre += '<div class="az_select_box_mid">';
		pre += '<div class="az_select_inner">';
		
		var app = '</div></div></div></div>';
		app += select_box_markup.dropdown + '</div>';
		
		return pre + select_box_markup.hidden_field + select_box_markup.select_box_text + app;
	};
	
	az_create_select_box = function(parentForm, theSelect){
		var items = '<ul>';		
		
		var selected_text = '';		
		var selected_value = '';
		
		$(theSelect).children('option').each(function(i, v){
			if(i == 0 || $(this).attr('selected') == 'selected' || $(theSelect).val() == $(this).attr('value')){
				selected_text = $(this).html();
				selected_value = $(this).attr('value');
			}
			items += '<li><a href="' + $(this).val() + '" title="'+ $(this).html() +'" onclick="return false;">'+$(this).html()+'</a></li>';
		});		
		
		items += '</ul>';
		
		return {
				hidden_field: '<input type="hidden" name="' + $(theSelect).attr('name') + '" value="'+selected_value+'"/>', 
				select_box_text: '<div class="az_select_val">'+selected_text+'</div>',
				dropdown: '<div class="az_option_container">' + items + '</div>'
				};
	}
	
	/************************************************************
						End Helpers
	************************************************************/
})(jQuery);
