
function list_control_select_item(list_id, model_id, list_model_id)
{
	if (!list_model_id) list_model_id = list_id + '_'+model_id;

	var element = document.getElementById(list_model_id);
	var element_dup = document.getElementById('dup_'+list_model_id);
	var element_row = document.getElementById(list_model_id + '_row');
	if(element_dup) element_dup.checked = element.checked;

	list_control_reset_button_state(list_id);
	list_control_update_row_state(list_model_id);

	$j('#' + list_id.camelize(true) + 'List').trigger('item_selected.list_control', element_row);
}

function activate_list_item(list_id, model_id) {
	var list_model_id = list_id+'_'+model_id;
	var model_tr = $j('#'+list_model_id+'_row');
	if(!model_tr.hasClass('Selection')) model_tr.addClass('Selection Pointer');
}

function deactivate_list_item(list_id, model_id) {
	var list_model_id = list_id+'_'+model_id;
	var model_tr = $j('#'+list_model_id+'_row');
	if (model_tr.hasClass('Selection Pointer')) model_tr.removeClass('Selection Pointer');
}

function submit_form(list_id, model_id, url, params) {
	forms = document.getElementsByTagName('form');
	form = forms[0];
	form.action = url+'?'+params+'&'+'result='+model_id;
	form.submit();
}

function list_control_update_row_state(list_model_id)
{
	var model_tr = $j('#'+list_model_id+'_row');
	if ($j('#'+list_model_id)) {
		if( $j('#'+list_model_id).attr('checked') )
		{
			$j('#'+list_model_id+'_row').addClass('Selection');
		}
		else
		{
			$j('#'+list_model_id+'_row').removeClass('Selection');
		}
	}
}

function list_control_reset_state(list_id)
{
	var models_field = $j('#'+list_id+'_models');
	if( models_field.length ) {
		var models = models_field.val().split(',');
		for(var mi in models)
		{
			model_id = models[mi];
			if( typeof model_id != 'string' ) { continue; }
			list_control_update_row_state(list_id+'_'+model_id);
		}
		list_control_reset_button_state(list_id);
	}
}

function list_control_reset_button_state(list_id)
{
	var actions_field = $j('#'+list_id+'_actions');
	var models_field = $j('#'+list_id+'_models');

	if( actions_field && models_field ) {
		var actions = (actions_field.val() || '').split(',');
		var models = (models_field.val() || '').split(',');

		for(var i in actions)
		{
			action = actions[i];
			if( typeof action != 'string' ) { continue; }
			enabled = true;
			any_checked = false;
			for(var mi in models)
			{
				model_id = models[mi];
				if( typeof model_id != 'string' ) { continue; }
				if($j('#'+list_id+'_'+model_id)) {
					if( $j('#'+list_id+'_'+model_id).attr('checked') )
					{
						any_checked = true;
						if( !list_control_action_enabled(list_id, action, model_id) )
						{
							enabled = false;
							break;
						}
					}
				}
			}
			if( !any_checked )
			{
				enabled = false;
			}
			list_control_button_enable(list_id, action, enabled);
		}
	}
}

function list_control_action_enabled(list_id, action, model_id) {
	var model_action_field = $j('#can_'+action+'_'+list_id+'_'+model_id);
	if( model_action_field && model_action_field.val() == 'false' ){
		return false;
	}
	return true;
}

function enableButton(element_id, enable) {
	var button = $j('#'+element_id);
	if(enable) button.removeAttr('disabled');
	else button.attr('disabled', 'disabled');
}

function list_control_button_enable(list_id, action, enable)
{
	enableButton(list_id+'_'+action+'_button', enable);
	el = document.getElementById('dup_'+list_id+'_'+action+'_button');
	if (el) enableButton('dup_'+list_id+'_'+action+'_button', enable);

		// Enable/disable action links
	var link_id = list_id+'_'+action+'_link';
	if( enable ) {
		$j('#'+link_id).show();
	} else {
		$j('#'+link_id).hide();
	}
}

function list_control_prompt(list_id, message)
{
	var comment = prompt(message);
	if( comment )
	{
		$j('#'+list_id+'_action_comment').val(comment)
		return true;
	}
	return false;
}

function list_control_set_duplicate_text(self_id) {
	ss = document.getElementById(self_id);
	el = document.getElementById('dup_'+self_id);
	el.value = ss.value;
	}

function list_control_set_duplicate_select(self_id) {
	ss = document.getElementById(''+self_id);
	el = document.getElementById('dup_'+self_id);
	el.selectedIndex = ss.selectedIndex;
	ss.options[ss.selectedIndex].defaultSelected = true;
	}

	function duplicate_form_fields(update_id) {
			var parent = $j('#'+update_id);
			var duplicate_form_block = $j('#'+update_id+'_duplicate_form_block');
			var inputs = parent.find('input');
			var selects = parent.find('select');

			var i, j;
			for (j=0;j<inputs.length;j++) {
				if (inputs[j].type!='hidden') {
					$j("<input disabled='disabled' type='" + inputs[j].type + "' id='dup_" + inputs[j].id + "' value='" + inputs[j].value + "'>").appendTo(duplicate_form_block);
				}
			}
			for (i=0;i<selects.length;i++) {
				$j("<select id='dup_" + selects[i].id + "'>" + selects[i].innerHTML + "</select>").appendTo(duplicate_form_block);
			}

	}

