// JavaScript Document

var tb_nums = 12;
var bt_nums = 8;

var tb_normals = new Array();
var tb_overs = new Array();
var tb_clicks = new Array();
var tb_downs = new Array();

var bt_normals = new Array();
var bt_overs = new Array();
var bt_clicks = new Array();
var bt_downs = new Array();

var tb_select = -1;
var tb_temp = -1;

// Set bt_select to tumble
var bt_select = 0;
var bt_temp = -1;

// By default light is on and color is on
var bt_light_on = true;
var bt_color_on = true;
var bt_image_rendering_on = false;
var bt_tumble_on = true;

var tb_ids = new Array(
	0,
	1,
	2,
	3,
	4,
	5,
	6,
	7,
	8,
	9,
	10,
	11
	
);

var bt_ids = new Array(
	"tumble",
	"pan",
	"zoom",
	"rotate",
	"light",
	"color",
	"image_rendering",
	"default"
);

var bt_tooltips = new Array(
	"Camera Tumble",
	"Camera Pan",
	"Camera Zoom",
	"Camera Rotate",
	"Toggle Light",
	"Toggle Color",
	"Toggle Image Rendering",
	"Fit to View"
);
function startBusyCursor()
{
		document.body.style.cursor = 'wait';
}

function stopBusyCursor()
{
		document.body.style.cursor = 'default';
}

function showElement(id)
{
	var v = document.getElementById(id);
	v.style.display = 'block';
}

function hideElement(id)
{
	var v = document.getElementById(id);
	v.style.display = 'none';
//	v.hide();
}

function newImage(arg) 
{
	if (document.images) 
	{
		rslt = new Image();
		rslt.src = arg;
		return rslt;
	}
}

function initThumbImages()
{
	var tbimgs = document.getElementsByTagName("a");
	var index = 0;
	for (var i = 0; i < tbimgs.length; i++)
	{
		if (tbimgs[i].className == "thumbImageRef") 
		{
			var image = document.createElement("img");
			image.src=tb_normals[index].src;
			image.alt=tb_tooltips[index];
			image.title=tb_tooltips[index];
			image.width="120";
			image.height="93";
			image.border="0";
			image.id=tb_ids[index];
			image.name="thumbImage";
			
			tbimgs[i].appendChild(image);
			
			index++;
		}
	}
}

function initButtonImages()
{
	var btimgs = document.getElementsByTagName("a");
	var index = 0;
	for (var i = 0; i < btimgs.length; i++)
	{
		if (btimgs[i].className == "buttonImage") 
		{
			var image = document.createElement("img");
			image.src=bt_normals[index].src;
			image.alt=bt_tooltips[index];
			image.title=bt_tooltips[index];
			image.width="18";
			image.height="18";
			image.border="0";
			image.id=bt_ids[index];
			
			btimgs[i].appendChild(image);
			
			index++;
		}
	}
}

var preloadFlag = false;
function preloadImages() 
{
	startBusyCursor();
	
	// Default to inherite rendering params
	PsPlayerCtrl.method_InheritRenderingParams(true);
	
	// Default to set camera mode to freeform
	PsPlayerCtrl.method_SetCameraMode(0);

	// Default set zooming level to free
	PsPlayerCtrl.method_SetZoomingLevel(-1);
	
	// Default set shadow to on
	PsPlayerCtrl.method_SetShadowSwitch(true);
	
	if (document.images) 
	{
		// Create thumbnail images
		for (var i = 0; i < tb_nums; i++)
		{
			tb_normals[i] = newImage(tb_thumbimage_urls[i * 4 + 0]);
			tb_overs[i]   = newImage(tb_thumbimage_urls[i * 4 + 1]);
			tb_downs[i]   = newImage(tb_thumbimage_urls[i * 4 + 2]);
			tb_clicks[i]  = newImage(tb_thumbimage_urls[i * 4 + 3]);
		}

		// Create button images
		for (var i = 0; i < tb_nums; i++)
		{
			bt_normals[i] = newImage(bt_image_urls[i * 4 + 0]);
			bt_overs[i]   = newImage(bt_image_urls[i * 4 + 1]);
			bt_clicks[i]   = newImage(bt_image_urls[i * 4 + 2]);
			bt_downs[i]  = newImage(bt_image_urls[i * 4 + 3]);
		}

		preloadFlag = true;

		// Initialize thumbnail images
		initThumbImages();
		
		// Initialize button images
		initButtonImages();
		
		// By default, camera mode is in tumble, light is on, color is on and image rendering is off

		if (bt_tumble_on)
			document.images[bt_ids[0]].src = bt_clicks[0].src;
		if (bt_light_on)
			document.images[bt_ids[4]].src = bt_clicks[4].src;
		if (bt_color_on)
			document.images[bt_ids[5]].src = bt_clicks[5].src;
		if (bt_image_rendering_on)
		{
			document.images[bt_ids[6]].src = bt_clicks[6].src;
			SetImageRenderingQuality_Click(2);
		}
		else
		{
			SetImageRenderingQuality_Click(0);
		}
	}

	stopBusyCursor();
}

