$(document).ready(function() {
    
    $.get('quote?hits', function(data) {
        if(data) setTimeout(popup,data);
    });
    
    var options = { 
        beforeSubmit:  quoteThanks  
    };
    
    $('#quote').ajaxForm(options);
     
    $(".pop-quote").live('click',popup);
    
    $('a.close, button.close, #fade').live('click', function() {
        
        $('#fade , .popup').fadeOut(250,function() {
            $('#fade, img.close').remove();  
        });
        
        return false;
    });

    function quoteThanks() {
        $('#step1').hide()
        $('#step2').show();
    }

});

function popup() {

    if($('#fade').length == 0) {
        
        var popID = 'popup';
        var popWidth = 550;
    
        $('#' + popID).fadeIn().css({ 'width': Number( popWidth ) }).prepend('<a href="#" class="close"><img src="img/close.png" class="close" title="Close Window" alt="Close" /></a>');
    
        var popMargTop = ($('#' + popID).height() + 80) / 2;
        var popMargLeft = ($('#' + popID).width() + 80) / 2;
    
        $('#' + popID).css({
            'margin-top' : -popMargTop,
            'margin-left' : -popMargLeft
        });
    
        $('body').append('<div id="fade"></div>');
        $('#fade').css({'filter' : 'alpha(opacity=80)'}).fadeIn(250);
        $('#focus').focus();
    
        return false;
    } 
}
