var FURNITURE_PACK_INDEX = 1;
var FIGURE_PACK_INDEX = 2;

// class Pack
function Pack(sku, category, index) {
	this.prods = new Array(0);
	
	this.Sku = sku;
	this.Category = category;
	this.ArrayIndex = index;
	
	this.GetProducts = function() {
		return this.prods;
	}
	this.AddProduct = function(prod) {
		this.prods.push(prod);
	}
}

var aryThumbIndex;
var aryPackSets = new Array(new Pack("B9618", "dora_furniture_packs", FURNITURE_PACK_INDEX), new Pack("B9617", "dora_figiture_packs", FIGURE_PACK_INDEX));
var bShowThumbnails = true;
var iCurrentThumb = 0;
var oPack = null;

function GetProdIndex(id){
	var iCurProd = -1;
	
	// Get products index using current product id
	for (var i = 0; i < products.length; i++){
		if (products[i][kProdID] == id){
			iCurProd = i;
			break;
		}
	}
	
	return iCurProd;
}

// Purpose:		Build html string for product detail page display
// Parameters:	iCurProd - index of product array to display data for
function BuildHtmlProductData(lCurProdId){
	var sHtml;
	var iCurProd = GetProdIndex(lCurProdId);
	var i = 0;
	var sMainImage = null;
	var prod = products[iCurProd];
	var packProds = null;
	
	// Initialize array of thumbnail indexes
	aryThumbIndex = new Array(thumbnails[iCurProd].length);
	for (i = 0; i < aryThumbIndex.length; i++){
		aryThumbIndex[i] = i;
	}
	
	SetPack(prod);
	sHtml = '<table width="547" cellspacing="0" cellpadding="0" border="0" bgcolor="#ffffff">' +
				'<tr>' +
					'<td valign="top">' +
						'<table width="239" cellspacing="0" cellpadding="0" border="0">' +
							'<tr>' +
								'<td rowspan="11"><img src="/nl/img/s.gif" width="15" height="1"></td>' +
							'</tr>' +
							'<tr>' +
								'<td><img src="/nl/img/s.gif" width="1" height="5"></td>' +
							'</tr>' +
							'<tr>' +
								'<td valign="top"><span class="title"><b>' + prod[kProdName] + '</b></span><span class="title_sm"><br><b>Producten#:</b>' + prod[kProdSku] +'<br><b>Leeftijd:</b>' + prod[kProdAge] +'</span></td></td>' +
							'</tr>' +
							'<tr>' +
								'<td><img src="/nl/img/s.gif" width="1" height="5"></td>' +
							'</tr>';
	
	if (bJSHasAwards || prod[kProdHasDemo]) {
		sHtml +=			'<tr>' +
								'<td>'
		if (prod[kProdHasDemo]) sHtml += '<a href="javascript:OpenNew(\'' + prod[kProdDemoLink] + '\', \'' + prod[kProdDemoWidth] + '\', \'' + prod[kProdDemoHeight] + '\')"><img src="/nl/dora/img/demo_pill.gif" width="75" height="25" alt="" border="0"></a><img src="/nl/img/s.gif" width="5" height="1">'
		if (bJSHasAwards) sHtml +=	'<a href="javascript: awardsPopUp(\''+ lCurProdId +'\', 10, 10, false);"><img src="/nl/dora/img/award_btn.gif" alt="Award Winner" border="0"></a>'
		sHtml +=				'</td>' +
							'</tr>' +
							'<tr>' +
								'<td><img src="/nl/img/s.gif" width="1" height="5"></td>' +
							'</tr>';
	}
	
	sHtml +=				'<tr>' +
								<!--'<td><a href="/nl/dora/locator.asp?pid=' + prod[kProdID] + '"><img src="/nl/dora/img/whereToBuy_btn.gif" width="118" height="25" alt="Where To Buy" border="0"></a></td>' +-->
							'</tr>' +
							'<tr>' +
								'<td><img src="/nl/img/s.gif" width="1" height="5"></td>' +
							'</tr>' +
							'<tr>' +
								'<td><span class="body_text">' + prod[kProdDscp] +'</span></td>' +
							'</tr>' +
						'</table>' +
					'</td>' +
					'<td><img src="/nl/img/s.gif" width="15" height="1"></td>' +
					'<td valign="top" align="right">' +
						'<table width="243" cellspacing="0" cellpadding="0" border="0" bgcolor="#ffffff">' +
							'<tr>' +
								'<td align="center">' +
									'<img src="' + prod[kProdImg] + '" width="243" height="243" id="mainImg" alt="' + prod[kProdAlt] + '" border="0"';
	
		// Make image clickable if applicable
	if (!bShowThumbnails && thumbnails[iCurProd].length > 0) {
		sHtml +=					' onclick="SetArrays(' + oPack.ArrayIndex + '); AdvanceMainImg(' + iCurProd + ');"';
	}
	
	
	sHtml +=					'></td>' +
							'</tr>';
							
	
			// Captions
	if (HasCaptions(iCurProd)) {
		sHtml +=			'<tr>' +
								'<td align="center" id="mainCaption" ' +
									' height="32"' +
								'><span class="caption">' + prod[kProdCaption] + '</span></td>' +
							'</tr>';
	}
	
			// Thumbnails
	if (oPack == null) {
		if (bShowThumbnails && thumbnails[iCurProd].length > 1) {
			sHtml +=		'<tr>' +
								'<td align="center" class="AltImgText">Klik op het foto voor meer details.' +
									'<div id="prodThumbs">' +
										BuildHtmlProductThumbs(iCurProd) +
									'</div>' +
								'</td>' +
							'</tr>';
		}
	}
	
	sHtml +=			'</table>' +
					'</td>' +
					'<td><img src="/nl/img/s.gif" width="15" height="1"></td>' +
					'<td><img src="/nl/img/s.gif" width="1" height="15"></td>' +
				'</tr>';
	
	if (oPack != null) {
		sHtml +='<tr>' +
					'<td align="center" class="SubScroll" colspan="5">Klik op het foto voor meer details.' +
						'<div id="prodThumbs">' +
							BuildPackProds(iCurProd, oPack) +
						'</div>' +
					'</td>' +
				'</tr>';
	}
				
	sHtml +='</table>';
	// Reset to initial state
	bShowThumbnails = true;
	oPack = null;
	SetArrays(0);
	
	return (sHtml);
}

