var myTickerArray = new Array();
var myTickerLinksArray = new Array();
var ticker_divObj;
var ticker_curTitle;
var ticker_curList;
var ticker_curLinksList;
var ticker_curSel;
var ticker_scroll=0;
var ticker_scrollWidth=50;
var ticker_userLink;
var ticker_ebay=false;

var webRoot = "http://www.terapeak.com/";

var alertTimerId;

function ticker_d(dontWait) {

	//  The 'dontWait' variable is there because before when the scripts we're calling it wasn't with ajax
	//  so we were getting errors where elements in the array weren't defined.  Basically it's there so
	//  older legacy code will work.
  if (typeof dontWait != 'undefined' ) {
  	var tempticker_scroll = 0;
  	var ticker_scrollLen = 0;
  	  	
  	// setup words and ticker_scrollText
  	var words = new Array(12);
  	words[0] = ticker_curTitle + ": ";
  	var ticker_scrollText = ticker_curTitle + ': ';
  	for(i = 0; i < myTickerArray.length; i++) {
  		words[i+1] = (i+1) + ". " + myTickerArray[i] + " ";
  		ticker_scrollText = ticker_scrollText + (i+1) + ". " + myTickerArray[i] + " ";
  	}
  	//ticker_scrollText = ticker_scrollText + "(Find out more by clicking a title) ";
  	words[11] = "(Find out more by clicking a title) ";
  
  	divHTML = "<table width='100%'><tr><td>";
  	divHTML = divHTML + "<font face='Courier' size=2>";
    
  	tempticker_scroll = ticker_scroll;
  	for(i=0; i<words.length; i++) {
  		if(tempticker_scroll < words[i].length) {
  			while(ticker_scrollLen < ticker_scrollWidth) {
  				if(i == 0 || i == 11) {
  					divHTML = divHTML + "";
  				} else {
  					link = myTickerLinksArray[(i-1)];
  					if(ticker_ebay==true) {
  						link = "http://www.qksrv.net/click-1569476-5463217?loc=http://search.ebay.com/" + link;
  					} else {
  						link = "http://www.terapeak.com/click.php?pageType=search&dosearch=1110480879&query=" + link + "&act=" + ticker_userLink;
  					}
  					divHTML = divHTML + "<a href='" + link + "' target='_blank'>";
  				}
  				// chop off the start and end?
  				
  				word = words[i].substr(tempticker_scroll);
  				if(word.length > 0) {
  					word = word.substr(0, word.length - 1);
  				}
  				if(word.length + ticker_scrollLen > ticker_scrollWidth) {
  					// if we can't fit it all, chop off the end
  					word = word.substr(0, ticker_scrollWidth - ticker_scrollLen + 1);
  				}
  				ticker_scrollLen = ticker_scrollLen + word.length;
  				// is first char a space? - replace with html equiv.
  				if(word.substr(0,1) == " ") {
  					word = "&nbsp;" + word.substr(1);
  				}
  				divHTML = divHTML + word;
  				if(i == 0 || i == 11) {
  					divHTML = divHTML + " ";
  				} else {
  					divHTML = divHTML + "</a> ";
  				}
  				if(word.length == 0) {
  					divHTML = divHTML + "&nbsp;";
  				}
  				tempticker_scroll = 0;
  				i++;
  				if(i > 11) { i = 0; }
  			}
  			break;
  		} else {
  			tempticker_scroll = tempticker_scroll - words[i].length;
  		}	
  	}
  
  	ticker_scroll = ticker_scroll + 1;
  	if(ticker_scroll > ticker_scrollText.length -1 ) {
  		ticker_scroll = 0;
  	}
  
  	divHTML = divHTML + "</font></td></tr></table>";
  
  	ticker_divObj.innerHTML = divHTML;
  
  	alertTimerId = setTimeout("ticker_d(1);", 200);
  }

}

function ticker_sl() {
	
	var selected = new Array(4);
	selected[ticker_curSel] = " selected";

	html = "<select id='ticker_sel' onChange='ticker_sc();' style='width:130px;'>";
	html = html + "<option value='movies'" + selected[0] + ">Top Ten Movies</option>";
	html = html + "<option value='albums'" + selected[1] + ">Top Ten Albums</option>";
	html = html + "<option value='games'" + selected[2] + ">Top Ten Games</option>";
	html = html + "<option value='books'" + selected[3] + ">Top Ten Books</option>";
	html = html + "</select>";
	return html;

}

