// ONLOADS
window.addEvent('domready', function(){		

	if ( $chk( $('chips_header') ) ) 
	{		
		$('chips_header').fade('out');
		setTimeout( "$('chips_header').setStyle('display','block');", 500 );
		$('chips_header_wood').fade('out');
		setTimeout( "$('chips_header_wood').setStyle('display','block');", 500 );
		unSetChips();
		setTimeout( "$$('.colorChip').setStyle('display','block');", 500 );
	}
});

// FINISHES FUNCTIONS
var currentProductID = '';
var currentMaterialID = '';
var currentDoorStyleID = '';
var currentMaterialType = '';
var currentBeautyImage = '';
var currentBeautyTextArea = '';
var currentThumb = '';
var last_orientation = 'none';
var orientation = '';
var currentDoorDisplayImage = '';
var doorSwapCount = 0;
var beautySwapCount = 0;
var pixelShift = '';
var vertScrollAmount = '';
var kB = false;
	
// Chips
currentChip = 'none';
function showChipDetail(chip)
{
	if ( currentChip != 'none' )
	{
		hideChipDetail(currentChip);
	}
	chipName = "colorChip" + chip;
	if ( $(chipName).getProperty('trigger') == 'true' )
	{
		chipControl = "chip" + chip;
		chipPop = "chip" + chip + "pop";
		$(chipPop).setStyle('visibility','visible');
		$(chipControl).setProperty('class','chip_up');
		currentChip = chip;
	}
}

function hideChipDetail(chip)
{
	chipControl = "chip" + chip;
	chipPop = "chip" + chip + "pop";
	$(chipPop).setStyle('visibility','hidden');
	$(chipControl).setProperty('class','chip');
	currentChip = 'none';
}

// GALLERY FUNCTIONS
// LOAD checks
if ( $chk( $('door_image2') ) ) 
{ 
	$('door_image_shadow').fade('out');
	$('door_image2').fade('out'); 
	currentDoorDisplayImage = 'door_image1';					
}

if ( $chk( $('beauty_image1') ) ) 
{ 
	$('beauty_image1').fade('out'); 
	$('beauty_image1_v').fade('out'); 
	$('beauty_image2').fade('out'); 
	$('beauty_image2_v').fade('out'); 
}

function showDoorSlider(thumb,pp_id)
{
	if ( currentChip != 'none' )
	{
		hideChipDetail(currentChip);
	}
	unSetChips();
	//console.log ("1: " + thumb + ", " + pp_id);
 	getImgData(pp_id,thumb,'door'); //JSON to Server
}

function flipBeautyShot(thumb,pp_id,pxlShift,vh)
{
	//console.log(orientation);
	pixelShift = parseInt(pxlShift);
	orientation = vh;
	//console.log(orientation);
 	getImgData(pp_id,thumb,'des'); //JSON to Server
}

function getChips(id,type,pp_id)
{
		//console.log("2: " + id + ", " + type + ", " + pp_id);
		if ( type == 'foil' )
		{
 			getImgData(id,pp_id,'chip-' + type ); //JSON to Server
		}
		else
		{
 			getImgData(id,currentDoorStyleID,'chip-' + type,pp_id ); //JSON to Server
		}
}

function chipLoadPause(chipName,imagePath)
{
	var chipImage = new Asset.image( imagePath, {
		onload: function() {
			$(chipName).src = chipImage.src;
			$(chipName).fade('in');			
			$(chipName).setAttribute('trigger','true');
		}
	});
}

function doorLoadPause(doorName,imagePath)
{
	var doorImage = new Asset.image( imagePath, {
		onload: function() {
			$(doorName).src = doorImage.src;
			if ( doorName == 'door_image2' )
			{
				DxFade('door_image2','door_image1');
			}
			else
			{
				DxFade('door_image1','door_image2');
			}
		}
	});
}

