/*
 * JavaScript for a the popup class and functions to show the reporting popup
 *
 * Dependencies:
 *    reporter.css
 *    jquery-latest.js
 *    thickbox.js
 *    thickbox.css
 */

var ReporterPopup = Class.create()

ReporterPopup.prototype = {

  initialize: function(click_obj, logged_in, is_owner, keys, values, article_guid, article_class, owner_photo, is_user)
  {
    if (document.body.reporter) document.body.reporter.close();
  
    this.options = {
      categories: keys.length,
      keys: keys,
      values: values,
      is_user: is_user
    };
    
    if ($("body #report_popup").length == 0)
      this.create_dom();

    document.body.sv_reportpopup = this;

    
    // update dynamic components
    $("#report_success").hide();
    $("#report_sending").hide();    
    $("#report_content").show();
    
    $("#report_content #report_key_list").html("");
    for (i=0;i<this.options.categories;i++)
    {
      html = '<li><input type="radio" name="category" id="'+this.options.values[i]+'" value="'+this.options.values[i]+'"> <label for="'+this.options.values[i]+'">'+this.options.keys[i]+'</label></li>';
      $("#report_content #report_key_list").append(html);
    }

    if (!logged_in || is_owner) {
      $("#report_content #block").hide();
    } else {
      $("#report_content #block").show();    
    }

    $("#report_content #_article_guid").val(article_guid);
    $("#report_content #_article_class").val(article_class);

    $("#report_content #submit").click(function() {document.body.sv_reportpopup.submit_report(); return false;});
    $("#report_content #cancel").click(function() {document.body.sv_reportpopup.close(); return false;});
    
    if (owner_photo != '')
    {
      $("#owner_img").html('<img src="' + owner_photo + '" alt="" />');
      $("#owner_img").show();
    }
    else
    {
      $("#owner_img").hide();
    }
  },

  
  show: function()
  {
    tb_show('<h3>Flag As Inappropriate</h3>', '#TB_inline?height=255&width=420&inlineId=report_popup&overlayBG=lt', null);
  },

    
  create_dom: function()
  {
    var html = '';
    
    html += '<div id="report_popup" style="display:none;">';
    html += '    <div class="popup_content" id="report_content">';
    if (this.options.is_user) {
      html += '      <h4>Please indicate the reason you are reporting this user:</h4>';
    } else {
      html += '      <h4>Please select the reason you are flagging this content as inappropriate.</h4>';
    }
    html += '      <div id="owner_img" style="display: none;"></div>';
    html += '      <form method="post" id="report_form" action="/content_flag/report">';
    html += '        <ul id="report_key_list" style="list-style: none;"></ul>'    
    html += '        <input id="_article_guid" name="article_guid" type="hidden" />';
    html += '        <input id="_article_class" name="article_class" type="hidden" />';
    html += '        <div class="links">';
    html += '          <a href="#" class="submit" id="submit">Submit</a> | <a href="#" class="cancel" id="cancel" >Cancel</a>';
    html += '          <span id="block"><input type="checkbox" name="user_block" id="r_block" /> <label for="r_block">Block this user</label></span>';
    html += '        </div>';
    html += '      </form>';
    if (this.options.is_user) {
      html += '      <div id="report_footer">Tip: Instead of reporting this user, flag the individual post, comment or photo in question so we can confirm inappropriate behavior.</div>';
    }
    html += '    </div>';
    html += '    <div id="report_sending" style="display:none;">';
    html += '      <h4>Sending...</h4>';
    html += '    </div>';
    html += '    <div id="report_success" style="display:none;">';
    html += '      <p>Thank you!! We will review the reported content. We look at each and every submission and promise to process them within 24 to 72 hours.</p><p>Thanks for helping make Snapvine a better place for everyone.</p>';
    html += '      <p><a href="#" onclick="document.body.sv_reportpopup.close();return false;">close</a></p>';
    html += '    </div>';
    html += '    <div id="report_failure_generic" style="display:none;">';
    html += '      <h4></h4>';
    html += '      <p><a href="#" onclick="$(\'#report_failure_generic\').hide();$(\'#report_content\').show();return false;">Go back</a></p>';
    html += '    </div>';
    html += '</div>';

    $("body").append(html);
  },

  submit_report: function()
  {
      $("#report_content").hide();
      $("#report_sending").show();
      
      new Ajax.Request('/content_flag/report',
                       { asynchronous:true,
                               evalScripts:true,
                               onSuccess:function(request) {
                                 $("#report_sending").hide();
                                 if (request.responseText=="success") {
                                   $("#report_success").show();
                                 } else {
                                   document.body.sv_reportpopup.display_error_message(request.responseText);
                                 }
                               },
                               onFailure:function(request) {},
                               parameters:Form.serialize(document.getElementById('report_form')) });
  },
  
  display_error_message: function(message)
  {
      $("#report_failure_generic").show();
      $("#report_failure_generic h4").get(0).innerHTML = message;
  },
  
  close: function(index)
  {
    tb_remove();
  }
};



/*
 * show share popup
 *
 *  click_obj        object: click source
 *  logged_in        boolean: is user logged in?
 *  article_guid     string
 *  article_class    string
 *  keys             array
 *  values           array
 */
function reporter_on(click_obj, logged_in, is_owner, article_guid, article_class, owner_photo, keys, values, is_user)
{
    var p = new ReporterPopup(click_obj, logged_in, is_owner, keys, values, article_guid, article_class, owner_photo, is_user);
    p.show();
  
    return false;
}

