$(document).ready(function(){

	var products = [] ;

	//Alle Produkte in Array laden
	$('.rstpb_item').each(function(index) {
		var itemId = $(this).attr('id');
		products[itemId] = 1;	
	});

	//Alle Checkboxen setzen
	$("#rstpb_selection input.rstpb_checkbox").attr('checked',true);
	$("#rstpb_selection input.rstpb_checkbox").removeAttr('disabled');
	//EMV Checkbox nicht setzen
	$("#rstpb_selection input.rstpb_emvcheckbox").attr('checked',false);

	//listener
   	$("#rstpb_selection input.rstpb_checkbox").click(function() {
   	
   		//Funktion
   		var classNameReal = $(this).attr('value');
		var className = '.'+$(this).attr('value');
		var classNameCheck = $(this).attr('value_check');
		var idNameImage = '#'+$(this).attr('value')+'_image';

		//Andere Checkbox deaktivieren, wenn letzte Checbox der Gruppe
		if(classNameReal=="rstpb_ex") {  //ex
			if($('#rstpb_noex_select').attr('checked')=='checked' && $(this).attr('checked')!='checked')
				$('#rstpb_noex_select').attr('disabled',true);
			else
				$('#rstpb_noex_select').removeAttr('disabled');
		}
		else if(classNameReal=="rstpb_noex") {   //noex
			if($('#rstpb_ex_select').attr('checked')=='checked' && $(this).attr('checked')!='checked')
				$('#rstpb_ex_select').attr('disabled',true);
			else
				$('#rstpb_ex_select').removeAttr('disabled');
		}
		else {  //Checkboxen mit abhängigkeiten
			var countDisable=0;
			var count = 0;
			//Prüfen
			$.each(classNameCheck.split(' '), function(index, value) { 
				if($('#'+value+'_select').attr('checked')!='checked')
					countDisable=countDisable+1;
				count=count+1;
			});
			//Verarbeiten
			if(count==(countDisable+1) && $(this).attr('checked')!='checked') {
				$.each(classNameCheck.split(' '), function(index, value) { 
					if($('#'+value+'_select').attr('checked')=='checked')
						$('#'+value+'_select').attr('disabled',true);
						
				});
			}
			else {
				$.each(classNameCheck.split(' '), function(index, value) { 
					$('#'+value+'_select').removeAttr('disabled');
				});		
			}
		}

		//Daten verarbeiten
		if($(this).is(':checked')) {

			$(className).each(function(index) {
				var classItem = $(this).attr('class');
				classItem = classItem.split('rstpb_item ').join('');
				classItem = classItem.split('rstpb_noemv ').join('');
								
				var showItem = 1;
				var showItemItem = 0;
				var classNameCheckCount = 0;
				var emvActive = 0
				
				//Nicht wieder anzeigen wenn EMV anzeigen gewählt und EMV Produkt 
				if($(this).hasClass("rstpb_noemv") && $("#rstpb_selection input.rstpb_emvcheckbox").attr('checked')=='checked') {
					emvActive = 1;
				}
				//else {
				
				$.each(classItem.split(' '), function(index, value) { 
					classNameCheckThis = $('#'+value+'_select').attr('value_check');
  						 
					//Prüfen ob abhängige Checkboxen vorhanden sind
					if(classNameCheckThis) {	
						
						$.each(classNameCheckThis.split(' '), function(index, valueItem) { 
  							if($('#'+value+'_select').attr('checked')=='checked')
  								showItemItem = 1;
						});
						classNameCheckCount = classNameCheckCount + 1;
					}
					else {
						if($('#'+value+'_select').attr('checked')!='checked')
  						 	showItem = 0;	
  					}				 
				});
				if(showItemItem!=1 && classNameCheckCount>0)
					showItem = 0;				
				
				var itemId = $(this).attr('id');
				products[itemId] = showItem;
				
				if(showItem==1 && emvActive==0) 
					$(this).fadeIn("slow");

			});
			
			//Bild tauschen
			$(idNameImage).attr('src',$(idNameImage).attr('src').split('_inactive.jpg').join('.jpg'));
		}
		else {		

			$(className).each(function(index) {
				//Prüfen ob abhängige Checkboxen vorhanden sind
				if(classNameCheck) {
					var classItem = $(this).attr('class');
					classItem = classItem.split('rstpb_item ').join('');
					classItem = classItem.split('rstpb_noemv ').join('');
				
					var showItem = 0;
					$.each(classNameCheck.split(' '), function(index, value) { 
						$.each(classItem.split(' '), function(indexItem, valueItem) { 		
							if(value==valueItem) {	
  								if($('#'+value+'_select').attr('checked')=='checked') {
  									showItem = 1;
  								}
  							}
						});
					});
					
					var itemId = $(this).attr('id');
					products[itemId] = showItem;
	
					if(showItem==0) 
						$(this).fadeOut("slow");
				
				}
				else {
					var itemId = $(this).attr('id');
					products[itemId] = 0;
				
					$(this).fadeOut("slow");
					
				}	
			});

			//Bild tauschen
			$(idNameImage).attr('src',$(idNameImage).attr('src').split('.jpg').join('_inactive.jpg'));

		}
   
      	});
   
   
	//listener emv
   	$("#rstpb_selection input.rstpb_emvcheckbox").click(function() {
		if($(this).is(':checked')) {
			$(".rstpb_noemv").each(function(index) {
				$(this).fadeOut("slow");	
			});
		}
		else {		
			$(".rstpb_noemv").each(function(index) {
				var itemId = $(this).attr('id');
				if(products[itemId]==1)
					$(this).fadeIn("slow");	
			});
		}
      	});
});
     


