$(document).ready( function () {
  
  $.post('./js/publiczne/spr_javascript.php', {}, function(data) {});
  odczytajgazetke();
  
  kliki_ed();
  panel_art();
  
  pokaz_ladowanie();
});

function kliki_ed()
{
  ustawienia_kategori();
  $("input[name='grupa']").change(function() {
     ustawienia_kategori();
  });
  
  $(".guziczek").click( function() {
    if ($(this).attr("id")=="rozwin_helpa") {
      $(this).parent().find(".opiski").toggle("slow");
    } else if ($(this).attr("id")=="zwin_ed") {
      $(this).parent().find("form").toggle("slow");
    }
  });
  
  $("h3").click( function() {
    //$(this).parent().find("form").hide("slow");
    $.post("./js/prywatne/nowe_info.php", {}, function(data) {
      $("div#dok").html(data);
      kliki_ed();
      //$("#dok form").show("slow");
    });
  });
  
  uruchom_edytor();
}

function ustawienia_kategori()
{
  //alert($("input[name='grupa']:checked").val());
  if ($("input[name='grupa']:checked").val())
  {
    $(".podspod").show();
    $(".opis_r .opiski").hide();
    
    if ($("input[name='grupa']:checked").attr("ref") == '9') {
      //Kredyt
      $("#podtytul").hide();
      $("#podtytul input").attr("disabled", "disabled");
      $("#data_koncowa").show();
      $("#data_koncowa input").attr("disabled", false);
      $(".opiski_kredyt").show();
    } else if ($("input[name='grupa']:checked").attr("ref") == '1') {
      //Rabat
      $("#podtytul").hide();
      $("#podtytul input").attr("disabled", "disabled");
      $("#data_koncowa").show();
      $("#data_koncowa input").attr("disabled", false);
      $(".opiski_rabat").show();
    } else if ($("input[name='grupa']:checked").attr("ref") == '2') {
      //HIT!
      $("#podtytul").hide();
      $("#podtytul input").attr("disabled", "disabled");
      $("#data_koncowa").show();
      $("#data_koncowa input").attr("disabled", false);
      $(".opiski_hit").show();
    } else if ($("input[name='grupa']:checked").attr("ref") == '3') {
      //Konkurs
      $("#podtytul").hide();
      $("#podtytul input").attr("disabled", "disabled");
      $("#data_koncowa").show();
      $("#data_koncowa input").attr("disabled", false);
      $(".opiski_konkurs").show();
    } else if ($("input[name='grupa']:checked").attr("ref") == '4') {
      //Nowość
      $("#data_koncowa").hide();
      $("#data_koncowa input").attr("disabled", "disabled");
      $("#podtytul").show();
      $("#podtytul input").attr("disabled", false);
      $(".opiski_nowosc").show();
    } else if ($("input[name='grupa']:checked").attr("ref") == '5') {
      //Ciekawostka
      $("#data_koncowa").hide();
      $("#data_koncowa input").attr("disabled", "disabled");
      $("#podtytul").show();
      $("#podtytul input").attr("disabled", false);
      $(".opiski_ciekawostka").show();
    } else if ($("input[name='grupa']:checked").attr("ref") == '6') {
      //Wiadomosc
      $("#data_koncowa").hide();
      $("#data_koncowa input").attr("disabled", "disabled");
      $("#podtytul").show();
      $("#podtytul input").attr("disabled", false);
      $(".opiski_wiadomosc").show();
    } else if ($("input[name='grupa']:checked").attr("ref") == '7') {
      //Osustwo
      $("#data_koncowa").hide();
      $("#data_koncowa input").attr("disabled", "disabled");
      $("#podtytul").show();
      $("#podtytul input").attr("disabled", false);
      $(".opiski_oszustwo").show();
    } else if ($("input[name='grupa']:checked").attr("ref") == '8') {
      //Bubel
      $("#data_koncowa").hide();
      $("#data_koncowa input").attr("disabled", "disabled");
      $("#podtytul").show();
      $("#podtytul input").attr("disabled", false);
      $(".opiski_bubel").show();
    } else if ($("input[name='grupa']:checked").attr("ref") == '10') {
      //Promocja
      $("#podtytul").hide();
      $("#podtytul input").attr("disabled", "disabled");
      $("#data_koncowa").show();
      $("#data_koncowa input").attr("disabled", false);
      $(".opiski_promocja").show();
    }
  }
}

