/*!
 *
 * Copyright 2011, Carlos De Oliveira
 * cardeol@gmail.com
 *
 * Thanks to Davide Cimatti (dcimatti@regulus.it) for his collaboration.
 * Date: July 2011 
 */
(function($) {
	$.fn.jCombo = function(url, user_options) {
		var default_options = {
				parent: "",
				selected_value : "null",
				parent_value : "",
				initial_text: "-- Please Select --"
		};				
		var user_options = $.extend( default_options, user_options) ;
		var obj = $(this);
		if(user_options.parent!="") {
			var $parent = $(user_options.parent);			
			$parent.bind('change',  function(e) {
				obj.attr("disabled","disabled");
				__fill(	obj,
						url,
						$(this).val(),
						user_options.initial_text,
						user_options.selected_value);				
			});
		} 
		__fill(obj,url,user_options.parent_value,user_options.initial_text,user_options.selected_value);					
		function __fill($obj,$url,$id,$initext,$inival) {			
			$.ajax({
				type: "GET",
				dataType:"json",					
				url: $url + $id,
				success: function(j){
					var choices = '';
					$obj.attr("disabled","disabled");
					if (j.length == 0) {
						choices += '<option value="null"></option>';
						$obj.html(choices);
					} else {
						if($initext!="" && $initext!=null) { 
							choices += '<option value="null">' + $initext + '</option>';
						}
						for (var i = 0; i < j.length; i++) {
							selected = (j[i][0]==$inival)?' selected="selected"':'';
							c = j[i];
							choices += '<option value="' + c[0] + '"' + 
							selected + '>' + c[1] + 
							'</option>';
						}
						$obj.removeAttr("disabled").html(choices);
					}           
					$obj.trigger("change");
				}
			});					
		}
	}
})(jQuery);