function setChips(imgData)
{
	type = unescape(imgData.type);
	material = unescape(imgData.material);
	stains = unescape(imgData.stains);
	paints = unescape(imgData.paints);
	colors = unescape(imgData.colors);
	woods = unescape(imgData.woods);
	
	if ( type == 'wood' )
	{
		allStains = stains.split("||");
		x2 = 0;
		for ( x = 0; x < allStains.length ; x++ )
		{
			thisStain = allStains[x].split(":");
			if ( typeof thisStain[1] != 'undefined' && thisStain[1] != '' )
			{
				chipName = "colorChip" + x2;
				bigChipName = "bigChip" + x2;
				chipText = "chipText" + x2;
				imagePath = URL_ADJUSTMENT + thisStain[1];
				
				document.images[bigChipName].src = imagePath;
				
				stainDescription = material + " with <BR>" + thisStain[0] + " Stain";
				$(chipText).set('html',stainDescription);
				
				//$(chipName).removeEvents();
				chipLoadPause(chipName,imagePath)
				x2++;
			}
		} 
		
		allPaints = paints.split("||");
		for ( x = 0; x < allPaints.length ; x++ )
		{
			thisPaint = allPaints[x].split(":");
			if ( typeof thisPaint[1] != 'undefined' && thisPaint[1] != '' )
			{
				chipName = "colorChip" + x2;
				bigChipName = "bigChip" + x2;
				chipText = "chipText" + x2;
				imagePath = URL_ADJUSTMENT + thisPaint[1];
	
				document.images[bigChipName].src = imagePath;
	
				paintDescription = material + " with <BR>" + thisPaint[0] + " Paint";
				$(chipText).set('html',paintDescription);
	
				//$(chipName).removeEvents();
				chipLoadPause(chipName,imagePath)
				x2++;
			}
		} 

		x2 = 48;
		allWoods = woods.split("||");
		for ( x = 0; x < allWoods.length ; x++ )
		{
			thisWood = allWoods[x].split(":");
			if ( typeof thisWood[0] != 'undefined' && thisWood[0] != '' ) //Change back to [1] when thumbs are in
			{
				chipName = "colorChip" + x2;
				bigChipName = "bigChip" + x2;
				chipText = "chipText" + x2;
				//imagePath = URL_ADJUSTMENT + thisWood[1];
				imagePath = URL_ADJUSTMENT + thisWood[1];
				document.images[bigChipName].src = imagePath;
	
				woodDescription = "<b>" + thisWood[0] + "</b><BR><BR>Click to view stains for " + thisWood[0];
				$(chipText).set('html',woodDescription);
	
				//$(chipName).removeEvents();
				woodChipLinkerID = chipName + '_link';
				chipLoadPause(chipName,imagePath)
				linkURL = "finishing/gallery.php?c=pfs&g=f&scr=" + thisWood[2];
				if ( $chk($(woodChipLinkerID)) ) { $(woodChipLinkerID).setProperty('href',linkURL); }
				x2++;
			}
		} 
		
	}
	else
	{
		x2 = 0;
		allColors = colors.split("||");
		for ( x = 0; x < allColors.length ; x++ )
		{
			thisColor = allColors[x].split(":");
			if ( typeof thisColor[1] != 'undefined' && thisColor[1] != '' )
			{
				chipName = "colorChip" + x2;
				bigChipName = "bigChip" + x2;
				chipText = "chipText" + x2;
				imagePath = URL_ADJUSTMENT + thisColor[1];
	
				document.images[bigChipName].src = imagePath;
	
				paintDescription = thisColor[0] + ' ' + material;
				$(chipText).set('html',paintDescription);
	
				//$(chipName).removeEvents();
				chipLoadPause(chipName,imagePath)
				x2++;
			}
		} 
	}
	
	if ( (paints == 'none' || paints == '') && (stains == 'none' || stains == '') && (colors == 'none' || colors == '') && (woods == 'none' || woods == '') )
	{
		$('chips_header').fade('out'); 
		$('chips_header_wood').fade('out'); 
	}
	else 
	{
		if ( type == 'wood' )
		{
			$('chips_header').fade('out'); 
			$('chips_header_wood').fade('in'); 
		}
		else
		{
			$('chips_header').fade('in'); 
			$('chips_header_wood').fade('out'); 
		}
	}
}

