var MaxTNs = 7;
var TnImgWidth = 90;
var TnImgHeight = 100;

// loads thumbnails into detail page
function loadProductViews ( id, aTN ) {
	var oTnCollection = document.getElementById( id );

	// add thumbnails from the left while alignment=left
	// addSpacer for all  but the last tn of a full (MaxTNs) list
	for(var i=0; (i<aTN.length) && (aTN[i][11]==tnalcLeft); i++){
		// load itemCount.js into environment -- this should only need be done in tnalcRight loop
		if ( aTN[i][3] == tnacActionPopupN ) include_js(aTN[i][10]);

//		addThumbnail( aTN, i, oTnCollection, tnalcLeft, i<MaxTNs-1 );
		addThumbnail( aTN, i, oTnCollection, tnalcLeft, true);
	}
	// add thumbnails from the right, while alignment=right, and we don't bump into the ones from the left
	// addSpacer for all  but the last tn of a full (MaxTNs) list
	for(var j=aTN.length-1; (j>=i) && (aTN[j][11]==tnalcRight); j--){
		// load itemCount.js into environment.  It holds the # of items in the series menu
		// Assumes there's only one series popup (which would have to be enforced by UI).
		// Otherwise, we'd need do something more sophisticated with async timing and saving itemCount
		if ( aTN[j][3] == tnacActionPopupN ) include_js(aTN[j][10]);

//		addThumbnail( aTN, j, oTnCollection, tnalcRight, !((j==i) && (aTN.length == MaxTNs)) );
		addThumbnail( aTN, j, oTnCollection, tnalcRight, true );
	}

	// Add the popup menu in the leftmost right-sided slot
	var oMenu = document.getElementById("toolBar");
	var oTn = document.createElement("div");
	oTn.className = "detail_thumbnail_right";
	oTn.appendChild(oMenu);
	oTnCollection.appendChild(oTn);
	oMenu.style.display = "block";
	// add vertical line
	if (aTN.length != MaxTNs) addVertLine( oTnCollection, tnalcRight, true );
}

// adds a detail thumbnail and the appropriate vertical line spacer
function addThumbnail( aItems, i, oTnCollection, alignment, bAddSpacer ) {
	
	if (bAddSpacer == null) { bAddSpacer = true; }

	var oTnItem = addTn(  aItems, i, oTnCollection, TnImgWidth, TnImgHeight, false, true );
	
	if ( alignment==tnalcRight ) {  // right aligned
		oTnItem.className = "detail_thumbnail_right";
		if ( bAddSpacer ) addVertLine( oTnCollection, tnalcRight, true );
	} else {
		oTnItem.className = "detail_thumbnail";
		if ( bAddSpacer ) addVertLine( oTnCollection, tnalcLeft, true );
	}
}

// handles activity when thumbnail is clicked
function tnOnClick ( event, iTN, bPopupN ) {
	tnOnClickHelper( event, iTN, productViews, bPopupN );
}

