$(document).ready(function(){
    
    prepare_login_inputs();   
    prepare_newsletter();
    rotate_news();
});


function prepare_login_inputs(){
   $("#id_username").focus(function(){
           
        if ($(this).val() == "nume utilizator")
                $(this).val("");
    });
    $("#id_password_fake").focus(function(){
        $("#id_password_fake").hide();
        $("#id_password").show();
        $("#id_password").focus();
    });  
}

function login(){

    username = $('#id_username').val();
    password = $('#id_password').val();

    $('#ajax_loader_login').show();
    $('#msg').text('');
    $.post('/auth/login/ajax/',{ 'username' : username, 'password' : password },
           
        function(response){
            $('#ajax_loader_login').hide();

                if (response == "ok"){
                    //$('#msg').text('Autentificare a reusit !');
                    //$('#is_authentificated').text('Bine ai venit '+username+'.');
                    //$('#authentification_form').toggle('slow');
                    window.location = next_after_login;
                }else{
                    $('#msg').text(response);
                }
    });
}



var headline_count;
var headline_interval;
var old_headline = 0;
var current_headline = 0;

function rotate_news()
{
	headline_count = $("div.headline").size();
	  $("div.headline:eq("+current_headline+")").css('top','5px');

	  headline_interval = setInterval(headline_rotate,5000); //time in milliseconds
	  $('#scrollup').hover(function() {
	    clearInterval(headline_interval);
	  }, function() {
	    headline_interval = setInterval(headline_rotate,5000); //time in milliseconds
	    headline_rotate();
	  });	
}

function headline_rotate() {
  current_headline = (old_headline + 1) % headline_count; 
  $("div.headline:eq(" + old_headline + ")").animate({top: -205},"slow", function() {
    $(this).css('top','210px');
  });
  $("div.headline:eq(" + current_headline + ")").show().animate({top: 5},"slow");  
  old_headline = current_headline;
}

function prepare_newsletter()
{
	$("#id_email").live('click', function(){
        
        if ($(this).val() == "Email")
                $(this).val("");
        
    });	
}

function subscribe()
{
	var name = $('#id_name').val()
	var email = $('#id_email').val()
	$('#ajax_loader_newsletter').show();
				
	$.post('/newsletter/widget/ajax/',{ 'email' : email },
		           
		function(response){
		$('#ajax_loader_newsletter').hide();
		            
		if (response == 'OK')
		{
			$('#message').text("Emailul a fost inscris!");
			$('#form_div').html('')
		}
		else
		{
			$('#form_div').html(response);
		}
						
});}
