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);
	}	
}

function init(){ 
	pokazSchowek(); 
}

function pokazSchowek(){
	count	=	countProductsIn(readCookie('myStuff'));
	if(count)
		$('#myStuffCount').html('('+count+')');
	else
		$('#myStuffCount').html('');
}

function displayProductComments(){
	$('#shops').hide();
	$('#comments').show();
	$('#maps').hide();
		
	$('#commentTab').addClass( 'selected' );
	$('#offerTab').removeClass( 'selected' );
	$('#mapTab').removeClass( 'selected' );
}

function displayProductOffers(){
	$('#comments').hide();
	$('#shops').show();
	$('#maps').hide();
		
	$('#offerTab').addClass( 'selected' );
	$('#commentTab').removeClass( 'selected' );
	$('#mapTab').removeClass( 'selected' );
}	

function displayProductMap(){
	$('#comments').hide();
	$('#shops').hide();
	$('#maps').show();
		
	$('#offerTab').removeClass( 'selected' );
	$('#commentTab').removeClass( 'selected' );
	$('#mapTab').addClass( 'selected' );
}

$(document).ready(function(){
	
	$("a[href^='?addToFold=']").click(function(){
		var id = $(this).attr('href').replace('?addToFold=', '');
		dodajDoSchowka(id);
		pokazSchowek();
		return false;
	});
	
	$("a[href^='?deleteFold=']").click(function(){
		var id = $(this).attr('href').replace('?deleteFold=', '');
		dodajDoSchowka(id);
		pokazSchowek();
		var sURL = unescape(window.location.pathname);
    	window.location.href = sURL;
		return false;
	});
	
	
	
	$("a[href^='?Offers']").click(function(){
		displayProductOffers();
		return false;
	});
	
	$("a[href^='?Comments']").click(function(){		
		displayProductComments();
		return false;
	});
	/*
	$("a[href^='?Map']").click(function(){		
		//displayProductMap();
		//return false;
	});
	*/
	
	$("a[href^='?rateProduct']").click(function(){
		var name = $('#main-content .rate h3').html();
		if(name == "Twoja ocena"){ 
			$("form[name='sendRate']").submit();
			$('#main-content .rate h3').html("Ocena");
		}else{
			$('#main-content .rate h3').html("Twoja ocena");
		}
		$('#main-content div .rate img').toggle();
		$('#main-content div .rate select').toggle();

		$(this).html("wy¶lij ocenê™");
		
		return false;
	});
	
	$("a[href^='?rateStore']").click(function(){
		var name = $('#main-content .rate h3').html();
		if(name == "Twoja ocena"){ 
			$("form[name='sendRate']").submit();
			$('#main-content .rate h3').html("Ocena");
		}else{
			$('#main-content .rate h3').html("Twoja ocena");
		}
		$('#main-content div .rate img').toggle();
		$('#main-content div .rate select').toggle();

		$(this).html("wy¶lij ocenê™");
		
		return false;
	});
	
	$("a[href^='?displayFilter=']").click(function(){
		var filter_id = $(this).attr('href').replace('?displayFilter=', '');
		if($('#filter_'+filter_id+'_options').is(':visible')){
			$('#filter_'+filter_id).removeClass('selected');
		}else{
			$('#filter_'+filter_id).addClass('selected');
		}		
		$('#filter_'+filter_id+'_options').toggle();
		return false;
	});
		
	init();

});
