
$(document).ready(function() {
    //$('img').pngfix();
    var done = 0;
    $('#evaluate form').submit(function() {
        doTheRequest();
        return false;
    });
    var doTheRequest = function() {
        $('#go, #uri').attr('disabled', 'disabled');
        $('#data').html('');
        $('#pulsate').hide();
        $('#loading').show();
        $.ajax({
            url: 'include/application/bootstrap.php',
            data: 'uri=' + $('#uri').val(),
            cache: false,
            success: function(html) {
                $('#loading').hide();
                $('#data').append(html);
                $("#results").accordion({
                    header: 'h2',
                    autoHeight: false,
                    clearStyle: true,
                    icons: {
                        header: 'header-plus',
                        headerSelected: 'header-minus'
                    },
                    animated: 'easeInOutCubic'
                });
                $('.link').click(function() {
                    var href = $(this).attr('href');
                    $('#uri').val(href);
                    doTheRequest();
                    return false;
                });
                $('#go, #uri').removeAttr('disabled');
                showTooltips();
                formSend();
                openNext();
                selectCode();
                //$('img').pngfix();
            },
            error: function() {
                alert('Network error');
            }
        });
    };
    var showTooltips = function() {
        $('a[rel=tooltip]').mouseover(function(e) {
            var tip = $(this).attr('title');    
            $(this).attr('title','');          
            $(this).append('<div id="tooltip"><div class="tipBody">' + tip + '</div></div>');        
            $('#tooltip').fadeIn('500');
            $('#tooltip').fadeTo('10', 1);
        }).mousemove(function(e) {
            $('#tooltip').css('top', e.pageY + 20);
            $('#tooltip').css('left', e.pageX - 370);
        }).mouseout(function() {
            //Put back the title attribute's value
            $(this).attr('title',$('.tipBody').html());
            $(this).children('div#tooltip').remove();
        }).click(function() {
            return false;
        });
    };
    var selectCode = function() {
        $('.rate-code').click(function() {
            $(this).trigger('select');
        });
    };
    var openNext = function() {
        $('#badges_open').click(function() {
            if ( $('#badges').css('display') == 'none' ) {
                $('#badges').css('display', 'block');
            } else {
                $('#badges').css('display', 'none');
            }
            return false;
        });
        $('#contact').dialog({
            bgiframe: true,
            autoOpen: false,
            width: 560,
            closeText: 'Fermer',
            modal: true
        });
        $('#contact_open').click(function() {
            $('.message').remove();
            $('#contact').dialog('open');
            return false;
        });
    };
    // Contact form
    var formSend = function() {
        $('#send').click(function() {
            var error = false;
            var emailreg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
            
            $('.message').remove();
            
            var email = $('#email').val();
            var phone = $('#phone').val();
            
            if ( ( email == '' || !emailreg.test(email) ) && phone == '' ) {
                error = true;
            }
            
            if ( error == true ) {
                $('.input-button').after('<span class="message error">Veuillez saisir au moins votre email ou votre numéro de téléphone</span>');
            } else {
                $('.message').remove();
                $('.input-button').after('<span class="message sending">Veuillez patienter, envoi en cours ...</span>');
                
                var name = $('#name').val();
                var uri = $('#uri').val();
                
                $.post('include/application/sendmail.php',
                    { name: name, email: email, phone: phone, uri: uri },
                    function(data) { 
                        $('#name, #email, #phone').val('');
                        $('.message').remove();
                        $('.input-button').after('<span class="message ok">Votre message à  bien été envoyé !</span>');  
                    }
                );                                                                                                                                                                                          
            }
            
            return false;
        });
    };
});

/**
 * Easing functions
 * Borrowed from GSGD (http://gsgd.co.uk/sandbox/jquery/easing/)
 */
jQuery.extend(jQuery.easing,
{
    easeInOutCubic: function (x, t, b, c, d) {
        if ((t/=d/2) < 1) return c/2*t*t*t + b;
        return c/2*((t-=2)*t*t + 2) + b;
    },
    easeOutBounce: function (x, t, b, c, d) {
        if ((t/=d) < (1/2.75)) {
            return c*(7.5625*t*t) + b;
        } else if (t < (2/2.75)) {
            return c*(7.5625*(t-=(1.5/2.75))*t + .75) + b;
        } else if (t < (2.5/2.75)) {
            return c*(7.5625*(t-=(2.25/2.75))*t + .9375) + b;
        } else {
            return c*(7.5625*(t-=(2.625/2.75))*t + .984375) + b;
        }
    }
});
