/*
     $('#primaryNav').bind('refresh', function(event) {
        $.ajax({
        }); 
    });

    data-myAttr="<?php echo url_for('@regen_primary_nav'); ?>"

 */

(function($)
{

    $(document).ready(function() {
        
        // triggers modal with some default assumptions
        $('.modal').live('click', function(event) {
           event.preventDefault();
            var link = $(this);
            var modal = $('#generalModalContainer');
            modal.html($('#ajaxLoader').html());
            modal.dialog({
                autoOpen: false,
                height: 480,
                width: 640,
                modal: true,
                title: link.attr('data-title'),
                open: function(event, ui) {
                    $.ajax({
                        url: link.attr('href'),
                        success: function(data, textStatus, XMLHttpRequest){
                            modal.html(data);
                        }
                    });
                }
            });
            modal.dialog('open');
        });

       if ($("#event_is_pay_per_view").val() == 1) {
           $("#event_observer_fee").parents('tr').show();
       } else {
           $("#event_observer_fee").parents('tr').hide();
       }

        $("#event_is_pay_per_view").change(function() {
           var eop = $("#event_is_pay_per_view");
           var eof = $("#event_observer_fee");
           if (eop.val() == 1) {
               eof.parents('tr').show();
           } else {
               eof.parents('tr').hide();
           }
        });

        // tooltips
        $("form span[title]").tooltip();

        // initialize scrollable
        $(".scrollable").scrollable();
        
        // handles the AJAX for notes to email system
        $('.sendNotesLink').live('click', null, function(event) {
           event.preventDefault();
           var a = $(this);
           $.ajax({
               url: a.attr('data-ajaxurl'),
               success: function(data, textStatus, XMLHttpRequest){
                   $('#notesToEmailRecipientList').html(data);
               }
           })
        });

        // handles the AJAX for removing note recipients
        $('.removeNoteRecipientLink').live('click', null, function(event) {
           event.preventDefault();
           var a = $(this);
           $.ajax({
               url: a.attr('data-ajaxurl'),
               success: function(data, textStatus, XMLHttpRequest){
                   $('#generalModalContainer').html(data);
               }
           })
        });

        // triggers modal with send notes form
        $('#emailNotesLink').live('click', function(event) {
           event.preventDefault();
            var link = $(this);
            var modal = $('#generalModalContainer');
            modal.html($('#ajaxLoader').html());
            modal.dialog({
                autoOpen: false,
                height: 480,
                width: 640,
                modal: true,
                title: 'Email Your Notes to People',
                open: function(event, ui) {
                    $.ajax({
                        url: link.attr('data-ajaxurl'),
                        success: function(data, textStatus, XMLHttpRequest){
                            modal.html(data);
                        }
                    });
                }
            });
            modal.dialog('open');
        });
        
        // handles the AJAX for the send notes to email form
        $('#notesToEmailRecipientForm').live('submit', null, function(event) {
            event.preventDefault();
            var form = $(this);
            var formData = form.serialize();
           $('#generalModalContainer').html($('#ajaxLoader').html());
            $.ajax({
                url: form.attr('action'),
                data: formData,
                type: 'POST',
                success: function(data, textStatus, XMLHttpRequest){
                    $('#generalModalContainer').html(data);
                }
            })
        });

        // triggers modal with invite form
        $('.invitePeopleLink').live('click', function(event) {
           event.preventDefault();
            var link = $(this);
            var modal = $('#generalModalContainer');
            modal.html($('#ajaxLoader').html());
            modal.dialog({
                autoOpen: false,
                height: 480,
                width: 640,
                modal: true,
                title: 'Invite People to this Event',
                open: function(event, ui) {
                    $.ajax({
                        url: link.attr('data-ajaxurl'),
                        success: function(data, textStatus, XMLHttpRequest){
                            modal.html(data);
                        }
                    });
                }
            });
            modal.dialog('open');
        });
        
        // handles the AJAX for the event invite form
        $('#eventInviteForm').live('submit', null, function(event) {
            event.preventDefault();
            var form = $(this);
            var formData = form.serialize();
           $('#generalModalContainer').html($('#ajaxLoader').html());
            $.ajax({
                url: form.attr('action'),
                data: formData,
                type: 'POST',
                success: function(data, textStatus, XMLHttpRequest){
                    $('#generalModalContainer').html(data);
                }
            })
        });

        
        $('.signInLinkInDialog').live('click', null, function(event) {
           event.preventDefault();
           var a = $(this);
           $('#generalModalContainer').html($('#ajaxLoader').html());
           $.ajax({
               url: a.attr('data-ajaxurl'),
               success: function(data, textStatus, XMLHttpRequest){
                   $('#generalModalContainer').html(data);
               }
           })
        });
        
        $('.resetRequestLinkInDialog').live('click', null, function(event) {
           event.preventDefault();
           var a = $(this);
           $('#generalModalContainer').html($('#ajaxLoader').html());
           $.ajax({
               url: a.attr('data-ajaxurl'),
               success: function(data, textStatus, XMLHttpRequest){
                   $('#generalModalContainer').html(data);
               }
           })
        });
        
        $('.registerLinkInDialog').live('click', null, function(event) {
           event.preventDefault();
           var a = $(this);
           $('#generalModalContainer').html($('#ajaxLoader').html());
           $.ajax({
               url: a.attr('data-ajaxurl'),
               success: function(data, textStatus, XMLHttpRequest){
                   $('#generalModalContainer').html(data);
               }
           })
        });
        
        $('.signInLink').live('click', null, function(event) {
            event.preventDefault();
            $('#generalModalContainer').html($('#ajaxLoader').html());
            $('#generalModalContainer').dialog({
                autoOpen: false,
                height: 450,
                width: 600,
                modal: true,
                title: 'Sign In',
                open: function(event, ui) {
                    $.ajax({
                        url: $('.signInLink').attr('href'),
                        success: function(data, textStatus, XMLHttpRequest){
                            $('#generalModalContainer').html(data);
                        }
                    });
                }
            });
            $('#generalModalContainer').dialog('open');
        });
        
        $('.registerLink').live('click', null, function(event) {
            event.preventDefault();
            $('#generalModalContainer').html($('#ajaxLoader').html());
            $('#generalModalContainer').dialog({
                autoOpen: false,
                height: 450,
                width: 600,
                modal: true,
                title: $('.registerLink').attr('data-title'),
                open: function(event, ui) {
                    $.ajax({
                        url: $('.registerLink').attr('href'),
                        success: function(data, textStatus, XMLHttpRequest) {
                            $('#generalModalContainer').html(data);
                        }
                    });
                }
            });
            $('#generalModalContainer').dialog('open');
        });
        
        // handles the AJAX for the feedback form
        $('#feedbackFormModal').live('submit', null, function(event) {
            event.preventDefault();
            var form = $(this);
            var formData = form.serialize();
            $('#generalModalContainer').html($('#ajaxLoader').html());
            $.ajax({
                url: form.attr('action'),
                data: formData,
                type: 'POST',
                success: function(data, textStatus, XMLHttpRequest){
                    $('#generalModalContainer').html(data);
                }
            })
        });

        // handles the AJAX for the sign in form
        $('#signInFormModal').live('submit', null, function(event) {
            event.preventDefault();
            var form = $(this);
            var formData = form.serialize();
            $('#generalModalContainer').html($('#ajaxLoader').html());
            $.ajax({
                url: form.attr('action'),
                data: formData,
                type: 'POST',
                success: function(data, textStatus, XMLHttpRequest){
                    if (XMLHttpRequest.status == 204) {
                        $('#primaryNav').trigger('refresh');
                        $.ajax({
                            url: $('#regenHeaderLink').html(),
                            success: function(data, textStatus, XMLHttpRequest){
                                $('#contextSensitiveHeader').html(data);
                                $('#generalModalContainer').dialog('close');
                            }
                        })
                    } else {
                        $('#generalModalContainer').html(data);
                    }
                }
            })
        });

        // handles the AJAX for the registration form
        $('#sf_apply_apply_form_modal').live('submit', null, function(event) {
            event.preventDefault();
            var form = $(this);
            var formData = form.serialize();
            $('#generalModalContainer').html($('#ajaxLoader').html());
            $.ajax({
                url: form.attr('action'),
                data: formData,
                type: 'POST',
                success: function(data, textStatus, XMLHttpRequest){
                    if (XMLHttpRequest.status == 204) {
                        $('#primaryNav').trigger('refresh');
                        $.ajax({
                            url: $('#regenHeaderLink').html(),
                            success: function(data, textStatus, XMLHttpRequest){
                                $('#contextSensitiveHeader').html(data);
                                $('#generalModalContainer').dialog('close');
                            }
                        })
                    } else {
                        $('#generalModalContainer').html(data);
                    }
                }
            })
        });

        // handles the AJAX for the registration form
        $('#sf_apply_reset_request').live('submit', null, function(event) {
            event.preventDefault();
            var form = $(this);
            var formData = form.serialize();
            $('#generalModalContainer').html($('#ajaxLoader').html());
            $.ajax({
                url: form.attr('action'),
                data: formData,
                type: 'POST',
                success: function(data, textStatus, XMLHttpRequest){
                    $('#generalModalContainer').html(data);
                }
            })
        });

        
// glr - 20110623 has been replaced with cloudsponge based system

//        // handles the AJAX for notes to email system
//        $('#importAddressBookLink').live('click', null, function(event) {
//           event.preventDefault();
//           var a = $(this);
//           $.ajax({
//               url: a.attr('href'),
//               success: function(data, textStatus, XMLHttpRequest){
//                   $('#importAddressBookFormContainer').html(data);
//               }
//           })
//        });
//
//        // handles the AJAX for the import address book form
//        $('#importAddressBookForm').live('submit', null, function(event) {
//            event.preventDefault();
//            var form = $(this);
//            $.ajax({
//                url: form.attr('action'),
//                data: form.serialize(),
//                type: 'POST',
//                success: function(data, textStatus, XMLHttpRequest){
//                    $('#importAddressBookFormContainer').html(data);
//                }
//            })
//        });

        // handles the AJAX for the event notes form
        $('#eventNotesForm').live('submit', null, function(event) {
            event.preventDefault();
            var form = $(this);
            $.ajax({
                url: form.attr('action'),
                data: form.serialize(),
                type: 'POST',
                success: function(data, textStatus, XMLHttpRequest){
                    $('#eventNotesFormAjaxContainer').html(data);
                }
            })
        });
        
        // handles the AJAX for the event settings form
        $('#eventSettingsForm').live('submit', null, function(event) {
            event.preventDefault();
            var form = $(this);
            $.ajax({
                url: form.attr('action'),
                data: form.serialize(),
                type: 'POST',
                success: function(data, textStatus, XMLHttpRequest){
                    $('#eventSettingsFormResults').html(data);
                }
            })
        });
        
    });

})(jQuery);

