/*global document jQuery*/
;(function($) {

$.dialog = {};

// find the dialog $element on the page
var $element, $buttonspace;
$(document).ready(function() {
    $element = $('#fd_dialog').hide();
});

// on hover styles
function onOver() {
    $element.addClass('over');
}
function onLeave() {
    $element.removeClass('over');
}

function buildDialog() {
    $('#fd_dialog div.dialog_container').remove();
    var content = [
        '<div class="dialog_container">',
        '<label></label>',
        '<p></p>',
        '<div class="buttonspace"></div>',
        '<div class="clear"></div></div>'
    ];
    ($element.hide()
        .hover(onOver, onLeave)
        .append(content.join(""))
    );
}


// buttons
function addButton(button, click_func) {
    ($element.find('div.buttonspace')
        .append( $('<input/>') 
            .attr({
                type: "button",
                id: button+'but'
            })
            .addClass("button_grey")
            .val(button)
            .click(function() { 
                try { 
                    click_func();
                } catch(e) { 
                    if( e instanceof $.dialog.Error ) { 
                        return;
                    }
                    throw e;
                }
                $.dialog.close();
            })
        )
    );
}
function clearButtons() {
    $('.buttonspace', $element).empty();
}

$.dialog.Error = function() {};

// open & close the dialogs 
$.dialog.open = function(label, message, buttons, configurer) {
    buildDialog();
    clearButtons();
    $.each(buttons, function(caption, func){ 
        addButton(caption, func);
    });
    $('.dialog_container p', $element).html(message);
    $('.dialog_container label', $element).html(label);
    // hack in a function to allow extending the dialog more 
    if( configurer ) { configurer($element); }
    $('#overlay').show();
    $element.fadeIn(1000, function() {
        $element.find('input:first').focus();
    });
};
$.dialog.close = function() {
    $('#overlay').hide();
    $element.fadeOut(200).removeClass('over');
};

// The actual dialog functions
$.dialog.ok = function(message, label) { 
    if( !label ) { label='Yo'; }
    var buttons = {
        "Ok": function(){}
    };
    $.dialog.open(label, message, buttons); 
};

$.dialog.yesno = function(message, success_func, label) { 
    if( !label ) { label='Confirm'; }
    var buttons = {
        "Yes": function(){ success_func(); },
        "No": function(){}
    };
    $.dialog.open(label, message, buttons); 
};

$.dialog.yesno2 = function(message, success_func, failure_func, label) {
    if( !label ) { label='Confirm'; }
    var buttons = {
        "Yes": function() { success_func(); },
        "No": function() { failure_func(); }
    };
    $.dialog.open(label, message, buttons); 
};

$.dialog.prompt = function(message, options) {
    options = options || {};
    options.ok_text = options.ok_text || "Ok";
    options.cancel_text = options.cancel_text || "Cancel";
    options.val = (options.val === undefined ? '' : options.val);
    options.label = options.label || "Prompt";
    var $input = null;
    var buttons = {};
    buttons[options.ok_text] = function() {
        if( options.success ) { 
            options.success($input.val());
        }   
    };
    buttons[options.cancel_text] = function() {
        if( options.cancel ) {
            options.cancel();
        }
    };
    $.dialog.open(options.label, message, buttons, function($element) {
        $input = ($('<input type="text"/>')
            .val(options.val)
            .appendTo($element.find('p'))
        );
        if( options.input_tail ) { 
            $('<span>'+options.input_tail+'</span>').insertAfter($input);
        }
    });
};

})(jQuery);
