// $id: Node Translation Javascript

Drupal.node_translation = Drupal.node_translation || {};

if (Drupal.jsEnabled) {
  $(document).ready(function() {
    var nodeTranslationDialog = $('#node-translation-dialog');
    var nodeTranslationToggle = $('.node-translation-link');
    if (nodeTranslationDialog.size() > 0 && nodeTranslationToggle.size() > 0) {
      nodeTranslationDialog.jqm({trigger: nodeTranslationToggle, onShow: function(hash) {
        var href = hash.t.pathname;
        // MSIE for some reason strips off the first "/" in the pathname
        if ($.browser.msie === true) {
          href = "/" + href;
        }
        $.ajax({
          type: "GET",
          url: href,
          processData: false,
          dataType: "json",
          success: function(data, status) {
            try {
              if (data.length !== 'undefined' && data.length === 1) {
                data = data[0];
                $('#node-translation-content .title').html(data.title);
                $('#node-translation-content .data').html(data.body);
              } else {
                $('#node-translation-content .title').html(data[0].title);
                $('#node-translation-content .data').html(data[0].body);
                $('#node-translation-content .data').append(data[1].title);
                $('#node-translation-content .data').append(data[1].body);
              }
              // Setting the click for nodes that we wish to translate
              if ($('a.node-translation-link-translate').size() > 0) {
                $('a.node-translation-link-translate').click(function() {
                  Drupal.node_translation.nodeTranslate(this.href);
                  return false;
                });
              }
              // Setting the click for nodes that we wish to set the language for
              if ($('a.node-translation-link-language').size() > 0) {
                $('a.node-translation-link-language').click(function() {
                  Drupal.node_translation.nodeSetLanguage(this.href);
                  return false;
                });
              }
            } catch (e) {}
          },
          error: function(xhr, status, thrown) {
            // Do nothing but at least have this function to handle it
          }
        });
        $('html').addClass('modal');
        hash.w.css('opacity', 1).show();
      }, onHide: function(hash) {
        $('#node-translation-content .title').html('');
        $('#node-translation-content .data').html('');
        $('html').removeClass('modal');
        hash.w.css('opacity', 0).hide();
        hash.o.remove();
      }});
    }
  });
}

/**
 *
 */
Drupal.node_translation.nodeTranslate = function(url) {
  $.ajax({
    type: "GET",
    url: url,
    processData: false,
    dataType: "json",
    success: function(data, status) {
      try {
        var body = data.body + data.footer;
        $('#node-translation-content .title').html(data.title);
        $('#node-translation-content .data').html(body);
      } catch (e) {}
    },
    error: function(xhr, status, thrown) {
      // Do nothing but at least have this function to handle it
    }
  });
};

/**
 *
 */
Drupal.node_translation.nodeSetLanguage = function(url) {
  $.ajax({
    type: "GET",
    url: url,
    processData: false,
    dataType: "json",
    success: function(data, status) {
      try {
        $('#node-translation-content .title').html(data.title);
        $('#node-translation-content .data').html(data.body);
        // Setting the click action for the links returned by the system
        if ($('a.node-translation-link-translate').size() > 0) {
          $('a.node-translation-link-translate').click(function() {
            Drupal.node_translation.nodeTranslate(this.href);
            return false;
          });
        }
      } catch (e) {}
    },
    error: function(xhr, status, thrown) {
      // Do nothing but at least have this function to handle it
    }
  });
};
