
///// requires Xelt, ?? , ?? 
//// from cm_std_xxx.js





function GetFirstNum (str) {
	var allnums = "0123456789";
	for (var i = 0; i < str.length; i++) {	// loop through all characters of the string
		if (allnums.indexOf(str.substr(i,1)) != -1)
			return i;
	}
	return -1;
}

/////////////////////////////////////////

var gGraphicsDir = "images/logos/";
var gLowSrcLogo = "images/icons/PikLoad.gif";
var gLoadIcon = "images/icons/PikLoad_dddddd.gif";
var gShowDataPerPage = 9;





var gData = [];
var gDataVars = [];

// <script src="http://spreadsheets.google.com/feeds/cells/pio_mnxreAWL1P3At5rCoJw/od6/public/basic?alt=json-in-script&callback=ParseJSONData">

function ParseJSONData (root) { 	// requires GetFirstNumber function
	
	var feed = root.feed;
	var entries = feed.entry || [];
	
	// Each	entry looks like this in the JSON data:
	/*
	{
		"id":	{"$t":"http://spreadsheets.google.com/feeds/cells/pio_mnxreAWL1P3At5rCoJw/od6/public/basic/R1C1"},
		"updated":	{"$t":"2008-06-05T20:15:24.525Z"},
		"category":	[{
				"scheme":"http://schemas.google.com/spreadsheets/2006",
				"term":"http://schemas.google.com/spreadsheets/2006#cell"
		}],
		"title":	{"type":"text","$t":"A1"},
		"content":	{"type":"text","$t":"Manufacturer Name"},
		"link":		[
			{"rel":"self",
			"type":"application/atom+xml",
			"href":"http://spreadsheets.google.com/feeds/cells/pio_mnxreAWL1P3At5rCoJw/od6/public/basic/R1C1"}
		]
	}
	*/	
	
	//var varName 	= [];		// this will hold the variable names from the first row
	var lastrow 	= -1;
	
	for (var i = 0; i < entries.length; ++i)
	{
		//Xelt("manufacturerlist").innerHTML = "Loading data [" + i + "]";
		
		var cell 	= entries[i].title.$t;		// title is column letter & row number
		var content 	= entries[i].content.$t;	// contents of cell
		var firstnum 	= GetFirstNum(cell);		// gets location of first number
		
		if (firstnum == -1) { } // error - skip this entry; should never happen
		else
		{
			var col 	= cell.substring(0, firstnum);	// column letter
			var row 	= cell.substring(firstnum);	// row number (as string)
			
			//Xelt("manufacturerlist").innerHTML += "<br>[entry " + i + ", cell " + cell + "] ";

			if (row == "1") 				// we're looking at the first row
			{
				content = content.replace(/ /g, "");	// remove spaces
				content = content.toLowerCase();	// make lowercase
				content = content.replace(/"/g, "'");	// replace double quotes with single
									// *** should do more checks here to make sure its valid
				gDataVars[col] = content;
				//Xelt("manufacturerlist").innerHTML += "header: " + content;
				
			} else {					// for all rows of data
			
				var rowarraynum = +row - 2;		// array # should start at zero; data rows start at 2
				if (rowarraynum != lastrow) 			// if we're looking at a new row
				{
					gData[rowarraynum] = new Object();	// then make a new object
				}
				lastrow = rowarraynum;

				gData[rowarraynum][gDataVars[col]] = content;
		
				//var x = "gData[" + rowarraynum + "][\"" + gDataVars[col] + "\"] = \"" + content + "\";";
				//Xelt("manufacturerlist").innerHTML += "<br>" + x; 
			}
		}
		
		
		
	}
			
	DisplayManufacturers(0);
	
}


function DisplayManufacturers (start) {

	var html = "<ul>";
	
	start = (start < 0) ? 0 : start;
	var end = start + gShowDataPerPage;
	end = (end > gData.length) ? gData.length : end;
	
	for (var i = start; i < end; ++i)
	{
		html += "<li>";
		
		if (typeof gData[i].link != "undefined" && gData[i].link != "")
		{
			html += "<a class='name' href='http://" + gData[i].link + "'>";
		} else {
			html += "<a class='name' href='javascript:alert(\"This manufacturer does not have a website. Please use the contact link instead.\");'>";
		}
		
		html += "<img src='" + gGraphicsDir + gData[i].logo + "' lowsrc='" + gLowSrcLogo + "' class='logo'>";
		html += gData[i].name  + "</a>";
		html += "<div class='specialty'>";
		
		if (typeof gData[i].specialty != "undefined")
		{
			html += gData[i].specialty;
		}
		
		html += "</div>";
		html += "<div class='email'>";
		if (typeof gData[i].contact != "undefined" && gData[i].contact != "")
		{
			html += "<a href='mailto:" + gData[i].contact;
			html += "@feenix.net'>Contact " + gData[i].contact + "</a";
		}
		html += "</div>";
		html += "</li>";
		
	}
	
	
	html += "</ul><br style='clear: both' />";
	var previous = (start == 0) ? gData.length - gShowDataPerPage : start - gShowDataPerPage;
	var next = (end == gData.length) ? 0 : end;
	
	// *** Add "Page x / y" text here ***
	
	html += "<a class='prev button' href='javascript: DisplayManufacturers(" + previous + ")'>Previous</a>";
	html += "<a class='next button' href='javascript: DisplayManufacturers(" + next + ")'>Next</a>";
	html += "<br style='clear: both' />";
	html += "</ul>";
	
	Xelt("manufacturerlist").innerHTML = html;

}


function WriteLoading () {
	
	var html = "<img src='" + gLoadIcon + "' class='loading' />";
	html += "<span class='loading'>Loading Data... </span>";

	document.write(html);
}