function fill_form_from_duplicate(update_id) {
	var parent = $j('#'+update_id+'_duplicate_form_block');


	var inputs = parent.find("input");
	var selects = parent.find("select");
	var i, j;

	for (j=0;j<inputs.length;j++) {
		el = document.getElementById(inputs[j].id.replace(/dup_/,""));
		if(el) {
			if(el.type == 'checkbox') el.checked = inputs[j].checked;
			if(el.type == 'submit') el.disabled = inputs[j].disabled;
			if(el.type == 'text') el.value = inputs[j].value;
			}
		}
	for (i=0;i<selects.length;i++) {
		el = document.getElementById(selects[i].id.replace(/dup_/,""));
		if (el) {
			el.selectedIndex = selects[i].selectedIndex;
			el.options[selects[i].selectedIndex].defaultSelected = true;
			}
		}
	}

function revert_selection(list_table_id, list_id) {
	var list_container = $j('#'+list_table_id);
	var inputs = list_container.find('input');
	var set_to_value = -1

	inputs.each(function(){
		var input = this
		if (input.type == 'checkbox') {
			if (set_to_value==-1) {
				set_to_value = !input.checked;
				}
			if (input.checked != set_to_value) {
				input.checked = set_to_value;
				$j(input).trigger('change');
				list_control_update_row_state(input.id);
				if (list_id) list_control_reset_button_state(list_id);
				var dup_element = $j('#dup_'+input.id);
				if (dup_element) {
					dup_element.attr('checked', set_to_value);
				}
			}
		}
	})
}

function list_control_tr_select_item(list_id, model_id, list_model_id)
{
	var element;
	element = document.getElementById(list_model_id);
 	element.checked = !element.checked;
	list_control_select_item(list_id, model_id, list_model_id);
}

function list_control_bind_tr_select(list_collection_id, list_table_id) {
	$j(document).ready(function(){
		var table_id = list_table_id + 'ListTable';

		// Bind multiselect checkboxes.
		$j('#' + table_id).find('td.Center.Selection > input').each(function() {
			$j(this).click(function(e) {
				var list_model_id =	this.id.split(/_row/)[0];
				var list_id =	list_model_id.split(/_(\d+)/)[0];
				var model_id =	list_model_id.split(/_(\d+)/)[1];
				list_control_select_item(list_id, model_id, list_model_id)
			});
		});

		// Bind list rows (<tr>) to select item on the list by 'click' on the row.
		$j('#' + table_id).find('tr[id^="' + list_collection_id + '"]').each(function() {
			$j(this).click(function() {
				var list_model_id = this.id.split(/_row/)[0];
				var list_id = list_model_id.split(/_(\d+)/)[0];
				var model_id = list_model_id.split(/_(\d+)/)[1];
				list_control_tr_select_item(list_id, model_id, list_model_id);
			});
		});

		// Bind all 'input' and 'a' elements to stop event propagation if they have already bind 'click' event.
		// It`s because we don`t want to select list item, only want to call 'click' event defined for element.
		$j('#' + table_id).find('input,a').each(function() {
			$j(this).click(function(e) {
				e.stopPropagation();
			});
		});
	});
}


/** Flexigrid extensions and helpers **
(function($) {
	$.fn.listControl = function(params) {
		return this.each(function() {
			var list_element = $(this);
			var list_control = new ListControl(list_element, params);
			list_control.draw();
		});
	};

	function ListControl(list_element, params)
	{
		this.list_element = list_element;
		this.params = params;
	}

	window.ListControl = ListControl;

	ListControl.DEFAULT_OPTIONS = {
		dataType: 'json',
		usepager: true,
		useRp: true,
		rp: 10,
		showTableToggleBtn: false,
		resizable: false,
		width: 'auto',
		height: 200,
		singleSelect: false
	};

	ListControl.prototype = {
		draw: function() {
			this.list_element.flexigrid(this.flexigridParams());
		},

		flexigridParams: function() {
			return $.extend({
				preProcess: $.proxy(this.prepareData, this)
			}, ListControl.DEFAULT_OPTIONS, this.params);
		},

		prepareData: function(data) {
			var self = this;
			for (ci = 0; ci < this.params.colModel.length; ci++) {
				var col = this.params.colModel[ci];
				if( $.isFunction(col.render) ) {
					$.each(data.rows, function (i, row) {
						row.cell[ci] = col.render.call(this, col.name, row.cell);
					});
				}
			}
			return data;
		}
	};

	// rendering m
	ListControl.render = {
		labelWithIcon: function(icon_attr) {
			return function(attr, data) {
				var label = data[attr] || '';
				var icon_class = data[icon_attr] ? "Icon"+data[icon_attr].camelize(true) : '';
				return "<span class='IconLink IconLeft " + icon_class + "'>" + label + "</span>";
			}
		},

		dateTime: function(format) {
			return function(attr, data) {
				var xmldate = data[attr] || '';
				return xmldate.xmlDateToString(format);
			}
		}
	}

})(jQuery);*/

/** jqGrid extensions **/
(function($) {
	$.extend($.fn.fmatter, {
		systemTime: function(cellvalue, options) {
			var opts = {};
			if(!$.fmatter.isUndefined(options.colModel.formatoptions)) {
				opts = $.extend({},opts,options.colModel.formatoptions);
			}
			return cellvalue.xmlDateToString(opts.format);
		},
		iconLink: function(cellvalue, options, object) {
			var opts = {};
			if(!$.fmatter.isUndefined(options.colModel.formatoptions)) {
				opts = $.extend({},opts,options.colModel.formatoptions);
			}
			var url = object.id && opts.route ? opts.route(object.id).url : '';
			var icon_class = object[opts.icon] ? "Icon"+object[opts.icon].camelize(true) : '';
			return "<a class='IconLink IconLeft "+icon_class+"' href='"+url+"'>"+cellvalue+"</a>";
		}
	})
})(jQuery);