function unSetChips()
{
	for ( x = 0 ; x < 60 ; x++ ) 
	{
		chipName = "colorChip" + x;
		$(chipName).fade('out');
		$(chipName).setAttribute('trigger','false');
		setTimeout( "$(chipName).setAttribute('src', URL_ADJUSTMENT + '/_framework/gfx/msc/pxl.gif');", 500 );
	}
}

function getImgData(var1,var2,source,product_id)
{	
	var source = new String(source);
	//alert(thumb);	
	//alert(id);	
	//alert(source);	
	new Request({
		url: 'http://' + DOMAIN + '/_pdwms/products/support/php/responder.php', 		
		method: 'post',
		data:{ 'id':var1, 'id2':var2, 'funky':source, 'product_id':product_id },
		onComplete: function (){
			imgData = JSON.decode(this.response.text);
			if ( source == "door" ) 
			{ 
				setDoorData(var2,imgData); // Door Gallery Path
			}
			else
			if ( source.indexOf('chip') > -1 ) 
			{ 
				setChips(imgData); 
			}
			else
			{
				setBeautyShotData(var2,imgData); // Design Gallery Path
			}
		}
	}).send();
	
}

function zInd(img1,img2)
{
	$(img1).setStyle('zIndex',200);
	if ( img2 ) { $(img2).setStyle('zIndex',100); }
}


function beautyLoadPause(beautyName,imagePath,nextBeautyImage,currentBeautyImage,beauty_text_space_name)
{
	var beautyImage = new Asset.image( imagePath, {
		onload: function() {
			$(beautyName).setStyle('top', 0 );
			$(beautyName).src = beautyImage.src;
			BIxFade(nextBeautyImage,currentBeautyImage);
		
			if ( pixelShift < 0 ) 
			{ 
				if ( orientation == 'h' )
				{
					$(nextBeautyImage).setStyle('top', pixelShift ); 
				}
				else
				{
					$(nextBeautyImage).setStyle('left', pixelShift ); 
				}
			}

			$(beauty_text_space_name).set('html',buildDescription);
			if ( orientation == 'h' )
			{
				$('beauty_text_desgal_v').fade('out');
				setTimeout("$('beauty_text_desgal').fade('in')", 250);
			}
			else
			{
				$('beauty_text_desgal').fade('out');
				setTimeout("$('beauty_text_desgal_v').fade('in')", 250);
			}
	
			currentBeautyTextArea = beauty_text_space_name;
			last_orientation = orientation;
		}
	});
}

function BIxFade(img1,img2) // Ambiguous for all paths, CROSSFADERS
{
	if ( orientation == 'v' )
	{
		$(img1).setStyle('width', '300px');
		heightCalc = $(img1).getStyle('height');
		
		if ( heightCalc.replace('px','') < 400 )
		{
			$(img1).setStyle('height', '400px');
		}
	}
	//console.log(	$(img1).getStyle('height') ) ;
	$(img1).fade('in');
	if ( orientation == 'h' )
	{
		if ( beautySwapCount > 0 )
		{
			if ( last_orientation == 'v' )
			{
				//console.log('beauty_image_desgal_v out');
				$('beauty_image_desgal_v').fade('out');
			}
		}
		//console.log('beauty_image_desgal in');
		$('beauty_image_desgal').fade('in');
	}
	else
	{
		if ( beautySwapCount > 0 )
		{
			if ( last_orientation == 'h' )
			{
				//console.log('beauty_image_desgal out');
				$('beauty_image_desgal').fade('out');
			}
		}
		//console.log('beauty_image_desgal_v in');
		$('beauty_image_desgal_v').fade('in');
	}
	
	if ( img2 ) 
	{ 
		//console.log('Fade out : =' + img2 + '=');
		$(img2).fade('out');
	}
	//zInd(img1,img2);
	currentBeautyImage = nextBeautyImage;	
	beautySwapCount++;
}

