var haslo_klikniete = 0 ;

 function poprawny_email(str) {

   return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
}

function dodaj_do_schowka( id ) {
 $.ajax({
   type: "GET",
   url: "ajax.php",
   data: "mod=schowek&id="+id,
	dataType: "json",
   success: function(msg){
	  $("#schowek_liczba").html( msg.liczba ) ;
	  if( msg.msg ) alert( msg.msg ) ;	
   }
 });
}

function pokaz_schowek() {
 $.ajax({
   type: "GET",
   url: "ajax.php",
   data: "mod=schowek_html",
   success: function(msg){
	  $("#schowek_zawartosc").html( msg ) ;
	  $("#schowek_tlo").show() ;	
	  $("span.arrow_s").css( 'background-position', '0 -35px')	
   }
 });
 
}

function usun_ze_schowka( id ) {
$.ajax({
   type: "GET",
   url: "ajax.php",
   data: "mod=schowek_del&id="+id,
	dataType: "json",
   success: function(msg){
	  $("#schowek_liczba").html( msg.liczba ) ;
	  if( msg.msg ) alert( msg.msg ) ;	
	  if( msg.liczba == 0 ) { $("#schowek_tlo").hide() ; 	  $("span.arrow_s").css( 'background-position', '0 0') ; } 	
   }
 });
}

$(document).ready( function() {

 $("#dodaj_newsletter").click( function() {
   var email = $("#inp_email").val() ;
	if( email == '' ) { alert( 'Wpisz adres email' ) ; return ; }
	if( email == $("#inp_email").attr( 'hint') ) { alert ('Wpisz adres email') ; return ; }
	
	if( poprawny_email( email ) == false ) return ;	
	$("#inp_email").val('') ;
	  $.ajax({	
   type: "GET",
   url: "ajax.php",
   data: "mod=newsletter&email="+email,
	dataType: "json",
   success: function(msg){
	    if( msg.wynik == 1 ) {
		   $("#newsletter_txt").html( 'Adres został dodany' ) ;
			alert( 'Adres email został dodany do bazy' ) ;
		 }
		 else {
		   $("#newsletter_txt").html( 'Adres już jest w bazie' ) ;
			alert( 'Adres email już jest w bazie' ) ;
			$("#newsletter_txt").addClass( 'error' ) ;
		 }
	    }
	  })	
 }) ;
 
 if( $("#alert").length > 0 ) {
   alert( $("#alert").html() ) ;
 }
  	$("#add-favourites").click( function() {
	  dodaj_do_schowka( $(this).attr( 'rel' ) ) ;
	}) ;
	
	$("#schowek span.info").click( function() {
	  if( $("#schowek_tlo").is( ":visible" )) {
		$("#schowek_tlo").hide() ;
 	   $("span.arrow_s").css( 'background-position', '0 0')	
		return ;
	  }
		
	  pokaz_schowek() ;	
   }) ;
	
	$("#schowek_zawartosc span.usun").live( 'click', function() {
	 // alert( $(this).parent().attr( 'rel' )) ;
	  var id = $(this).parent().attr( 'rel' ) ;
	  usun_ze_schowka( id ) ;	
	  $(this).parent().hide() ;		
	}) ;
	
    $("input").each( function() {
        if( $(this).attr( 'title' ) != '' ) {
            $(this).attr( 'hint', $(this).attr( 'title' ) ) ;
            $(this).val( $(this).attr( 'title' ) ) ;
            $(this).attr( 'title', '' ) ;
            
            $(this).blur( function() {
				   
                if( $(this).val() === '' ) $(this).val( $(this).attr( 'hint' ))
            }
            ) ;
            $(this).focus( function() {
				  
                if( $(this).val() == $(this).attr( 'hint' )) $(this).val('' ) ;
            }
            ) ;
        }
    }
    ) ;
	 
	 $("#zmien_woj").click( function() {
	   $("ul.sub").toggle() ;
	 }) ;
	 
	 $("li.main_kat a").click( function() {
	   if( $(this).siblings('ul').length == 0 ) return true ;
		else {
	   $("li.main_kat ul").hide() ;
		$(this).siblings('ul').show() ;
		
		$("li.main_kat").removeClass( 'active' ) ;
		$(this).parent().addClass( 'active' ) ;
	   return false ;
		}
	 }) ;
}
) ;

