
$(document).ready(function() {
	
	
	var getCategories = true;
	// Zuerst Checkboxdaten = Mögliche Kategorien laden
	$.ajax({
	  url: 'index.php?type=9991',
	  data: 'getCategories=true',
	  success: getGrid
	});
	

	function getGrid(data){
		jQuery('.FUNCTION_greyWrapperBox_db').digiWrapper({mode: 'click',  toggler: '.FUNCTION_wrapperToggler_db', element: '.FUNCTION_wrapperElement_db', wrapperGroup: '.FUNCTION_wrapper', speed: 300, classOpen: 'STYLE_greyWrapperOpen', classClose: 'STYLE_greyWrapperClose'});	
		
		$('#dienstleisterOverviewCheckboxes').append(data);
		
		
	    checkData = $("input:checkbox:checked").serializeArray();
	
		var mygrid = jQuery("#dienstleisterGrid").jqGrid({
			url: 'index.php?type=9991',
			datatype: "json",
			serializeGridData: function(postdata){
				
				if ($("input:checkbox:checked").serializeArray() != '') {
					postdata.katValues = $("input:checkbox:checked").serializeArray();
					postdata._search = true;
					
				} else{
					postdata.katValues = '';					
				}
				
				postdata.freeDienstleisterSearch = $("#freeDienstleisterSearchInput").val();
				postdata._search = true;
				
				
				return postdata;
			},
			width: 915,
			height: '100%',
			altRows: true,
			altclass: 'gridEvenRow',
			colNames: ['Dienstleister', 'Logo', 'PLZ','Ort', 'ID'],
			forceFit: true,
			colModel: [{
				name: 'company',
				index: 'company',
				width: 350
			}, {
				name: 'logo',
				index: 'logo',
				sortable: false,
				search:false,
				width: 150
			}, {
				name: 'zip',
				index: 'zip',
				width: 75
			},{
				name: 'city',
				index: 'city',
				width: 75
			}, {
				name: 'id',
				index: 'id',
				hidden: true
			}],
			cellLayout: 30,
			rowNum: 10,
			mtype: "POST",
			rowList: [10, 20, 30],
			pager: '#dienstleisterGridPager',
			sortname: 'company',
			viewrecords: true,
			sortorder: "asc",
			gridview: true,
			caption: "Dienstleister Prototype Grid",
			onSelectRow: function(ids){
				var ret = jQuery("#dienstleisterGrid").jqGrid('getRowData', ids);
				window.location.href = '/index.php?id=' + digiID + '&tx_digipetseic_pi2[dienstleister]=' + ret.id + '&tx_digipetseic_pi2[action]=detailview&tx_digipetseic_pi2[controller]=Dienstleister';
			},
			gridComplete: function(){
				/* Hover */
				$('.ui-jqgrid-btable tr').hover(function(){
					$(this).find('h5,a,p').css('textDecoration', 'underline');
					$(this).find('td').css('cursor', 'pointer');
				}, function(){
					$(this).find('h5,a,p').css('textDecoration', 'none');
				});
				
					// Suchanfrage Events Checkboxen
					$('input:checkbox').change(function() {					  
					  mygrid.trigger("reloadGrid");
					});

					// Suchanfrage Events Freetext Input
					$('#freeDienstleisterSearch').click(function() {					  
					  mygrid.trigger("reloadGrid");
					});

					$('#freeDienstleisterSearchInput').keypress(function(event){
					  if (event.keyCode == 13) {
						 mygrid.trigger("reloadGrid");
					  }
					});
					
					$('#checkDienstleisterAlle').click(function() {					  
					 $('input:checkbox').attr('checked', true);
					 
					  mygrid.trigger("reloadGrid");
					});
				
					$('#checkDienstleisterKeine').click(function() {					  
					 $('input:checkbox').attr('checked', false);
					 
					  mygrid.trigger("reloadGrid");
					});
				
				
			},

			beforeRequest: function() {
				$('.jqgrid-overlay').css('display','block'); //Overlay während dem Laden
			},

			loadComplete: function() {
				$('.jqgrid-overlay').css('display','none'); //Entfernen des Overlays nach dem Laden
			}
			
		});
		
		jQuery("#dienstleisterGrid").jqGrid('navGrid', '#dienstleisterGridPager', {
			edit: false,
			add: false,
			del: false,
			search: false,
			refresh: false
		});
		jQuery("#dienstleisterGrid").jqGrid('navButtonAdd', "#dienstleisterGridPager", {
			caption: "Toggle",
			title: "Toggle Search Toolbar",
			buttonicon: 'ui-icon-pin-s',
			onClickButton: function(){
				mygrid[0].toggleToolbar()
			}
		});
		jQuery("#dienstleisterGrid").jqGrid('navButtonAdd', "#dienstleisterGridPager", {
			caption: "Clear",
			title: "Clear Search",
			buttonicon: 'ui-icon-refresh',
			onClickButton: function(){
				mygrid[0].clearToolbar()
			}
		});
		jQuery("#dienstleisterGrid").jqGrid('filterToolbar');
		
		
		//Hinzugefügt von Simon - 01.02.2010 BEGIN
		$('.ui-jqgrid-labels th:first').addClass('STYLE_datenbankTabelleHeadSelect');
		$('.ui-jqgrid-labels th:first').attr('aria-selected', 'true');
		
		$('.ui-jqgrid-labels th .ui-jqgrid-sortable').not(".ui-jqgrid-labels th:nth-child(2) .ui-jqgrid-sortable").click(function(){
			$('.ui-jqgrid-labels th').not($(this).parent()).removeClass('STYLE_datenbankTabelleHeadSelect');
			$(this).parent().addClass('STYLE_datenbankTabelleHeadSelect');
			
			if ($(this).find('.ui-icon-asc').hasClass('ui-state-disabled') && $(this).find('.ui-icon-desc').hasClass('ui-state-disabled')) {
				$(this).find('.ui-icon-desc').css('backgroundPosition', '100% 100%');
				$(this).find('.ui-icon-asc').css('backgroundPosition', '0 100%');
			}
			else {
				if ($(this).find('.ui-icon-asc').hasClass('ui-state-disabled')) {
					$(this).find('.ui-icon-asc').css('backgroundPosition', '0 100%');
				}
				else {
					$(this).find('.ui-icon-asc').css('backgroundPosition', '0 0');
				}
				
				if ($(this).find('.ui-icon-desc').hasClass('ui-state-disabled')) {
					$(this).find('.ui-icon-desc').css('backgroundPosition', '100% 0');
				}
				else {
					$(this).find('.ui-icon-desc').css('backgroundPosition', '100% 100%');
				}
			}
			
		});
		
		$('.ui-jqgrid-labels th').not(".ui-jqgrid-labels th:nth-child(2)").hover(	//mouseover
		function(){
			$(this).addClass('STYLE_datenbankTabelleHeadSelect');
			$(this).find('.s-ico').css('display', 'block');
		//mouseleave	
		}, function(){
			if ($(this).attr('aria-selected') != 'true') {
				$(this).removeClass('STYLE_datenbankTabelleHeadSelect');
				$(this).find('.s-ico').css('display', 'none');
			}
		});
	//Hinzugefügt von Simon - 01.02.2010 END
	
	

	
	
	
	} // End of getGrid function	

}); 
