/*
 * Custom Combo box  - jQuery plugin for changing select element
 * Author: Anoop Chauhan
 * Licensed under the MIT license:
 * http://www.opensource.org/licenses/mit-license.php
 *	Version 1.0
 */


(function($) {
    
		$.fn.customCombo =  function(settings){
			
			var settings = jQuery.extend({
																												
			// Configuration related to Flyout
			},settings);
			
				
			var _ccWrapper = '<div>';
			var _ccSelected = '<a href="#" class="selected"><span>Loading...</span></a>'
			var _ccOptions = ''			
			
			//Bind Click funciton on every instance
			$(this).each(function(i){
				
					$(this).wrap(_ccWrapper);
					$(this).parent().attr("id","cc_"+i);
						$(this).parent().css({"z-index":"555"+i});
					$(this).parent().addClass($(this).attr("class"))
					//Get options and populate into ul li structure
					$("option",this).each(function(){_ccOptions=_ccOptions + ("<li><a href='#' rel='" + $(this).val() + "' title='" + $(this).text() + "'>" + $(this).text() + "</a></li>")});
					$(this).parent().append(_ccSelected+ '<ul class="ccOptions">'+_ccOptions+'</ul>');
					//$(document).append('<ul class="ccOptions" >'+_ccOptions+'</ul>');
					$(this).parent().find(".selected").attr("rel",$(":selected",this).val());
					$(this).parent().find(".selected span").text($(":selected",this).text());

					$(this).parent().find(".ccOptions li a").each(function(j){
						$(this).bind("click",function(){
							var sLink = $(this).parents(".customCombo").find(".selected");
							$(sLink).find("span").text($(this).text());
							var g = $(this).closest("ul").siblings("select");
							
							$(g).val($(this).text())
							//$(this).closest("ul").siblings("select option:eq("+j+")").attr("selected","selected");
							
							$(sLink).attr("rel",$(this).attr("rel"));
							$(sLink).siblings(".ccOptions").slideUp("fast");
							
							$(this).closest("ul").siblings("select").trigger('change');
							return false;
						});
					});
		
					var ddH = 5
					//Attach click event on custom combo
					$(this).parent().find(".selected").click(function(){
							$(".customCombo .ccOptions").parent().css({"z-index":"100"});
							$(".customCombo .ccOptions").slideUp("fast");
							if(!$(this).hasClass("disabled")){
							var g = $(this).parent().find("li").size();
							if(g>ddH){
										$(this).siblings(".ccOptions").addClass("scroll");	
										$(this).siblings(".ccOptions").width($(this).siblings(".ccOptions").width()+25)	
										$(this).siblings(".ccOptions").height(ddH*20);	
							}
							$(this).parent().css({"z-index":"999999999"})
							$(this).siblings(".ccOptions").slideDown("normal");
							}
							return false;
					});

					//Remove Classes form old Select box & hide it
					$(this).removeAttr("class");
					$(this).hide();
					
					 $(document).click(function(e){
							var obj = $(e.target);
							obj=obj.attr("class");
					
							if(obj.indexOf("customCombo")==-1){
								$(".customCombo .ccOptions").slideUp("fast",function(){
										$(this).parent().css({"z-index":"100"});										
										$(this).width("auto");
									});
							}
						});
					_ccOptions = ''			
			});
			

		
	};		
})(jQuery);