var RssCategory = {

  // NB: /shop/kampagne_dk-180c1.rss, in each language!

  maxChars: 20, // How many characters (max) should be used (if more, words will be removed)
  _divClassName: '', // Name of the class ini which to insert the resulting html
  _xmlDoc: null, // Initialized xmlDoc
  _loadXml: null, // Will contain a function to load XML string into the xml Doc
  _items: [], // Parsed items

  insert: function (url, divClassName) {
    
    var self = RssCategory;

    self._divClassName = divClassName;

    self._initXmlDoc();

    new Ajax.Request(url, {
      method: 'get',
      onSuccess: function (transport) {
       
        var xmlString = transport.responseText;
        xmlString = xmlString.replace(/(Price|Preis|Hinta|Vores pris|Pris |Pris|Prix|Prix  ):.*?([0-9\.\,]+).(DKK|NOK|EUR|SEK)/g, "Vores pris: $2 DKK"); // Remove strange invisible character (spoils IE parser)

        self._loadXml(xmlString);
        self._parse();
        self._insertItems();
      }
    }
		);

  },

  _initXmlDoc: function () { // Initializes 

    var self = RssCategory;

    if (typeof DOMParser != "undefined") { // Most browsers
      self._xmlDoc = new DOMParser();
      self._loadXml = function (xmlString) { self._xmlDoc = self._xmlDoc.parseFromString(xmlString, 'text/xml') };
    }
    else if (window.ActiveXObject) { // IE
      self._xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
      self._xmlDoc.async = false;
      self._loadXml = function (xmlString) { self._xmlDoc.loadXML(xmlString) };
    }
    else { // Not compatible
      // What to do?
    }

  },

  _parse: function () { // Parse the rss document and insert items into items
    var self = RssCategory;

    var root = self._xmlDoc.getElementsByTagName('rss')[0]; // Root element

    var channels = root.getElementsByTagName("channel"); // Channel element
    var items = channels[0].getElementsByTagName("item"); // All items

    for (var i = 0; i < items.length; i++) { // Create item objects
      var item = {};

      item.title = self._childTag(items[i], "title");
      item.description = self._childTag(items[i], "description");
      item.link = self._childTag(items[i], "link");
      // item.link = item.link.replace(new RegExp('^http://.*?/'), '/'); // Turn into absolute link

      self._items.push(item);
    }

  },

  _childTag: function (node, tagName) { // Utility function to get content of a child tag 
    var value = '';
    var c = node.getElementsByTagName(tagName)[0];

    for (var i = 0; i < c.childNodes.length; i++) {
      value += c.childNodes[i].nodeValue;
    }

    return value;

  },

  _insertItems: function () { // Insert each of the items into the document html
    var self = RssCategory;

    var html = '';

    for (var i = 0; i < self._items.length; i++) {
      html += self._item(self._items[i]);
    }

    document.getElementById(self._divClassName).innerHTML = html;

  },

  _item: function (item) { // Returns item html from item object 
    var self = RssCategory;

    // Titles
    var titles = item.title.split(" ");
    var title1 = titles.shift();
    var title2 = titles.join(" ");

    title2 = title2.replace(/^[\s\-\/]*(\S+\s+([\-\/]\s+)?\S+).*$/, "$1");

    if (title2.length > self.maxChars) { // Cut off words until 
      var title2New = title2;

      while (title2New.length > self.maxChars) {
        title2New = title2New.replace(/(^|\s+([\-\/]\s+)?)\S+$/, "");
      }

      if (title2 != "") {
        title2 = title2New;
      }

    }


    // Description + price
    var description = '';
    var price = '';

    var matches = item.description.match(/(<a.*?<\/a>)/);
    if (matches) {
      description = matches[1];
    }

    var matches = item.description.match(/Vores pris:.*?([0-9\.\,]+)/);
    if (matches) {
      price = matches[1];
      /*
      price = price.replace(',', '*');
      price = price.replace('.', ',');
      price = price.replace('*', '.');
      */
    }

    // Link
    var link = item.link

    var html =
			'<div class="prwrap">' +
			'<div class="prtopnavn">' + title1 + '</div>' +
			'<div class="prsubnavne">' + title2 + '</div>' +
			'<div class="prwrappix">' + description + '</div>' +
			'<div class="prpris">' + price + '</div>' +
    // '<div class="prsparpris">spar 100,00</div>' +
			'<a href="' + link + '"><span class="prbestil"></span></a>' +
			'</div>';


    return html;

  }


};
