/**
 * 
 * Author : Julien Van Den Bossche 
 *
 */  
 
 ToolTip = {
     show : function (e,desc){
      if(document.all)e = event;
      if (e.target) source = e.target;
		  else if (e.srcElement) source = e.srcElement;
      if (source.nodeType == 3)	source = source.parentNode;
      var noeudDiv = source;
      while(noeudDiv.tagName!="H3"){
        noeudDiv = noeudDiv.parentNode;
      }
      
       while(noeudDiv.tagName!="DIV"){
        noeudDiv = noeudDiv.nextSibling;
      }
      if(!document.getElementById("toolTip")){
        noeud = document.createElement("DIV");
        noeud.id="toolTip";
        document.body.appendChild(noeud);
      }
      else{
        noeud = document.getElementById("toolTip")
      }
      
      var divContenu = noeudDiv;
      noeud.innerHTML = (noeudDiv.firstChild)?noeudDiv.firstChild.nodeValue:"...";
      
      var st = Math.max(document.body.scrollTop,document.documentElement.scrollTop);
      var leftPos = e.clientX - 100;
      if(leftPos<0)leftPos = 0;
      noeud.style.left = leftPos + 'px';
      noeud.style.top = e.clientY + st + 20 + 'px'; 
      noeud.style.position="absolute"; 
      noeud.style.display="block";
      noeud.className = "toolTip";
      noeud.style.overflow="hidden";
     
    },
        
    hide : function(e){
      noeud = document.getElementById("toolTip");
      noeud.style.display="none";
    }
  }
 
 
 
 
String.prototype.parseJSON=function(){try{return eval('('+this+')');}catch(e){}
throw new SyntaxError("parseJSON");
};

RssReader = function(id, url, maxItems, title, favicon){
  this.baseHost = 'http://julienvdb.com/srcofk/rssReader';
  this.maxItems = maxItems;
  this.params = {
    images : {
      iconify : this.baseHost+'/skins/grey/images/masque_icon.gif',
      expand : this.baseHost+'/skins/grey/images/masque_icon.gif',
      refresh : this.baseHost+'/skins/grey/images/masque_icon.gif',
      close : this.baseHost+'/skins/grey/images/masque_icon.gif',
      defaultIco : this.baseHost+'/skins/grey/images/masque_icon.gif'
    }
  };
  
  this.id = id; 
  this.url = url;
  this.title = title;
  this.favicon = this.params.images.defaultIco;
  if(favicon != "")  this.favicon = favicon;
  
  this.refresh = function(){
    this.loadRss();
  }
  
  this.setTitle = function(title){
    document.getElementById('entete_txt_'+this.id).innerHTML = title;
  }
  
  this.setIcon = function(favicon){
    document.getElementById('box_Ico_'+this.id).src = favicon;
  }


  this.loadRss = function(){
    var a = new Ajax({module : this});
    a.sendData('../rssReader/rss/rssJson.php', 'urlfeed='+encodeURIComponent(this.url), 'GET', {onsuccess : function(xhr){
      var json = xhr.xhrObject.responseText.parseJSON();
      if(json.state != "done"){
        alert("Error while loading feed");
      }
      else{
        xhr.params.module.displayFeed(json);
      }
    }, onfailure : function (xhr){alert("Error while loading feed");}});
  }
 

  this.displayFeed = function(json){
    this.title = json.title;
    if(this.favicon == this.params.images.defaultIco) this.favicon = json.favicon;
    this.setTitle(this.title);
    this.setIcon(this.favicon);
    var items = json.items;
    var n = items.length;
    if(this.maxItems!="") n = this.maxItems;
    for(var i=0; i<n; i++){
      var item = items[i];
      var title = item.title;
      var desc = item.desc;
      var t = document.createElement("h3");
      var a = document.createElement("a");
      a.href = item.url;
      a.alt = "Voir l'article";
      a.title = "Voir l'article";
      a.target = "_blank";
      a.innerHTML = title;
      t.appendChild(a);
      t.onmouseover=ToolTip.show;
      t.onmouseout=ToolTip.hide;
      var desc = document.createElement("div");
      desc.style.display = "none";
      desc.innerHTML = item.desc;
      document.getElementById("wrapper_"+this.id).appendChild(t);
      document.getElementById("wrapper_"+this.id).appendChild(desc);
    }
  }


  this.init = function(){
    var head = document.getElementsByTagName('head').item(0);
    var link = document.createElement("link");
    link.rel = "stylesheet";
    link.id = "cssbox";
    link.type = "text/css";
    link.href = this.baseHost+"/skins/grey/css/box.css";
    head.appendChild(link);
  
    
 
   
  var content = '<div id="box_'+this.id+'" class="box" style="width:99%">'+
  '<b class="xtop"><b class="xb1top"></b><b class="xb2top"></b><b class="xb3top"></b><b class="xb4top"></b></b>'+
  '<div id="entete"   class="entete">'+
  '<img title="Rafraichir ce flux" onclick="rssRefresh('+this.id+');" class="imageRefresh" id="box_RefreshSource" src="'+this.params.images.refresh+'"/>'+
   '<img style="overflow: hidden; float: left; vertical-align: middle; cursor: pointer; margin-left: 2px; margin-right: 5px; width: 16px; height: 16px;" alt=" " src="'+this.favicon+'" id="box_Ico_'+this.id+'"/>'+
  '<span style="overflow: hidden; float:left;text-align:left; vertical-align: middle;" id="entete_txt_'+this.id+'">'+this.title+'</span></div>'+
  '<div id="box_content" class="box_content">'+
  '<div class="wrapper" id="wrapper_'+this.id+'"></div></div>'+
  '<div id="box_StatusBar1" class="box_StatusBar">&nbsp;</div><b class="xbottom"><b class="xb4bottom"></b><b class="xb3bottom"></b><b class="xb2bottom"></b><b class="xb1bottom"></b></b></div>';
  
  document.write(content);
  this.loadRss();
  }
} 
