jQuery(document).ready(function() {

jQuery('#signIn').keypress( function( event ) {
	if(event.keyCode == '13')
	{
		zaloguj();
	}
});

jQuery( '#previous' ).bt({
  contentSelector: "$('#previousWindow')",
  fill: '#E8E8E8', 
  strokeStyle: '#B7B7B7',
  spikeLength: 10, 
  spikeGirth: 10, 
  padding: 3,
  width: 275,
  cornerRadius: 0
});
	
jQuery( '#next' ).bt({
  contentSelector: "$('#nextWindow')",
  fill: '#E8E8E8', 
  strokeStyle: '#B7B7B7',
  spikeLength: 10, 
  spikeGirth: 10, 
  padding: 3,
  width: 275,
  cornerRadius: 0
});

});

function zaloguj() {
  var login = jQuery('#signin_login').val();
  var haslo = jQuery('#signin_haslo').val();
  var zapamietaj = jQuery('#signin_remember').attr('checked');

  jQuery('#loader').show();

  jQuery.post(
    '/logowanie',
		{ 'login': login, 'haslo': haslo, 'zapamietaj': zapamietaj },
		function(msg) {			      
      if(msg.msg == 0)
      {
        location.reload(true);
        jQuery('#loader').hide();
      }
      if(msg.msg > 0)
      {
        jQuery('#loader').hide();
        alert('Podałeś błędny login lub hasło.');
      }
    }, "json");
}

function wyloguj() {
  jQuery.post(
    '/wyloguj',
		{ },
		function(msg) {
      if(msg.msg == 0)
      {
        location.reload(true);
      }
      if(msg.msg > 0)
      {
        alert('Wystąpił błąd podczas wylogowywania.');
      }
    }, "json");
}

function dodajkomentarz(sztuczka_id) {

  var tresc = jQuery('#komentarze_tresc').val();
  
  if(tresc == "")
  {
    alert("Podaj treść komentarza.");
  } else {    
      jQuery.post(
      '/dodajkomentarz',
      {'sztuczka_id': sztuczka_id, 'tresc': tresc},
      function(msg) {
        if(msg.status == 1)
        {
          alert("Wystąpił błąd podczas dodawania komentarza.");
        } else {
          jQuery(".pusty_komentarz").hide();
          jQuery("#nowy_komentarz").replaceWith('<li id="nowy_komentarz"></li><li>' + msg.komunikat + '</li>').fadeIn('slow');
          jQuery("#komentarze_tresc").val('');
          //jQuery(".addcomment").hide();
        }
      }, "json");
  }
}

function test()
{
  var nazwa_cookie = 'sztuczki-oceny';
  var cookie = jQuery.cookie(nazwa_cookie);
  
  var date = new Date();
  date.setTime(date.getTime() + (1 * 24 * 60 * 60 * 1000));
  jQuery.cookie(nazwa_cookie, 'rozwiniete', { path: '/', expires: date });
}

function usun(typ, id)
{  
  if(typ == 'komentarz' || typ == 'sztuczka' && id > 0 )
  {
    jQuery.post(
    '/usun',
    {'typ': typ, 'id': id},
    function(msg) {
      var komunikat1;
      var komunikat2;

      if(typ == 'komentarz')
      {
        komunikat1 = 'Komentrz został usunięty.';
        komunikat2 = 'komentarza.'
      }
      if(typ == 'sztuczka')
      {
        komunikat1 = 'Sztuczka została usunięta.';
        komunikat2 = 'sztuczki.';
      }
      if(msg.komunikat == 0)
      {
        alert(komunikat1);
        if(typ == 'komentarz')
        {
          jQuery('#komentarz_' + id).fadeOut();
        }
        if(typ == 'sztuczka')
        {
          location.reload(true);
        }
      } else {
        alert('Wystąpił błąd podczas usuwania ' + komunikat2);
      }
    }, "json");
  } else {
    alert('Błędne parametry wejściowe podczas usuwania komentarza.');
  }
}