function readCookie(nazwa){
	nazwa+="=";
	//uzupełnia nazwę
	
	startCookie=document.cookie.indexOf(nazwa);
	//sprawdza czy istnieje cookie o podanej nazwie
	
	if (startCookie==-1) {return ""}
	//jeżeli nie istnieje
	
	startCookie+=nazwa.length;
	//początek treści cookie
	
	if (document.cookie.indexOf(";",startCookie)==-1){
		//jeżeli jest więcej cookie
		koniecCookie=document.cookie.length;
		//jeżeli nie ma podstawia jako koniec cookie, koniec całego tekstu
	}else{
		koniecCookie=document.cookie.indexOf(";",startCookie);
		/*
		podstawia pod koniec miejsce występowania ";"
		(czyli początek następnego cookie)
		*/
	}
	
	textCookie=document.cookie.substring(startCookie,koniecCookie);
	//kopiuje treść cookie
	
	textCookie=unescape(textCookie);
	//rozkodowuje treść
	
	return textCookie;
	//zwraca wartość
}

function countProductsIn(tekst){
	var ciag = '.';
	var indeks = 0;
	var indeks_w = 0;
	var counter = 0;
	while (indeks_w!=-1){
		indeks_w = tekst.indexOf(ciag,indeks);
		if(indeks_w!=-1){
			indeks = indeks_w + 1;
			counter++;
		}
	}
	return counter;	
}
/*
function dodajDoSchowka(id){
	if(id){
		cookie=readCookie('myStuff');
		exists=cookie.indexOf('#'+id+',', 0);
		
		var waznosc=new Date();
		waznosc.setTime(waznosc.getTime()+1000*60*60*24*7);
		
		if(exists==-1){data="myStuff=" + cookie+"#"+id+","+";expires=" + waznosc.toGMTString() + ";path=/;"; document.cookie=data; alert("Produkt dodano do schowka.");}
		else{document.cookie="myStuff="+cookie.replace('#'+id+',', '')+";expires=" + waznosc.toGMTString() + ";path=/;"; alert("Produkt usunięto ze schowka.");}
		countProductsIn(cookie);
	}
	pokazSchowek();	
}

function pokazSchowek(){
	count = countProductsIn(readCookie('myStuff'));
	$('#portal-top-options li em').html(count);
}*/

function addToLib(id, library){
	if(id){
		cookie=readCookie(library);
		exists=cookie.indexOf('.'+id+',', 0);
		
		var waznosc=new Date();
		waznosc.setTime(waznosc.getTime()+1000*60*60*24*7);
		
		if(exists==-1){data=library+"=" + cookie+"."+id+","+";expires=" + waznosc.toGMTString() + ";path=/;"; document.cookie=data; return 1; }
		else{document.cookie=library+"="+cookie.replace('.'+id+',', '')+";expires=" + waznosc.toGMTString() + ";path=/;"; return 0; }
	}
}

function getLibValue(library){
	library+="=";
	//uzupełnia nazwę
	
	startCookie=document.cookie.indexOf(library);
	//sprawdza czy istnieje cookie o podanej nazwie
	
	if (startCookie==-1) {return ""}
	//jeżeli nie istnieje
	
	startCookie+=library.length;
	//początek treści cookie
	
	if (document.cookie.indexOf(";",startCookie)==-1){
		//jeżeli jest więcej cookie
		koniecCookie=document.cookie.length;
		//jeżeli nie ma podstawia jako koniec cookie, koniec całego tekstu
	}else{
		koniecCookie=document.cookie.indexOf(";",startCookie);
		/*
		podstawia pod koniec miejsce występowania ";"
		(czyli początek następnego cookie)
		*/
	}
	
	textCookie=document.cookie.substring(startCookie,koniecCookie);
	//kopiuje treść cookie
	
	textCookie=unescape(textCookie);
	//rozkodowuje treść
	
	return textCookie;
	//zwraca wartość
}

function loadSelectCategory( parent, table, source, show ){
	var ts = new Date().getTime();
	$.ajax({
		type: "POST",
		url: "/ELEMENTS/element.select-categories.php",
		data: "parent="+parent+"&table="+table+"&ts="+ts,
		success: function(msg){
			$('#'+source).html(msg);
		},
		error: function(){ alert("An error occurred (load category list), please try again later or report to adminstrator."); }
	});
	
	if(show){ $('#'+source+'_row').show(); }
}

