// $Id: save_descriptors.js 1118 2010-05-03 16:48:29Z nbt $
// nbt, 13.4.2010

"use strict";

/*global YAHOO document window navigator*/

YAHOO.namespace('zbw.stw');
(YAHOO.zbw.stw.save_descriptors = function () {

  var panel;

  function build_econis_search_url(operator) {
    var url, cookie_content, op, i, uri, pref_label;

    if (operator === 'or') {
      op = '%2B';
    }
    if (operator === 'and') {
      op = '*';
    }
    
    url = document.getElementById('econis_base').value;
    cookie_content = YAHOO.util.Cookie.getSubs("zbw-saved-descriptors");
    i = 0;
    for (uri in cookie_content) {
      pref_label = cookie_content[uri];
      if (i === 0) {
        url = url + "&ACT0=SRCH&IKT0=46&TRM0='" + pref_label + "'";
      } else {
        url = url + "&ACT" + i + "=" + op + "&IKT" + i + "=46&TRM" + i + "='" + pref_label + "'";
      }
      i = i + 1;
    }
    return url;
  }

  function build_google_search_url() {
    var GOOGLE_SEARCH_BASE, i, cookie_content, uri, search_url, label;
    
    GOOGLE_SEARCH_BASE = 'http://google.com/search?hl=de&q=';
    cookie_content = YAHOO.util.Cookie.getSubs("zbw-saved-descriptors");

    // for each saved descriptor
    i = 0;
    for (uri in cookie_content) {
      label = cookie_content[uri];
      if (i === 0) {
        search_url = GOOGLE_SEARCH_BASE + "'" + label + "'";
      } else {
        search_url = search_url + " '" + label + "'";
      }
      i = i + 1;
    }
    return search_url;
  }

  function show_saved_descriptors() {
    var cookie_content, result_list, uri, pref_label, node, result;

    cookie_content = YAHOO.util.Cookie.getSubs("zbw-saved-descriptors");
    
    if (cookie_content) {
      result_list = document.createElement('ul');
      for (uri in cookie_content) {
        pref_label = cookie_content[uri];
        node = document.createElement('li');
        node.innerHTML = pref_label;
        result_list.appendChild(node);
      }
      result = document.getElementById('result');
      if (result.firstChild) {
        result.removeChild(result.firstChild);
      }
      result.appendChild(result_list);

      // set Econis link
      document.getElementById('search_econis_and_link').setAttribute('href', build_econis_search_url('and'));
      document.getElementById('search_econis_or_link').setAttribute('href', build_econis_search_url('or'));
      document.getElementById('search_google_link').setAttribute('href', build_google_search_url());

      // Instantiate a Panel from markup
      panel = new YAHOO.widget.Panel("saved_descriptors", { width: "170px", constraintoviewport: true, context: ['yui-main', 'tr', 'tr'] });
      panel.cfg.setProperty('draggable', 'true');
      panel.render();
    }
  }
  
  function add_current_descriptor() {
    var uri, pref_label;

    // read RDFa attributes
    uri = YAHOO.util.Selector.query("div[typeOf~='zbwext:Descriptor']", 
        'yui-main', true).getAttribute('about');
    // textContent doesn't work in MSIE 7
    pref_label = YAHOO.util.Selector.query("h1 span[property='skos:prefLabel']",
        'yui-main', true);
    pref_label = getTextContent(pref_label);

    // add as an subcookie (with uri as key)
    // (the cookie and all its sub-cookies are shared in path /stw)
    YAHOO.util.Cookie.setSub("zbw-saved-descriptors", uri, pref_label, {
      path: '/stw'
    });

    // create and show panel
    show_saved_descriptors();
  }

  function remove_all() {

    // remove cookie completely
    YAHOO.util.Cookie.remove("zbw-saved-descriptors", {
      path: '/stw'
    });
    
    panel.hide();
  }

  function init() {

    // does not work at all if cookies are disabled
    if (navigator.cookieEnabled) {
      var add_descriptor, remove_all_saved;

      // add event handler for adding a descriptor
      add_descriptor = document.getElementById('add_descriptor');
      //add_descriptor.addEventListener('click', add_current_descriptor, false);
      YAHOO.util.Event.addListener(add_descriptor, 'click', add_current_descriptor);
      add_descriptor.style.visibility = 'visible';

      // show saved descriptors when document loading has finished
      YAHOO.util.Event.addListener(window, "load", show_saved_descriptors);

      // remove all saved descriptors
      remove_all_saved = document.getElementById('remove_all_saved');
      YAHOO.util.Event.addListener(remove_all_saved, 'click', remove_all);
    }
  }

  // initialize after DOM loading has finished
  YAHOO.util.Event.onDOMReady(init);
  
  /**
  * Gets the text content of the specified element.
  * @param element {HTMLElement} The html element
  * @return {String} The string content of the specified element.
  * http://dandean.com/category/code/2009/hatin-on-textcontent-and-innertext/
  */
  function getTextContent(element) {
    if (typeof element.textContent != "undefined") {
      return element.textContent;
    }
    return element.innerText;
  }

}());

