//
// This file and the code in this file are CopyRighted (c) 2004
//
//           to EasyByte Software Corporation Ltd
//
// Only once you have purchased a NewsFeed license from us are 
// you allowed to make any changes to this file beyond the general
// config parameters.
//
// www.easybyte.com/buy/ 
//
//
// You may try the NewsFeeds out on your website for upto one week
// for free, beyond that you will have to pay for them.
// 

// Version 2.0.1

//constants
var NO_SCROLL   = 0;
var SCROLL_UP   = 1;
var SCROLL_LEFT = 2;

// BEGIN general DEFAULT config parameters
var nArticlesInGroup = 5;

var openLinkInNewWindow  = true;
var displaySource = true;
var displayTime = false;

var stylePrefix = "";

//absolute positioning
var containerTop = 0;
var containerLeft = 0;
var newsfeedWidth = 190;
var newsfeedHeight = 270;

//NO_SCROLL, SCROLL_UP, SCROLL_LEFT
var scrollType = SCROLL_UP;
var continousScroll = true;

//in milliseconds
var scrollTime = 500;
var pauseLength = 4000;

// BEGIN news items separarators
//  ________ Article ________
// /                          \
// Headline Source Access Time
//^        ^      ^      ^
//|        |      |      |
//|        |      |      timeSeparator(*)
//|        |      accessSeparator
//|        sourceSeparator
//headlineSeparator
//
var headlineSeparator="<br><br><br>";
var sourceSeparator="<br>";
var accessSeparator="&nbsp;";
var timeSeparator="<br>";
// END news items separarators

// END   general DEFAULT config parameters

//==================================
// BEGIN global variables  
//timer step
var speedTimeOut = 10;

//scroller internal state
var STARTED   = 10;
var STOPPED   = 11;
var FINISHED  = 12;
//=====================

var article = new Array();
var newsfeedScrollInstances = new Array();
var timer = 0;
var noScrollInstancesCounter = 1000;

var target = "";
var mouseEvents = "";
// END global variables  

//window.onLoad = new function () {timer = setInterval("tick()", speedTimeOut);}
window.onUnload = clearInterval(timer);

function detectBrowser() {
  var b = navigator.appName
	if (b=="Netscape")
	  this.type = "ns";
	else if (b=="Microsoft Internet Explorer") 
	  this.type = "ie";
	this.ns = (this.type == "ns");
	this.ie = (this.type == "ie");
}

var nf_browser = new detectBrowser();

function createLink(id, content, URL, className) {
  var theClass = ""; 
  if (className != null)
    theClass = "class='"+className+"'";
  
  document.write("<a id='"+id+"' "+theClass+" href='"+URL+"' "+target+">"+content+"</a>");
  return document.getElementById(id);
}

function createText(id, content, className) {
  var theClass = ""; 
  if (className != null)
    theClass = "class='"+className+"'";
  
  document.write("<span id='"+id+"' "+theClass+">"+content+"</span>");
  return document.getElementById(id);
}

function createArticle(prefixId, anArticle, displayHeadlineSeparator) {
  if (displayHeadlineSeparator)
    createText(prefixId+"hs", headlineSeparator, stylePrefix+"headline");
  createLink(prefixId+"h", anArticle.headline_text + "...", anArticle.url, stylePrefix+"headline");
  
  if (displaySource) {
    createText(prefixId+"ss", sourceSeparator, stylePrefix+"source");
    createLink(prefixId+"s", anArticle.source , anArticle.document_url, stylePrefix+"source");
  }
  
  if (anArticle.access_status == "sub" || anArticle.access_status == "reg") {
    createText(prefixId+"as", accessSeparator, stylePrefix+"access");
    createLink(prefixId+"a", anArticle.access_status , anArticle.access_registration, stylePrefix+"access");
  }
  
  if (displayTime) {
    time = new Date(anArticle.harvest_time);
    time.setHours(time.getHours() - (time.getTimezoneOffset() / 60 ));
    
    createText(prefixId+"ts", timeSeparator, stylePrefix+"time");
    createText(prefixId+"t", time.toString(), stylePrefix+"time");
  }
}