// Purpose:	Generate html for thumbnail scroller
function BuildHtmlProductThumbs(iCurProd){
	var sHtml;
	var iThumbIndex;
	var iThumbCount = thumbnails[iCurProd].length;
	
	if (thumbnails[iCurProd].length > 1){
		sHtml = '<table cellpadding="0" cellspacing="0" border="0">';
		sHtml += 	'<tr>';
		sHtml += 		'<td><img src="/img/s.gif" width="10" height="1" border="0"></td>';
		
		// Left arrow
		sHtml +=		'<td id="prevThumb" valign="center">';
		if (iThumbCount > kMaxThumbs){
			sHtml += 		'<a href="javascript: ScrollProdThumbs(\'-\');"><img src="/nl/dora/img/larrow.gif" alt="" width="12" height="25" border="0"></a>';
		}
		sHtml += 			'<img src="/img/s.gif" width="10" height="1" border="0">';
		sHtml += 		'</td>';
		
		
		// Thumbnails
		for (var i = 0; i < iThumbCount; i++){
			if(i < kMaxThumbs){
				iThumbIndex = aryThumbIndex[i];
				sHtml += 	'<td id="thumb' + i + '"><a href="javascript: imageSwap(\'' + thumbnails[iCurProd][iThumbIndex][kThumbPath].replace("_60_", "_b_").replace("-60-", "-b-") + '\', \'' + thumbnails[iCurProd][iThumbIndex][kThumbCaption] + '\', \'' + sProdLink + '&img=' + iThumbIndex + '&id=' + products[iCurProd][kProdID] + '\');"><img name="thumb' + i + '" src="' + thumbnails[iCurProd][iThumbIndex][kThumbPath] + '" alt="" width="60" height="60" border="0"></a></td>';
				sHtml += 	'<td><img src="/img/s.gif" width="10" height="1" border="0"></td>';
			}
		}
		
		// Right arrow
		sHtml += 		'<td id="prevThumb" valign="center">';
		if (iThumbCount > kMaxThumbs){
			sHtml += 		'<a href="javascript: ScrollProdThumbs(\'+\');"><img src="/nl/dora/img/rarrow.gif" alt="" width="12" height="25" border="0"></a>';
		}
		sHtml += 			'<img src="/img/s.gif" width="10" height="1" border="0">';
		sHtml += 		'</td>';
		
		sHtml += 		'<td><img src=/img/s.gif" width="9" height="1" border="0"></td>';
		sHtml += 	'</tr>';
		sHtml += '</table>';
	}
	
	return (sHtml);
}

//	Purpose:	Display initial product detail page data
function InitProd(){
	document.write(BuildHtmlProductData(lProdId));
}

// Purpose:	To switch a thumbnail image with the main image
function EnlargeThumbnails(sJSImgSrc) {
	document.mainImg.src = sJSImgSrc.replace("_60_", "_b_").replace("-60-", "-b-");
}

