/**
 
	RADIO Group Widget

**/

(function($){

	var xlradio = {
		_init : function()
		{
			this.create_divs();
			//this.set_state(null,true);
			
		}
		,get: function (val) { return this._getData( val ); }
		,set: function (key , val) { this._setData( key , val ); }
		,returnlen: function( a )
		{
			if (typeof(a.length) != 'undefined'  
				&& typeof(a.length) == 'function') 
			{
				return a.length;
			}
			if (typeof(a) != 'object') return 0;
			
			var c=0;
			for (var i in a )
			{
				if ( typeof(a[i]) == 'function') continue;
				c++;
			}
			return c;
		}		
		,create_divs: function ()
		{
			var c = this.element.children();
			if ( c.length > 0 ) return;
			
			var name = this.get('name');
			var members = this.get('members');
			var labelfirst = this.get('labelfirst');
			var dodisplay = this.get('display_on_update');
			
			var radiowrapper = this.get('radiowrapperid');
			var groupclass = this.get('groupclass');
			var iconclass = this.get('iconclass');
			
			
			if (this.returnlen( members ) == 0)
			{
				$('<div id="error"><b>No Members found for radiogroup</b></div>').appendTo(this.element);
				return;
			}
			
			var base = null;
			var c=0;
			var len = this.returnlen( members );
			var cf = '';
			
			var states = {};
			var parID = this.element.attr('id');
			for ( var value in members )
			{
				cf = c==0 ? 'first' : (c+1 == len?'last':'');
				states[value] = 0;// reset states
				
				$('<div id="'+radiowrapper+'_'+name+value+'" class="'+groupclass+' '+ cf +'"></div>').appendTo(this.element);
				
				base = $('#'+radiowrapper+'_'+name+value);


				if (labelfirst)
				{
					$('<div id="label">'+ members[value] +'</div>'
					).appendTo(base);
					
					$('<a id="'+name+'_'+value+'" obj="'+parID+'" value="'+value+'" class="'+iconclass+'"></a>'
					).appendTo(base);
				}
				else
				{
					$('<a id="'+name+'_'+value+'" obj="'+parID+'" class="'+iconclass+'"></a>'
					).appendTo(base);
					
					$('<div id="label">'+ members[value] +'</div>'
					).appendTo(base);
				
				}
				$('a#'+name+'_'+value).bind('click', function(e){
						var o = $(this).attr('obj');
						var v=  $(this).attr('value');
						$('#'+o).xlradio('set_state',v, true);
				});
				c++;
			}
			if (dodisplay)
			{
				$('<div id="xlradio_display_'+name+'"></div>').appendTo(this.element.parent());
				this.set('displayid', 'xlradio_display_'+name);
			}
			
			this.set('states',states);
			
		}
		,set_state: function( state, internal)
		{
			if(typeof(state) == 'undefined' || state == null)
			{
				var state = 0;
				var s = this.get('defaultselected');
				state = s;
			}

			var st = this.get('states');
			var name = this.get('name');
			var selclass = this.get('selectclass');

			var disp = this.get('display_on_update');
			
			if (this.returnlen(st) == 0) return;
			var newstate = {};
			for(var value in st)
			{
				if ( state != value)
				{
					$('a#'+name+'_'+value).removeClass(selclass);
					newstate[value] = 0;
					continue;
				}
				
				newstate[value] = 1;
				$('a#'+name+'_'+value).addClass(selclass);				
				if (internal == true)
				{
					var cd = this.get('callbackdata');
					var cf = this.get('callback');
					if ( typeof(cf) == 'function')
					{
						_cd = cd;
						if (typeof(cd) == 'object') _cd = cd[value];
						_cd['updatecores'] = 1;
						this.docallback( cf, _cd);
					}					
				}
				if (disp)
				{
					this.do_display( value );
				}
			}
			this.set('state',newstate);
		}
		,docallback: function(_fun, args)
		{
			if (_fun !== null && typeof(_fun) == 'function')
			{
				_fun(args);
			}
		}
		,do_display: function( val )
		{
			var d = this.get('displayid');
			var v = this.get('displayitems');
			var p = this.get('display_prefix');
			
			var dv = val;
			if (typeof(v) == 'object') dv = v[val];
			
			$('#'+d).html(p+' '+dv);
		}
		,get_data: function( idx )
		{
			var cd = this.get('callbackdata');
			return typeof(cd) == 'object' && typeof(idx) !='undefined' 
				? cd[idx] : cd;
		}
		,get_selected: function()
		{
			var s = this.get('state'); 
			for (var i in s )
			{
				if (s[i]) return i;
			}
			return false;
		}
		,get_index: function( idx, value )
		{
			var call = this.get('callbackdata');
			var t = 0;
			for( var i in call )
			{
				if (call[i][idx] <= value) t = i;
			}
			return t;
		}
		
	};
	/**
		Sets up the widget
	**/
	$.xls = $.xls || {}; // create the namespace
	$.widget("xls.xlradio", xlradio);
	$.xls.xlradio.defaults = {
		 name: 'xlradio'
		,members: {1: 'xlradio' } // value, label pairs
		,states:{}
		,defaultselected: 1
		,labelfirst: true
		,radiowrapperid: 'xlradio_wrapper'
		,iconclass: 'xlradio_iconclass'
		,selectclass: 'selected'
		,display_on_update: true
		,displayid: null
		,displayitems: null
		,display_prefix: ''
		,callbackdata:{}
		,callback:null
		,follower:[]
	};
	$.xls.xlradio.getter = [
		"get_data" ,"get" ,"get_selected" ,"get_index" ,"returnlen"
	];

})(jQuery);

