
/* **********************************************************************
Code for user feedback functionality 

This uses the objects URL of the objectcontroller to get the overlay Form
and to submit the individual votes back to the server.
********************************************************************** */

/* We need the idw namespace to be set up and everything the feedback does goes
into the user_feedback sub-namespace. */
idw.user_feedback = {};
idw.user_feedback.question_stars = {};
idw.user_feedback.question_star_response = {};


idw.user_feedback.show_user_feedback_panel = function(document_type, document_id, lang)
{    /* Remember the information of this document for the vote submission */
    idw.user_feedback.document_type = document_type;
    idw.user_feedback.document_id = document_id;
    idw.user_feedback.lang = lang;
    /* Prepare the request and request the form from the server */
    var ud_dict = {'document_type': document_type,  'document_id': document_id, 'lang': lang};
    $.post("/objects/" + 'feedback' + "/get_form", JSON.stringify(ud_dict), idw.user_feedback.update_user_feedback_panel_callback, "json");
};


/* **********************************************************************
Callback that displays the form in the YUI Dialog panel 
This is also used to set up the path to the star images (needed on submission)
********************************************************************** */
idw.user_feedback.update_user_feedback_panel_callback = function(data_in, status)
{
    var data = data_in['data'];
    if ('empty_star_path' in data)
    {
        idw.user_feedback.empty_star_path = data.empty_star_path;
    }
    else
    { /* If for some reason we should not habe the correct response,
         put something sensible in as default. */
        idw.user_feedback.empty_star_path = "/images/stars/GrayStar.png";
    }
    if ('full_star_path' in data)
    {
        idw.user_feedback.full_star_path = data.full_star_path;
    }
    else
    {
        idw.user_feedback.empty_star_path = "/images/stars/OrangeStar.png";
    }
    if ('overlay_form' in data) 
    { 
	 if (idw.lang=='de_DE') 
      {
        var dialogtitle="Ihre Bewertung";
      }
    else
      {
        var dialogtitle="Your vote";
      }
	/* This replaced the 'please wait' in the dialog with the form. */
	$("#reportuserfeedback").html(data.overlay_form);
	$("#reportuserfeedback").dialog({ minWidth: 500, minHeight: 300, title: dialogtitle, buttons: {
		"Ok": function(){
			$(this).dialog("close");
		}
	}});
	
      //  idw.user_feedback.overlay.setBody(data.overlay_form);
        /* This next block sets up convenience handlers to the stars. */
        for (var qid=1; qid<=3; qid++)
          {
            idw.user_feedback.question_stars[qid] = {};
            for (var s=1; s<=5; s++)
              {
                idw.user_feedback.question_stars[qid][s] = document.getElementById('idw_uf_star_' + qid + '_' + s);
              }
            idw.user_feedback.question_star_response[qid] = document.getElementById('idw_uf_star_result_' + qid);
          }
    };
};

/* This Function is called by the 'onclick' handler of the stars. 
It updates the stars (change color) and send the vote to the server. */
idw.user_feedback.rate = function(question_id, vote)
{
    /* Iterate 'out' star line and set either full or empty stars, 
       depending on the vote. */
    for (var i = 1; i<=5; i++)
      {
        if (i <= vote)
          {
            idw.user_feedback.question_stars[question_id][i].src= idw.user_feedback.full_star_path;
			if (question_id==1) document.getElementById('idw_uf_starm_' + question_id + '_' + i).src=idw.user_feedback.full_star_path;
          }
        else
          {
            idw.user_feedback.question_stars[question_id][i].src= idw.user_feedback.empty_star_path;
			if (question_id==1) document.getElementById('idw_uf_starm_' + question_id + '_' + i).src=idw.user_feedback.empty_star_path;
          };
      };
    /* Send the vote to the server */  
    var ud_dict = {'document_type': idw.user_feedback.document_type,  
                   'document_id': idw.user_feedback.document_id, 
                   'lang': idw.user_feedback.lang, 
                   'question_id': question_id, 
                   'vote': vote};
    $.post("/objects/" + 'feedback' + "/update_field_data", JSON.stringify(ud_dict), idw.user_feedback.update_user_feedback_rating_callback, "json");
};

/* This function is called by the onchange handler of the text area.
It sends the comment to the server. */
idw.user_feedback.setcomment = function(question_id, comment_id)
{
    comment_value = document.getElementById(comment_id).value;
    var ud_dict = {'document_type': idw.user_feedback.document_type,  
                   'document_id': idw.user_feedback.document_id, 
                   'lang': idw.user_feedback.lang, 
                   'question_id': question_id, 
                   'comment': comment_value};
    $.post("/objects/" + 'feedback' + "/update_field_data", JSON.stringify(ud_dict), idw.user_feedback.update_user_feedback_rating_callback, "json");
};

/* Callback for the vote and comment server requests. Does nothing currently. */
idw.user_feedback.update_user_feedback_rating_callback = function(data_in, status)
{
    var data = data_in['data'];
    if (('response_string' in data) && ('question_id' in data)) 
      {
        qid = data.question_id;
        idw.user_feedback.question_star_response[qid].innerHTML = data.response_string;
      }
};

/* Callback for the vote and comment server requests. */
idw.user_feedback.closewindow = function()
{
  idw.user_feedback.overlay.hide();
  location.reload();  
};

