//set up the compliment dialog

var show_compliment_dialog;
function setup_compliment_dialog() {
    //make sure the dialog data is ok
    var validate = function() {
        var txt = YAHOO.util.Dom.get('compliment-text').value;
        txt = YAHOO.lang.trim(txt);
        var validated = txt.length > 0; 
        return validated;
    };

    // Define various event handlers for Dialog
    var handle_submit = function(o) {
        if(validate()) {
            window.compliment_dialog.doSubmit();
            YAHOO.util.Dom.get('compliment-dialog-submit').disabled = true;
        } 
        else {
            show_flashing_message_cb('compliment-notext', 'Please type something', 'WARNING', handle_failure_complete);
        }
    }
    var handle_success_complete = function() {
        window.compliment_dialog.hide();
    }
    var handle_failure_complete = function() {
        hide_flashing_message('compliment-notext');
    }
    var handle_success = function(o) {
        show_flashing_message_cb('compliment-success', 'Message sent!', 'SUCCESS', handle_success_complete);
    };
    var handle_failure = function(o) {
        alert('Sorry, the server is not responding right now.');
    };
    show_compliment_dialog = function(click_target) {
        var xy = YAHOO.util.Dom.getXY(click_target);
        var click_target_id_parts = click_target.id.split('-');
        var entity_type = click_target_id_parts[2];
        var entity_id = click_target_id_parts[3];

        hide_flashing_message('compliment-notext');
        hide_flashing_message('compliment-success');
        YAHOO.util.Dom.get('compliment-dialog-submit').disabled = false;

        var txt = YAHOO.util.Dom.get('compliment-text');
        txt.value = ""; //clear the previous result
        YAHOO.util.Dom.get("compliment-dialog-entity-type").value = entity_type;
        YAHOO.util.Dom.get("compliment-dialog-entity-id").value = entity_id;
        window.compliment_dialog.show();
        YAHOO.util.Dom.setXY(YAHOO.util.Dom.get('compliment-wrapper'), xy);
    }
    // Instantiate the Dialog
    window.compliment_dialog = new YAHOO.widget.Dialog('compliment-wrapper', 
            { width : '300px',
              draggable : false,
              fixedcenter : false,
              visible : false, 
              underlay : 'none',
              zIndex : 1000,
              constraintoviewport : true
             } );
    
    //override submission so that we can display the result message
    window.compliment_dialog.submit = handle_submit;    

    // Wire up the success and failure handlers
    window.compliment_dialog.callback = {
                                         success: handle_success,
                                         failure: handle_failure };
    
    // Render the Dialog
    window.compliment_dialog.render();
    YAHOO.util.Dom.setStyle('compliment-wrapper', 'display', 'block');
}
YAHOO.util.Event.onDOMReady(setup_compliment_dialog);
