var FURNITURE_PACK_INDEX = 1;
var FIGURE_PACK_INDEX = 2;
var FASHION_PACK_INDEX = 1;
var ADV_FURNITURE_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("B9618B", "dora_furniture_packs", FURNITURE_PACK_INDEX), new Pack("B9617", "dora_figure_packs", FIGURE_PACK_INDEX));
var bShowThumbnails = true;
var iCurrentThumb = 0;
var oPack = null;
var transformProductSkus = new Array();		// This holds products that are to have their main product shot clickable while not displaying it's thumbnails

// Determines if main product thumbnails should be displayed or the main image is clickable
// Lookup is performed by checking sku against an array of skus built from the asp page using a category list to retrieve products
// that need the desired functionality
function ShowThumbnails(prodId) {
	var sku = products[GetProdIndex(prodId)][kProdSku];
	var show = true;
	
			// Loop all products set to have the main image transform
	for (var i = 0; i < transformProductSkus.length; i++) {
				// Test for match by sku
		if (sku == transformProductSkus[i]) {
			show = false;
			i = transformProductSkus.length;
		}
	}
	
	return show;
}

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="/uk/img/s.gif" width="15" height="1"></td>' +
							'</tr>';
							if (prod[kProdHasCallout] == 'True') {
								sHtml +='<tr>' +
								        '<td colspan="2">' + 
									    '<img src="' + prod[kProdCalloutImage] + '"></td>' +
							            '</tr>';
							}
				
							sHtml += '<tr>' +
								'<td valign="top"><span class="title"><b>' + prod[kProdName] + '</b></span><span class="title_sm"><br><b>Product#:</b>' + prod[kProdSku] +'<br><b>Ages:</b>' + prod[kProdAge] +'</span></td></td>' +
							'</tr>' +
							'<tr>' +
								'<td><img src="/uk/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="/uk/dora/img/demo_pill.gif" width="75" height="25" alt="" border="0"></a><img src="/uk/img/s.gif" width="5" height="1">'
		if (bJSHasAwards) sHtml +=	'<a href="javascript: awardsPopUp(\''+ lCurProdId +'\', 10, 10, false);"><img src="/uk/dora/img/award_btn.gif" alt="Award Winner" border="0"></a>'
		sHtml +=				'</td>' +
							'</tr>' +
							'<tr>' +
								'<td><img src="/uk/img/s.gif" width="1" height="5"></td>' +
							'</tr>';
	}
	
	sHtml +=				//'<tr>' +
						//		'<td><a href="/uk/locator.asp?pid=' + prod[kProdID] + '"><img src="/uk/dora/img/whereToBuy_btn.gif" width="118" height="25" alt="Where To Buy" border="0"></a></td>' +
						//	'</tr>' +
						//	'<tr>' +
						//		'<td><img src="/uk/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="/uk/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">'
									if (oPack == null) {
									sHtml += '<img src="' + mainImg[iCurProd][img][kThumbPath].replace("_60_", "_b_") + '" width="243" height="243" id="mainImg" alt="' + prod[kProdAlt] + '" border="0"';
									}
									else {
									sHtml += '<img src="' + thumbnails[iCurProd][img][kThumbPath].replace("_60_", "_b_") + '" width="243" height="243" id="mainImg" alt="' + prod[kProdAlt] + '" border="0"';
									}
	
		// Make image clickable if applicable
	if (!bShowThumbnails && thumbnails[iCurProd].length > 0) {
		var packIndex = (oPack != null) ? oPack.ArrayIndex : 0;
		
		sHtml +=					' onclick="SetArrays(' + packIndex + '); 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">Click on an item below for more detail.' +
									'<div id="prodThumbs">' +
										BuildHtmlProductThumbs(iCurProd) +
									'</div>' +
								'</td>' +
							'</tr>';
		}
	}
	
	sHtml +=			'</table>' +
					'</td>' +
					'<td><img src="/uk/img/s.gif" width="15" height="1"></td>' +
					'<td><img src="/uk/img/s.gif" width="1" height="15"></td>' +
				'</tr>';
	
	if (oPack != null) {
		sHtml +='<tr>' +
					'<td align="center" class="SubScroll" colspan="5">Click on an item below for more detail.' +
						'<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="/uk/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(\'' + mainImg[iCurProd][iThumbIndex][kThumbPath] + '\', \'' + mainImg[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="/uk/dora/img/rarrow.gif" alt="" width="12" height="25" border="0"></a>';
		}
		sHtml += 			'<img src="/uk/img/s.gif" width="10" height="1" border="0">';
		sHtml += 		'</td>';
		
		sHtml += 		'<td><img src=/uk/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(){
	bShowThumbnails = ShowThumbnails(lProdId);
	document.write(BuildHtmlProductData(lProdId));
}

// Purpose:	To switch a thumbnail image with the main image
function EnlargeThumbnails(sJSImgSrc) {
	document.mainImg.src = sJSImgSrc.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 MAX_PROD_IN_ROW = 7;
	
	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">';
	
	// Thumbnails
	for (var i = 0; i < thumbs.length; i++){
		if (i == 0 || i == MAX_PROD_IN_ROW) { sHtml += '<tr><td align="center">'; }
		
		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 += 	'<a href="' + sLink + '"><img name="thumb' + i + '" src="' + thumbs[i][0][kThumbPath] + '" alt="" width="60" height="60" border="0"></a>' +
					'<img src="/img/s.gif" width="10" height="1" border="0">';
					
		if ((i + 1) == MAX_PROD_IN_ROW) { sHtml += '</td></tr>'; }
	}
	
	sHtml += 	'' +
			'</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(mainImg[iCurProd][iCurrentThumb][kThumbPath], 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;
		}
	}
}
