// multi column radio select

(function(jq){
	// Defining Common Functions
	jq.multiColumnRadioSelect={
		init: function(context)
		{
			context.width  = context.table.width(); 
			context.height = context.table.height();

			if ($.browser.msie)
			{
				context.cursorHand = 'hand';
			} else {
				context.cursorHand = 'pointer';
			}

			context.allTableRows=$("tr",context.table);
			context.tableRows=$("tr",context.table);
//			context.headerRow=$("tr/th",context.table).parent();
//			context.headerCells=$("th",context.table);
			context.tableCells=$("td",context.table);


			// create selector
			context.table
				.before('<div><a href="#"><span id="' + 
					context.option.formElementName + '_selval"></span></a></div>');

			context.selector = context.table.prev();
			context.selectorText = context.table.prev().children().eq(0).children().eq(0);


			// create scroll area
			context.table
				.css({
					border: 'none'
				})	
				.wrap('<div></div>')
				.parent()
					.css({	position:  'absolute',
						display:   'block',
						width:     context.width + "px"
//						height:    context.elementHeight + "px",
//						overflowY: 'scroll',
//						overflowX: 'hidden'
					})
					.addClass(context.option.scrollAreaClass)
			;

			context.table
				.after('<div>' + context.option.cancelText+ '</div>')
				.next()
					.css({
						paddingLeft: '1px',
						paddingTop:  '1px',
						cursor:      context.cursorHand
					})
					.click(function(){
						jq.multiColumnRadioSelect.close(context);
					});

			context.scrollArea = context.table.parent();

			context._docclick = jq.multiColumnRadioSelect._getdocclick(context);


			context.scrollArea
				.mouseout(function(){
					$(document).click(context._docclick);
				});

			context.scrollArea
				.mouseover(function(){
					//jq.multiColumnRadioSelect.close(context)
					$(document).unbind('click', context._docclick);
			});

			context.selector
				.click(function(){jq.multiColumnRadioSelect.onClick(context)});

			context.selector
				.attr('title', context.option.hintText);

			context.selector
				.children()
				.eq(1)
				.click(function(){jq.multiColumnRadioSelect.onClick(context)});
			
			//Apply Events
			context.tableRows
				.css({cursor: context.cursorHand})
				.click(function(){
					// check corresponding option
					$('input[type=radio]', this).eq(0).attr('checked', true);
					jq.multiColumnRadioSelect.getSelected(context);

					context.option.onChange(context);
					jq.multiColumnRadioSelect.close(context);
				});
			
		},


		open : function(context)
		{
			context.state = 1;
			jq.multiColumnRadioSelect.render(context);
		},

		close : function(context)
		{
			context.state = 0;
			$(document).unbind('click', context._docclick);
			jq.multiColumnRadioSelect.render(context);
		},

		onClick : function(context)
		{
			if (context.state == 0) {
				jq.multiColumnRadioSelect.open(context);
			} else {
				jq.multiColumnRadioSelect.close(context);
			}
		},

		setSelectedValue : function(context, id)
		{
		},


		_getdocclick : function(context)
		{
			return function(){
				$(document).unbind('click', jq.multiColumnRadioSelect._docclick);
				jq.multiColumnRadioSelect.close(context);
			}
		},

		getSelected : function(context)
		{
			context.selectedIndex = context.option.notSelectedIndex;
			context.selectedValue = context.option.notSelectedValue;


			$("input[type=radio]", context.table).each(function(index){
				if ($(this).attr('checked'))
				{
					context.selectedIndex = index;
					context.selectedValue = $(this).val();
				}
			})

			return context.selectedIndex;
		},
	
		render: function(context)
		{
			if (context.state == 0)
			{
				context.scrollArea.hide();
				_html = context.option.onRenderSelector(context);
				context.selectorText.html(_html);
//				context.selector.show();
			} else {
				context.scrollArea.show();
//				context.selector.hide();
			}
		}	
	}

	jq.fn.multiColumnRadioSelect = function(opt) {
		// Defining Default Options	
		var defaultOption = {
			indexColumn: 0,
		       defaultIndex: 0,
		   notSelectedIndex: -1,
		   notSelectedValue: 0,
			  separator: ' &mdash; ',
			 cancelText: 'Отмена',
			   hintText: 'Нажмите для выбора вариантов',

//			 lineHeight: 0,
//			       rows: 2,
//		     scrollBarWidth: 16,
		    scrollAreaClass: 'multiColRadioSelect_scroll',

//			 haveHeader: false,

		    formElementName: 'option',

			   onChange: function(context){},
		   onRenderSelector: function(context){
				if (context.selectedIndex == context.notSelectedIndex) {
					return context.option.separator;
				} else {
					// build a string from all table cells of selected row.
					context.str = '';
					context.tableRows.each(function(rowIndex){
						if (rowIndex == context.selectedIndex) {
							var l = $(this).children().length, i;
							for (i = 0; i<l; ++i) {
								if (context.str != '') context.str = context.str + context.option.separator;
								context.str = context.str + $(this).children().eq(i).text();
							}
						}
					})
				}
				if (context.str == '') return context.option.separator;
				return context.str;
			}

		}
		
		//Applying Default Options
		defaultOption = jq.extend(defaultOption, opt);
		
		// Applying to each selected object
		return $(this).each(function(){

			//
			var context = {
			  	        table: null,
				       option: defaultOption,

			  		state: 0	// 0 - not visible, 1 - visible

/*				        input: null,
		   		     selector: null,
				   scrollArea: null,
				   
				  headerCells: null,
				    headerRow: null,
				 allTableRows: null,
				    tableRows: null,
				   tableCells: null,

				  selectedRow: null,
				 selectedText: null,
				
				        width: null,
				       height: null,
				   lineHeight: null,
				elementHeight: null,
				
					   
					isVisible: true */
			}
			
			context.table = $(this);
			
			jq.multiColumnRadioSelect.init(context);

			$('input[type=radio]', context.tableRows.eq(context.option.defaultIndex)).eq(0).attr('checked', true);
			jq.multiColumnRadioSelect.getSelected(context);

			jq.multiColumnRadioSelect.render(context);			
			
		});
	}
})(jQuery);
