NewsConfig = jsClass.makeClass ("NewsConfig");

NewsConfig.imageDirectory = "ressources/images/";
NewsConfig.codeFrame = "code";
NewsConfig.newsContainer = "text";
NewsConfig.newsDefFrame = "news";
NewsConfig.refreshRate = 4000;

NewsConfig.header = "";
NewsConfig.footer = "";

NewsConfig.getValue = function (name, def)
{
  var value = def;
  for (m in this)
    {
      if (m == name)
	{
	  value = this[m];
	  break;
	}
    }
  return value;
}

NewsConfig.setValue = function (name, value)
{
}


News = jsClass.makeClass ("News");
News.prototype.text = '';
News.prototype.header = '';
News.prototype.url = '';
News.prototype.target = '';
News.prototype.baseUrl = '';
News.prototype.target = 0;

News.prototype.News = function (header, text, url, target, index)
{
  this.text = text;
  this.header = header;
  this.url = url;
  this.target = target;
  this.index = index;
  this.baseUrl =
    location.href.substring (0, location.href.lastIndexOf ("/") + 1);
}

News.prototype.toHtml = function ()
{
  var tag = '';
  var ie = ((document.all) ? true : false);
  var dom = ((document.getElementById) && (!ie)) ? true : false;
  if (dom)
    tag += '<div id=op' + this.index + ' class="dom">';
  tag += '<div id=dh' + this.index + ' class="header">';
  tag += this.header;
  tag += '</div>';
  tag += '<div id=d' + this.index + ' class="text">';
  tag += '<a href="' + this.resolvedUrl(this.url) +'" ';
  if (this.target && this.target != '')
	tag += 'target="' + this.target +'"'
  tag += '>' + this.text + '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</a>';
  tag += '</div>';
  if (dom)
    tag += '</div>';
  return tag;
}


News.prototype.isAbsoluteUrl = function (url)
{
  var schemes =
    new Array ("http://", "https://", "mailto:", "ftp://", "telnet:",
	       "news:", "gopher:", "nntp:", "javascript:", "file:");

  for (var i = 0; i < schemes.length; ++i)
    if (url.indexOf (schemes[i]) == 0)
      return true;

  return false;
}

News.prototype.resolvedUrl = function ()
{
  var url = this.url;
  /* Resolve an URL against the current document URL. */
  if (this.isAbsoluteUrl (url))
    return url;

  if (url.indexOf ("/") == 0)
    {
      url = location.protocol + "//" + location.host + url;
      return url;
    }

  if (url.indexOf ("../") == 0 || url.indexOf ("./") == 0)
    {
      var base = this.baseUrl;
      while (1)
	{
	  if (url.indexOf ("../") == 0)
	    {
	      url = url.substr (3);
	      base =
		base.substr (0, base.lastIndexOf ("/", base.length - 2) + 1);
	    }
	  else if (url.indexOf ("./") == 0)
	    url = url.substr (2);
	  else
	    break;
	}
      return base + url;
    }

  return this.baseUrl + url;
}

/*************************************************
news scroller
**************************************************/

NewsScroller = jsClass.makeClass ("NewsScroller");

NewsScroller.visible = false;
NewsScroller.current_news = null;
NewsScroller.DisplayAttempt = 0;
NewsScroller.newsCategories = new Array ();
NewsScroller.useDOM = false

NewsScroller.onload = function (evt)
{
  if (jsNavigator.isMozilla ||
 	(jsNavigator.isNetscape && jsNavigator.major >= 5) ||
	(jsNavigator.isIE && jsNavigator.major >= 5) ||
	(jsNavigator.isOpera && jsNavigator.major >= 5))
	NewsScroller.useDOM = true;

  if (jsNavigator.isOpera && jsNavigator.major == 5)
    {
      parent.onload = NewsScroller.onload;
      if (evt)
	return;
    }

  var profile = "";
  var url = parent.document.URL;
  if (url.indexOf ("?") != -1)
    {
      url = url.split ("?");
      profile = url[url.length - 1];
      this.selectCategory (profile, false);
    }
  setTimeout('NewsScroller.loadDefs()',20);
  this.loaded = true;
}