function loadPriceRange( table, id, value0, value1 ){
	var name = "";
	if(table == 'g'){ name = 'groupID'; 		}
	if(table == 'c'){ name = 'categoryID'; 		}
	if(table == 's'){ name = 'subcategoryID'; 	}
	
	$.ajax({
		type: "POST",
		url: "/ELEMENTS/element.price-range.php",
		data: name+"="+id,
		success: function(msg){ 
			var range = new Array();
			range = msg.split(':');
			setSlider(range[0], range[1], value0, value1);
			
			$('#ps_r').val(range[0]);
			$('#pt_r').val(range[1]);
		},
		error: function(){ /*alert("An error occurred, please try again later or report to adminstrator.");*/ }
	});
}

function setSlider(range0, range1, value0, value1){
	
	if(!range0) range0 = 0;
	if(!range1) range1 = 300000;
	if(!value0) value0 = range0;
	if(!value1) value1 = range1;
	
	if(parseInt(value0) < parseInt(range0)){ value0 = range0; }
	if(parseInt(value1) > parseInt(range1)){ value1 = range1; }
	
	$('#ps').val(value0);
	$('#pt').val(value1);

	$("#slider-range").slider('option', 'max', parseInt(range1));
	$("#slider-range").slider('option', 'min', parseInt(range0));
	
	$("#slider-range").slider('option', 'values', [parseInt(value0),parseInt(value1)]);

	//alert('change '+range0+' ('+value0+') to '+range1+' ('+value1+') zł');

};


