/**
 * @author jasse
 */

/**
 * http://jehiah.cz/archive/firing-javascript-events-properly
 * 
 * @param {Object} element
 * @param {Object} event
 */
function fireEvent(element,event){
    if (document.createEventObject){
        // dispatch for IE
        var evt = document.createEventObject();
        return element.fireEvent('on'+event,evt)
    }
    else{
        // dispatch for firefox + others
        var evt = document.createEvent("HTMLEvents");
        evt.initEvent(event, true, true ); // event type,bubbling,cancelable
        return !element.dispatchEvent(evt);
    }
}
/**
 * Opens a confirm dialog
 * 
 * @param String title
 * @param String content
 * @param Object options
 */
var openConfirm = function(title, content) {
    
    var options = Object.extend({
        title:          title,

        closable:       true,
        destroyOnClose: true,

        width:          PrototypeWindowSettings.confirmDialog.width, 
        height:         PrototypeWindowSettings.confirmDialog.height,
        className:      PrototypeWindowSettings.theme,
        
        showEffect:     PrototypeWindowSettings.dialog.showEffect,
        hideEffect:     PrototypeWindowSettings.dialog.hideEffect,
        effectOptions:  PrototypeWindowSettings.effectOptions,
        
        okCallback:     null,
        cancelCallback: null,
        
    }, arguments[2] || {});
    
    Dialog.confirm(content, options);
}

$(document).observe('dom:loaded', function() {
    
    $$('a[rel=confirm]').each(function(link) {
        link.onclick = function() {
            openConfirm(link.getAttribute('title'), link.getAttribute('alt'), {
                onOk: function() { window.location.href = link.href; return true; } 
            });
            return false;
        };
    })
    
    $$('input[rel=confirm]').each(function(button) {
        var onClick = button.onclick;
        button.onclick = function() {
            openConfirm(button.getAttribute('title'), button.getAttribute('alt'), {
                onOk: function() { 
                    onClick(); 
                    return true; 
                } 
            });
            return false;
        };
    })
})