function uruchom_edytor()
{
  // Add markItUp! to your textarea in one line
	// $('textarea').markItUp( { Settings }, { OptionalExtraSettings } );
	$('#markItUp').markItUp(mySettings);
	
	// You can add content from anywhere in your page
	// $.markItUp( { Settings } );	
	$('.add').click(function() {
 		$.markItUp( { 	openWith:'<opening tag>',
						closeWith:'<\/closing tag>',
						placeHolder:"New content"
					}
				);
 		return false;
	});
	
	// And you can add/remove markItUp! whenever you want
	// $(textarea).markItUpRemove();
	$('.toggle').click(function() {
		if ($("#markItUp.markItUpEditor").length === 1) {
 			$("#markItUp").markItUpRemove();
			$("span", this).text("get markItUp! back");
		} else {
			$('#markItUp').markItUp(mySettings);
			$("span", this).text("remove markItUp!");
		}
 		return false;
	});
}

function pokaz_ladowanie()
{
  if ($('div#zaslona').attr("class")=="transparentDiv")
  {
    $('div#zaslona').addClass("zaslon");
  } else if ($('div#zaslona').attr("class")=="transparentDiv zaslon")
  {
    $('div#zaslona').removeClass("zaslon");
  }
}

function panel_art()
{
  if ($(".przewijanie strong").text())
  {
    var strona = $("#przew strong#str").text();
    strona = parseInt(strona.substring(strona.indexOf("[")+1, strona.indexOf("]")));
  } else {
    var strona = 1;
  }
  
  if ($("#listaart .filtry #ten_filtr").attr("class"))
  {
    var filtr = $("#listaart .filtry #ten_filtr").attr("class");
    var autor = "";
    $("#listaart .filtry #ten_filtr option").each( function() {
      if ($(this).attr("selected")) autor = $(this).val();
    });
    
  } else {
    var filtr = "wszystkie";
  }
  
  $("#listaart .przewijanie a").click( function()
  {
    var nrstr = parseInt($(this).attr("rel"));
    
    if (nrstr) {
      $.post("./js/prywatne/panel_art.php", { filtrowanie: filtr, autor: autor, source: "js", str: nrstr }, function(data) {
        $("div#listaart").html(data);
        panel_art();
      });
    }
  });
  
  $("#listaart form span").click( function()
  {
    klasa = $(this).attr("class");
    if (klasa=="zatw")
    {
      //$("input:checkbox", $("form[name=zatwfo]"));
      queryString = $('#formzatw').formSerialize();
      idi = queryString;
    } else idi = $(this).attr("title");
    //alert(idi);
    
    if (klasa=="usun")
    {
      if (confirm("Napewno usunąć?"))
      {
        $.post("./js/prywatne/panel_art.php", { id: idi, zm: klasa, source: "js", str: strona, filtrowanie: filtr, autor: autor }, function(data) {
          $("div#listaart").html(data);
          panel_art();
        });
        if (klasa=="usun") $(this).parent().parent().remove();
      } 
    } else if (klasa=="zatw") {
      $.post("./js/prywatne/panel_art.php", { id: idi, zm: klasa, source: "js", str: strona, filtrowanie: filtr, autor: autor }, function(data) {
        $("div#listaart").html(data);
        panel_art();
      });
    } else if (klasa=="edytuj") {
      $.post("./js/prywatne/edyt_info.php", { id: idi, zm: klasa, source: "js", str: strona }, function(data) {
        $("div#dok").html(data);
        kliki_ed();
      });
    }
  });
  $("form select[name=uwagi]").change( function() {
    idi = $(this).attr("title");
    $.post("./js/prywatne/panel_art.php", { id: idi, zm: "uwagi", uwagi: $(this).val(), source: "js", str: strona, filtrowanie: filtr, autor: autor }, function(data) {
      $("div#listaart").html(data);
      panel_art();
    });
  });
  $("form input[name=koment_uwagi]").change( function() {
    idi = $(this).attr("title");
    $.post("./js/prywatne/panel_art.php", { id: idi, zm: "koment_uwagi", uwagi_k: $(this).val(), source: "js", str: strona, filtrowanie: filtr, autor: autor }, function(data) {
      $("div#listaart").html(data);
      panel_art();
    });
  });
  
  $("#listaart .filtry span").click( function()
  {
    klasa = $(this).attr("class");
    
    if (klasa=="niepotwierdzone" || klasa=="potwierdzone" || klasa=="wszystkie")
    {
      $.post("./js/prywatne/panel_art.php", { filtrowanie: klasa, source: "js", str: 1 }, function(data) {
        $("div#listaart").html(data);
        panel_art();
      });
    }
  });
  $(".filtry select[name=autor]").change( function() {
    $.post("./js/prywatne/panel_art.php", { filtrowanie: "autor", autor: $(this).val(), source: "js", str: 1 }, function(data) {
      $("div#listaart").html(data);
      panel_art();
    });
  });
  
  $("input[name=nowy]").click( function () {
    //if ($("input[name=tytul]").val()=="" || $("input[name=podtytul]").val()=="" || $("input[name=podtytul]").val()=="" || $("textarea[name='keywords']").val()=="")
    
    if ($("input[name=tytul]").val()=="" ||   $("textarea[name='keywords']").val()=="") 
    {
      alert ("Pole tytuł, bądź keyword's nie zostało uzupełnione!");
      return false;
    } else if (!$("input[name='dowyczerpania']").attr("checked")) {
      
      str_date = $('input[name=data_end]').val();
      var re_date = /^(\d+)\-(\d+)\-(\d+)$/;
    	
      zn=$("input[name='grupa']:checked").attr("ref");
      if ( zn== '1' || zn=='2' || zn=='3' || zn=='9' ) {
        if (!re_date.exec(str_date))  {
        alert("Źle wprowadzona data: "+ str_date);
        return false;
        }
      }
    }
  });
  
}

