// <![CDATA[
/**
 * dependency of this plugin:
 *
 * //@var global string http_root
 * //@var global string rp_global_image
 * //@var global string image_loader
 * //@var global string rp_cms
 *
 * //@function close_popup()
 *
*/

// You need an anonymous function to wrap around your function to avoid conflict
(function($){
 
    // Attach this new method to jQuery
    $.fn.extend({ 
         
        // This is where you write your plugin's name
        replace_selectbox: function(options) {
		
			// Set the default values, use comma to separate the settings, example:
            var defaults = {
				increment: 0 // The increment number.
            }
                 
            var options = $.extend(defaults, options);
			
			var $cm = this.each(function(){
				
				var o = options;
				var object = $(this);
				
				object.hide();
				object.after('<ul class="select-replacement"></ul>');
				
				if(object.length > 0)
				{
					var parent = object.parent();
					var first = $("option:first",object);
					
					$(".select-replacement",parent).append("<li name='"+first.val()+"'>"+first.text()+"</li>");
					
					$("option",object).each(function () {
						
						// You must NOT use 'title' in <li> element.
						$(".select-replacement",parent).append("<li name='"+$(this).val()+"'>"+$(this).text()+"</li>");
						//alert($(this).text());
					});
					
					$(".select-replacement li",parent).not(':first').hide();
					
					if($(".select-replacement li:first",parent).text() === $(".select-replacement li:first",parent).next().text())
					$(".select-replacement li:first",parent).next().addClass('current');
					
					$(".select-replacement li",parent).click(function(){
						//alert($(this).attr('name'));
						//alert($(this).index());
						//alert($(this).attr('class'));
						//alert($(".select-replacement li.current",parent).text());
						var name = $(this).attr('name');
						var current = $(".select-replacement li.current",parent).text();

						if($(this).index() != 0) 
						{
							$(".select-replacement li",parent).removeClass('current');
							$(".select-replacement li",parent).not(':first').hide();
							$(this).addClass('current');
						}
						else if($(this).siblings().is(":visible"))
						{
							$(".select-replacement li",parent).not(':first').hide();
						}
						else
						{
							$(".select-replacement li",parent).not(':first').show();
						}

						$(".select-replacement li:first",parent).text($(this).text());
						$(".select-replacement li:first",parent).attr('name',name);
						
						$("option[value='"+name+"']").attr('selected', 'selected');
						
					});
					
				}

			});

        }
    });
     
//pass jQuery to the function, 
//So that we will able to use any valid Javascript variable name 
//to replace "$" SIGN. But, we'll stick to $ (I like dollar sign: ) )       
})(jQuery);

// ]]>
