/*
 * funkcje ogolne dla b2b
 * hint: do developerki uzywajcie tego pliku, natomiast przy przerzucaniu na produkcje
 * pakujcie js packerem: http://dean.edwards.name/packer/
 * i linkujcie do wersji spakowanej. oszczednosc miejsca - zauwazalna
 * c/d nazewnictwa plikow proponuje name.js -> name.pack.js
 * 
 */
function expand_produkty(ob, event) {
    var tableId = "produkty_" + $(ob).attr("id");

    if ($(ob).parent().children(".bullet").hasClass("bul_minus")) {
        $("#" + tableId + " tr.ukryte").hide();
        $(ob).parent().children(".bullet").removeClass("bul_minus").addClass("bul_plus");
        $(ob).text("rozwiń wszystkie");
        $(ob).parent().children(".ile_produktow").css("visibility", "visible");

    } else {
        $("#" + tableId + " tr.ukryte").show();
        $(ob).parent().children(".bullet").removeClass("bul_plus").addClass("bul_minus");
        $(ob).text("zwiń listę");
        $(ob).parent().children(".ile_produktow").css("visibility", "hidden");
    }
    event.stopPropagation();
    event.preventDefault();

}
// wczytuje z ciacha i ustawia aktualny stan zakladek 
function retrieveAndSetFolds() {
	// dla kazdego menu
	$('div#str_pasek_lewy ul.submenu').each(function(){
		// pobierz id ul z lista
		var sCritListId = $(this).attr('id');
		// zbuduj id paragrafu z opisem listy 
		var sCritParId = 'nadkid_' + sCritListId.substring(String('menu_nadkid_').length);
		// jezeli istnieje w ciachu klucz odpowiadajacy danemu kryterium to go pobierz
		if ($.trim($.cookie(sCritListId)) == '1') {
			$('ul#'+sCritListId+' li').show(); // pokaz elementy listy
			$('p#'+sCritParId).attr("title", "rozwiń listę");
			$('p#'+sCritParId+' span').addClass("bul_minus");
			$('p#'+sCritParId+' span').removeClass("bul_plus");
			$.cookie(sCritListId, '1', { expires: 2, path: '/', secure: false });
		}
	});
}
/*
var sCritListId = $(this).attr('id');
$.cookie(sCritListId, '1');
*/
$(document).ready(function() {
  $("a.lightbox").lightBox();
  $(".ukryte").hide();

  $("#mapaBranz").hover(
    function(){
      $("#mapaBranzList").show("fast").css("position", "absolute");
    },
    function(){

    }
  );

  $("#mapaBranzList").mouseleave(function(){$(this).hide();});
  $("#mapaBranzList").mouseenter(function(){$(this).show("fast").css("position", "absolute");});


  $("#wyszukiwarkaLewa .notclicked").click(
    function() {
      if ($(this).hasClass("notclicked")) {
        $(this).val("");
        $(this).removeClass("notclicked");
      }
    }
  );

  $(".expand").toggle(
      function () {
        menuId = "menu_" + $(this).attr("id").substring(7);
        $("ul#" + menuId + " li.ukryte").show();
        $(this).text("...mniej");
      },
      function () {
        menuId = "menu_" + $(this).attr("id").substring(7);
        $("ul#" + menuId + " li.ukryte").hide();
        $(this).text("więcej...");
      }
  );

  $(".kryterium_img img").click(
      function () {
        $(this).toggleClass("active");
      }
  );

  $(".menuTitle").click(
      function() {
          itemId = $(this).attr("id");
          menuId = "menu_" + $(this).attr("id");
          if ($(this).children("span").hasClass("bul_minus")) {
              //$("ul#" + menuId + " li").hide();
        	  $("ul#" + menuId).children("li").hide();
              $(this).children("span").removeClass("bul_minus");
              $(this).children("span").addClass("bul_plus");
              $(this).attr("title", "rozwiń listę");
              $.cookie(menuId, '0', { expires: 2, path: '/', secure: false });
          } else {
              //$("ul#" + menuId + " li").show();
        	  $("ul#" + menuId).children("li").show();
              $("#expand_" + itemId).click();
              $(this).children("span").removeClass("bul_plus");
              $(this).children("span").addClass("bul_minus");
              $(this).attr("title", "zwiń listę");
              $.cookie(menuId, '1', { expires: 2, path: '/', secure: false });
          }
      }
  );

  $(".expand_produkty").bind("click", function(event){
    expand_produkty(this, event);
  });

  $(".dodaj_do_schowka").click(
      function(event) {
        var el = this;
        //$.post("/ajax/koszyk", { id: $(this).attr("id"), branza_id: branza_id },
        $.post("/ajax/koszyk", { id: $(this).find('input[type="hidden"]').val(), branza_id: branza_id },
          function(data){
            $("#ajaxplaceholder_koszyk").html(data);
            $(el).html("firma w schowku");
            $(el).addClass("ajax_updated");
          });

        event.stopPropagation();
        event.preventDefault();
      }
  );

   $(".zobacz_kontakt").click(
      function(event) {
        statKontaktStart();

        /*
        // hu de fak uzywa wlasnych atrybutow?!? potem walidacja sie wali!!!
        $.post("/ajax/telefon", { id: $(this).attr("id"), bid: $(this).attr("bid") },
          function(data){
            $("#ajaxplaceholder_telefon").html("<strong>" + data + "</strong>");
          });
        event.stopPropagation();
        event.preventDefault();
        */
      }
  );

  $(".podmiana").live('click',function(){
	var stary=$(this).attr("href");
	var curlatlng=map.getCenter();
	var curZoom=map.getZoom();
	var bounds = map.getBounds();
	$(this).attr("href",stary + curlatlng.lat()+','+curlatlng.lng()+','+curZoom+','+bounds.getSouthWest().lat()+','+bounds.getSouthWest().lng()+','+bounds.getNorthEast().lat()+','+bounds.getNorthEast().lng()+'/');
  });
  
  // ustaw slidery z filtrami
  retrieveAndSetFolds();
});
