(function ($) { $(document).ready(function() {
    
    var cover_id    = 'popup-cover';
    var frame_id    = 'popup-frame';
    var close_id    = 'popup-close-button';
    var popup_path  = '/hotwire/data/popups/';
    
    var popup_height   = $(window).height();
    
    $('#'+close_id).live('click', function() {
        close_popup();
        return false;       
    });
    
    popup = function(screen)
    {
        var popup_html = build_html(screen);
        
        $('body').append(popup_html);
        
        $('#'+cover_id).fadeIn('1000', function() {
            $('#'+frame_id).animate({
                top: '0'   
            }, 750, 'easeOutBounce');            
        });
    }    
    
    function close_popup()
    {        
        $('#'+frame_id).animate({
            top: '-' + $(this).height()
        }, 250, 'linear', function() {
            $('#'+cover_id).fadeOut('1000', function() {
                $('#'+cover_id).remove();
            });     
        });
    }
    
    function build_html(screen)
    {
        // set a variable to load the html into, and a default message if the page isn't loaded
        var page = 'Page not loaded';
        
        // grab the pages content, async is set to false to wait for the request
        $.ajax({
            url: popup_path + screen,
            async: false,
            success: function(data) {
                page = data;
            }
        });
        
        // build the html for the popup, inserting the page from the ajax call 
        var popup_html  = '<div id="'+cover_id+'" style="height: '+popup_height+'px;">';
                    popup_html += '<div id="popup-cover-wrap">';
                        popup_html += '<div id="'+frame_id+'">';
                            popup_html += '<div id="popup-frame-content">';
                                popup_html += '<a href="#" id="'+close_id+'"></a>';
                                popup_html += page;                                
                            popup_html += '</div>';
                        popup_html += '</div>';
                    popup_html += '</div>';
        popup_html     += '</div>';
        
        return popup_html;
    }

    
    
    /* german actions */
    
    $('#german-yes').live('click', function() {
        if ($('#german-remember:checked').val())
        {
            $.cookie('german_popup', 'noshow');
        }
        
        _gaq.push(['_trackEvent', 'German Popup', 'Clicked Yes']);
    });
    
    $('#german-no').live('click', function() {
        if ($('#german-remember:checked').val())
        {
            $.cookie('german_popup', 'noshow');
        }
        _gaq.push(['_trackEvent', 'German Popup', 'Clicked No']);
        
        close_popup();
        return false;
    });
    
    
}); })(jQuery);