function tb_mouseOver(id) 
{
	if (document.images && (preloadFlag == true) && (tb_select != id)) 
	{
		document.images[tb_ids[id]].src = tb_overs[id].src;
	}
		
}

function tb_mouseOut(id) 
{
	if (document.images && (preloadFlag == true) && (tb_select != id)) 
	{
		document.images[tb_ids[id]].src = tb_normals[id].src;
	}
}

function tb_mouseDown(id) 
{
	if (document.images && (preloadFlag == true) && (tb_select != id)) 
	{
		document.images[tb_ids[id]].src = tb_downs[id].src;
	}
}

function tb_mouseClick(id) 
{
	if (document.images && (preloadFlag == true)) 
	{		
		document.images[tb_ids[id]].src = tb_clicks[id].src
		tb_temp = tb_select;
		if (tb_temp != -1) 
		{
			tb_mouseOut(tb_temp);
		}
		var v = document.getElementById("pageTitle");
		v.innerText = page_titles[id];

		var v = document.getElementById("pageDesc");
		v.innerText = page_description[id];
		
	}
	
	if (tb_temp != id)
	{
		PsPlayerCtrl.method_SetZoomingLevel(zooming_levels[id]);
		PsPlayerCtrl.method_SetRenderingEnv(tb_render_env_urls[id], true);
		var vRet = PsPlayerCtrl.method_SetDataURL(tb_3dimage_urls[id], true);
		if (vRet >= 0)
			tb_select = id;
	}	
}

function bt_mouseOver(id) 
{
	if (document.images && (preloadFlag == true))
	{ 
		if (id == 4)
		{
			if (bt_light_on)
				document.images[bt_ids[id]].src = bt_clicks[id].src;
			else
				document.images[bt_ids[id]].src = bt_overs[id].src;
		}
		else if (id == 5)
		{
			if (bt_color_on)
				document.images[bt_ids[id]].src = bt_clicks[id].src;
			else
				document.images[bt_ids[id]].src = bt_overs[id].src;
		}
		else if (id == 6)
		{
			if (bt_image_rendering_on)
				document.images[bt_ids[id]].src = bt_clicks[id].src;
			else
				document.images[bt_ids[id]].src = bt_overs[id].src;
		}
		else
		{
			if (bt_select != id)
				document.images[bt_ids[id]].src = bt_overs[id].src;
		}
	}
}

function bt_mouseOut(id) 
{
	if (document.images && (preloadFlag == true))
	{ 
		if (id == 4)
		{
			if (bt_light_on)
				document.images[bt_ids[id]].src = bt_clicks[id].src;
			else
				document.images[bt_ids[id]].src = bt_normals[id].src;
		}
		else if (id == 5)
		{
			if (bt_color_on)
				document.images[bt_ids[id]].src = bt_clicks[id].src;
			else
				document.images[bt_ids[id]].src = bt_normals[id].src;
		}
		else if (id == 6)
		{
			if (bt_image_rendering_on)
				document.images[bt_ids[id]].src = bt_clicks[id].src;
			else
				document.images[bt_ids[id]].src = bt_normals[id].src;
		}
		else
		{
			if (bt_select != id) 
				document.images[bt_ids[id]].src = bt_normals[id].src;
		}
	}
}