// opens a popup page (determined by header, not details)
// pointers to these pages are held at the end of the array of pointers for detail items
// ndx is the zero-based index into the array beyond the details
// bUnique ensures that the window name is unique and won't be overwritten 
// by subsequent attempts or other pages (used for email form to prevent data loss)
// bPreventAutoClose performs same f() as for details (preventing closePopups from closing this window)
function popupPage( page, width, height, top, left, ndx, bUnique, bPreventAutoClose, action ) {

	// set defaults, if not overridden
	if (width == null)  	{ width = 1100};
	if (height == null) 	{ height = 425};
	if (top == null)    	{ top = 40};
	if (left == null)   	{ left = 40};
	if (ndx == null)    	{ ndx = 0};
	if (bUnique == null)    { bUnique = false};
	if (bPreventAutoClose == null) { bPreventAutoClose = false};

	var strWinPage = page;
	var strWinName = "";

	// if it's a not .pdf file or it's not IE, use a windowName when .open'ing
	if (( page.search(/\.pdf/i) == -1 ) || !isIE ) {
		strWinName = "MyHeaderPopup_"+ndx.toString();
		if ( bUnique ) strWinName += randInt(100000000,1000000000).toString();
	}
	var strWinFeatures = 
		"toolbar=no,menubar=no,scrollbars=yes," + 
		"resizable=yes,location=no,directories=no,status=no," +
		"top="+top.toString()+","+"left="+left.toString()+"," +
		"width="+width.toString()+","+"height="+height.toString()+";"

//alert("b/4 action, ndx: " + ndx.toString());
//if ( ahPopups[productViews.length + ndx][0] ) alert(ahPopups[productViews.length + ndx][0].closed);
	// hold the pointer to the PDF page in the "n+1"+ndx position at end of array
	if (( !ahPopups[productViews.length + ndx][0] ) || ahPopups[productViews.length + ndx][0].closed) {
//	if (( !ahPopups[productViews.length + ndx][0] ) || !ahPopups[productViews.length + ndx][0].focus) {
		// open window and hold pointer,  if it's not yet been opened
//alert("b/4 open");
		ahPopups[productViews.length + ndx][0] = window.open( strWinPage, strWinName, strWinFeatures );
//		alert( ahPopups[productViews.length + ndx][0] );
//		alert( ahPopups[productViews.length + ndx][0].closed );
//		ahPopups[productViews.length + ndx][0].focus();
		// save its autoClose value
		ahPopups[productViews.length + ndx][1] = bPreventAutoClose;
	} else {
		// it's open already, just bring it forward
//alert("b/4 focus");
		ahPopups[productViews.length + ndx][0].focus();
	}

	// execute the requested action(print, save), if any
	if ( action == ppacSave ) {
		if ( ahPopups[productViews.length + ndx][0].document.execCommand ) {
			setTimeout("trySave(" + ndx.toString() + ")", 500)
//			ahPopups[productViews.length + ndx][0].document.execCommand('SaveAs', true, null);
//alert("after save");
		}
	} else if ( action == ppacPrint ) {
		setTimeout("tryPrint(" + ndx.toString() + ")", 500)
//		ahPopups[productViews.length + ndx][0].print();
	}
}

var ct = 0;
function tryPrint( ndx ) {
	ct++;
	if ( ahPopups[productViews.length + ndx][0].print != null ) {
//alert("b/4 print")
		ahPopups[productViews.length + ndx][0].print();
		ct = 0;
//		alert(ct);
	} else if ( ct < 10 ) {
		setTimeout("tryPrint(" + ndx.toString() + ")", 500)
	}

}

function trySave( ndx ) {
	ct++;
	if ( ahPopups[productViews.length + ndx][0].document.execCommand != null ) {
		try {
			ahPopups[productViews.length + ndx][0].document.execCommand('SaveAs', true, null);
		}
		catch(err) {
		//Handle errors here
		} 

//alert("after save")
		ct = 0;
//		alert(ct);
	} else if ( ct < 10 ) {
		setTimeout("tryPrint(" + ndx.toString() + ")", 500)
	}

}

/*
function popupPDF( page ) {
	var strWinPage = page;
	var strWinName = "MyPop_PDF";  // only one per page, or is name cross system?
	var strWinFeatures = 
		"top=40,left=40,toolbar=no,menubar=no,scrollbars=no," + 
		"resizable=yes,location=no,directories=no,status=no," +
    		"width=1000, height=425;"

			// hold the pointer to the PDF page in the "n+1" position at end of array
if ( ahPopups[productViews.length][0] ) alert(ahPopups[productViews.length][0].closed);
	if (( !ahPopups[productViews.length][0] ) || ahPopups[productViews.length][0].closed) {
alert("b4 open");
//		ahPopups[productViews.length] = window.open( strWinPage, strWinName, strWinFeatures );
//		ahPopups[productViews.length][0] = window.open( strWinPage, "", strWinFeatures );
		ahPopups[productViews.length][0] = window.open( strWinPage, strWinName, strWinFeatures );
		ahPopups[productViews.length][1] = false;
	} else {
alert("b4 focus");
		ahPopups[productViews.length][0].focus();
alert("after focus");
	}
		
}
*/