$(document).ready(function(){
	//pokazSchowek();
	
	/* WYSZUKIWANIE */
	var ranges = "";
	var range = new Array();
	range[0] = parseInt($('input[name="pr_range_0"]').val());
	range[1] = parseInt($('input[name="pr_range_1"]').val());
	
	//range[0] = 10;
	//range[1] = 2000;
	
	if(range[0].length == 0) range[0] = 1;
	if(range[1].length == 0) range[1] = 10000;
	
	$('#ps_r').val(range[0]);
	$('#pt_r').val(range[1]);


	$("#slider-range").slider({
		range: true,
		min: range[0],
		max: range[1],
		animate: true, 
		values: [$('#ps').val(), $('#pt').val()],
		slide: function(event, ui) {
			$('#ps').val(ui.values[0]);
			$('#pt').val(ui.values[1]);
		}
	});
	
	$('#ps').change(function(){
		var values = $("#slider-range").slider('option', 'values');
		var value = parseInt($(this).val());
		values[0] = parseInt(values[0]);
		values[1] = parseInt(values[1]);
		
		if(value < values[0] || value > values[1]){
			value = values[0];
			$(this).val(value);
		}else{
			$("#slider-range").slider('option', 'values', [value,values[1]]);
		}
	});
	
	$('#pt').change(function(){
		var values = $("#slider-range").slider('option', 'values');
		values[0] = parseInt(values[0]);
		values[1] = parseInt(values[1]);
		var value = $(this).val();
		if(value < values[0] || value > values[1]){
			value = values[1];
			$(this).val(value);
		}else{
			//alert(values[0]+' '+value);
			$("#slider-range").slider('option', 'values', [values[0],value]);
			$("#slider-range").slider('moveTo', values[0], 1);
			//var valuesss = $("#slider-range").slider('option', 'values');
			//alert(valuesss[0]+' '+valuesss[1]);
		}
	});
	
	/* PRODUCT OFFERS VARIANTS */
	$('a[href^="?variants="]').click(function(){
		
		var store_id = $(this).attr('id').replace('store_','');
		$('li[store_variants="'+store_id+'"]').fadeToggle();
		
		return false;
	});
	
	/* PRODUCT OFFERS SORT */
	$('select[name="order_sort"]').change(function(){
		var value = $(this).val();
		document.location.href = '?order_sort='+value+'&location='+$('select[name="location"]').val();	
	});
	
	
	/* PRODUCT OFFERS LOCATION CHANGE */
	$('select[name="location"]').change(function(){
		var value = $(this).val();
		document.location.href = '?order_sort='+$('select[name="order_sort"]').val()+'&location='+value;	
	});
	
	/* ERROR REPORT SEND */
	var width = screen.width;
	var x = (width - 490) / 2;
		
	$('#errorReportSend').css( 'left', x+'px' );
	$('#errorReportSend').show();
	
	$('#errorReportSend .errorReportBoxHeader a').click(function(){
		$('#errorReportSend').hide();
		return false;
	});
	
	/* ERROR REPORT FAIL */
	var width = screen.width;
	var x = (width - 490) / 2;
		
	$('#errorReportFail').css( 'left', x+'px' );
	$('#errorReportFail').show();
	
	$('#errorReportFail .errorReportBoxHeader a').click(function(){
		$('#errorReportFail').hide();
		return false;
	});
	
	/* CATEGORY TABS */
	$('#categories-tab').click(function(){
		$('#box_category').show();
		$('#box_similary').hide();
		$('#box_more').hide();
		
		$('#categories-tab').addClass('active');
		$('#similary-tab').removeClass('active');
		$('#more-tab').removeClass('active');
		
		return false;
	});
	$('#similary-tab').click(function(){
		$('#box_similary').show();
		$('#box_category').hide();
		$('#box_more').hide();
		
		$('#categories-tab').removeClass('active');
		$('#similary-tab').addClass('active');
		$('#more-tab').removeClass('active');
		
		return false;
	});
	$('#more-tab').click(function(){
		$('#box_more').show();
		$('#box_similary').hide();
		$('#box_category').hide();
		
		$('#categories-tab').removeClass('active');
		$('#similary-tab').removeClass('active');
		$('#more-tab').addClass('active');
		
		return false;
	});
	
	$("a[href^='?display:more-tab']").click(function(){
		$('#box_more').show();
		$('#box_similary').hide();
		$('#box_category').hide();
		
		$('#categories-tab').removeClass('active');
		$('#similary-tab').removeClass('active');
		$('#more-tab').addClass('active');
		
		return false;											 
	});
	
	$('p.message a.close').click(function(){
		$('#msgbox').hide();
		return false;
		
	});

	/* PAGE TABS */
	$('#smartbay-tab').click(function(){
		$('#box_smartbay').show();
		$('#box_stores').hide();
		$('#box_more').hide();
		
		$('#smartbay-tab').addClass('active');
		$('#stores-tab').removeClass('active');
		$('#more-tab').removeClass('active');
		
		return false;
	});
	$('#stores-tab').click(function(){
		$('#box_smartbay').hide();
		$('#box_stores').show();
		$('#box_more').hide();
		
		$('#smartbay-tab').removeClass('active');
		$('#stores-tab').addClass('active');
		$('#more-tab').removeClass('active');
		
		return false;
	});
	$('#more-tab').click(function(){
		$('#box_smartbay').hide();
		$('#box_stores').hide();
		$('#box_more').show();
		
		$('#smartbay-tab').removeClass('active');
		$('#stores-tab').removeClass('active');
		$('#more-tab').addClass('active');
		
		return false;
	});
	
	$("a[href^='?display:more-tab']").click(function(){
		$('#box_more').show();
		$('#box_similary').hide();
		$('#box_category').hide();
		
		$('#categories-tab').removeClass('active');
		$('#similary-tab').removeClass('active');
		$('#more-tab').addClass('active');
		
		return false;											 
	});

	/* MAIN ERROR REPORT */
	$('#error-info a').click(function(){
		var width = screen.width;
		var x = (width - 490) / 2;
		
		$('.video-box object').hide();
		$('.video-box').css('background', '#333333');
		
		$('.errorReportBox').hide();
		
		$('#mainErrorReport').css( 'left', x+'px' );
		$('#mainErrorReport').show();

		return false;
	});
	
	$('#mainErrorReport p.submit a').click(function(){
		$('form[name="errorReportForm"]').submit();
		return false;
	});
	
	$('#mainErrorReport .errorReportBoxHeader a').click(function(){
		$('#mainErrorReport').hide();
		$('.video-box').css('background', 'none');
		$('.video-box object').show();

		return false;
	});
	
	/* PRODUCT ERROR REPORT */
	$("a[href^='/zgloszenie-problemu.html?productProblemReport=']").click(function(){
		var id = $(this).attr('href').replace('/zgloszenie-problemu.html?productProblemReport=', '');
		$('#productIDProblem').val(id);
		
		var width = screen.width;
		var x = (width - 490) / 2;
		
		$('.errorReportBox').hide();
		
		$('#productErrorReport').css( 'left', x+'px' );
		$('#productErrorReport').show();
		
		return false;
	});
	
	$('#productErrorReport p.submit a').click(function(){
		$('form[name="errorProductReportForm"]').submit();
		return false;
	});
	
	$('#productErrorReport .errorReportBoxHeader a').click(function(){
		$('#productErrorReport').hide();
		
		return false;
	});
	
	$("#productProblemType").change(function(){
		if($(this).val() == 5){ 
			$('#productErrorReport #additional-description').show(); 
		}else{
			$('#productErrorReport #additional-description').hide(); 
		}
	});
	
	/* COMMENTS ERROR REPORT */
	$("a[href^='/zgloszenie-problemu.html?commentProblemReport=']").click(function(){
		var id = $(this).attr('href').replace('/zgloszenie-problemu.html?commentProblemReport=', '');
		$('#commentIDProblem').val(id);
		
		var width = screen.width;
		var x = (width - 490) / 2;
		
		$('.errorReportBox').hide();
		
		$('#commentErrorReport').css( 'left', x+'px' );
		$('#commentErrorReport').show();
		
		return false;
	});
	
	$('#commentErrorReport p.submit a').click(function(){
		$('form[name="errorCommentReportForm"]').submit();
		return false;
	});
	
	$('#commentErrorReport .errorReportBoxHeader a').click(function(){
		$('#commentErrorReport').hide();
		
		return false;
	});
	
	$("#commentProblemType").change(function(){
		if($(this).val() == 3){ 
			$('#commentErrorReport #additional-description').show(); 
		}else{
			$('#commentErrorReport #additional-description').hide(); 
		}
	});
	
	/* VIDEO ERROR REPORT */
	$("a[href^='/zgloszenie-problemu.html?videoProblemReport=']").click(function(){
		var id = $(this).attr('href').replace('/zgloszenie-problemu.html?videoProblemReport=', '');
		$('#videoIDProblem').val(id);
		
		var width = screen.width;
		var x = (width - 490) / 2;
		
		$('.errorReportBox').hide();
		
		$('.video-box object').hide();
		$('.video-box').css('background', '#333333');
		
		$('#videoErrorReport').css( 'left', x+'px' );
		$('#videoErrorReport').show();
		
		return false;
	});
	
	$('#videoErrorReport p.submit a').click(function(){
		$('form[name="errorVideoReportForm"]').submit();
		return false;
	});
	
	$('#videoErrorReport .errorReportBoxHeader a').click(function(){
		$('#videoErrorReport').hide();
		$('.video-box').css('background', 'none');
		$('.video-box object').show();
		
		return false;
	});
	
	$("#videoProblemType").change(function(){
		if($(this).val() == 3){ 
			$('#videoErrorReport #additional-description').show(); 
		}else{
			$('#videoErrorReport #additional-description').hide(); 
		}
	});
	
	/* PROPOSE VIDEO */
	$("a[href^='/zgloszenie-informacji.html?videoPropose=']").click(function(){
		var id = $(this).attr('href').replace('/zgloszenie-informacji.html?videoPropose=', '');
		$('#proposeVideoProductID').val(id);
		
		var width = screen.width;
		var x = (width - 490) / 2;
		
		$('.errorReportBox').hide();
		
		$('.video-box object').hide();
		$('.video-box').css('background', '#333333');
		
		$('#proposeVideoReport').css( 'left', x+'px' );
		$('#proposeVideoReport').show();
		
		return false;
	});
	
	$('#proposeVideoReport p.submit a').click(function(){
		$('form[name="proposeVideoForm"]').submit();
		return false;
	});
	
	$('#proposeVideoReport .errorReportBoxHeader a').click(function(){
		$('#proposeVideoReport').hide();
		$('.video-box').css('background', 'none');
		$('.video-box object').show();
		
		return false;
	});
	
	/* PROPOSE VIDEO */
	$("a[href^='/zgloszenie-informacji.html?pricePropose=']").click(function(){
		var id = $(this).attr('href').replace('/zgloszenie-informacji.html?pricePropose=', '');
		$('#proposePriceProductID').val(id);
		
		var width = screen.width;
		var x = (width - 490) / 2;
		
		$('.errorReportBox').hide();
		
		$('#proposePriceReport').css( 'left', x+'px' );
		$('#proposePriceReport').show();
		
		return false;
	});
	
	$('#proposePriceReport p.submit a').click(function(){
		$('form[name="proposePriceForm"]').submit();
		return false;
	});
	
	$('#proposePriceReport .errorReportBoxHeader a').click(function(){
		$('#proposePriceReport').hide();
		
		return false;
	});
	
	$('a[href^="?addToList="]').click(function(){
		var productId = $(this).attr('href').replace('?addToList=', '');
		dodajDoSchowka(productId);
		
		return false;
	});
	
	$('.accordion .head').click(function() {
		$(this).next().toggle('slow');
		return false;
	}).next().hide();
	
	$("#accordion").accordion({ autoHeight: false  });

	// tips
	$("a.tip img").live('mouseover', function(e) {
		
		var x = e.pageX;// - this.offsetLeft;
		var y = e.pageY;// - this.offsetTop;
		
		var p = $(this);//.children();
		x = p.offset().left;
		y = p.offset().top;
		
		var tip_content = "";
		tip_content = $(this).attr("alt");
		if(tip_content.length <= 1) tip_content = "brak podpowiedzi";

		$('#tip div').html(tip_content);
		
		var xx = $(this).width();
		var yy = $(this).height();

		$('#tip').css("top", (y+(yy/2))-15);
		if(x>850){
			$('#tip').addClass('right');
			$('#tip').css("left", x-242);		
		}else{
			$('#tip').removeClass('right');
			$('#tip').css("left", x+xx+8);
		}
		
		$('#tip').show();
	});
	
	$("a.tip img").live('mouseout', function(e) {
		$('#tip').hide();
		$('#tip div').html('...');
	});
	
	$('a[href^="?d="]').click(function(){
		var tab = $(this).attr('href').replace('?d=','');
		
		$('.box.tabs').hide();
		$('#'+tab).show();
		$('ul.box-tabs li').removeClass('active');
		$('#'+tab+'_tab').addClass('active');
		
		return false;
	});
	
	$('a[href*="schowek.html#side"]').click(function(){
		if($('#side-box').hasClass('close-side')){
			$('#side-box').removeClass('close-side');
			
			$('.side-box-content.compare').hide();
			$('.side-box-content.fold').show();
			
		}else{
			
			if( $('.side-box-content.compare').is(':visible') ){
				$('.side-box-content.compare').hide();
				$('.side-box-content.fold').show();
			}else{
				$('.side-box-content.compare').hide();
				$('.side-box-content.fold').hide();
				$('#side-box').addClass('close-side');
			}
			
		}
		
		return false;
	});
	$('a[href*="porownanie.html#side"]').click(function(){
		if($('#side-box').hasClass('close-side')){
			$('#side-box').removeClass('close-side');
			
			$('.side-box-content.compare').show();
			$('.side-box-content.fold').hide();
			
		}else{
			
			if( $('.side-box-content.fold').is(':visible') ){
				$('.side-box-content.fold').hide();
				$('.side-box-content.compare').show();
			}else{
				$('.side-box-content.compare').hide();
				$('.side-box-content.fold').hide();
				$('#side-box').addClass('close-side');
			}
			
		}
		
		return false;
	});
	
	$('a[href^="?addProductToFold="]').click(function(){
		var product_id = $(this).attr('href').replace('?addProductToFold=','');
		
		if( addToLib(product_id,'fold') == 1 ){
			$.ajax({
				type: "GET",
				url: "/CORE/core.ajax.product.php",
				data: "askType=get-small-details&user_products=."+product_id+",",
				dataType: "xml",
				success: function(xml){
					if( $(xml).find('product_id').text() ){
						var product_name = $(xml).find('name').text();
						var product_picture = $(xml).find('image').text();
						var product_url = $(xml).find('product_url').text();
						var min_price = $(xml).find('min_price').text();
						var max_price = $(xml).find('max_price').text();
						var offers = $(xml).find('offers').text();
						var offers_wrd = $(xml).find('offers_wrd').text();
						
						var html = '<li id="fold_product_'+product_id+'"><ul><li class="picture"><img src="'+product_picture+'" alt="" /></li><li class="main"><a href="'+product_url+'">'+product_name+'</a><br /><b>'+min_price+' PLN - '+max_price+' PLN</b>, w <em>'+offers+'</em> '+offers_wrd+'</li><li class="option"><a href="?rmvFold='+product_id+'" class="tip"><img src="/IMAGES/icons/12.png" alt="usuń produkt" /></a></li></ul></li>';
						
						$('#side-box').removeClass('close-side');
						$('.side-box-content.compare').hide();
						$('.side-box-content.fold').show();
						
						$('#fold_products .empty').hide();
						$('#fold_products').append( html );
						
						$('#product_fold_link_'+product_id).html("Usuń ze schowka");
						$('#product_fold_img_'+product_id).attr('src',"/IMAGES/icons/12.png");
					}
				},
				error: function(){}
			});
		}else{
			$('#fold_product_'+product_id).remove();
			
			$('#product_fold_link_'+product_id).html("Dodaj do schowka");
			$('#product_fold_img_'+product_id).attr('src',"/IMAGES/icons/11.png");
			
			if( countProductsIn(getLibValue('fold')) == '0' ){
				$('#fold_products .empty').show();
			}
		}
		return false;
	});
	$('a[href^="?rmvFold="]').live('click', function() {
		var product_id = $(this).attr('href').replace('?rmvFold=','');
		if( addToLib(product_id,'fold') == 0 ){
			$('#fold_product_'+product_id).remove();
			if( countProductsIn(getLibValue('fold')) == '0' ){
				$('#fold_products .empty').show();
			}
			$('#product_fold_link_'+product_id).html("Dodaj do schowka");
			$('#product_fold_img_'+product_id).attr('src',"/IMAGES/icons/11.png");
		}
		return false;
	});
	

	if( getLibValue('fold') ){
		$.ajax({
			type: "GET",
			url: "/CORE/core.ajax.product.php",
			data: "askType=get-small-details&user_products="+getLibValue('fold'),
			dataType: "xml",
			success: function(xml){
				$('#fold_products .empty').hide();
				$(xml).find('product').each(function(){
					var product_id = $(this).find('product_id').text();
					var product_name = $(this).find('name').text();
					var product_picture = $(this).find('image').text();
					var product_url = $(this).find('product_url').text();
					var min_price = $(this).find('min_price').text();
					var max_price = $(this).find('max_price').text();
					var offers = $(this).find('offers').text();
					var offers_wrd = $(this).find('offers_wrd').text();
							
					var html = '<li id="fold_product_'+product_id+'"><ul><li class="picture"><img src="'+product_picture+'" alt="" /></li><li class="main"><a href="'+product_url+'">'+product_name+'</a><br /><b>'+min_price+' PLN - '+max_price+' PLN</b>, w <em>'+offers+'</em> '+offers_wrd+'</li><li class="option"><a href="?rmvFold='+product_id+'" class="tip"><img src="/IMAGES/icons/12.png" alt="usuń produkt" /></a></li></ul></li>';
					$('#fold_products').append( html );	
					
					$('#product_fold_link_'+product_id).html("Usuń ze schowka");
					$('#product_fold_img_'+product_id).attr('src',"/IMAGES/icons/12.png");
				});				
			},
			error: function(){}
		});
	}
	
	$('a[href^="?addProductToCompare="]').click(function(){
		var product_id = $(this).attr('href').replace('?addProductToCompare=','');
		
		if( addToLib(product_id,'compare') == 1 ){
			$.ajax({
				type: "GET",
				url: "/CORE/core.ajax.product.php",
				data: "askType=get-small-details&user_products=."+product_id+",",
				dataType: "xml",
				success: function(xml){
					if( $(xml).find('product_id').text() ){
						var product_name = $(xml).find('name').text();
						var product_picture = $(xml).find('image').text();
						var product_url = $(xml).find('product_url').text();
						var min_price = $(xml).find('min_price').text();
						var max_price = $(xml).find('max_price').text();
						var offers = $(xml).find('offers').text();
						var offers_wrd = $(xml).find('offers_wrd').text();
						
						var html = '<li id="compare_product_'+product_id+'"><ul><li class="picture"><img src="'+product_picture+'" alt="" /></li><li class="main"><a href="'+product_url+'">'+product_name+'</a><br /><b>'+min_price+' PLN - '+max_price+' PLN</b>, w <em>'+offers+'</em> '+offers_wrd+'</li><li class="option"><a href="?rmvCompare='+product_id+'" class="tip"><img src="/IMAGES/icons/12.png" alt="usuń produkt" /></a></li></ul></li>';
						
						$('#side-box').removeClass('close-side');
						$('.side-box-content.fold').hide();
						$('.side-box-content.compare').show();
						
						$('#compare_products .empty').hide();
						$('#compare_products').append( html );
						
						$('#product_compare_check_'+product_id).attr('checked','checked');
						$('#product_compare_link_'+product_id).html("usuń z porównania");
						$('#product_compare_img_'+product_id).attr('src',"/IMAGES/icons/12.png");
						
						if( countProductsIn(getLibValue('compare')) < '2' ){
							$('#compareit').hide();
							$('div.navigator-footer a.compareproducts').hide();
						}else{
							$('#compareit').show();
							$('div.navigator-footer a.compareproducts').show();
						}
					}
				},
				error: function(){}
			});
		}else{
			$('#compare_product_'+product_id).remove();
			$('.product_'+product_id).remove();
			
			$('#product_compare_check_'+product_id).attr('checked','');
			$('#product_compare_link_'+product_id).html("dodaj produkt do porównania");
			$('#product_compare_img_'+product_id).attr('src',"/IMAGES/icons/11.png");
			
			if( countProductsIn(getLibValue('compare')) == '0' ){
				$('#compare_products .empty').show();
			}
			if( countProductsIn(getLibValue('compare')) < '2' ){
				$('#compareit').hide();
				$('div.navigator-footer a.compareproducts').hide();
			}else{
				$('#compareit').show();
				$('div.navigator-footer a.compareproducts').show();
			}
		}
		return false;
	});
	
	$('input[name="product_compare"]').change(function(){
		var product_id = $(this).val();
		
		if( addToLib(product_id,'compare') == 1 ){
			$.ajax({
				type: "GET",
				url: "/CORE/core.ajax.product.php",
				data: "askType=get-small-details&user_products=."+product_id+",",
				dataType: "xml",
				success: function(xml){
					if( $(xml).find('product_id').text() ){
						var product_name = $(xml).find('name').text();
						var product_picture = $(xml).find('image').text();
						var product_url = $(xml).find('product_url').text();
						var min_price = $(xml).find('min_price').text();
						var max_price = $(xml).find('max_price').text();
						var offers = $(xml).find('offers').text();
						var offers_wrd = $(xml).find('offers_wrd').text();
						
						var html = '<li id="compare_product_'+product_id+'"><ul><li class="picture"><img src="'+product_picture+'" alt="" /></li><li class="main"><a href="'+product_url+'">'+product_name+'</a><br /><b>'+min_price+' PLN - '+max_price+' PLN</b>, w <em>'+offers+'</em> '+offers_wrd+'</li><li class="option"><a href="?rmvCompare='+product_id+'" class="tip"><img src="/IMAGES/icons/12.png" alt="usuń produkt" /></a></li></ul></li>';
						
						$('#side-box').removeClass('close-side');
						$('.side-box-content.fold').hide();
						$('.side-box-content.compare').show();
						
						$('#compare_products .empty').hide();
						$('#compare_products').append( html );
						
						$('#product_compare_link_'+product_id).html("usuń z porównania");
						$('#product_compare_img_'+product_id).attr('src',"/IMAGES/icons/12.png");
						
						if( countProductsIn(getLibValue('compare')) < '2' ){
							$('#compareit').hide();
							$('div.navigator-footer a.compareproducts').hide();
						}else{
							$('#compareit').show();
							$('div.navigator-footer a.compareproducts').show();
						}
						$('#product_compare_check_'+product_id).attr('checked','checked');
					}
				},
				error: function(){}
			});
			
				
		}else{
			$('#compare_product_'+product_id).remove();
			$('.product_'+product_id).remove();
			
			$('#product_compare_link_'+product_id).html("dodaj produkt do porównania");
			$('#product_compare_img_'+product_id).attr('src',"/IMAGES/icons/11.png");
			
			if( countProductsIn(getLibValue('compare')) == '0' ){
				$('#compare_products .empty').show();
			}
			if( countProductsIn(getLibValue('compare')) < '2' ){
				$('#compareit').hide();
				$('div.navigator-footer a.compareproducts').hide();
			}else{
				$('#compareit').show();
				$('div.navigator-footer a.compareproducts').show();
			}
			$('#product_compare_check_'+product_id).attr('checked','');

		}
		return false;
	});
	$('a[href^="?rmvCompare="]').live('click', function() {
		var product_id = $(this).attr('href').replace('?rmvCompare=','');
		if( addToLib(product_id,'compare') == 0 ){
			$('#compare_product_'+product_id).remove();
			$('.product_'+product_id).remove();
			
			if( countProductsIn(getLibValue('compare')) == '0' ){
				$('#compare_products .empty').show();
			}
			$('#product_compare_check_'+product_id).attr('checked','');
			$('#product_compare_link_'+product_id).html("dodaj produkt do porównania");
			$('#product_compare_img_'+product_id).attr('src',"/IMAGES/icons/11.png");
			
			if( countProductsIn(getLibValue('compare')) < '2' ){
				$('#compareit').hide();
				$('div.navigator-footer a.compareproducts').hide();
			}else{
				$('#compareit').show();
				$('div.navigator-footer a.compareproducts').show();
			}
		}
		return false;
	});
	
	if( getLibValue('compare') ){
		$.ajax({
			type: "GET",
			url: "/CORE/core.ajax.product.php",
			data: "askType=get-small-details&user_products="+getLibValue('compare'),
			dataType: "xml",
			success: function(xml){
				$('#compare_products .empty').hide();
				if( countProductsIn(getLibValue('compare')) < '2' ){
					$('#compareit').hide();
					$('div.navigator-footer a.compareproducts').hide();
				}else{
					$('#compareit').show();
					$('div.navigator-footer a.compareproducts').show();
				}
				
				$(xml).find('product').each(function(){
					var product_id = $(this).find('product_id').text();
					var product_name = $(this).find('name').text();
					var product_picture = $(this).find('image').text();
					var product_url = $(this).find('product_url').text();
					var min_price = $(this).find('min_price').text();
					var max_price = $(this).find('max_price').text();
					var offers = $(this).find('offers').text();
					var offers_wrd = $(this).find('offers_wrd').text();
							
					var html = '<li id="compare_product_'+product_id+'"><ul><li class="picture"><img src="'+product_picture+'" alt="" /></li><li class="main"><a href="'+product_url+'">'+product_name+'</a><br /><b>'+min_price+' PLN - '+max_price+' PLN</b>, w <em>'+offers+'</em> '+offers_wrd+'</li><li class="option"><a href="?rmvCompare='+product_id+'" class="tip"><img src="/IMAGES/icons/12.png" alt="usuń produkt" /></a></li></ul></li>';
					$('#compare_products').append( html );	
					
					$('#product_compare_check_'+product_id).attr('checked','checked');
					$('#product_compare_link_'+product_id).html("usuń z porównania");
					$('#product_compare_img_'+product_id).attr('src',"/IMAGES/icons/12.png");
				});				
			},
			error: function(){}
		});
	}
	
	$('a[href^="?dpb="]').click(function(){
		var dpb_index = $(this).attr('href').replace('?dpb=','');
		$('.presenter').hide();
		$('.presenter-navigator a').removeClass('active');
		
		$('#promotion_'+dpb_index).show();
		$('#promotion_'+dpb_index+'_link').addClass('active');
		return false;
	});
	
	$('.faq_accordion .head').click(function() {
		$(this).next().toggle('slow');
		return false;
	}).next().hide();
	
	$(".faq_accordion").accordion({ autoHeight: false, collapsible: true, active: false });
	
	$('.partners_accordion .head').click(function() {
		$(this).next().toggle('slow');
		return false;
	}).next().hide();
	
	$(".partners_accordion").accordion({ autoHeight: false, collapsible: true, active: 0  });
	
	/* search form */
	if( $('#searchInput').val() == '' ){
		$('#searchInput').val( $('#searchInput').attr('title') );
	}
	$('#searchInput').focus(function(){
		if($('#searchInput').val() == $('#searchInput').attr('title')){
			$('#searchInput').val('');	
		}
	});
	$('#searchInput').blur(function(){
		if($('#searchInput').val() == ''){
			$('#searchInput').val( $('#searchInput').attr('title') );
		}
	});
	$('#searchForm').submit(function(){
		if($('#searchInput').val() == '' || $('#searchInput').val() == $('#searchInput').attr('title')){
			$('#searchInput').val('');
			$('#searchInput').focus();
			return false;
		}else{
			$('#searchForm').submit();
			return true;
		}
		return false;
	});
	/* search form : end */
});