function createArticleGroup(id, startIndex, nArticles) {
  if (startIndex+nArticles > article.length)
    nArticles = article.length - startIndex;
    
  document.write("<div id='"+id+"' class='"+stylePrefix+"group'>"); 
  
  //headline separator is not displayed for the first article in group
  createArticle(id+"a"+startIndex, article[startIndex], false);
  for(var i = startIndex+1; i<startIndex+nArticles; i++) {
    createArticle(id+"a"+i, article[i], true);
  }
   
  document.write("</div>");
  return document.getElementById(id);
}

function NewfeedArticleGroup(id, startIndex, nArticles, overflow) {
  this.id = id;
  
  this.tag = createArticleGroup(id, startIndex, nArticles);
  this.css = this.tag.style;
  
  this.x = 0;
	this.y = 0;
		
  this.w = newsfeedWidth;
  this.h = newsfeedHeight;
  
  this.show = NewfeedArticleGroupShow;
  this.hide = NewfeedArticleGroupHide;
  this.moveTo = NewfeedArticleGroupMoveTo;
  this.moveBy = NewfeedArticleGroupMoveBy;
  this.clip = NewfeedArticleGroupClip;
  
  this.updateStyle = updateStyle;
  
  this.updateStyle("absolute",0, overflow);
  this.hide();
}

function NewfeedArticleGroupMoveTo(x,y) {
	if (x!=null) {
		this.x = x;
		if (nf_browser.ns) this.css.left = this.x
		else this.css.pixelLeft = this.x
	}
	if (y!=null) {
		this.y = y
		if (nf_browser.ns) this.css.top = this.y
		else this.css.pixelTop = this.y
	}
}

function NewfeedArticleGroupMoveBy(x,y) {
	this.moveTo(this.x+x,this.y+y)
}

function NewfeedArticleGroupShow() {
	this.css.visibility = "visible";
}

function NewfeedArticleGroupHide() {
	this.css.visibility = "hidden";
}

function NewfeedArticleGroupClip(newsfeedWidth, newsfeedHeight) {
  left   = 0;
  top    = 0;
  right  = this.x + (this.x > newsfeedWidth? 0: newsfeedWidth - this.x);
  bottom = this.y + (this.y > newsfeedHeight? 0 : newsfeedHeight-this.y);
  
  this.css.clip ="rect("+top+"px "+right+"px "+bottom+"px "+left+"px)"; 
}

function updateStyle(position, z, overflow) {
  if (nf_browser.ns) {
     this.css.left = this.x
     this.css.top = this.y
     this.css.width = this.w
     this.css.height = this.h
	} else {
	  this.css.pixelLeft = this.x
	  this.css.pixelTop = this.y
	  this.css.pixelWidth = this.w
	  this.css.pixelHeight = this.h
	}
  this.css.position = position;
  this.css.zIndex = z;
  this.css.overflow = overflow;
}

function Newsfeed(id) {
  this.id = "erfnews"+id;

  if (scrollType == NO_SCROLL)
	  this.overflow = "visible";
  else
	  this.overflow = "hidden";

  this.init = NewsfeedInit;
  this.init();
  
  this.tag = document.getElementById(this.id);
  this.css = this.tag.style;
  this.index = -1;
  
  this.speedTimeOut = speedTimeOut;
  this.pauseLength = pauseLength;
  this.scrollTime = scrollTime;
  this.scrollType = scrollType;
  this.continousScroll = continousScroll;
  
  this.x = containerLeft;
  this.y = containerTop;
  this.w = newsfeedWidth;
  this.h = newsfeedHeight;
  
  if (scrollType == SCROLL_LEFT){
    this.startX = newsfeedWidth;
    this.startY = 0;
  } else if (scrollType == SCROLL_UP) {
    this.startX = 0;
    this.startY = newsfeedHeight;
  }
  
	this.dx = (this.startX*speedTimeOut)/scrollTime;
	this.dy = (this.startY*speedTimeOut)/scrollTime;
	    
	this.updateStyle = updateStyle;
	this.updateStyle("relative",1, this.overflow);

  this.state = FINISHED;
  this.remainingTime = 0;//this.pauseLength;
}

