/**
 * 
 * Client side script for Datagrid
 * @author Jaroslav Moravec (jaroslav.moravec@e-invent.eu)
 * @version 1.0.10.20
 * 
 */

function Datagrid() {
	
	var self = this;
	
	/**
	 * Initialize event listeners
	 */
	self.init = function() {
	
		/**
		 * Check all checkboxes in datagrid
		 */
		$(".jq_datagridCheckAll").live('click', function(){
			$(".jq_datagridCheckbox").attr("checked", $(this).attr("checked"));
		});
		
		/**
		 * Go to the first page event
		 */
		$(".jq_datagridPaginatorFirst").live('click', function(){
			var page = 1;
			self.changePage(page);
			return false;
		});
		
		/**
		 * Go to the previous page event
		 */
		$(".jq_datagridPaginatorPre").live('click', function(){
			var page = parseInt($(".jq_datagridPaginatorPage").val()) - 1;
			if (page < 1) {
				return false;
			}
			self.changePage(page);
			return false;
		});
		
		/**
		 * Go to the next page event
		 */
		$(".jq_datagridPaginatorNext").live('click', function(){
			var page = parseInt($(".jq_datagridPaginatorPage").val()) + 1;
			var lastPage = parseInt($("#datagridPaginatorLastPage").html());
			if (page > lastPage) {
				return false;
			}
			self.changePage(page);
			return false;
		});
		
		/**
		 * Go to the last page event
		 */
		$(".jq_datagridPaginatorLast").live('click', function(){
			var page = parseInt($("#datagridPaginatorLastPage").html());
			self.changePage(page);
			return false;
		});
		
		/**
		 * Go to the page event (when pressed enter)
		 * @param {Object} event
		 */
		$(".jq_datagridPaginatorPage").live('keypress', function(event){
			var code = event.keyCode || event.which;
			if(code == 13) {
				var page = parseInt($(".jq_datagridPaginatorPage").val());
				var lastPage = parseInt($("#datagridPaginatorLastPage").html());
				if (page < 1) {
					page = 1;
				}
				if (page > lastPage) {
					page = lastPage;
				}
				self.changePage(page);
			}
		});
		
		/**
		 * Change showed items per page event
		 */
		$(".jq_datagridPaginatorItems").live('change', function(){
			var items = $(this).val();
			self.changeItems(items);
			return false;
		});
		
		/**
		 * Filter event
		 */
		$(".jq_datagridFilterText").live('keypress', function(event){
			var code = event.keyCode || event.which;
			if(code == 13) {
				var column = $(this).attr('name');
				var filter = $(this).val();
				self.addFilter(column, filter);
			}
		});
		
		/**
		 * Show/hide filter inputs event
		 */
		$(".jq_datagridFilter").live('click', function(){
			
			var filterInputsVisible = $('div.filter:visible');
			
			if (filterInputsVisible.length > 0) {
				$('div.filter').slideUp();
			} else {
				$('div.filter').slideDown();
			}
			
			return false;
		});
		
		/**
		 * Checkbox action auto event
		 */
		$(".jq_datagridCheckboxAction").live('change', function() {
			if (confirm(php.lang.trans.dgConfirm)) {
				var action = $(".jq_datagridCheckboxAction").val();
				var checked = datagrid.getChecked();
				eval(action+"(datagrid.getChecked())");
			}
		});
		
		/**
		 * Checkbox action submit event
		 */
		$(".jq_datagridCheckboxActionSubmit").live('click', function() {
			if (confirm(php.lang.trans.dgConfirm)) {
				var action = $(".jq_datagridCheckboxAction").val();
				var checked = datagrid.getChecked();
				eval(action+"(datagrid.getChecked())");
			}
		});
		
		/**
		 * Hide filter textfields
		 */
		self.checkFilter(true);
	}
	
	/**
	 * Change page (AJAX)
	 * @param Int page
	 */
	self.changePage = function(page) {
		system.ajaxRequest (
			[ 'ext', php.extId ],
			'actionChangePage',
			[
				[ 'page', page ]
			],
			{ fce : self.updateSnippet }
		);
		return false;
	}
	
	/**
	 * Change order (AJAX)
	 * @param String column name of column
	 * @param String way (DESC|ASC)
	 */
	self.changeOrder = function(column, way) {
		system.ajaxRequest (
			[ 'ext', php.extId ],
			'actionChangeOrder',
			[
				[ 'column', column ],
				[ 'way', way]
			],
			{ fce : self.updateSnippet }
		);
	}
	
	/**
	 * Change items per page (AJAX)
	 * @param Int items
	 */
	self.changeItems = function(items) {
		system.ajaxRequest (
			[ 'ext', php.extId ],
			'actionChangeItemsPerPage',
			[
				[ 'items', items ]
			],
			{ fce : self.updateSnippet }
		);
	}
	
	/**
	 * Apply filter (AJAX)
	 * @param String column name of column
	 * @param String filter text of filter
	 */
	self.addFilter = function(column, filter) {
		system.ajaxRequest (
			[ 'ext', php.extId ],
			'actionAddFilter',
			[
				[ 'column', column ],
				[ 'filter', filter]
			],
			{ fce : self.updateSnippet }
		);
	}
	
	/**
	 * Reload datagrid (update/rerender)
	 */
	self.reload = function(){
		system.ajaxRequest (
			[ 'ext', php.extId ],
			'actionReload',
			[
			],
			{ fce : self.updateSnippet }
		);
	}
	
	/**
	 * Get ids of checked rows
	 * Return ids of checked rows
	 */
	self.getChecked = function() {
		var checked = new Array();
		$(".jq_datagridCheckbox:checked").each(function() {
			checked.push($(this).val());
		});
		return checked;
	}
	
	/**
	 * Gets id of item (from actions)
	 * @param jQuery object
	 * @return int id of db row
	 */
	self.getItemId = function(element) {
		var tmp = $(element).attr('id').split("-");
		return tmp[1];
	}
	
	/**
	 * Update datagrid snippet 
	 * @param String xmldata
	 */
	self.updateSnippet = function(xmldata) {
		$("#datagridContainer").html(xmldata);
		self.checkFilter(false);
	}
	
	/**
	 * Check filter and hide filter textfields
	 * @param Boolean allowHide 
	 */
	self.checkFilter = function(allowHide) {
		
		var hide = true; 
		
		$('div.filter input').each(function() {
			if ($(this).val() != '') {
				hide = false;
			}
		});
		
		if (!hide) {
			$('a.datagridFilterButton').css('background-color', '#9F644F');
		} else {
			if (allowHide) $('div.filter').hide(); 
		}
		
	}
}

var datagrid = new Datagrid();

$(document).ready(function() {
	datagrid.init();
});

