
function process_tellafriend(e) {

    e.preventDefault();

    $.post(
        '/petition/taf.pl',
        $('#f_tellafriend').serialize(),
        function (response) {
            if ( response.status == 'success' ) {
                target = 'signed.html'
                if (document.taf &&  // only defined in some petitions
                    document.taf.occ && 
                    document.taf.occ.value) { target = 'occ_signed.html' }
                window.location = target + '?id=' + response.id 
                    + '&sent' + response.sent
            }
            else {
                show_errors($('#f_tellafriend'),'#tellafriend-error',response);
            }
        },
        'json'
    );
    
}

function show_tellafriend(id) {

    if(!$('#overlay').length) {

        is_occ = /occ\.html/.test(window.location.pathname) ? 1 : 0

        var modal_content = null;
        $.ajax({
            url: 'tell_multiple_friends.html?id=' + id + '&occ=' + is_occ ,
            success: function (data) {
                
                $('<div id="overlay"></div>')
                    .css({width:$(document).width(), height: $(document).height(),opacity:'0'})
                    .appendTo('body')
                    .fadeTo("slow",0.6, function() {
                        var one_box = $(data).appendTo('body');
                        var modal_top = parseInt( $(document).scrollTop() + ( $(window).height() / 2 ) - ( one_box.height() / 2 ) );
                        if(modal_top < 0) modal_top = 20;
                        var modal_left = parseInt( ( $(window).width() / 2 ) - ( one_box.width() / 2 ) );
                        one_box.css({
                            display:'block',
                            top: modal_top,
                            left: modal_left
                        });
                        
                        $('#overlay, #one-box .close-overlay').click(function(e){
                            e.preventDefault();
                            $('#one-box').remove();
                            $('#overlay').fadeTo("slow",0, function(){
                            $(this).remove();
                        });
                        
                    });

                    // Submit tell-a-friend
                    $('.tellafriend-form .submit').click( process_tellafriend );
                        
            });
                
            }
        });
    }
}


jQuery(function($){

	
    if ( $('.user-pointer').ifixpng ) {
        $('.user-pointer').ifixpng();
    }
	
	$('.actions-ov .mod-action .rep').hover(function(){
		$(this).find('.content').addClass('content-hover');
	},function(){
		$(this).find('.content').removeClass('content-hover');
	});
	
	// Act now - additional information sliders
	$('#action .additional-info .slider').hide();
	$('#action .additional-info h3').click(function(e){
		e.preventDefault();
		if(!$(this).hasClass('active')){
			$(this).addClass('active').next('.slider').slideDown("slow");
		} else {
			$(this).removeClass('active').next('.slider').slideUp("slow");	
		}
	});
	
	// Store pre-written letter
	if($('.act-compose textarea').length){
		var prewritten_subject = $('.act-compose .text[name="letter_subject"]').val();
		var prewritten_body = $('.act-compose textarea').val();
		var current_selection = null;
		
		$('.act-compose input[name="letter_type"]').click(function(){
			if($(this) != current_selection){
				switch($(this).val()){
					case 'pre_written':
						$('.act-compose .text[name="letter_subject"]').val(prewritten_subject);
						$('.act-compose textarea').val(prewritten_body);
						break;
					case 'own_words':
						$('.act-compose .text[name="letter_subject"]').val('').focus();
						$('.act-compose textarea').val('');
						break;
				}
				current_selection = $(this);
			}
		});
	}
	
    // Submit a petition, on success show the tell-a-friend form in an 
    // overlay
	$('.petition-form .submit').click(function(e){
		e.preventDefault();

        $.post(
            '/petition/process_petition.html',
            $('#fpetition').serialize(),
            function(data) {
                if ( data.status == 'success' ) {
                    show_tellafriend(data.id);             
                }
                else {
                    if ( data.sent == 0 ) {
                        data.missing = [ 'friends' ];
                    }
                    show_errors($('#fpetition'),'#petition-error', data);
                }
            },
            "json"
        );	

		
	});


});

function show_errors(el, id, data) {
    var errors = [].concat(data.invalid, data.missing); 
    var error_text = '';
    for ( var i=0; i<errors.length;i++) {
        var e = errors[i];
        el.find('label[for="' + e + '"]').addClass('error');
    }
    el.find(id).html('Whoooooops!');
    el.find(id).show();
}
