$(function()
{    
    prepare_login_inputs();   
    prepare_newsletter();
    rotate_news();
    bind_hover();
    try{
    	$('#id_newsletter_email').tipsy({gravity: 's', fallback: "Atentie!!! Veti primi un email de confirmare pe email. Se poate ca acesta sa ajunga in spam. Daca acest lucru se intampla va rugam setati optiuni de 'Not Spam' din casuta de email."});    		
    }catch(err){}
});

$(function() { clear_emails(); })

function clear_emails()
{
	$('.imal').each(function(){
		if ($(this).length > 0)
		{
			if (typeof($(this).html) == 'function')
			{	
			var email = $(this).html();
			email = email.replace(' abcdef ', '@');
			email = email.replace(/ xyz /gi, '.');
			$(this).html("<a href='mailto:"+email+"'>"+email+"</a>");
			}
		}
		})	
}

function send_remember_password()
{
	$('#ajax_loader_login').show();
	var email = $('#remember_password_email').val()
	if (email.match("\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b"))
		{
			$('#ajax_loader_login').hide();
			$('#remember_password_email_error').html("Introduceti un email valid.");
		}
	else
		{
			//$('#')
			$('#ajax_loader_login').hide();
		}
}

function prepare_login_inputs()
{
   $("#id_username_widget").focus(function(){    
        if ($(this).val() == "email")
                $(this).val("");
    });
    $("#id_password_fake").focus(function(){
        $("#id_password_fake").hide();
        $("#id_password_widget").show();
        $("#id_password_widget").focus();
    });  
}

function login()
{
    username = $('#id_username_widget').val();
    password = $('#id_password_widget').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');
                	next_after_login = $('#next_after_login').val();
                	if (next_after_login)
                	{
                		window.location =  next_after_login ;
                	}
                	else
                	{	
                		if (window.location.pathname == "/auth/logout/")
                			{window.location = '/';}
                		else
                			{window.location.reload();}
                	}
                }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
}

function bind_hover()
{
		
	$('#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_newsletter_email").live('click', function(){
        
        if ($(this).val() == "Email")
                $(this).val("");
        
    });	
}

function subscribe()
{
	//var name = $('#id_name').val()
	var email = $('#id_newsletter_email').val()
	$('#ajax_loader_newsletter').show();
				
	$.post('/newsletter/widget/',{ 'newsletter_email' : email },
		           
		function(response){
		$('#ajax_loader_newsletter').hide();
		            
		if (response == 'OK')
		{
			$('#message').html("<span class='succes_txt'>Emailul a fost inscris!</span>");
			$('#form_div').html('')
		}
		else
		{
			$('#form_div').html(response);
		}
						
	});
}
	
function vote_choice(poll_id)
{
	var radio = $("#poll_form_" + poll_id + " input[type='radio']:checked");
	var choice_id = radio.attr('choice');
	if (choice_id)
	{
		url = '/polls/vote/choice/'+choice_id+'/';
		$('#poll_form_'+poll_id).load(url);
	}
}

function show_results(poll_id)
{
	url = '/polls/show_result/'+poll_id+'/';
	$('#poll_form_'+poll_id).load(url);
}

function hide_results(poll_id)
{
	url = '/polls/hide_result/'+poll_id+'/';
	$('#'+poll_id).load(url);
}


function select_rss(comment)
{
	if (comment == true)
		{ var url = $('#posts_rss_select :selected').val(); }
	else
		{ var url = $('#comments_rss_select :selected').val(); }
	if (url == 0)
		{return false;}
	if (url == 1)
		{
		if (comment == true)
		{
			link = $('#posts_rss_select :selected').attr('link');
			website_title = $('#posts_rss_select :selected').attr('website');
		}
		else
		{
			link = $('#comments_rss_select :selected').attr('link');
			website_title = $('#comments_rss_select :selected').attr('website');
		}
		bookmark_us(link, website_title)
		return true;
		}
	window.location = url;
}


function search_values()
{
	if ($('#id_search_blog').val().length == 0)
	{
		$('#search_widget_error').html('Va rugam adaugati completati casuta de mai sus.')
		return false;
	}
	$('#search_widget_form').submit();
}

function bookmark_us(url, title){

	if (window.sidebar) // firefox
	    window.sidebar.addPanel(title, url, "");
	else if(window.opera && window.print){ // opera
	    var elem = document.createElement('a');
	    elem.setAttribute('href',url);
	    elem.setAttribute('title',title);
	    elem.setAttribute('rel','sidebar');
	    elem.click();
	}
	else if(document.all)// ie
	    window.external.AddFavorite(url, title);
	}