function zapiszgazetki()
{
 tytul=$("input[name='tytul']").val();
 podtytul=$("input[name='podtytul']").val();
 data_end=$("input[name='data_end']").val();
 //tresc=tinyMCE.get('tresc').getContent();
 tresc=$("textarea[name='tresc']").val();
 keywords=$("textarea[name='keywords']").val();

 $.cookie('tytul', tytul, { path: '/', expires: 10 });
 $.cookie('podtytul', podtytul, { path: '/', expires: 10 });
 $.cookie('data_end', data_end, { path: '/', expires: 10 });
 $.cookie('tresc', tresc, { path: '/', expires: 10 });
 $.cookie('keywords', keywords, { path: '/', expires: 10 });
}
function odczytajgazetke()
{
 if (  $.cookie('tytul')!='' )
    $("input[name='tytul']").val($.cookie('tytul'));

 if (  $.cookie('podtytul')!='' )
    $("input[name='podtytul']").val($.cookie('podtytul'));
 if (  $.cookie('data_end')!='' )
    $("input[name='data_end']").val($.cookie('data_end'));

 if (  $.cookie('tresc')!='' )
    $("textarea[name='tresc']").text($.cookie('tresc'));
 if (  $.cookie('keywords')!='' )
    $("textarea[name='keywords']").text($.cookie('keywords'));
}

