$(document).ready( function() {

  //autoprodukt();
  inicjacja("wsz");
  pole_znaki();
  
  $("#gazetkanowa td.akcja .guzik_zmien").click( function() {
    if ($(this).attr("id")=="guzik_zmien_nazwa")
    {
      $(this).parent().parent().find("input[type=text]").each( function() {
        $(this).attr("disabled", false);
      });
    } else if ($(this).attr("id")=="guzik_zmien_data")
    {
      $(this).parent().parent().find("input[type=text]").each( function() {
        $(this).attr("disabled", false);
      });
      $(this).parent().parent().find("a").each( function() {
        $(this).show();
      });
    } else if ($(this).attr("id")=="guzik_zmien_sklepy")
    {
      $(this).parent().parent().find("#drzewko_sklepow").show("slow");
    } else if ($(this).attr("id")=="guzik_zapisz")
    {
      $(this).parent().parent().parent().find("input[type=text]").each( function() {
        $(this).attr("disabled", false);
      });
    }
    
  });
  
  $("#wybrane_sklepy .wybrany_sklep").each( function() {
    bind_wybr_sklep($(this).attr("id"));
  });
  
  potwierdzenie_bind();
  
  $(".listagazetek a").click( function () {
    if ($(this).attr("name")=="usuń")
    {
      if (confirm ("Napewno chcesz usunąć te gazetkę?")) return true;
      else return false;
    }
  });
});


function potwierdzenie_bind()
{
  $("form[name=form_potwierdzenia] input").each ( function() {
    $(this).click( function() {
      potwierdzenie($(this).attr("id"));
    });
  });
}

function pole_znaki()
{
  var ile_znakow = 45;
  $("input[name=nazwa]").keyup( function() {
    if (ile_znakow-$('input[name=nazwa]').val().length<0) {
      $('input[name=nazwa]').attr("value", $('input[name=nazwa]').val().substr(0, ile_znakow));
    }
    $("#op_gaz").text("Jeszcze "+(ile_znakow-$('input[name=nazwa]').val().length)+" znaków");
  });
  
}

function inicjacja(inic)
{
  // inicjacja drzewka firm-sklepow
  $('#firma').fileTree({
      root: 'firma',
      script: './js/jqueryFileTree_fGn.php',
      argD: inic,
      expandSpeed: 750, //chowanie
      collapseSpeed: 750, //rozwijanie
      multiFolder: false
    }, function(filet, filei, stan)
    {
      if (filet=="sklep")
      {
        $.post('./js/prywatne/wybor_sklepu.php', { id_sklep: filei }, function(data) {
          $("#wybrane_sklepy #tama").remove();
          $("#wybrane_sklepy").html(data+$("#wybrane_sklepy").html());
          $("#wybrane_sklepy #tama").remove();
          $("#wybrane_sklepy").append('<div id="tama"></div>');
          bind_wybr_sklep("skl_"+filei);
        });
      }
    });
}

function bind_wybr_sklep(id_sklepu)
{
  $("#"+id_sklepu+" .krzyzyk").css({cursor: "pointer"});
  $("#"+id_sklepu+" .krzyzyk").click( function() {
    $("#"+id_sklepu).remove();
  });
}

function potwierdzenie(id)
{
  //alert('potwier: '+id);
  var id2=document.getElementById(id);
  if (id2.checked==true)
  {
    if (id=="ckek3")
    {
      if (confirm ("Napewno chcesz, aby ta gazetka ukazała się już w serwisie?"))
      {
        $.post('./js/prywatne/zatwierdzanie_gazetek.php', { zatwierdz: 1, ust:id }, function (data) {
          alert(data);
          location.href=location.href;
        });
      } else id2.checked=false;
    } else if (id=="ckek1")
    {
      $.post('./js/prywatne/zatwierdzanie_gazetek.php', { zatwierdz: 1, ust:id }, function (data) {
          $("form[name=form_potwierdzenia]").html(data);
          potwierdzenie_bind();
      });
    }
  } else {
    if (id=="ckek1")
    {
      $.post('./js/prywatne/zatwierdzanie_gazetek.php', { zatwierdz:1, ust:id }, function (data) {
        $("form[name=form_potwierdzenia]").html(data);
        potwierdzenie_bind();
      });
    }
  }
  // startGETRequest(url, onComplete_potwier, onEnd_potwier);
}

/*

function formatItem(row) {		return row[0] + " (<strong>id: " + row[1] + "</strong>)";	}
function formatResult(row) {return row[0].replace(/(<.+?>)/gi, '');	}
function formatItem1(row) {return row[0];}
function formatResult1(row) {return row[0].replace(/(<.+?>)/gi, '');	}

function autoprodukt() {
 $("#sauto1").autocomplete('function/gazetkanowa_uzupelnij.php', {
    minChars:1,
		width: 200,
		multiple: false,
		matchContains: true,
		formatItem: formatItem1,
		formatResult: formatResult1,
		extraParams: {n:"1"}
	});

	 $("#sauto2").autocomplete('function/gazetkanowa_uzupelnij.php', {
	  minChars:1,
	  max:20,
		width: 200,
		multiple: false,
		matchContains: true,
		formatItem: formatItem1,
		formatResult: formatResult1,
		extraParams: {n:"2", firma:  function() { return $("#sauto3").val();} }
	});

$("#sauto3").autocomplete('function/gazetkanowa_uzupelnij.php', {
	  minChars:0,
	  max:20,
		width: 200,
		multiple: false,
		matchContains: true,
		formatItem: formatItem1,
		formatResult: formatResult1,
		extraParams: {n:"3", siec: function() { return $("#sauto2").val();}}
	});
$("#sauto4").autocomplete('function/gazetkanowa_uzupelnij.php', {
	  minChars:1,
		width: 200,
		multiple: false,
		matchContains: true,
		formatItem: formatItem1,
		formatResult: formatResult1,
		extraParams: {n:"4", siec: function() { return $("#sauto2").val();}}
	});
	$("#sauto5").autocomplete('function/gazetkanowa_uzupelnij.php', {
	  matchContains: true,
		minChars: 0,
		max:5,
		formatItem: formatItem1,
		formatResult: formatResult1,
		extraParams: {n:"5", adres: function() { return $("#sauto4").val();}}
	});
	$("#sauto6").autocomplete('function/gazetkanowa_uzupelnij.php', {
	  minChars:0,
		autoFill: true,
    max:5,
    width: 200,
		multiple: false,
		matchContains: true,
		formatItem: formatItem1,
		formatResult: formatResult1,
		extraParams: {n:"6", adres: function() { return $("#auto4").val();}}
	});
	
}*/