function DxFade(img1,img2) // Ambiguous for all paths, CROSSFADERS
{

	//console.log("Fade: " + img1 + ", " + img2);
	$(img1).fade('in');
	if ( img2 ) { $(img2).fade('out'); }
	if ( workbook_logged_in )
	{
		set_gallery_action([wbiconimage],$('door_image'));
	}
}

function doorShadowFadein() // Ambiguous for all paths, CROSSFADERS
{	
	if ( doorSwapCount == 0 )
	{
		$('door_image_shadow').fade('in');
	}
	doorSwapCount++;
}

function swapThumbs(thumb)
{
	if ( currentThumb != '' )
	{
		$(currentThumb).setStyle('border-color','#F0E5C7');
	}
	$(thumb).setStyle('border-color','#ffffff');
	currentThumb = thumb;
}

function setBeautyShotData(thumb,imgData)
{
	imgPath = unescape(imgData.imagePath);

	orientationAddon = ( orientation == 'v' ) ?  "_v" : "";

	switch(currentBeautyImage)
	{
		case 'beauty_image1':
			nextBeautyImage = ( orientation == 'h' ) ? 'beauty_image2' : 'beauty_image1_v';
			break;
		case 'beauty_image2':
			nextBeautyImage = ( orientation == 'h' ) ? 'beauty_image1' : 'beauty_image1_v';
			break;
		case 'beauty_image1_v':
			nextBeautyImage = ( orientation == 'h' ) ? 'beauty_image1' : 'beauty_image2_v';
			break;
		case 'beauty_image2_v':
			nextBeautyImage = ( orientation == 'h' ) ? 'beauty_image1' : 'beauty_image1_v';
			break;
		default:
			nextBeautyImage = ( orientation == 'h' ) ? 'beauty_image1' : 'beauty_image1_v';
			break;
	}	
		
	doorstyle = unescape(imgData.doorstyle);
	doortype = unescape(imgData.doortype);
	material = unescape(imgData.material);
	materialID = unescape(imgData.materialID);
	finish = unescape(imgData.finish);
	//artistry = unescape(imgData.artistry);
	drawer = unescape(imgData.drawer);
	credit = unescape(imgData.credit);
	
	buildDescription = '';
	if ( orientation == 'v' ) { brspInj = "<BR>"; } else { brspInj = '&nbsp;'; }
	
	if ( doorstyle != '' ) { buildDescription += "<B>Door&nbsp;Style:</B>" + brspInj + unescape(imgData.doorstyle).replace(" ","&nbsp;") + " "; }
	
	if ( doortype != '' ) { buildDescription += brspInj + brspInj + " "; }
	if ( doortype != '' ) { buildDescription += "<B>Door&nbsp;Construction:</B>" + brspInj + unescape(imgData.doortype).replace(" ","&nbsp;") + " "; }
	
	if ( material != '' ) { buildDescription += brspInj + brspInj + " "; }
	if ( material != '' ) { buildDescription += "<B>Material:</B>" + brspInj + unescape(imgData.material).replace(" ","&nbsp;") + " "; }
	
	if ( finish != '' ) { buildDescription += brspInj + brspInj + " "; }
	if ( finish != '' ) { buildDescription += "<B>Finish:</B>" + brspInj + unescape(imgData.finish).replace(" ","&nbsp;") + " "; }

	//if ( artistry != '' ) { buildDescription += "<B>Artistry:</B>" + brspInj + " + unescape(imgData.artistry).replace(" ","&nbsp;") + "<BR>"; }
	
	if ( drawer != '' ) { buildDescription += brspInj + brspInj + " "; }
	if ( drawer != '' ) { buildDescription += "<B>Drawer:</B>" + brspInj + drawer.replace(" ","&nbsp;"); }

	if ( credit != '' ) { buildDescription += brspInj + brspInj + " "; }
	if ( credit != '' ) { buildDescription += "<B>Photo&nbsp;Credit:</B>" + brspInj + credit.replace(" ","&nbsp;") + " "; }

	if ( orientation == 'v' ) { buildDescription += "<BR><BR>Click the image to see full size." }
	
	beauty_text_space_name = 'beauty_text_desgal' + orientationAddon;
	beautyLoadPause(nextBeautyImage,'..' + imgPath,nextBeautyImage,currentBeautyImage,beauty_text_space_name)	
	
	imageLinkerID = nextBeautyImage + '_link';
	linkURL = URL_ADJUSTMENT + imgPath;
	if ( $chk($(imageLinkerID)) ) { $(imageLinkerID).setProperty('href',linkURL); }
	if ( $defined(Shadowbox) && $chk($(imageLinkerID)) )
	{
		tmpArr = new Array(document.getElementById(imageLinkerID));
		Shadowbox.setup(tmpArr); 	
	}
	// add workbook icon.
	
	if (workbook_logged_in)
	{
		//console.log($(nextBeautyImage));
		set_gallery_action([$(nextBeautyImage)],$(nextBeautyImage).getParent());
	}
}

