// JavaScript Document
/*Start Modifiable*/
//For any extra contents You will have to add thsoe properties
//If you have image seperator then assign it to variable bgColImag and bgRowImage
//Currently script shows the follwing contents ("Name","Image","Price","Saleprice") add and remove as required 
var showContents = new Array("Image","Name","Price","Saleprice"); //Change the order the way you want if name appears first first row shows name
var showSeperatorRow = false;
var showSeperatorColumn = false;
var seperatorRowBgColor = "#ffffff";
var seperatorColBgColor = "#ffffff";
/*End Modifiable*/

//Dont Modify the code below unless needed
var uniquearr = new Array();
var y,z;
var duplicate=false;
var namecell, pricecell, salepricecell, imgcell;
var itemsArr = new Array();

function round(n) {
  var s = "" + Math.round(n * 100) / 100
  var i = s.indexOf('.')
  if (i < 0) return s + ".00"
  var t = s.substring(0, i + 1) + s.substring(i + 1, i + 3)
  if (i + 2 == s.length) t += "0"
  return t
}

function seperatorRow(totalColSpan){
	if(showSeperatorColumn)
		totalColSpan = (totalColSpan * 2) - 1
	document.write("<tr><td colspan='" + totalColSpan + "' bgcolor='" + seperatorRowBgColor + "'>");
	document.write("<img src=http://us.st1.yimg.com/store1.yimg.com/Img/trans_1x1.gif width=1 height=1>");
	document.write("</td></tr>");
}

function seperatorColumn(totalRowSpan){
	document.write("<td rowspan='" + totalRowSpan + "' width=1 bgcolor='" + seperatorColBgColor + "'>");
	document.write("<img src=http://us.st1.yimg.com/store1.yimg.com/Img/trans_1x1.gif width=1 height=1>");
	document.write("</td>");
}

function itemObj(id, name, img, price, saleprice) {
	this.id=id.toLowerCase();
	this.name=name;
	this.img=img;
	this.price=price;
	this.saleprice=saleprice;
	itemsArr[itemsArr.length] = this;
}

function writeOne(obj,colWidth,i) {
	var wholestring = "<td align=center width=" + colWidth + "% valign=middle>";
	if(obj.img != "-@NULL@-" && (i % 2 == 0))
		wholestring += "<div id=rotateImage><a href=" + obj.id +".html><img src=" + obj.img + " border=0 vspace=5></a></div>";
	if(obj.name != "-@NULL@-")
		wholestring += "<div id=rotateName><a href=" + obj.id +".html>" + obj.name + "</a></div>";
	if(obj.price != "-@NULL@-")
		wholestring += "<div id=rotatePrice>" + regpriceTxt + " $" + round(obj.price) + "</div>";
	if(obj.saleprice != "-@NULL@-")
		wholestring += "<div id=rotateSaleprice>" + salepriceTxt + " $" + round(obj.saleprice) + "</div>";
	if(obj.img != "-@NULL@-" && (i % 2 != 0))
		wholestring += "<div id=rotateImage><a href=" + obj.id +".html><img src=" + obj.img + " border=0 vspace=5></a></div>";
	wholestring += "</td>";
	document.write(wholestring);
}

function writeName(obj,colWidth){
/*
	if(obj.name != "-@NULL@-")
		var namecell = "<td align=center id=rotateName width=" + colWidth + "% valign=top><a href=" + obj.id +".html>" + obj.name + "</a></td>";
	else
		var namecell = "<td align=center>&nbsp;</td>";
	document.write(namecell);
*/
}

function writeImage(obj){
	if(obj.img != "-@NULL@-")
		var imgcell = "<td align=center valign=middle><img src=" + obj.img + " border=0></td>";
	else
		var imgcell = "<td align=center>&nbsp;</td>";
	document.write(imgcell);
}

function writePrice(obj){
/*
	if(obj.price != "-@NULL@-")
		var pricecell = "<td align=center id=rotatePrice width=" + colWidth + "%>" + regpriceTxt + " $" + round(obj.price) + "</td>";
	else
		var pricecell = "<td align=center>&nbsp;</td>";
	document.write(pricecell);
*/
}

function writeSaleprice(obj){
/*
	if(obj.saleprice != "-@NULL@-")
		var salepricecell = "<td align=center width=" + colWidth + "%><div id=rotateSaleprice>" + salepriceTxt + " $" + round(obj.saleprice) + "</div></td>";
	else
		var salepricecell = "<td align=center>&nbsp;</td>";
	document.write(salepricecell);
*/
}


function showTable(request,arrayName,colWidth,startPos,endPos,i){
	document.write("<tr>");
	for(a = startPos; a <= endPos; a++){
		if(a >= uniquearr.length)
			request = "NA";
		switch(request){
			case "Name":
				writeName(arrayName[uniquearr[a] - 1],colWidth);
				break;
			case "Image" :
				writeImage(arrayName[uniquearr[a] - 1],colWidth);
				break;
			case "Price" :
				writePrice(arrayName[uniquearr[a] - 1],colWidth);
				break;
			case "Saleprice" :
				writeSaleprice(arrayName[uniquearr[a] - 1],colWidth);
				break;
			case "ONE" :
				writeOne(arrayName[uniquearr[a] - 1],colWidth,a);
				break;
			default :
				//document.write("<td align=center>&nbsp;<td>");	
				break;
		}
		if(showSeperatorColumn && a != endPos && request == showContents[0])
			seperatorColumn(showContents.length);
	}
	document.write("</tr>")
}




function display(totalelems,showtotal,arrayName,colperrow){
	z = 0;
	if(showtotal > arrayName.length)
		showtotal = arrayName.length;
	while (z != showtotal){
			y = Math.ceil(Math.random()*totalelems);
			var repeatchk = uniquearr;

			for(i = 0; i < repeatchk.length; i++){
				if (y==repeatchk[i])
					duplicate=true;
			}

			if (duplicate == true){
				duplicate = false;
				continue;
			}
			if (duplicate == false){
				uniquearr[uniquearr.length] = y;
				z+=1;
			}
	}
	totalRows = Math.ceil(showtotal/colperrow);
	endPos =-1;
	colWidth = Math.ceil(100/colperrow);
	for(i = 0; i < totalRows; i++){
		startPos = endPos + 1;
		endPos = endPos + colperrow;
		for(j = 0; j < showContents.length; j++){
			showTable(showContents[j],arrayName,colWidth,startPos,endPos);			
			if(j == showContents.length - 1 && i != totalRows -1)
				if(showSeperatorRow)
					seperatorRow(endPos - startPos + 1);
		}
	}
}





function displayStaggered(totalelems,showtotal,arrayName,colperrow){
	z = 0;
	if(showtotal > arrayName.length)
		showtotal = arrayName.length;
	while (z != showtotal){
			y = Math.ceil(Math.random()*totalelems);
			var repeatchk = uniquearr;

			for(i = 0; i < repeatchk.length; i++){
				if (y==repeatchk[i])
					duplicate=true;
			}

			if (duplicate == true){
				duplicate = false;
				continue;
			}
			if (duplicate == false){
				uniquearr[uniquearr.length] = y;
				z+=1;
			}
	}
	totalRows = Math.ceil(showtotal/colperrow);
	endPos =-1;
	colWidth = Math.ceil(100/colperrow);
	for(i = 0; i < totalRows; i++){
		startPos = endPos + 1;
		endPos = endPos + colperrow;
		showTable("ONE",arrayName,colWidth,startPos,endPos,i);			
		if(i != totalRows -1)
			if(showSeperatorRow)
				seperatorRow(endPos - startPos + 1);
	}
}