NewsScroller.loadDefs = function()
{
	var defsdoc = null;
	for (var i = 0; i < parent.frames.length; ++i)
    {
      if (parent.frames[i].name == NewsConfig.getValue ("newsDefFrame"))
      {
		defsdoc = parent.frames[i].document;
	  }
	}
	if (defsdoc != null && NewsScroller.useDOM)
	{
		//categories are chapters
		//category name is h2
		//new def is row in table 
		var catObjs = defsdoc.getElementsByTagName("div");
		var i;
		var attrClass = (jsNavigator.isIE && jsNavigator.major >= 5)? "className" : "class";
		for (i = 0; i < catObjs.length; i++)
		{
			if( catObjs[i].getAttribute(attrClass) == "chapter")
			{
				var h2s = catObjs[i].getElementsByTagName("h2");
				if (h2s != null && h2s.length > 0)
				{
				        var childs = h2s[0].childNodes;
					if (childs != null && childs.length > 0)
					{
						var category = catObjs[i].getElementsByTagName("h2")[0].childNodes[0].nodeValue;
						var newsTableObj = catObjs[i].getElementsByTagName("table")
						var newdoc = new NewsDocument();
						if(newsTableObj)
						{	
							newdoc.name = category;
							newsTableObj = newsTableObj[0];
							for (var rowi = 0; rowi < newsTableObj.rows.length; rowi++)
							{
								var row = newsTableObj.rows[rowi];
								var header  = row.cells[0].innerHTML.replace(/<[^>]+>/g,"");
								var linkobj = row.cells[1].getElementsByTagName("a");
								if (linkobj)
								{
								    var href = linkobj[0].href;
								    var text = linkobj[0].innerHTML.replace(/<[^>]+>/g,"");
								    var target = linkobj[0].target;
								    newdoc.addNews(new News(header, text, href, target , rowi));
								}
							}
						}
						NewsScroller.newsCategories[NewsScroller.newsCategories.length] = newdoc;
					}
				}
			}
		}
	}
}


NewsScroller.display = function ()
{
	setTimeout ("NewsScroller._display()", 400);
}
NewsScroller._display = function ()
{
	if (this.currentNews ())
	{
		this.currentNews ().display ();
		NewsScroller.DisplayAttempt = 0;
		return;
	}
	else
	{
	    if (NewsScroller.DisplayAttempt > 1000)
	    {
	         NewsScroller.DisplayAttempt = 0;
	         return;
	    }
	    else
	    {
	        NewsScroller.DisplayAttempt ++;
	        NewsScroller.display();
	    }
	}
}

NewsScroller.makeNextNewsItem = function ()
{
	if (this.currentNews ())
		this.currentNews ().makeNextNewsItem ();
}

NewsScroller.nextNewsItem = function ()
{
	if (this.currentNews ())
		this.currentNews ().nextNewsItem ();
}

NewsScroller.hide = function ()
{
  //this.currentNews ().hide ();
}

NewsScroller.nameSelected="";
NewsScroller.displaySelected=false;

NewsScroller.selectCategory = function (name, display)
{ if (arguments.length < 2)
     display = false;

  NewsScroller.nameSelected = name;
  NewsScroller.displaySelected = display;
  if (this.newsCategories.length == 0)
      setTimeout('NewsScroller._selectCategory()',10);
  else
      NewsScroller._selectCategory();
}


NewsScroller._selectCategory = function ()
{
  var name= NewsScroller.nameSelected;
  var display = NewsScroller.displaySelected;
  var found = false;
  for (var i = 0; i < this.newsCategories.length; ++i)
    { 
      if (this.newsCategories[i].name.toLowerCase() == name.toLowerCase())
	{
	  this.current_news = this.newsCategories[i];
	  found = true;
	  break;
	}
    }
  if (display && found)
    NewsScroller.display ();
  else
    NewsScroller.hide ();
}

NewsScroller.currentNews = function ()
{
  return this.current_news ? this.current_news : this.newsCategories[0];
}

NewsScroller.prototype.NewsScroller = function ()
{
}	


NewsDocument = jsClass.makeClass ("NewsDocument");
NewsDocument.headlineElement =
  '<center><span id="headline"><div class="container"><div id="spage" class="spage"></div></div></span><center>';
NewsDocument.mdoc = null;
NewsDocument.doc = function ()
{
  if (this.mdoc == null)
    this.mdoc = new NewsDocument ();
  return this.mdoc;
}

NewsDocument.prototype.head = null;
NewsDocument.prototype.document = null;
NewsDocument.prototype.frames = null;
NewsDocument.prototype.newsContainer = null;
NewsDocument.prototype.initialized = false;
NewsDocument.prototype.useDOM = false;
NewsDocument.prototype.newsDef = new Array ();
NewsDocument.prototype.name = false;
NewsDocument.prototype.fisrtDisplay = false;
NewsDocument.prototype.newsIndex = 0;
NewsDocument.prototype.objst = new Array ();
NewsDocument.prototype.objs = new Array ();
NewsDocument.prototype.objsh = new Array ();

