
/**********************************************
*                                             *
*  NFO News Scroller v2                       *
*  Created by: Aric Ellinghuysen              *
*  Created on: 14 October 2006                *
*  Tested On: IE6, FireFox 1.0.1, Opera 9.00  *
*                                             *
**********************************************/

// Pixel sizes for: Font Arial, Size 11px, ASCII Characters 32-125
var letter_size = new Array(3,2,4,6,6,10,7,2,4,4,4,6,3,4,3,3,6,6,6,6,6,6,6,6,6,6,3,3,6,6,6,6,11,8,7,7,7,6,6,8,7,2,5,7,6,8,7,8,6,8,7,7,6,7,8,10,7,8,7,3,3,3,5,6,4,6,6,6,6,6,4,6,6,2,2,5,2,8,6,6,6,6,4,6,3,6,6,10,6,6,6,4,2,4);


// News Object Positions
var startingPositions = new Array();
var stoppingPositions = new Array();
var restartPositions = new Array();
var currentPositions = new Array();


var timer;                  // Marquee Time until next move
var pauseTime;              // Time delay before Scrolling Resumes


var scrollerWidth = 760;    // Scroller Width
var newObjOffSet = 20;      // Offset between News Objects


// Embedded style sheet
var style_out = "" +
"<style type='text/css'>\n"+
"#newsScroll {position:relative; font-family:arial; font-size:11px; background:#cccccc; border-style:solid; border-color:#cccccc; border-width:1px; width:"+scrollerWidth+"px; height:16px; overflow:hidden;}\n"+
".newsStream a {font-family:arial; font-size:11px;  color:black; text-decoration:none;}\n"+
".newsStream a:hover {color:#3C6398;}\n"+
"</style>\n";
document.writeln(style_out);


var articleObjects = new Array(
 new NewsObj("http://www.ellinghuysen.com/news/articles/129019.shtml","02/03/12 - Cattle stand-off, Hogs steady"),
 new NewsObj("http://www.ellinghuysen.com/news/articles/129018.shtml","02/03/12 - Beef Producers Struggle"),
 new NewsObj("http://www.ellinghuysen.com/news/articles/129017.shtml","02/03/12 - Blizzard, storms to the Plains"),
 new NewsObj("http://www.ellinghuysen.com/news/articles/129016.shtml","02/03/12 - Farm Groups Talk Farm Bill"),
 new NewsObj("http://www.ellinghuysen.com/news/articles/129029.shtml","02/03/12 - DJ LIVESTOCK HIGHLIGHTS"),
 new NewsObj("http://www.ellinghuysen.com/news/articles/129028.shtml","02/03/12 - Rabo- Bright Q1 for Pork"),
 new NewsObj("http://www.ellinghuysen.com/news/articles/129027.shtml","02/03/12 - HSUS now targets Tyson"),
 new NewsObj("http://www.ellinghuysen.com/news/articles/129026.shtml","02/03/12 - NYT:  Pig to Pork. Troubling."),
 new NewsObj("http://www.ellinghuysen.com/news/articles/129049.shtml","02/03/12 - Antibiotic Conundrum"),
 new NewsObj("http://www.ellinghuysen.com/news/articles/129048.shtml","02/03/12 - China buying up S. America"),
 new NewsObj("http://www.ellinghuysen.com/news/articles/129047.shtml","02/03/12 - Pig Image Slips Into Cop Logo"),
 new NewsObj("http://www.ellinghuysen.com/news/articles/129046.shtml","02/03/12 - Eye on Grains")
);




/***************************
*  News Class Initalizers  *
***************************/



/* News Class */
function NewsObj(url,title) {
 this.link = url;      // article link
 this.content = measureTitle(title);
 
 this.title = this.content.shift();  // article title
 this.length = this.content.shift();  // article title length
 
 this.length += newObjOffSet;  // adjust length for image separtor and spacing
 
// alert(this.link+" "+this.title+" "+this.length)
 
 // return marquee table data
 this.toString = new Function("","return this.link+' '+this.title+' '+this.length");
}




// Return length of title and strip none standard characters
function measureTitle(data) {
 var leng = 0;
 var name = "";
 
 data = formatTitle(data);
 
 for (i=0;i<data.length;++i) {
  var chr = data.charCodeAt(i);
  if (chr >= 32 && chr <= 125) {
   leng+= letter_size[chr-32];
   name+= data.charAt(i);
  }
 }
 
 return new Array(name,leng);
}


// Format Date from mm/dd/yy to mm.dd
function formatTitle(data) {
 data = data.replace(/0(\d)\//g,"$1\/"); // remove starting zeros from month and day
 return data.replace(/(\d{1,2})\/(\d{1,2})\/(\d{1,2}) - /,"$1.$2 ");  // reformat date
}


/****************************
*  News Content and Coords  *
****************************/

// Write Scroller Content
function displayNewsContainers() {
 var content = "";

 for (var i=0;i<articleObjects.length;++i) {
  content += '<div id="newObj'+i+'" class="newsStream" style="position:absolute; top:0px; left:'+(startingPositions[i])+'px; width:'+articleObjects[i].length+'px;" onmouseover="pauseScrolling()">'+
  '<a href="javascript:scrollerWin(\''+articleObjects[i].link+'\')">'+articleObjects[i].title+'</a></div>';
 }

 document.write('<div id="newsScroll" onmouseover="pauseScrolling()" onmouseout="resumeScrolling()">'+content+'</div>');
}




// Calculate News Objects Positions
function calculatePositions() {

 var sumLeng = 0;
 for (var i=0;i<articleObjects.length;++i) {
  var leng = articleObjects[i].length;
  startingPositions.push(sumLeng + scrollerWidth);
  currentPositions.push(sumLeng + scrollerWidth);
  stoppingPositions.push(-leng);  
  sumLeng+= leng;
 }

 for (var i=0;i<stoppingPositions.length;++i) {
  restartPositions.push(sumLeng+stoppingPositions[i]-1)
 }
 
}




/***************************
*  Scroller Functionality  *
***************************/



// Moving Scroller
function updateScoller() {

 for (var i=0;i<articleObjects.length;++i) {
  currentPositions[i] -= 1;

  if (currentPositions[i] < stoppingPositions[i]) {
   currentPositions[i] = restartPositions[i];
  }
   
   
  // Move News Objects if visible
  if (currentPositions[i] < scrollerWidth) {
   document.getElementById("newObj"+i).style.left = currentPositions[i]+'px';
  }

 }
 
 
 timer = window.setTimeout("updateScoller()",20);
}






// Pause Scrolling
function pauseScrolling() {
 window.clearTimeout(timer);
 window.clearTimeout(pauseTime);
}



// Resume Scrolling
function resumeScrolling() {
 pauseTime = window.setTimeout("updateScoller()",250);
}




/**********
*  Other  *
**********/


// Open URL in New Window
function scrollerWin(a) {
 var win = window.open(a,"win1","height=400,width=600,scrollbars=yes,resizable=yes");
 win.focus();
}



// Error Message
function domainNotValid() {
 var content = '<div id="newsScroll" style="text-align:center; width:'+scrollerWidth+';">For use only on NFO.org</div>';
 document.writeln(content);
}




/**********************
*  Initialize Script  *
**********************/

var valid = 0;
var host=location.hostname;

var sites = new Array("nfo.org","localhost","2000server");
if (host.indexOf("www.")+1) {host=host.substring(4,host.length);}
for (a in sites) if (host==sites[a]){valid=1;break;}

// Run Script
if (valid) {
 calculatePositions();
 displayNewsContainers();
 updateScoller();
}
else {
 domainNotValid();
}




