(function($){

   $.fn.linkedSelect = function(url,destination,params) {

      var params = $.extend({
         firstOption : 'Please select ...',
         noOption : '',
         loadingText : 'Loading...',
         otherWord: '',
         otherHandler: function (){}
      }, params);
      
      var $dest = $(destination);

	  if(params.otherWord.length > 0)
	  {
	  	$dest.change(function(){
			params.otherHandler($dest.attr('value')||$dest.attr('value')=='null'?false:true);
	  	});
	  }

	  return this.each(function(){

         $(this).bind('change', function() {

            var $$ = $(this);

            $dest.attr('disabled','false')
                 .append('<option value="null">' +params.loadingText+ '</option>')
                 .ajaxStart(function(){
                    $$.show();
	            });


			$.ajax({
				url : url,
				type : 'GET',
				data : {q:$$.val()},
				dataType : 'json',
				success : function(data) {
					if(data.status == 'ok') {
						
	            		$('.rgn').show();
	            		
	            		if(data.region) params.firstOption = 'Select '+data.region.toLowerCase()+' ...';
	            		//$dest.parent().prev().html(req.responseJS.region+':');
    
    					/*
			            if(false && destination.indexOf('region') != -1 && req.responseJS.result)
			            {
			            	if(req.responseJS.region)
			            	{
			            		$('.rgn').show();
			            		params.firstOption = 'Select '+req.responseJS.region.toLowerCase()+' ...';
			            		//alert($dest.parent().prev().html());
			            		//$('#region_label')
			            		$dest.parent().prev().html(req.responseJS.region+':');
			            	} else {
			            		if($('.rgn').length) {
			            			$('.rgn').hide();
			            		}
			            	}
			            }
			            */
			            if(data.content) {
			            	params.otherHandler(false);
			            	$dest.removeAttr('disabled')
			                    .html('<option value="null">' + params.firstOption + '</option>'+data.content+
			                    	(params.otherWord.length > 0?'<option value="" selected="selected">'+params.otherWord+'</option>':'')
			                    )
			                    .find('option:first')
			                    .attr('selected', 'selected');
			            } else {
				            if(destination.indexOf('region') != -1 && $('.rgn').length)
				            {
			            		$('.rgn').hide();
				            }
			            	if(params.otherWord.length > 0 && $$.val() != 'null')
			            	{
				            	$dest.removeAttr('disabled')
				               		.html('<option value="null">' + params.firstOption + '</option><option value="" selected="selected">'+params.otherWord+'</option>');
				               	params.otherHandler(true);
			            	} else {
			            		if(params.noOption != '') {
					            	$dest.attr('disabled', 'false')
					               		.html('<option value="null">' + params.noOption + '</option>');
			            		} else {
					            	$dest.removeAttr('disabled')
					               		.html('<option value="null">' + params.firstOption + '</option>');
				               	}
			            	}
			            }
	          	  }
				},
				error : function() {
            		if(params.noOption != '') {
		            	$dest.attr('disabled', 'false')
		               		.html('<option value="null">' + params.noOption + '</option>');
            		} else {
		            	$dest.removeAttr('disabled')
		               		.html('<option value="null">' + params.firstOption + '</option>');
	               	}
				}
			});
         });  // end change

      }); // end return each

   };  // end function

})(jQuery);