NewsDocument.prototype.NewsDocument = function ()
{
  this.newsDef = new Array ();
  this.objst = new Array ();
  this.objs = new Array ();
  this.objsh = new Array ();
  this.fisrtDisplay = true;
  if (jsNavigator.isMozilla ||
      (jsNavigator.isNetscape && jsNavigator.major >= 5) ||
      (jsNavigator.isIE && jsNavigator.major >= 5) ||
      (jsNavigator.isOpera && jsNavigator.major >= 5))
    this.useDOM = true;

  /* Walk the document frames */
  this.frames = new Array ();
  for (var i = 0; i < parent.frames.length; ++i)
    {
      this.frames.push (parent.frames[i].name);
      if (parent.frames[i].name == NewsConfig.getValue ("newsContainer"))
	{
	  this.newsContainer = parent.frames[i];
	  this.document = parent.frames[i].document;

	  if (this.useDOM)
	    this.head = jsNavigator.isIE ? this.document.all.tags ('head')[0]
	      : this.document.getElementsByTagName ('head').item (0);
	}
    }
}

NewsDocument.prototype.init = function ()
{
  if (this.useDOM && !this.initialized)
    this.initDOM ();
  else if (!this.initialized || !jsNavigator.isIE)
    {
      this.document.
	writeln
	('<div style="position:absolute;top:1;left:1;margin-bottom:30;border:1px solid #000000;margin:0px;overflow:hidden; height:20;width:500;"><div id="spage" class="spage"></div></div>');
      this.document.close ();
    }

  this.initiazed = true;
}


NewsDocument.prototype.initDOM = function ()
{
  if (jsNavigator.isIE)
    this.document.body.insertAdjacentHTML ("afterBegin",
					   NewsDocument.headlineElement);
  else
    {
      var range = this.document.createRange ();
      range.setStart (this.document.body, 0);
      var html = range.createContextualFragment (NewsDocument.headlineElement);
      this.document.body.insertBefore (html,this.document.body.firstChild);
    }
}


NewsDocument.prototype.addNews = function (news)
{
  
  this.newsDef[this.newsDef.length] = news;
}




NewsDocument.prototype.display = function ()
{
  this.init ();
  this.start ();
}


NewsDocument.prototype.nextNewsItem = function ()
{
  if (this.newsDef.length)
	{
      if (this.useDOM)
		{
			if (this.fisrtDisplay)
			{
				this.fisrtDisplay = false;
				this.objsh[this.newsIndex].style.visibility = 'visible';
				this.objs[this.newsIndex].style.visibility = 'visible';
				if (this.objst.length && this.objst[this.newsIndex])
				{
					this.objst[this.newsIndex].style.visibility = 'visible';
				}
			}
			else
			{
				var prevent = this.newsIndex;
				this.newsIndex++;
				if (this.newsIndex >= this.newsDef.length)
					this.newsIndex = 0
				
				this.objsh[prevent].style.visibility = 'hidden';
				this.objs[prevent].style.visibility = 'hidden';
				if (this.objst.length && this.objst[this.newsIndex])
				{
					this.objst[prevent].style.visibility = 'hidden';
				}
				
				if (jsNavigator.isIE && jsNavigator.major > 5)
					this.document.all.spage.filters[0].apply ();
					
				this.objsh[this.newsIndex].style.visibility = 'visible';
				this.objs[this.newsIndex].style.visibility = 'visible';
				if (this.objst.length && this.objst[this.newsIndex])
				{
					this.objst[this.newsIndex].style.visibility = 'visible';
				}
				
				if (jsNavigator.isIE && jsNavigator.major > 5)
					this.document.all.spage.filters[0].play ();	
			}
		}
      else
	{
	  this.document.all.spage.innerHTML = this.newsDef[this.newsIndex].toHtml();
	}
	setTimeout ('NewsScroller.nextNewsItem()', NewsConfig.getValue ("refreshRate"));
    }

}

NewsDocument.prototype.makeNextNewsItem = function ()
{
  var i = 0;
  var str = "";
  for (i = 0; i < this.newsDef.length; i++)
    {
      str = "d" + i;
      this.objs[i] = this.document.getElementById (str);
      this.objs[i].style.visibility = (i==0)?'visible':'hidden';
      str = "dh" + i;
      this.objsh[i] = this.document.getElementById (str);
      this.objsh[i].style.visibility = (i==0)?'visible':'hidden';
      str = "op" + i;
      this.objst[i] = this.document.getElementById (str);
      if (this.objst[i])
		this.objst[i].style.visibility = (i==0)?'visible':'hidden';
    }
  this.nextNewsItem();  
}

NewsDocument.prototype.start = function ()
{
  if (this.newsDef.length)
    {
		if (this.useDOM)
		{
			nsobj = this.document.getElementById ('spage');
			var html = '';
			for (i = 0; i < this.newsDef.length; i++)
				html += this.newsDef[i].toHtml ();
			nsobj.innerHTML = html;
			setTimeout ('NewsScroller.makeNextNewsItem()', 10);
		}
		else
		{
		this.document.all.spage.innerHTML = this.newsDef[0];
		setTimeout ('NewsScroller.nextNewsItem()', 10);
		}
    }
}  

