jQuery(function($) {
    function handle_download_click(e) {
        e.preventDefault();
        var download = $(this).attr('href');
        var form_html =
            '<form id="download-contact-info" style="width: 400px;" action="/contact_info.py/save" method="POST">' +
            'Keep me up to date on GlideTV news, software updates, and the latest information on products and services.' +
            '<p>' +
            '<label for="email"><strong>Email Address:</strong><br> <input type="text" id="email" name="email" style="width: 250px;" /></label>' +
            '</p>' +
            '<p>' +
            '</label>' +
            '</p>' +
            '<input type="hidden" name="download" value="' + download + '" />' +
            '<input type="submit" value="Download Now" />' +
            '</form>';

        $.fancybox(form_html, {
            titleShow: false,
            showCloseButton: false,
            scrolling: 'no',
            onComplete: function() {
                $('#download-contact-info').submit(function(e) {

                    var email = $('#email').val();
                    var contact_me = $('#contact_me').val();

                    $('#download-contact-info').hide();
                    $.fancybox('<div id="thanks-message">Thank you! Your download will begin momentarily.</div>');
                    return true;

                    $.ajax({
                        type: 'POST',
                        url: '/contact_info.py/save',
                        dataType: 'json',
                        data: {
                            email: email,
                            contact_me: contact_me,
                            download: download
                        },
                        success: function(data) {
                            if (data.result == 'ok') {
                                $.fancybox('<div id="thanks-message">Thank you! Your download will begin momentarily.</div>');

                                if ($.browser.msie) {
                                    //window.open('/contact_info.py/download?file=' + download);
                                    var iframe = $('<iframe src="/contact_info.py/download?file=' + download + '"></iframe>');
                                    $('#thanks-message').append(iframe);
                                }
                                else window.location = download;

                                setTimeout($.fancybox.close, 3000);
                            }
                            else if (data.result == 'invalid_email') {
                                $('.validation-error').remove();
                                $('#email').after('<span class="validation-error" style="color: red;"><br>Please enter a valid email address.</span>');
                                $.fancybox.resize();
                            }
                            else {
                                $.fancybox('There was an error saving your information. However, your download will begin momentarily.');

                                if ($.browser.msie) window.open('/contact_info.py/download?file=' + download);
                                else window.location = download;

                                setTimeout($.fancybox.close, 3000);
                            }
                        }
                    });
                });
            }
        });
    }
    $('.download').click(handle_download_click);
});
