var polecane_offset = 0 ;
var mouse_on = 0 ;

 function polecane_step() {
   if( mouse_on == 0 ) 
   $("#polecane_next").click() ;
	
	setTimeout( "polecane_step()", 6000 ) ;
 }

 function poprawny_email(str) {

   return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
}

$(document).ready( function() {
var polecane_liczba = $("#polecane_in div.polecane_single").length ;
var polecane_tab = $("#polecane_in div.polecane_single") ;

 $("#dodaj_newsletter").click( function() {
   var email = $("#inp_email").val() ;
	if( email == '' ) { alert( 'Podaj adres email' ) ; return ;}
	if( email == $("#inp_email").attr( 'hint') ) { alert( 'Podaj 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' ) ;
			$("#newsletter_txt").addClass( 'error' ) ;
			alert( 'Adres już jest w bazie' ) ;
		 }
	    }
	  })	
 }) ;
 
$("#polecane_next").click( function() {
 if( polecane_offset < polecane_liczba - 1 ) {
  
  polecane_offset++ ;
  $("#polecane_in div.polecane_single").each( 
    function() {
	   $(this).animate({"left": "-=580px"}, "slow");

	 }	
	) ;
 }
 else {
   var off = (polecane_liczba-1)*580 ;
	$("#polecane_in div.polecane_single").each( 
    function() {
	   $(this).animate({"left": "+="+off+"px"}, "fast");

	 }	
	) ;
	polecane_offset = 0 ;
 }
} ) ;

$("#polecane_prev").click( function() {
 if( polecane_offset > 0 ) {
 
  polecane_offset-- ;
 $("#polecane_in div.polecane_single").each( 
    function() {
	   $(this).animate({"left": "+=580px"}, "slow");

	 }	
	) ; 
 }
 else {
   var off = (polecane_liczba-1)*580 ;
	$("#polecane_in div.polecane_single").each( 
    function() {
	   $(this).animate({"left": "-="+off+"px"}, "fast");

	 }	
	) ;
	polecane_offset = polecane_liczba-1 ;
 } 
 
} ) ;

 $("#polecane_box").mouseover( function() {
   mouse_on = 1 ;
 });

 $("#polecane_box").mouseout( function() {
   mouse_on = 0 ;
 });

	setTimeout( "polecane_step()", 6000 ) ;
 
 if( $("#alert").length > 0 ) {
   alert( $("#alert").html() ) ;
 }
    $("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('' ) ;
            }
            ) ;
        }
    }
    ) ;
	 
	  $("textarea").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 ;
		}
	 }) ;
}
) ;

