jQuery(document).ready(function() {

	jQuery('#signIn').keypress( function( event ) {
		if(event.keyCode == '13') {
			zaloguj();
		}
	});
	
	jQuery.fn.showErrors = function( classname, value, time )
	{	
		jQuery( '.' + classname ).html( value ).show();
		var div = classname.split('_');
		jQuery( '#' + div[1] ).parent( 'div' ).addClass( 'error' );
		
		if ( time == undefined ) {
			time = 3500;			
		} 
		
		if ( time != 'noRemove' ) {
			setTimeout( function() {
				jQuery( '.' + classname ).fadeOut();
				jQuery( '#' + div[1] ).parent( 'div' ).removeClass( 'error' );
			}, time );
		}
	}
		
	
	jQuery( '#previous' ).bt({
	  contentSelector: "$('#previousWindow')",
	  fill: '#E8E8E8', 
	  strokeStyle: '#B7B7B7',
	  spikeLength: 10, 
	  spikeGirth: 10, 
	  width: 280,	  
	  cornerRadius: 5
	});
		
	jQuery( '#next' ).bt({
	  contentSelector: "$('#nextWindow')",
	  fill: '#E8E8E8', 
	  strokeStyle: '#B7B7B7',
	  spikeLength: 10, 
	  spikeGirth: 10, 
	  width: 280,	  
	  cornerRadius: 5
	});
	
	for( var i = 1; i <= 9; i++)
	{
		jQuery( '.todayTrick_' + i ).bt({
		  contentSelector: $('#topTrick' + i + 'Window'),
		  fill: '#E8E8E8', 
		  strokeStyle: '#B7B7B7',
		  spikeLength: 10, 
		  spikeGirth: 10, 
		  width: 280,		  
		  cornerRadius: 5
		});
	}
	
	jQuery.fn.deleteValue = function( sDivName, sVal ) {
		jQuery( sDivName ).bind( 'click', function(){
			if ( jQuery( sDivName ).val() == sVal )
			{ 
				jQuery( sDivName ).val('');
			}		
		});		
		jQuery('body').bind( 'mousedown', function(){
			if ( jQuery( sDivName ).val() == "" )
			{
				jQuery( sDivName ).val( sVal );
			}
		});		
	};
			
	jQuery("#subscribe_text").deleteValue( "#subscribe_text", "wpisz swój adres e-mail..." );
	
	jQuery("#subscribe_btn").bind('click', function(){
		var sEmail = jQuery("#subscribe_text").val();
		jQuery.post('/subskrybuj', { email: sEmail }, function(data) {
			if ( data.status == 0 ) {
				alert(data.message);
				jQuery("#subscribe_text").val('wpisz swój adres e-mail...');
			} else
			if ( data.status == 2 ) { 
				jQuery('.error_subskrypcja').showErrors('error_subskrypcja', data.message, 2500);				
				jQuery("#subscribe_text").val('wpisz swój adres e-mail...');
			} else {				
				jQuery('.error_subskrypcja').showErrors('error_subskrypcja', data.message, 2500);
			}
		}, "json");
	});
});

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) {			      
      jQuery('#loader').hide();
      if( msg.msg == 0 ) {
        window.location = msg.url;       
      } else {
	      alert(msg.message);    	  
      }
    }, "json");
}

function wyloguj() {
  jQuery.get( '/wyloguj', function( msg ) { location.reload(true); } );
}

function dodajkomentarz( sztuczka_id ) {
  
  jQuery('.addcomment').hide();
  
  var tresc = jQuery('#komentarze_tresc').val();  
  jQuery.post( '/dodajkomentarz', { 'sztuczka_id': sztuczka_id, 'tresc': tresc }, function(msg) {
	if( msg.status == 1 ) {
		jQuery('.addcomment').show();              
    	jQuery('.error_tresc').showErrors( "error_tresc", msg.komunikat, 2500);
    } 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').show();
  }, "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.');
  }
}
