function redirect(url,absolute)
{
window.location=(absolute ? '':baseUrl)+url;
}


(function($){
			$(
				
				////////////////////////////	
				function()
				{
					Box_Switchable(jQuery);
					
					$('form.quickSearch input,form.quickSearch select').change(
										function()
										{
											$(this).parents('form').submit();
										});
										
				
					$('form.quickSearch').submit(
							function()
							{						
											sex=0;
											if($(this).find('#sex_men').attr('checked'))
												sex|=1;
											if($(this).find('#sex_women').attr('checked'))
												sex|=2;
											if($(this).find('#sex_unisex').attr('checked'))
												sex|=4;
											
											type=0;
											if($(this).find('#type_perfumy').attr('checked'))
												type|=1;
											if($(this).find('#type_kosmetyki').attr('checked'))
												type|=2;
											if($(this).find('#type_zestaw').attr('checked'))
												type|=4;
											
											vendor=$(this).find('#vendor').val();

											
															
											
											
											promo=0;
											if($(this).find('#promo_promo').attr('checked'))
												promo|=4;
											if($(this).find('#promo_new').attr('checked'))
												promo|=8;
											if($(this).find('#promo_sale').attr('checked'))
												promo|=2;
											
											tester=0;
											if($(this).find('#wrapper_tester').attr('checked'))
												tester|=4;
											
											url='produkty,'+'s,'+sex+',type,'+type+',vendor,'+vendor+',promo,'+promo+',tester,'+tester;
										 		
											redirect(url,true);
											
											return false;
											
										}
										);
					
					
					$('form.advancedSearch').submit(
							function()
							{						
											sex=0;
											if($(this).find('#sex_men').attr('checked'))
												sex|=1;
											if($(this).find('#sex_women').attr('checked'))
												sex|=2;
											if($(this).find('#sex_unisex').attr('checked'))
												sex|=4;
											
											type=0;
											if($(this).find('#type_perfumy').attr('checked'))
												type|=1;
											if($(this).find('#type_kosmetyki').attr('checked'))
												type|=2;
											if($(this).find('#type_zestaw').attr('checked'))
												type|=4;
											
											vendor=$(this).find('#vendor').val();

											classId=$(this).find('#class').val();

											
											promo=0;
											if($(this).find('#promo_promo').attr('checked'))
												promo|=4;
											if($(this).find('#promo_new').attr('checked'))
												promo|=8;
											if($(this).find('#promo_sale').attr('checked'))
												promo|=2;
											
											tester=0;
											if($(this).find('#wrapper_tester').attr('checked'))
												tester|=4;
											
											
											
											
											$.notes={head: new Array(), heart: new Array(), body: new Array(), anylevel: new Array()};
											$('.notes-holder tr').each(	function()	{
																	
																	id=$(this).find("select[name^='note_']").val();
																		if (id==0) return true;
																	
																	if ($(this).find("input[name^='head']").attr('checked') )
																		$.notes.head.push(id);
																	
																	if ($(this).find("input[name^='heart']").attr('checked') )
																		$.notes.heart.push(id);
																		
																	
																	if ($(this).find("input[name^='body']").attr('checked') )
																		$.notes.body.push(id);
																		
																	if ($(this).find("input[name^='anylevel']").attr('checked') )
																		$.notes.anylevel.push(id);
																	
																	}
																	);
											
											notes=''; i=0; 
											for (key in $.notes)
											{
												if($.notes[key].length)
												{
													l=0;
													notes+= (i++ ? '-':'') + key+'_';
													for(n=0; n<$.notes[key].length; n++)
													{
														notes+=  (l++ ? '.':'')+$.notes[key][n];
													}
												}
													
											} 
											if (notes=='') notes=0;
											
											
											
											url='produkty,'+'s,'+sex+',type,'+type+',vendor,'+vendor+',promo,'+promo+',tester,'+tester+',class,'+classId+',notes,'+notes;
										 		
											redirect(url,true);
											
											return false;
											
										}
						);
					
					
					
					$('form.searchByString').submit(
											function()
											{
												
												
												searchString=$("form.searchByString input[name='string']").val();
												
												searchString=searchString.replace('/','');
												searchString=searchString.replace('\\','');
												
												url='produkty'+',search,'+encodeURIComponent(searchString);
											 		
												redirect(url,true);
												
												return false;
											}
											);
					
					$('.submitButton').click(
											function()
											{
												$(this).parents('form').append('<input type="hidden" name="link-href" value="'+$(this).attr('href')+'">');
												$(this).parents('form').submit();
												return false;
											}
											);
					//////////////////////////////////
					
					
					//opakowanie:
					
					$('a.zap-war').hover(function() { $('div#pack-helper').show();  } ,
										function() { $('div#pack-helper').hide(); } );
					
					
					//komentarze:
					$('a.pokaz-wszystkie').click(function(){
						$('.com:not(:first)').remove();
								$.get( $(this).attr('href'),function(data){
									$('.com').replaceWith(data);
								} );
								
								$(this).hide();
								return false;
					});
					
					function showCommentBox()
					{
					$('#dodaj-kom').css('top', ($(window).height()-$('#dodaj-kom').height())/2 );
					$('#dodaj-kom').css('left', ($(window).width()-$('#dodaj-kom').width())/2);
					$('#dodaj-kom').show();
					return false;
					}
					
					$('a.dodaj-opinie').click(showCommentBox);
					
					(function dodajKomentarzBoxInit()
					{
						$('a.zamknij-kom').click(function()
								{
								$('#dodaj-kom').hide();
								return false;
								});
						
						$('#dodaj-kom-form').submit(function(){
								$.post('produkt,dodajkomentarz',$(this).serialize(),function(data) {
									$('#dodaj-kom').html(data);
									dodajKomentarzBoxInit();
									}
									,'html' );
		
								return false;
						});

					}) ();
					
					$('#dodaj-kom.show').each(showCommentBox);
					
				}
			);
			
			}
		
)(jQuery);

