/*
Copyright (c) 2008 Nexedi SA and Contributors. All Rights Reserved.

This program is Free Software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/


function selectArrayElement(array){
  // Select one random element into the array.
  return array[Math.floor(((array.length))*Math.random())];
}

function replaceAdvertisementContent(meta) {
  var span_message = getElementsByTagAndClassName('span', 'second')[0];
  var gadget_ad = getFirstElementByTagAndClassName('div', 'gadget_advertisement');
  var gadget_doc = getFirstElementByTagAndClassName('div', 'gadget_documentation');
  var site_url = 'http://www.tiolibre.com';

  /* Everything point to front page */
  if (span_message) {
    div = document.createElement('div');
    div.className = 'ads_context_bar';
    a = document.createElement('a')
    ads = selectArrayElement(meta.message)
    a.href = site_url + "/view?erp5_site_selected_tab=express_support_tab&jumptab=1&reference=" + ads.url;
    a.innerHTML = ads.content;
    setStyle(div, {'font-size': '14px', 'margin-right': '5px', 'margin-top': '5px' });
    div.appendChild(a); 
    span_message.insertBefore(div, span_message.firstChild);
  }

  if (gadget_ad) {
    var ads = selectArrayElement(meta.gadget);
    if ((ads.url == "null") || (ads.url == null)) {
      gadget_ad.innerHTML = ads.content;
     } else {
      a = document.createElement('a')
      a.href = site_url + "/view?erp5_site_selected_tab=express_support_tab&jumptab=1&reference=" + ads.url;
      a.innerHTML = ads.content;
      gadget_ad.appendChild(a);
    }
  }

  if (gadget_doc) {
    gadget_doc.innerHTML = selectArrayElement(meta.documentation).content;
  }
}

var loadAdvertisement = function () { 
  site_url = "http://www.tiolibre.com/portal_wizard/proxy/nexedi";
  

  var span_message = getElementsByTagAndClassName('span', 'second')[0];
  var gadget = getElement("gadget_area_wrapper");

  gadget_type = ""
  if (span_message) { gadget_type +=  "&type_list=message" };
  if (gadget) { gadget_type +=  "&type_list=gadget&type_list=documentation" };

  static_url_part = "/web_site_module/express_frame/advertisement_section/WebSection_getTextContentAsJSON?language="
  select_language = document.getElementsByName("select_language")
  options = select_language[0].getElementsByTagName("option")
  language = null;
  for (index in options) {
    if (options[index].selected == true) {
      language = options[index].value;
      break;
    }
  }

  // Getting only the first and second character
  language = language[0] + language[1]
  if (span_message || gadget) {
    var d = loadJSONDoc(site_url+static_url_part+language+gadget_type);
    d.addCallbacks(replaceAdvertisementContent)
  };
};

addLoadEvent(loadAdvertisement);
