/**
 * Shows a dialog for the contact email on top of the page and
 * attaches the ajaxForm to the form within. On success, it will
 * change to a success callback.
 *
 * @param options.title The title for the dialog window
 * @param options.action The response action on form submission
 */
function majistiw_dialog_contact(options)
{
    $("a[id^='contact']").click(function() {
        $('#dialog-email').dialog({
            autoOpen: false,
            width: '500px',
            title: options.title,
            modal: true,
            dialogClass: 'dialog'
        }).dialog('open');
    });

    /* boolean flag indicating wheither or not the form has been loaded */
    hasLoaded = false;

    $('#dialog-email').bind('dialogopen', function() {
       /* only load form once and display same form afterward */
       if( hasLoaded ) {
           return;
       }
       $.ajax({
            url: majisti.app.baseUrl + '/index/email-captcha',
            dataType: 'json',
            success: function(response) {
                hasLoaded = true;

                /* loading invisible div with response data */
                $('#dialog-email').html(response.content)

                /* building jQuery object with freshly loaded form */
                $form = $('#dialogEmailForm');

                /* applying ajax form processing script */
                $form.ajaxForm({
                    action: options.action,
                    successCallback: function(response) {
                        $form.remove();
                        $('#dialog-email').html(response)
                    }
                });

                $('#dialogEmailForm .captcha input:last-child').focus();
            }
        });
    });
}

/* Configure AJAX form located on the contact page */
$(function(){
    $('#yourProject').ajaxForm({
        successCallback: function(data) {
            $('#yourProject').remove();
            $('td.contact-form-intro').remove();
            $('<div/>', {
                id: 'contact-success',
                html: data
            }).appendTo('#contact');
        }
    });

    $('#yourProject textarea').charCounter(150, {
        container: "<div></div>",
        classname: "counter",
        format: "%1",
        pulse: true,
        delay: 100
    });
});

/* on dom ready custom elements */
$(function() {
   /* bind fancybox on all rel links */
   $('a[rel=fancybox]').fancybox();
});

