String.prototype.polskie_znaki = function(){		
	var string = this;
	var re;
	var polskie = new Array(",","ł","Ł","ó","Ó","ń","Ń","ę","Ę","ć","Ć","ś","Ś","ź","Ź","ż","Ż","Ą","ą");
	var podmiana = new Array(" ","l","L","o","O","n","N","e","E","c","C","s","S","z","Z","z","Z","A","a");	
	for(var i=0;i<polskie.length;i++){
		re = new RegExp(polskie[i], "g"); 
		string = string.replace(re,podmiana[i]);
	}		
	return string;
}

function trim(str) { 
	a = str.replace(/^\s+/, ''); 
	return a.replace(/\s+$/, '');
}	
	
function get_home_url(){
	return $("base").attr("href");
}

function get_url_to_ajax(){
	return get_home_url()+"ajax/";
}

function przejdz(adres, id){
	
	$.post(get_url_to_ajax() + "naliczanie.html", {
		id: id,
		adres: adres,
		nalicz: "ok"
	}, function(data){
		
		location.href = adres;
		
	});			
}

function wyszukaj(){
	
	var url = "";
	var nazwa_szukaj = $("#nazwa_szukaj").val();
	var kategoria_szukaj = $("#kategoria_szukaj").val();
	
	if(nazwa_szukaj!="szukaj w serwisie..."){
		url += "nazwa="+nazwa_szukaj;
	}	
	if(trim(kategoria_szukaj)!=""){
		if(url!=""){
			url += ";grupa="+kategoria_szukaj;
		}else{
			url += "grupa="+kategoria_szukaj;
		}
		
	}
	
	if(url!=""){
		url = get_home_url()+"lista/"+url+".html";
	}else{
		url = get_home_url()+"lista/all.html";
	}
	
	location.href=url;//.polskie_znaki();	
	
}

function reklama_klik(adres, id){
	
	$.post(get_url_to_ajax() + "reklama.html", {
		id: id,
		adres: adres,
		reklama: "ok"
	}, function(data){
		
		location.href = adres;
		
	});	
	
}


   function okazje_select(numer) {

		$("#okazje_bg").animate({
			left: "-"+(730*(numer-1))+"px"
		},500);
		
		$(".selector_button").children("a").each(function() {
			$(this).removeClass("selector_on");
		});
		$("#sb_"+numer).children("a").addClass("selector_on");
		
   };
   
   function promo_okazje_select(numer) {
		
		//alert(numer);
		$("#promo_okazje").animate({
			top: "-"+(190*numer)+"px"
		},500);
		
		$(".promo_sel_button").children("a").each(function() {
			$(this).removeClass("promo_sel_on");
		});
		$("#ps_"+numer).children("a").addClass("promo_sel_on");
		
   };

   function hide_kategorie(){	
   	
   	$("#box").fadeOut(250,function(){
   		$("#box").empty();
   		$("#box").remove();	
   		$(".menu_left_links a").animate({opacity: 1},250);
   		$(".boxFiltra .head").removeClass("active");
   		//$(".boxFiltra .more").css({display: "block"});	
   	});				
   	
   }

   function ajax_kategorie(box, mode, url, url_main){
		
		//hide_kategorie();

	   	$.post(get_url_to_ajax()+"kategorie.html",
	   	{ mode: mode,
	   	  url: url,
	   	  url_main: url_main },
	   	function(data){	
	  		
	   		$(box).parent().find('.more img').stop(true, false).animate({marginLeft: -16}, 100);  
	   		$("#box").empty();
	   		$("#box").remove();	
	   		$(".menu_left_links a").css({opacity: 1});
	   		$(".boxFiltra .head").removeClass("active");
	   		  
	  		$(box).parent().find(".head").addClass("active");
	  		$(box).parent().find(".menu_left_links a").animate({opacity: 0}, 250);
	  		//$(box).parent().find(".more").css({display: "none"});				
	   		
	   		$(box).parent().append('<div id="box" style="display: none; z-index: 100; position: absolute; top: 0; left: 218px; width: 741px;"></div>');
					
	   		$("#box").html(data);
	   		$("#box").fadeIn(250,function(){
	   			if ( !$.browser.msie ) $("#box").corner("4px right bottom");
	   		});	
	   				
	   	});	
	   	
   }

   function resetujLogowanie(){
	   $(".form_loguj").resetForm();
   }

   function zaloguj(){
	   $(".form_loguj").submit();
   }

   function zapisz_dane(){
   	
   	if($(".ja_ajax").attr("checked")){
   		var owner = "ja";
   		var osoba = "";
   	}else if($(".znajomy_ajax").attr("checked")){
   		var owner = "znajomy";
   		var osoba = $(".select_lista").val();
   	}
   	
   	var id = $("#dodaj_box input.id_produktu").val();
   			
   	$.post(get_url_to_ajax()+"lista/dodaj.html",
   	{ 
   		id: id,
   		osoba: osoba,
   		owner: owner	
   	},
   	function(data){	
   					
   		if(trim(data)=="ok"){
   			$(".box_content").fadeOut(200,function(){				
   				$(".box_content").html('<div style="text-align: center; margin-top: 30px; margin-bottom: 20px; color: #f8da0f; font-weight: bold;">Prezent został dopisany do listy</div>');
   				$(".box_content").fadeIn(200,function(){
   					setTimeout(function(){ hide_box(); },1250);
   				});
   			});						
   		}else{
   			$(".error_box").html(data);
   			$(".error_box").fadeIn(200);			
   		}
   				
   	});		
   	
   }
      
   function save_list(){
   		
   	if($(".ja_ajax").attr("checked")==true || $(".znajomy_ajax").attr("checked")==true){
   		if($(".znajomy_ajax").attr("checked")==true){
   			if($(".select_lista").val()==""){
   				$(".error_box").html("wybierz osobę z rozwijanego menu");
   				$(".error_box").fadeIn(200);				
   			}else{
   				zapisz_dane();	
   			}
   		}else{
   			zapisz_dane();	
   		}		
   	}else{
   		$(".error_box").html("wybierz jedną z opcji po lewej");
   		$(".error_box").fadeIn(200);
   	}
   		
   }
      
   function znajomy_ajax(){
   		
   	$(".error_box").fadeOut(200);
   	$(".select_lista").fadeIn(200);
   		
   }
      
   function ja_ajax(){
   		
   	$(".error_box").fadeOut(200);
   	$(".select_lista").fadeOut(200);
   		
   }

   function ajax_get_user(){

   	$.post(get_url_to_ajax()+"users.html",
   	{ get_user: "ok" },
   	function(data){				
   					
   		$(".ladowanie").fadeOut(200,function(){
   			$(".box_content").css("display","none");
   			$(".box_content").html(data);
   			$(".box_content").fadeIn(200);			
   		});	
   				
   	});	
   	
   }

   function hide_box(){
   	
   	$(".box_content").fadeOut(200,function(){
   	
   		$("#dodaj_box").fadeOut(200,function(){
   			$("#dodaj_box").empty();
   			$("#dodaj_box").remove();		
   		});			
   		
   	});			
   	
   }
   
