$(document).ready( function() {
	
		// pokazanie dwoch slow dla kazdego z filtrow (alfabetycznie lub wg popularnosci)
		$('#pojProductListFilterFeatures ul ul').each(function(){
		
				iPopularCount = $(this).children('.popularFilterWord').length;
				
				// brak popularnych slow -> pokazujemy pierwsze dwa
				if(iPopularCount == 0){					
						$(this).children('li').eq(0).removeClass('pojFilterHiddenWord');
						$(this).children('li').eq(1).removeClass('pojFilterHiddenWord');
				}
				
				// jest jedno popularne slowo -> sprawdzamy jego pozycje
				else if(iPopularCount == 1){
						iPopularPosition = $(this).children('li.pojFilterHiddenWord').index($(this).children('li.popularFilterWord'));
						if(iPopularPosition == 0 || iPopularPosition == 1){
								$(this).children('li').eq(0).removeClass('pojFilterHiddenWord');
								$(this).children('li').eq(1).removeClass('pojFilterHiddenWord');
						}else{
								$(this).children('li').eq(0).removeClass('pojFilterHiddenWord');
								$(this).children('li.popularFilterWord').removeClass('pojFilterHiddenWord');
						}
				}
				
				// sa dwa popularne slowa (lub wiecej) -> pokazujemy je
				else if(iPopularCount >= 2){
						$(this).children('li.popularFilterWord').removeClass('pojFilterHiddenWord');
				}
			
		})	

		$('#pojProductListFilterFeatures input:checkbox').click(function(){
			
			$('#pojLiczbaProduktowPreloader').show();
			$('#pojLiczbaProduktow').hide();
			
			var sQueryString = $('#pojProductListFilterForm').formSerialize();
			
			sUrl = 'index.php?sModul=mProductsXML&sAction=showNumberOfProductsByFilter';
		
			$.ajax({
					type: 'GET',
					url: sUrl,
					dataType : 'xml',
					data: sQueryString,
					success: function(oXml){				
							
							var iStatus = $(oXml).find('iStatus').text();
							
							if (iStatus == 1){
								
									$('#pojLiczbaProduktow b').text($(oXml).find('content').text());
								
							} else {
									
									alert( $(oXml).find('sMessage').text() );
							}
							
							$('#pojLiczbaProduktowPreloader').hide();
							$('#pojLiczbaProduktow').show();
					}
			});
			
		})
		
		// ukrycie przycisku pokazania dodatkowych opcji filtru, jesli nie istnieja
		if (!($('.pojFilterHiddenWord').length > 0)) {
			$('#pojProductListFilterFeatures .btn-options').hide();
			$('.pojFormCols ul').css('border-bottom','none');
		}
		
		// naprawa linkow o klasie linkRightArrow (przesuniecie strzalki na srodek w pionie, dosuniecie jej do tekstu)
		fRepairLinkRightArrow();


})

function fToggleHiddenFeaturesFilters(){
	
		// zwin
		if ($('#pojProductListFilterFeatures .btn-options a').hasClass('btn-callapse')) {
		
			$('#pojProductListFilterFeatures .pojFilterHiddenWord').hide();
			$('#pojProductListFilterFeatures .btn-options a').removeClass('btn-callapse');
			$('#txtHideFilter').hide();
			$('.pojBtnExpandOptions a').show();
			$('.pojBtnExpandOptions').css('line-height','15px');
			$('.pojBtnExpandOptions').css('font-size','11px');
			//$('#txtMoreFilter').show();
		
		}
		else {
		// rozwin
		
			$('#pojProductListFilterFeatures .pojFilterHiddenWord').show();
			$('#pojProductListFilterFeatures .btn-options a').addClass('btn-callapse');
			$('#txtHideFilter').show();
			$('.pojBtnExpandOptions a').hide();
			$('.pojBtnExpandOptions').css('line-height','0px');
			$('.pojBtnExpandOptions').css('font-size','0px');
			//$('#txtMoreFilter').hide();
			
		}
	
}


/**
 * zapisuje typ sortowania do sesji i odswieza strone
 * @param {int} iGroupId - id grupy
 * @param {string} sSortTypeCode - kod typu sortowania
 * @param {string} sFilterCode - ewentualny kod wybranego filtru
 */
function fSetSortTypeToSession(iGroupId,sSortTypeCode,sFilterCode){
	
		sUrl = 'index.php?sModul=mProductsXML&sAction=setSortTypeForGroup&iGroupId='+iGroupId+'&sSortTypeCode='+sSortTypeCode;
		
		$.ajax({
				type: 'GET',
				url: sUrl,
				dataType : 'xml',
				success: function(oXml){				
						
						var iStatus = $(oXml).find('iStatus').text();
						
						if (iStatus == 1){
							
								sHref = location.href;
								aHref = sHref.split('#');
								if(aHref.length>0)
										sHref = aHref[0];
										
								// dodanie ewentualnych informacji o filtrze
								if(sFilterCode){
									
										if (sHref.indexOf('sFilter')<0) {
										
											aHref = sHref.split('?');
											sSign = '?';
											if (aHref.length > 1) 
												sSign = '&';
											
											sHref = sHref + sSign + 'sFilter='+sFilterCode;
											
										}
									
								}
							
								location.href = sHref;
							
						} else {
								
								alert( $(oXml).find('sMessage').text() );
						}
				}
		});
	
}
