var myArray = new Array();
var myLinksArray = new Array();
var typer_divObj;
var typer_curTitle;
var myArray;
var myLinksArray;
var typer_curSel;
var typer_curWord=0;
var typer_curChar=0;
var typer_userLink;
var typer_ebay=false;
var alertTyperId;

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

function typer_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' ) {
  	divHTML = "<table width='100%'><tr><td>";
  	divHTML = divHTML + "<font style='font-family: Arial; font-size: 10pt;'>";
  
  	words = new Array(12);
  	words[0] = typer_curTitle + ": ";
  	words[11] = "Find out more by clicking a title! ";
  	for(i=0;i<myArray.length;i++) {
  		words[(i+1)] = (i+1) + ". " + myArray[i];
  	}
  
  	typer_curChar++;
  	if(typer_curWord == 0 || typer_curWord == 11) {
  		divHTML = divHTML + "<b>";
  	} else {
  		link = myLinksArray[(typer_curWord-1)];
  		if(typer_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=" + typer_userLink;
  		}
  		divHTML = divHTML + "<a href='"+link+"' target=_blank>";
  	}
  	divHTML = divHTML + words[typer_curWord].substr(0,typer_curChar);
  	if(typer_curWord == 0 || typer_curWord == 11) {
  		divHTML = divHTML + "</b>";
  	} else {
  		divHTML = divHTML + "</a>";
  	}
  
  	divHTML = divHTML + "</font></td></tr></table>";
  
  	typer_divObj.innerHTML = divHTML;
  
  	if(typer_curChar > words[typer_curWord].length) {
  		typer_curChar = 0;
  		typer_curWord = typer_curWord + 1;
  		if(typer_curWord > 11) { typer_curWord = 0; }
  		alertTyperId = setTimeout("typer_d(1);", 1000);
  	} else {
  		alertTyperId = setTimeout("typer_d(1);", 150);
  	}
  }

}

function typer_sl() {
	
	var selected = new Array(4);
	selected[typer_curSel] = " selected";

	html = "<select id='typer_sel' onChange='typer_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 typer_sc() {

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

	if(sel == 'movies') {
		typer_curTitle = 'Top 10 Movies on eBay.com';
		typer_curSel = 0;
	}
	if(sel == 'albums') {
		typer_curTitle = 'Top 10 Albums on eBay.com';
		typer_curSel = 1;
	}
	if(sel == 'games') {
		typer_curTitle = 'Top 10 Games on eBay.com';
		typer_curSel = 2;
	}
	if(sel == 'books') {
		typer_curTitle = 'Top 10 Books on eBay.com';
		typer_curSel = 3;
	}

	typer_curChar = 0;
	typer_curWord = 0;
	doTyperxml(sel);

}

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

	div.innerHTML = "<table cellpadding=0 cellspacing=0 width=600><tr><td align=center width=132>" + typer_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_typer'></div></td></tr></table>";
	typer_divObj = document.getElementById("foo_typer");

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

}

  function sendTyperRequest(url,callback,postData) {
  	
  	var req = createTyperXMLHTTPObject();
  	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 createTyperXMLHTTPObject() {
  	var xmlhttp = false;
  	for (var i=0;i<XMLHttpFactories.length;i++) {
  		try {
  			xmlhttp = XMLHttpFactories[i]();
  		}
  		catch (e) {
  			continue;
  		}
  		break;
  	}
  	return xmlhttp;
  }


  function handleTyperRequest(req) {

	  parseTyperXML(req.responseXML);
	  clearTimeout(alertTyperId);
	  typer_d(1);
  }

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

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

  }
  function doTyperxml(productType) {

    var xmlCall = productType;
    sendTyperRequest(webRoot + 'top10/topTenTransfer.php?type='+xmlCall, handleTyperRequest,'');
    
  }