function GetIndexes(){
	var sTest = '';
	
	for(i = 0; i < aryThumbIndex.length; i++){
		sTest += aryThumbIndex[i] + ',';
	}
	
	return sTest;
}

///	<summary>Determines if the product has captions</summary>
function HasCaptions(index) {
	var hasCaptions = false;
	var i = 0;
	
			// Thumbails
	for (; i < thumbnails[index].length; i++) {
		if (thumbnails[index][i][kThumbCaption] != '') {
			hasCaptions = true;
			break;
		}
	}
	
			// Main image
	if (!hasCaptions && products[index][kProdCaption] != '') {
		hasCaptions = true;
	}
	
	return hasCaptions;
}

// Purpose:	Regenerate html for thumbnail scroller
function ScrollProdThumbs(sDirection) {
	var iFirst = aryThumbIndex[0];
	var iLast = aryThumbIndex[aryThumbIndex.length];
	var i;
	var j;
	var iTemp;
	var iProdIndex = GetProdIndex(lProdId);
	
	switch(sDirection){
		case '+':
			iTemp = aryThumbIndex[0];
			for(i = 1; i < aryThumbIndex.length; i++)
				aryThumbIndex[i - 1] = aryThumbIndex[i];
			aryThumbIndex[aryThumbIndex.length - 1] = iTemp;
			break;
		case '-':
			iTemp = aryThumbIndex[aryThumbIndex.length - 1];
			for(i = aryThumbIndex.length - 1; i >= 0 ; i--)
				aryThumbIndex[i] = aryThumbIndex[i - 1];
			aryThumbIndex[0] = iTemp;
			break;
	}
	
	document.getElementById("prodThumbs").innerHTML = BuildHtmlProductThumbs(iProdIndex);
}
function test(index, showthumb, id) {
	
}
function BuildPackProds(iCurProd, pack) {
	var sHtml = "";
	var thumbs = eval("aryThumbs" + pack.ArrayIndex);
	var sLink = "";
	
	// Set working array to the needed one
	products = eval("aryProds" + pack.ArrayIndex); 
	
	sHtml = '<table cellpadding="0" cellspacing="0" border="0">' +
				'<tr>';
	// Thumbnails
	for (var i = 0; i < thumbs.length; i++){
		if (bDhtml) {
			sLink = "javascript: SetArrays(" + pack.ArrayIndex + "); bShowThumbnails = false; SwapProducts(" + products[i][kProdID] + ");";
		} else {
			if (sProdLink.indexOf('?') == -1){
				sLink = sProdLink + '?prodIndex=' + i + '&section=' + sSection + '&id=' + products[i][kProdID];
			} else {
				sLink = sProdLink + '&prodIndex=' + i + '&section=' + sSection + '&id=' + products[i][kProdID];
			}
		}
		
		sHtml += 	'<td><a href="' + sLink + '"><img name="thumb' + i + '" src="' + thumbs[i][0][kThumbPath] + '" alt="" width="60" height="60" border="0"></a></td>' +
					'<td><img src="/img/s.gif" width="10" height="1" border="0"></td>';
	}
	
	sHtml += 	'</tr>' +
			'</table>';
	
	return sHtml;
}

// Purpose:	Advances the main image to the next item in the thumbnail list
function AdvanceMainImg(iCurProd) {
	var thumbnail = null;
	
			// Only swap image if there is more than 1 thumbnail
	if (thumbnails[iCurProd].length > 1) {
		iCurrentThumb++;
		
				// Reset counter if above array bounds
		if (iCurrentThumb >= thumbnails[iCurProd].length) {
			iCurrentThumb = 0;
		}
		
				// Set thumbnail and swap
		thumbnail = thumbnails[iCurProd][iCurrentThumb];
		imageSwap(thumbnail[kThumbPath].replace("_60_", "_b_").replace("-60-", "-b-"), thumbnail[kThumbCaption], sProdLink + '&img=' + iCurrentThumb + '&id=' + products[iCurProd][kProdID]);
	}
}

function SetPack(prod) {
			// Determine if pack display should be used
			// Loop pack product sets
	for (i = 0; i < aryPackSets.length; i++) {
				// Set pack to use if the current product displayed equals the current pack product
		if (prod[kProdSku] == aryPackSets[i].Sku) {
			oPack = aryPackSets[i];
			break;
		}
		
				// Get all products associated with this pack
		packProds = aryPackSets[i].GetProducts();
		
		
				// Loop products in pack
		for (var j = 0; j < packProds.length; j++) {
					// Set pack to use if the current product displayed equals the current product in the pack
			if (prod[kProdSku] == packProds[j][kProdSku]) {
				oPack = aryPackSets[i];
				break;
			}
		}
		
		if (oPack != null) {
			break;
		}
	}
}