function NewsfeedInit() {
  this.items = new Array();
  
  document.writeln("<div id='"+this.id+"' class='"+stylePrefix+"container' "+mouseEvents+" >");
  var i=0;
  var k=0;
  while (i<article.length) {
    this.items[k++] = new NewfeedArticleGroup(this.id+"g"+k, i, nArticlesInGroup, this.overflow);
    i += nArticlesInGroup;
  }
  
  document.writeln("</div>");
}

function NewsfeedShow() {
  if (this.index<0) return;
  this.items[this.index].show();
}

function NewsfeedHide() {
  if (this.index<0) return;
  this.items[this.index].hide();
}

function NewsfeedNext() {
  if (!this.continousScroll && this.index >= 0) {
        this.hide();
  }
  
  this.index = (this.index == this.items.length-1)? 0: this.index+1;
  this.show();     
}

function NewsfeedPositionGroup(){
	this.items[this.index].moveTo(this.startX, this.startY);
}
function NewsfeedStart() {
    if (this.state == STOPPED)
        this.state = STARTED;
}

function NewsfeedStop() {
    if (this.state == STARTED)
        this.state = STOPPED;
}

function NewsfeedSlide() {  
  if (this.index < 0) {
    window.status="Index < 0 !";
    return;
  }
    
  var activeGroup = this.items[this.index];
  if (this.index > 0)
    previousGroup = this.items[this.index-1];
  else
    previousGroup = this.items[this.items.length-1];
    
  if (activeGroup.x >= this.dx && activeGroup.y >= this.dy){
      if (this.continousScroll)
        previousGroup.moveBy(-this.dx,-this.dy);
    
      activeGroup.moveBy(-this.dx,-this.dy);
      //activeGroup.clip(this.w,this.h);
  } else {
    if (this.continousScroll)
        previousGroup.hide();
  	activeGroup.moveTo(0,0);
  	this.state = FINISHED;
  	this.remainingTime = this.pauseLength;
	}
}

function NewsfeedRun() {
  if (this.state == STOPPED) 
    return;
  
	this.remainingTime -= speedTimeOut;
	
	if (this.state == STARTED){
		this.slide();
	} else	if (this.remainingTime <= 0){
		this.next();
		this.positionGroup();	
		this.state = STARTED
		this.remainingTime = this.scrollTime;
	}
}

Newsfeed.prototype.show = NewsfeedShow;
Newsfeed.prototype.hide = NewsfeedHide;
Newsfeed.prototype.start = NewsfeedStart;
Newsfeed.prototype.stop = NewsfeedStop;
Newsfeed.prototype.slide = NewsfeedSlide;
Newsfeed.prototype.next = NewsfeedNext;
Newsfeed.prototype.positionGroup = NewsfeedPositionGroup;
Newsfeed.prototype.run = NewsfeedRun;

function tick() {
	for(var i=0;i<newsfeedScrollInstances.length;i++) {
		nf = newsfeedScrollInstances[i];
		nf.run();
	}
}

function configOK() {
  // Check to make sure we have the news feed file ok
  //	

  if (article.length == 0)
  {
    document.writeln("<center>Please reload this page to view the News/Information</center>");
    return false;
  } 
  
  if (openLinkInNewWindow)
    target = "target='_blank'";
  return true;
}

function createNewsfeedScroll() {
  if (configOK()) {
    if (scrollType == NO_SCROLL){
        mouseEvents = "";
        nf  = new Newsfeed(noScrollInstancesCounter);
        nf.next();
        noScrollInstancesCounter++;
    } else {
        newsfeedCount = newsfeedScrollInstances.length;
        mouseEvents = "onmouseover='newsfeedScrollInstances["+newsfeedCount+"].stop();' onmouseout='newsfeedScrollInstances["+newsfeedCount+"].start();'"
        newsfeedScrollInstances[newsfeedCount] = new Newsfeed(newsfeedCount);
        if (timer == 0) {
        	timer = setInterval("tick()",speedTimeOut);
        }
    }
  }
}