function setDoorData(thumb,imgData)
{	
	/*
	console.log("doorstyle" + unescape(imgData.doorstyle));
	console.log("doortype" + unescape(imgData.doortype));
	console.log("material" + unescape(imgData.material));
	console.log("finish" + unescape(imgData.finish));
	console.log("drawer" + unescape(imgData.drawer));
	console.log("pp_id" + unescape(imgData.pp_id));
	console.log("pds_id" + unescape(imgData.pds_id));
	console.log("materialID" + unescape(imgData.materialID));
	console.log("materialType" + unescape(imgData.materialType));
	*/
		
	swapThumbs(thumb);
		
	doorstyle = unescape(imgData.doorstyle);
	doortype = unescape(imgData.doortype);
	material = unescape(imgData.material);
	finish = unescape(imgData.finish);
	//artistry = unescape(imgData.artistry);
	drawer = unescape(imgData.drawer);
	
	buildDescription = '';
	if ( doorstyle != '' ) { buildDescription += "<B>Door Style:</B><Br>" + doorstyle; }
	if ( doortype != '' ) { buildDescription += "<Br><BR>"; }

	if ( doortype != '' ) { buildDescription += "<B>Door Type:</B><Br>" + doortype; }
	if ( material != '' ) { buildDescription += "<Br><BR>"; }

	if ( material != '' ) { buildDescription += "<B>Material:</B><Br>" + material; }
	$('woodinsert').set('html',material);
	
	if ( finish != '' ) { buildDescription += "<Br><BR>"; }

	if ( finish != '' ) { buildDescription += "<B>Finish:</B><Br>" + finish; }
	if ( drawer != '' ) { buildDescription += "<Br><BR>"; }

	//if ( artistry != '' ) { buildDescription += "<B>Artistry:</B> " + unescape(imgData.artistry) + "<BR>"; }
	//if ( doortype != '' ) { buildDescription += "<Br><BR>"; }

	if ( drawer != '' ) { buildDescription += "<B>Drawer:</B><Br>" + drawer + ""; }
	//console.log(imgData);
	currentProductID = unescape(imgData.pp_id);
	currentDoorStyleID = unescape(imgData.pds_id);
	currentMaterialID = unescape(imgData.materialID);
	currentMaterialType = unescape(imgData.materialType);
	//alert(currentMaterialType + "++" + currentMaterialID);		
	//console.log("3: " + currentMaterialID + "," + currentMaterialType + "," + currentProductID );
	if ( currentDoorDisplayImage == 'door_image1' )
	{
		doorLoadPause('door_image2','../' + unescape(imgData.imagePath));
		$('door_text').set('html',buildDescription);
		currentDoorDisplayImage = 'door_image2';
		doorShadowFadein();
		getChips(currentMaterialID,currentMaterialType,currentProductID);
		wbiconimage = $('door_image2');
	}
	else 
	{
		doorLoadPause('door_image1','../' + unescape(imgData.imagePath));
		$('door_text').set('html',buildDescription);
		currentDoorDisplayImage = 'door_image1';	
		doorShadowFadein();
		getChips(currentMaterialID,currentMaterialType,currentProductID);
		wbiconimage = $('door_image1');
			
	}

	if ( workbook_logged_in )
	{
		set_gallery_action([wbiconimage],$('door_image'));
	}
	
}