function ticker_sc() {

	var elem = document.getElementById("ticker_sel");
	var sel = elem.value;

	if(sel == 'movies') {
		ticker_curTitle = 'Top 10 Movies on eBay.com';
		linkAdd = ' dvd';
		ticker_curSel = 0;
	}
	if(sel == 'albums') {
		ticker_curTitle = 'Top 10 Albums on eBay.com';
		linkAdd = ' cd';
		ticker_curSel = 1;
	}
	if(sel == 'games') {
		ticker_curTitle = 'Top 10 Games on eBay.com';
		linkAdd = ' game';
		ticker_curSel = 2;
	}
	if(sel == 'books') {
		ticker_curTitle = 'Top 10 Books on eBay.com';
		linkAdd = ' book';
		ticker_curSel = 3;
	}
	
	do_xml(sel);

}

function ticker_i(div, userID, dflt, useebay) {

	div.innerHTML = "<table cellpadding=0 cellspacing=0 width=600><tr><td align=center width=132>" + ticker_sl() + "<br><font style='font-family: arial; font-size: 8pt;'>Powered by <b><i><font style='font-family: arial; color:#6B6B73;font-size:10pt;'>tera</font><font style='font-family: arial; color:#0039A5;font-size:10pt;'>peak</font></i></font></td><td align=left valign=top><div id='foo_ticker'></div></td></tr></table>";
	ticker_divObj = document.getElementById("foo_ticker");

	ticker_ebay = useebay;
	var elem = document.getElementById("ticker_sel");
	if(dflt == 'movies' || dflt == 'albums' || dflt == 'games' || dflt == 'books') {
		elem.value = dflt;
	} else {
		elem.value = 'movies'; // default selected
	}
  ticker_sc();
	if(!userID) {
		ticker_userLink = "37856-4640"; // default user id
	} else {
		ticker_userLink = userID;
	}

}

  function sendRequest(url,callback,postData) {
  	
  	var req = createXMLHTTPObject();
  	if (!req) return;
  	var requestMethod = (postData) ? "POST" : "GET";
  	 	
  	req.open(requestMethod,url,true);
  	req.setRequestHeader('User-Agent','XMLHTTP/1.0');
  	if (postData)
  		req.setRequestHeader('Content-type','application/x-www-form-urlencoded');
  	req.onreadystatechange = function () {
  		if (req.readyState != 4) return;
  		if (req.status != 200 && req.status != 304) {
  //			alert('HTTP error ' + req.status);
  			return;
  		}
  		callback(req);
  	}
  	if (req.readyState == 4) return;
  	req.send(postData);
  }
  
  var XMLHttpFactories = [
  	function () {return new XMLHttpRequest()},
  	function () {return new ActiveXObject("Msxml2.XMLHTTP")},
  	function () {return new ActiveXObject("Msxml3.XMLHTTP")},
  	function () {return new ActiveXObject("Microsoft.XMLHTTP")}
  ];
  
  function createXMLHTTPObject() {
  	var xmlhttp = false;
  	for (var i=0;i<XMLHttpFactories.length;i++) {
  		try {
  			xmlhttp = XMLHttpFactories[i]();
  		}
  		catch (e) {
  			continue;
  		}
  		break;
  	}
  	return xmlhttp;
  }


  function handleRequest(req) {

	  parseXML(req.responseXML);
	  clearTimeout(alertTimerId);
	  ticker_d(1);
  }

  function parseXML(xmlSource) {
    var titleArray = xmlSource.getElementsByTagName('Title');
    var sampleArray = xmlSource.getElementsByTagName('UPC');
    if(sampleArray.length == 0) {
      var sampleArray = xmlSource.getElementsByTagName('ISBN');
    }
    var n_elems = 10;

    myTickerArray = new Array(); // clear previous
    myTickerLinksArray = new Array(); // clear previous
    for (i = 0; i < n_elems; i++) {
      myTickerArray.push(titleArray[i].firstChild.nodeValue);
      myTickerLinksArray.push(sampleArray[i].firstChild.nodeValue);
    }
    

  }
  function do_xml(productType) {
  
    var xmlCall = productType;
    sendRequest(webRoot + 'top10/topTenTransfer.php?type='+xmlCall, handleRequest,'');
    
  }