(function($){
	$.fn.bindLocationTree = function(options){
		if (this.length == 0) { 
			return(this); 
		};
		if (arguments.length=0)return _self;


		settings={
			docroot:'',
			country:{defaultValue:'',container:'cab_country',uiid:'country',dataprovider:'/ajax_funcs/getCountry.php',otherLink:false},
			state:{defaultValue:'',container:'cab_state',uiid:'state',dataprovider:'/ajax_funcs/getstate.php',otherLink:false},
			city:{defaultValue:'',container:'cab_city',uiid:'city',dataprovider:'/ajax_funcs/getcity.php',otherLink:false},
			golfcourse:{defaultValue:'',container:'cab_golfcourse',uiid:'golfcourse',dataprovider:'/ajax_funcs/getgolfcourse.php',otherLink:false}
			}
		$.extend(settings,options);
		
		var _self=this;
		
		_self.main=function(){
			_self.init();
			_self.loadCountry();
		}
		_self.init=function(){
			s=settings;
			_self.createSelectObject({idx:'country',clearContainer:true});
			_self.createSelectObject({idx:'state',clearContainer:true});
			_self.createSelectObject({idx:'city',clearContainer:true});
			_self.createSelectObject({idx:'golfcourse',clearContainer:true});
			
		}
		_self.loadCountry=function(){
			params='';
			_self.loadRemoteData('country',params);
		}
		/*
		_self.loadCountry=function(){
			objParent=$('#'+settings.location.uiid).get(0);
			parent_id=objParent.options[objParent.selectedIndex].value;
			eval('params={parent_id:"'+parent_id+'"}');
			_self.loadRemoteData('country',params);

			//alert("country");
		}
		*/
		_self.loadState=function(){
			objParent=$('#'+settings.country.uiid).get(0);
			parent_id=objParent.options[objParent.selectedIndex].value;
			eval('params={parent_id:"'+parent_id+'"}');
			_self.loadRemoteData('state',params);
		}
		_self.loadCity=function(){
			objParent=$('#'+settings.state.uiid).get(0);
			parent_id=objParent.options[objParent.selectedIndex].value;
			eval('params={parent_id:"'+parent_id+'"}');
			_self.loadRemoteData('city',params);
		}
		_self.loadGolfCourse=function(){
			objParent=$('#'+settings.city.uiid).get(0);
			if ($(objParent).is('select') && objParent.options.length>0){
				parent_id=objParent.options[objParent.selectedIndex].value;
				eval('params={parent_id:"'+parent_id+'"}');
				_self.loadRemoteData('golfcourse',params);
			}else{
				_self.createInputObject({idx:'golfcourse',objectId: settings.golfcourse.uiid,containerId:settings.golfcourse.container,objectType:'text',clearContainer:true});
			}
		}

		_self.loadRemoteData=function(idx,params){
			s=settings;
			str='retrieving data...please wait...';
			if ($('#'+s[idx].uiid).is('select')){
				_self.addOption($('#'+s[idx].uiid),'',str);
			}else{
				$('#'+s[idx].uiid).val(str);
			}
			$.getJSON(s.docroot+s[idx].dataprovider,params,function(json){
				if (json.length>0 && !$('#'+s[idx].uiid).is('select') ){
					_self.createSelectObject({idx:idx,clearContainer:true,fireEvent:true});
				}else if (json.length==0 && $(obj).is('select')){
					_self.createHiddenObject({
											idx:idx,
											objectId:s[idx].uiid,
											containerId:s[idx].container,
											objectType:'text',
											clearContainer:true,
											fireEvent:true});
				}
				if ($('#'+s[idx].uiid).is('select')){
					_self.fillSelectObject({
										   idx:idx,
										   objectId:s[idx].uiid,
										   data:json,
										   valueField:'id',
										   textField:'name'});
					$('#'+s[idx].uiid).trigger('load');
				}
			});
		}
		_self.fillSelectObject=function(/*{idx:'',objectId:{},data:{},valueField:'id',textField:'name'}*/){
			if (arguments.length!=1) return this;
			args=arguments[0];
			obj=$('#'+args.objectId);
			data=args.data;
			valueField=args.valueField;
			textField=args.textField;
			$(obj).get(0).options.length=0;
			for (i=0;i<data.length;i++)_self.addOption(obj,data[i][valueField],data[i][textField]);
			if(settings[args.idx].defaultValue)_self.setDefaultOption(args.objectId,settings[args.idx].defaultValue)
		};
		_self.addOption=function(obj,value,text){$(obj).append('<option value="'+(value?value:text)+'">'+text+'</option>');};
		_self.setDefaultOption=function(objectId,defaultValue){
			obj=$('#'+objectId).get(0);
			if (obj.options.length<=0)return;
			$(obj).children($(obj).children());
			for(var i=0;i<obj.options.length;i++){
				if ($.browser.msie){
					try{
						if (obj.options[i].value==defaultValue){
						  obj.options[i].setAttribute('selected',true);
						  obj.selectedIndex=i;
						  
						  var option = document.createElement("option");
						  option.setAttribute("value", defaultValue);
						  option.setAttribute("selected",true);
						  option.appendChild(document.createTextNode(obj.options[i].innerText));
						  obj.appendChild(option);
						  
						  break;
						}
					}catch(e){}
				}else{
					if (obj.options[i].value==defaultValue){
					 obj.options[i].selected=true;
					 break;
					}
				}
			}
		}
		_self.setDefaultOption2=function(objectId,defaultValue){
			obj=$('#'+objectId).get(0);
			if (obj.options.length==0)return;
			//..IE6 fix applied here
			for(i=0;i<obj.options.length;i++){
				if ($.browser.msie){
				//obj.options[i].setAttribute('selected',(obj.options[i].value==defaultValue));
                
                //if(obj.options[i].value==defaultValue){
					
				    //obj.options[i].setAttribute("selected",true);
                    //obj.options[i].selected = defaultValue;
					//obj.options[i].selected = true;
					obj.options[i].setAttribute('selected',(obj.options[i].value==defaultValue));
				//}		
				

				}else{
					obj.options[i].selected=(obj.options[i].value==defaultValue);
				}
			}
			//..//
		};
		_self.createSelectObject=function(/*{idx:"",clearContainer:true-false,fireEvent:true-false}*/){
			if (arguments.length!=1) return this;
			arg=arguments[0];
			s=settings[arg.idx];
			containerId=s.container;
			objectId=s.uiid;
			if (arg.clearContainer)$('#'+s.container).html('');
			$('#'+s.container).append('<select name="'+s.uiid+'" id="'+s.uiid+'"></select>');
			//if (s.otherLink)$('#'+s.container).append('<a href="javascript:;" onclick="javascript:bindLocationTree_showOther(this);">Other</a>');
			_self.bindEvents(arg.idx);
			if(arg.fireEvent)$('#'+s.uiid).trigger('load');
			return($('#'+arg.objectId));
		}
		_self.createInputObject=function(/*{idx:"",objectId:"",containerId:"",objectType:"",clearContainer:true-false}*/){
			if (arguments.length!=1) return this;
			arg=arguments[0];
			s=settings[arg.idx];
			if (arguments[0].clearContainer)$('#'+arguments[0].containerId).html('');
			$('#'+arguments[0].containerId).append('<input type="'+arguments[0].objectType+'" name="'+arguments[0].objectId+'" id="'+arguments[0].objectId+'" />');
			_self.bindEvents(arguments[0].idx);
			if(arguments[0].fireEvent)$('#'+arguments[0].objectId).trigger('change');
			return($('#'+arguments[0].objectId));
		}
		_self.createHiddenObject=function(/*{idx:"",objectId:"",containerId:"",objectType:"",clearContainer:true-false}*/){
			if (arguments.length!=1) return this;
			arg=arguments[0];
			s=settings[arg.idx];
			if (arguments[0].clearContainer)$('#'+arguments[0].containerId).html('');
			$('#'+arguments[0].containerId).append('<p>No data found, please try again.</p>');
			_self.bindEvents(arguments[0].idx);
			if(arguments[0].fireEvent)$('#'+arguments[0].objectId).trigger('change');
			return($('#'+arguments[0].objectId));
		}
		_self.bindEvents=function(idx){
			s=settings;
			switch(idx){
				case 'country':
					$('#'+s.country.uiid).load(_self.loadState);
					$('#'+s.country.uiid).change(_self.loadState);
					break;
				case 'state':
					$('#'+s.state.uiid).load(_self.loadCity);
					$('#'+s.state.uiid).change(_self.loadCity);
					break;
				case 'city':
					$('#'+s.city.uiid).load(_self.loadGolfCourse);
					$('#'+s.city.uiid).change(_self.loadGolfCourse);
					break;
			}
		}
		_self.main();
	}
})(jQuery);