$(document).ready(function(){
	
	$(".dodaj_button").click(function(e){
		
		var mouseX = e.pageX;
		var mouseY = e.pageY;
		var top = parseInt(mouseY) - 30;
		var left = parseInt(mouseX) + 30;	
		
		if($(window).width()<left+300){
			left = $(window).width() - 320;
		}
		
		var id = $(this).next(".id_prod").val();
				
		$("#dodaj_box").empty();
		$("#dodaj_box").remove();
		
		$("body").append('<div id="dodaj_box" style="display: none; z-index: 100; position: absolute; top: '+top+'px; left: '+left+'px;"><input type="hidden" value="'+id+'" class="id_produktu" /><div class="frame_main"></div></div>');
		$(".frame_main").html('<div class="butt_wybierz"><img style="cursor: pointer;" onclick="hide_box();" src="dom/gfx/butt_close.gif" title="" alt=""/></a></div><div class="box_content"></div>');
		
		$(".box_content").html('<div class="ladowanie" style="text-align: center; margin-top: 30px; margin-bottom: 20px;">ładowanie.....</div>');
		
		$("#dodaj_box").fadeIn(200,function(){
			
			ajax_get_user();
			
		});		
		
	});		
	
	for (i=0; i<document.links.length;) {
		document.links[i].onfocus=document.links[i++].blur;
	}
	
	$(".bony").tooltip({ 
       showURL: false 
	});

   $(".boxFiltra .more, .boxFiltra .head").not(".inactive").click(function(e){
		
		var mode = $(this).parent().children(".name_hidden_input").val();	
		var url = $("#url_"+mode).val();
		var url_main = $("#url_main").val();	
				
		ajax_kategorie(this, mode, url, url_main);
		
   });
   
	/* szczegolne okazje */
	$(".selector_button").click(function(){
		var numer = $(this).children("a").text();
		okazje_select(numer);
	});
	setInterval(function() {
		var akt_okazja = parseInt($("#okazje_bg").attr("class"));
		//alert("akt_okazja");
		var ilosc_okazji = parseInt($("#ilosc_okazji").val());
		if(akt_okazja == ilosc_okazji) {
			akt_okazja = 1;
			$("#okazje_bg").attr("class",akt_okazja);
			okazje_select(akt_okazja);
		} else {
			akt_okazja += 1;
			$("#okazje_bg").attr("class",akt_okazja);
			okazje_select(akt_okazja);
		}
	},5000);
	
	/* promowane okazje */
	$(".promo_sel_button").click(function(){
		var numer = $(this).children("input").val();
		promo_okazje_select(numer);
	});
	setInterval(function() {
		var akt_pokazja = parseInt($("#promo_okazje").attr("class"));
		//alert("akt_pokazja");
		var ilosc_pokazji = parseInt($("#ilosc_promo").val());
		if(akt_pokazja == ilosc_pokazji) {
			akt_pokazja = 1;
			$("#promo_okazje").attr("class",akt_pokazja);
			promo_okazje_select(akt_pokazja);
		} else {
			akt_pokazja += 1;
			$("#promo_okazje").attr("class",akt_pokazja);
			promo_okazje_select(akt_pokazja);
		}
	},5000);
    
	$("#nazwa_szukaj").keypress(function (e) {
      if (e.which == 13) {
		wyszukaj();
      }
    });

	if ( !$.browser.msie ) {
		$(".c4").corner("4px");
		$(".c3").corner("3px");
		$(".c2").corner("2px");
		$(".c1").corner("1px");
		$(".c4b").corner("4px bl br");
		$(".c4r").corner("4px tr br");
		$(".boxFiltra .head").corner("4px bl tl");
		$(".c3bl").corner("3px bl");
		$(".selector.home_select").corner("3px tl br");
	}

	//$("#menu_left .boxFiltra").hover(
	$("#menu_left .head, #menu_left .menu_left_links, #menu_left .more").hover(
		function() {
			if(!($(this).hasClass("head") && $(this).hasClass("active")) && !($(this).hasClass("menu_left_links") && $(this).siblings("#box").css("display")=="block")) $(this).parent().find('.more img').stop(true, false).animate({marginLeft: 0}, 100);
		}, function() {
			$(this).parent().find('.more img').stop(true, false).animate({marginLeft: -16}, 100);
		});
	
	$(".noLogin").hover(
		function() {
			//$(this).parent().find('.logowanie').slideDown(200);
			$(this).parent().find('.logowanie').stop(true, false).animate({height: 192}, 100);
		}, function() {
			$(this).parent().find('.logowanie').stop(true, false).animate({height: 0}, 100);
		});
		
	$('.logowanie input').bind('keypress', function(e) {
        if(e.keyCode==13){
        	zaloguj();
        }
	});
	
	/* produkty polecane na stronie głównej */
	var first = 0;
	var speed = 750;
	var pause = 3500;
	var ilosc = $('#polecane .box').length;
		function removeFirst(){
			first = $('#polecane .box:first').clone();
			$('#polecane .box:first').animate({marginTop: -115, opacity: 0}, speed, function() {$(this).remove();})
			addLast(first);
		}
		function addLast(first){
			last = first;
			$('#polecane .boxy').append(last)
			$('#polecane .box:last')
			.animate({opacity: 1}, speed)
			.fadeIn('slow')
		}
	interval = setInterval(removeFirst, pause);

	var first2 = 0;
	var speed2 = 750;
	var pause2 = 5000;
	var ilosc2 = $('#nowosci .suwadlo a').length;
		function removeFirst2(){
			first2 = $('#nowosci .suwadlo a:first').clone();
			$('#nowosci .suwadlo a:first').animate({marginLeft: -117, opacity: 0}, speed2, function() {$(this).remove();})
			addLast2(first2);
		}
		function addLast2(first2){
			last2 = first2;
			$(last2).css({opacity: 0});
			$('#nowosci .suwadlo div').append(last2)
			$('#nowosci .suwadlo a:last').animate({opacity: 1}, speed2).fadeIn(speed2)
		}
	interval = setInterval(removeFirst2, pause2);


	$(".wyszukaj_switch").click(function(){
		$(".wyszukajka").slideToggle(200);
	});
	
});

function wybierz_kategorie(url_kategorie, mode){
		
	//alert(url_kategorie + " home: "+get_home_url());
	var akt_link = (get_home_url()+"lista/"!=url_kategorie) ? get_home_url()+url_kategorie : url_kategorie;
	var kategorie = ""	
		
	$(".content input").each(function(){		
	
		if($(this).attr("checked")){
			kategorie += mode + "=" + $(this).val() + ";";
		}		
		
	});
	
	if(trim(kategorie)==""){				
		if(url_kategorie.charAt(url_kategorie.length-1)==";"){
			url_kategorie = url_kategorie.substring(0, url_kategorie.length-1);
			kategorie = ".html"; 	
		}else{
			kategorie = "all.html";
		}		
	}else{
		kategorie = kategorie.substring(0, kategorie.length-1) + ".html";
	}	
	location.href = akt_link+kategorie;
	
}
$(function() {
	$('.powiekszenie a').lightBox({fixedNavigation:true});
});