function bt_mouseDown(id) 
{
	if (document.images && (preloadFlag == true) && (bt_select != id)) 
	{
		document.images[bt_ids[id]].src = bt_downs[id].src;
	}
}

function bt_mouseClick(id) 
{
	if (id <= 3)
	{
		if (document.images && (preloadFlag == true)) 
		{
			document.images[bt_ids[id]].src = bt_clicks[id].src
			bt_temp = bt_select;
			bt_select = id;
			if (bt_temp != -1) 
			{
				bt_mouseOut(bt_temp);
			}
		}
		
		if (bt_temp != id)
		{
			if (id == 0)
			{
				CameraActionTumble_Click();
			}
			else if (id == 1)
			{
				CameraActionPanning_Click();
			}
			else if (id == 2)
			{
				CameraActionZooming_Click();
			}
			else if (id == 3)
			{
				CameraActionTwist_Click()
			}
		}		
	}
	else if (id == 4)
	{
		bt_light_on = !bt_light_on;
		if (document.images && (preloadFlag == true)) 
		{
			if (bt_light_on)
			{
				document.images[bt_ids[id]].src = bt_normals[id].src;
			}
			else
			{
				document.images[bt_ids[id]].src = bt_clicks[id].src;
			}			
		}
		
		ToggleLightSwitch_Click();
	}
	else if (id == 5)
	{
		bt_color_on = !bt_color_on;
		if (document.images && (preloadFlag == true)) 
		{
			if (bt_color_on)
			{
				document.images[bt_ids[id]].src = bt_normals[id].src;
			}
			else
			{
				document.images[bt_ids[id]].src = bt_clicks[id].src;
			}			
		}
		
		ToggleColor_Click();
	}
	else if (id == 6)
	{
		bt_image_rendering_on = !bt_image_rendering_on;
		if (document.images && (preloadFlag == true)) 
		{
			if (bt_image_rendering_on)
			{
				document.images[bt_ids[id]].src = bt_normals[id].src;
				SetImageRenderingQuality_Click(2);
			}
			else
			{
				document.images[bt_ids[id]].src = bt_clicks[id].src;
				SetImageRenderingQuality_Click(0);
			}			
		}
	}
	else if (id == 7) 
	{
		if (document.images && (preloadFlag == true)) 
		{
			document.images[bt_ids[id]].src = bt_normals[id].src
		}

		FitViewToDocument_Click();
	}
}	

function changeImages() {
	if (document.images && (preloadFlag == true)) {
		for (var i=0; i<changeImages.arguments.length; i+=2) {
			document[changeImages.arguments[i]].src = changeImages.arguments[i+1];
		}
	}
}

function CameraActionFreeForm_Click() {
	PsPlayerCtrl.method_SetCameraAction(4);
}

function CameraActionTumble_Click() {
	PsPlayerCtrl.method_SetCameraAction(4);
}

function CameraActionPanning_Click() {
	PsPlayerCtrl.method_SetCameraAction(1);
}

function CameraActionZooming_Click() {
	PsPlayerCtrl.method_SetCameraAction(2);
}

function CameraActionTwist_Click() {
	PsPlayerCtrl.method_SetCameraAction(3);
}

function ToggleLightSwitch_Click() {
	PsPlayerCtrl.method_ToggleLight(true);
}

function ToggleColor_Click() {
	PsPlayerCtrl.method_ToggleColor(true);
}

function SetImageRenderingQuality_Click(quality) {
	PsPlayerCtrl.method_SetImageRenderingQuality(quality, true);
}

function FitViewToDocument_Click() {
	PsPlayerCtrl.method_FitViewToDocument();
}

