// Illuminatus HTML4 export Javascript include file
// (C) 1999 Digital Workshop

// This file may only be used with HTML4 exported from Illuminatus 4.5, and may be freely distributed
// with files exported from Illuminatus 4.5. Any other use of this file, or any part of this file, is 
// forbidden without express permission from Digital Workshop.

// See http://www.digitalworkshop.co.uk for more information on Illuminatus.

function Show(object)
{
	//if (object.style.visibility == "visible") return;

	var has_filter = (object.filters[0] != null && object.filters[0].duration != null);
	var has_scroll_on = object.ILMScrollOn != null;

	if (has_scroll_on) {
		if (object.ILMScrollOn.scroll_position != -2) return;
		object.ILMScrollOn.scroll_position = -1;
		object.ILMScrollOn.Update();
		object.style.visibility = "visible";
		return;
	}
	
	if (has_filter) object.filters[0].Apply();
	object.style.visibility = "visible";

	if (has_filter) {
		if (object.onILMShow != null) {
			object.onfilterchange = FilterChange;
		}
		object.filters[0].Play();
	} else {
		if (object.onILMShow != null) {
			object.onILMShow();
		}
	}
}

function FilterChange()
{
	event.srcElement.onILMShow();
}

function Hide(object)
{
	//if (object.style.visibility == "hidden") return;

	var has_filter = (object.filters[1] != null && object.filters[1].duration != null);
	var has_scroll_off = object.ILMScrollOff != null;

	if (has_scroll_off) {
		object.ILMScrollOff.scroll_position = -1;
		object.ILMScrollOff.Update();
		return;
	}

	if (has_filter) object.filters[1].Apply();
	object.style.visibility = "hidden";
	if (has_filter) object.filters[1].Play();
}

function MakeTextInput(letters, numbers, others, limit_length, translate_case)
{
	this.letters = letters;
	this.numbers = numbers;
	this.others = others;
	this.limit_length = limit_length;
	this.translate_case = translate_case;
	return this;
}

function TextInputKeyPressed(object, event)
{
	var key = String.fromCharCode(event.keyCode);
	if ((key >= 'a' && key <= 'z') || (key >= 'A' && key <= 'Z')) {
		if (!(object.ILMTextInput.letters)) event.returnValue = false;
	} else if (key >= '0' && key <= '9') {
		if (!(object.ILMTextInput.numbers)) event.returnValue = false;
	} else {
		if (!(object.ILMTextInput.others)) event.returnValue = false;
	}
	if (object.ILMTextInput.limit_length > 0) {
		var contents = object.innerText;
		if (contents.length >= object.ILMTextInput.limit_length) event.returnValue = false;
	}

	if (object.ILMTextInput.translate_case == "uppercase" && key >= 'a' && key <= 'z') {
		event.keyCode = key.toUpperCase().charCodeAt(0);
	}
	if (object.ILMTextInput.translate_case == "lowercase" && key >= 'A' && key <= 'Z') {
		event.keyCode = key.toLowerCase().charCodeAt(0);
	}
}

//=============================================================================
//  Variable Functions

function SetVariable(variable, value, overwrite, avoid_object)
{
	if (parent.ILMVariables == null) parent.ILMVariables = new Object;

	if (!overwrite) value = parent.ILMVariables[variable] + value;

	parent.ILMVariables[variable] = value;

	for (var i=0; i < document.all.length; i++) {
		var object = document.all[i];
		if (object.ILMVARIABLE == variable && object != avoid_object) {
			object.innerText = value;
		}
	}
}

function GetVariable(variable)
{
	if (parent.ILMVariables == null) parent.ILMVariables = new Object;

	var value = parent.ILMVariables[variable];
	if (value == null) value = "";

	return value;
}

function GetVariableInt(variable)
{
	var value = GetVariable(variable);
	return parseInt(value+0);
}

//=============================================================================
//  Score Functions

function ResetScore()
{
	SetVariable("SCORE_VALUE", 0, true);
	SetVariable("SCORE_CORRECT", 0, true);
	SetVariable("SCORE_TOTAL", 0, true);
	SetVariable("SCORE_PERCENT", 0, true);
}

function ResetAllVariables()
{
	if (parent.ILMVariables == null) parent.ILMVariables = new Object;
	for (var_name in parent.ILMVariables) {
		SetVariable(var_name, "", true);
	}
	ResetScore();

}

function StoreScore(correct, value)
{
	if (value != 0) {
		var score_value = GetVariableInt("SCORE_VALUE");
		SetVariable("SCORE_VALUE", score_value + value, true);
	}

	var score_correct = GetVariableInt("SCORE_CORRECT");
	var score_total = GetVariableInt("SCORE_TOTAL");

	if (correct) {
		score_correct++;
		SetVariable("SCORE_CORRECT", score_correct, true);
	}

	score_total++;
	SetVariable("SCORE_TOTAL",   score_total, true);

	var score_percent = Math.round((score_correct*100)/score_total);
	SetVariable("SCORE_PERCENT", score_percent, true);
}

function UpdateScore(variable, add, value)
{
	var score_value   = GetVariableInt("SCORE_VALUE");
	var score_correct = GetVariableInt("SCORE_CORRECT");
	var score_total   = GetVariableInt("SCORE_TOTAL");

	if (variable.length <= 0) return;
	var update_value = GetVariable(variable);
	var question_scored_previous = (""+update_value).length > 0;
	var question_previous_score  = update_value + 0;

	var new_question_score = value;
	if (add) new_question_score += question_previous_score;
	SetVariable(variable, new_question_score, true);

	score_value += new_question_score - question_previous_score;
	SetVariable("SCORE_VALUE", score_value, true);

	if (question_scored_previous && question_previous_score > 0) score_correct--;
	if (new_question_score > 0) score_correct++;
	SetVariable("SCORE_CORRECT", score_correct, true);

	if (!question_scored_previous) score_total++;
	SetVariable("SCORE_TOTAL", score_total, true);

	var score_percent = Math.round((score_correct*100)/score_total);
	SetVariable("SCORE_PERCENT", score_percent, true);
}

//=============================================================================
//  Animation Functions

function makeAnimatedObject(type,object,x,y,param1,param2)
{
	this.type = type;
	this.object = object;
	this.startX = x;
	this.startY = y;
	if (type == "scrollOn" || type == "scrollOff") {
		this.scroll_time  = param1;
		this.scroll_angle = param2;
		this.scroll_position = -2;
	} else {
		this.speed = param1;
		this.times = param2;
		this.times_index = 0;
		this.dx = 1;
		this.dy = 1;
	}

	this.Update = UpdateAnimation;
	return (this);
}

function UpdateAnimation()
{
	if (this.type == "scrollOn" || this.type == "scrollOff") {
		if (this.object.style.visibility == "hidden") return;
		if (this.scroll_position == -2) return;
		if (this.scroll_position == -1) {
			this.scroll_position = 0;

			var bounding = new Object;
			bounding.left   = -this.object.style.posWidth;
			bounding.right  = this.object.parentElement.style.posWidth + this.object.style.posWidth;
			bounding.top    = -this.object.style.posHeight;
			bounding.bottom = this.object.parentElement.style.posHeight + this.object.style.posHeight;

			var pos = new Object;
			pos.x = this.startX;
			pos.y = this.startY;

			start = EdgeIntersect(bounding, pos, this.scroll_angle);

			this.scroll_end_x = start.x;
			this.scroll_end_y = start.y;

			if (this.type == "scrollOn") {
				this.object.style.posLeft = start.x;
				this.object.style.posTop  = start.y;
			}

			return;
		}
		if (this.scroll_position > this.scroll_time) {
			if (this.type == "scrollOff") this.object.style.visibility = "hidden";
			if (this.type == "scrollOn") {
				if (this.object.onILMShow != null) {
					this.object.onILMShow();
				}
			}
			this.scroll_position = -2;
			return;
		}
		this.scroll_position += 10;

		var fraction = this.scroll_position/this.scroll_time;
		if (this.type == "scrollOff") fraction = 1.0 - fraction;

		this.object.style.posLeft = this.scroll_end_x*(1.0-fraction) + this.startX*fraction;
		this.object.style.posTop  = this.scroll_end_y*(1.0-fraction) + this.startY*fraction;

		return;
	}

	if (this.object.style.visibility == "hidden") 
	{
		this.times_index = 0;
		this.object.style.posLeft = this.startX;
		this.object.style.posTop  = this.startY;
		return;
	}
	if (this.times_index >= this.times &&
		this.times != -1 && this.object.style.posLeft == this.startX && this.object.style.posTop == this.startY) return; 

	var obj = this.object;
	if (this.type == "bounceH" || this.type == "bounce") {
		obj.style.posLeft += (this.dx*this.speed);
		if (obj.style.posLeft > this.object.parentElement.style.posWidth - obj.style.posWidth) {
			obj.style.posLeft = this.object.parentElement.style.posWidth - obj.style.posWidth;
			this.dx = -1;
			this.times_index++;
		}
		if (obj.style.posLeft < 0) {
			obj.style.posLeft = 0
			this.dx = 1;
			this.times_index++;
		}
	}
	if (this.type == "bounceV" || this.type == "bounce") {
		obj.style.posTop += (this.dy*this.speed);
		if (obj.style.posTop > this.object.parentElement.style.posHeight - obj.style.posHeight) {
			obj.style.posTop = this.object.parentElement.style.posHeight - obj.style.posHeight;
			this.dy = -1;
			this.times_index++;
		}
		if (obj.style.posTop < 0) {
			obj.style.posTop = 0
			this.dy = 1;
			this.times_index++;
		}
	}
}

function EdgeIntersect(bounding, start, angle)
{
	var result = new Object;
	var DEG_TO_RAD = 3.141592653/180.0;

	if (angle == 90 && angle == 270) {
		// Special cases left and right
		if (angle ==  90) { result.x = bounding.right; result.y = start.y; return result; }
		if (angle == 270) { result.x = bounding.left;  result.y = start.y; return result; }
	}
	else if (angle > 90 && angle < 270) {
		// Intersect with bottom
		result.x = start.x + (start.y - bounding.bottom) * Math.tan(DEG_TO_RAD * angle);
		result.y = bounding.bottom;
		if (result.x >= bounding.left && result.x < bounding.right) return result;
	} 
	else	{
		// Intersect with top
		result.x = start.x - (bounding.top - start.y) * Math.tan(DEG_TO_RAD * angle);
		result.y = bounding.top;
		if (result.x >= bounding.left && result.x < bounding.right) return result;
	}

	if (angle < 180) {
		// Intersect with right
		result.x = bounding.right;
		result.y = start.y + (bounding.right - start.x) * Math.tan(DEG_TO_RAD * (angle-90));
		return result;
	} else {
		// Intersect with left
		result.x = bounding.left;
		result.y = start.y + (start.x - bounding.right) * Math.tan(DEG_TO_RAD * (angle-270));
		return result;
	}
}

function ResizePageContents()
{
	for (var i=0; i < document.all.length; i++) {
		var object = document.all[i];
		if (object.ILMVARIABLE != null) {
			object.innerText = GetVariable(object.ILMVARIABLE);
		}
		if (object.onILMUpdate != null) {
			ILMUpdateList[update_index] = object;
			update_index++;
		}
		if (object.style.position == "absolute") {
			object.ILMOriginalPosition = new Object;
			object.ILMOriginalPosition.left   = object.style.posLeft;
			object.ILMOriginalPosition.top    = object.style.posTop;
			object.ILMOriginalPosition.width  = object.style.posWidth;
			object.ILMOriginalPosition.height = object.style.posHeight;
		}
	}

	OnWindowResize();
	Show(ILMPageDIV);
}

function Start()
{
	ILMUpdateList = new Array();
	var update_index = 0;

	for (i=0; i < document.all.length; i++) {
		var object = document.all[i];
		if (object.onILMPageLoad != null) {
			object.onILMPageLoad();
		}
	}

	if (ILMUpdateList.length > 0) {
		tick();
	}
}

function tick()
{
	for (var i=0; i < ILMUpdateList.length; i++) {
		ILMUpdateList[i].onILMUpdate();
	}
	setTimeout("tick();", 10);
}

function OnWindowResize()
{
	var page_frame = document.all.tags("DIV")[0];

	if (page_frame.ILMRESIZE != "fixed") {
		var scale_x = document.body.clientWidth/page_frame.ILMOriginalPosition.width;
		var scale_y = document.body.clientHeight/page_frame.ILMOriginalPosition.height;
		if (scale_x < 1.0) scale_x = 1.0;
		if (scale_y < 1.0) scale_y = 1.0;
		if (page_frame.ILMRESIZE == "aspect") {
			if (scale_x < scale_y) scale_y = scale_x;
			else                   scale_x = scale_y;
		}

		ResizeObject(page_frame, scale_x, scale_y);
	} else {
		if (page_frame.style.clip.substring(0,4) != "rect") {
			ResizeObject(page_frame, 1.0, 1.0);
		}
	}
	if (page_frame.ILMRESIZE == "fixed" || page_frame.ILMRESIZE == "aspect") {
		var left = (document.body.clientWidth  - page_frame.style.posWidth)/2;
		var top  = (document.body.clientHeight - page_frame.style.posHeight)/2;
		if (left < 0) left = 0;
		if (top  < 0) top  = 0;
		page_frame.style.posLeft = left;
		page_frame.style.posTop  = top;
	}
}

function ResizeObject(object, scale_x, scale_y)
{
	object.style.posLeft = object.ILMOriginalPosition.left * scale_x;
	object.style.posTop  = object.ILMOriginalPosition.top  * scale_y;
	object.style.posWidth  = object.ILMOriginalPosition.width  * scale_x;
	object.style.posHeight = object.ILMOriginalPosition.height * scale_y;

	object.style.clip = "rect(0 "+object.style.posWidth+" "+object.style.posHeight+" 0)";

	for (var i=0; i < object.children.length; i++) {
		var child = object.children[i];
		if (child.style != null && child.style.position == "absolute") {
			ResizeObject(child, scale_x, scale_y);
		}
	}
}

function ILMKeyDown(event)
{
	if (event.srcElement.tagName == "INPUT" || event.srcElement.tagName == "TEXTAREA") {
		var parent_object = event.srcElement.parentElement;
		if (parent_object.onILMKeyDown != null) {
			parent_object.onILMKeyDown(event.keyCode);
		}
	} else {
		for (var i=0; i < document.all.length; i++) {
			var object = document.all[i];
			if (object.onILMKeyDown != null) {
				object.onILMKeyDown(event.keyCode);
			}
		}
	}
}

//=============================================================================
// Video Functions

function PlayVideo(object)
{
	object.all.tags("OBJECT")[0].Run();
}

function StopVideo(object)
{
	object.all.tags("OBJECT")[0].Stop();
}

//=============================================================================
// Sound Functions

function PlaySound(object, times)
{
	object.object.playCount = times;
	object.Run();
}

function StopSound(object)
{
	object.Stop();
}

//=============================================================================
// Slideshow Functions

function PlaySlideshow(object)
{
	if (object.ILMSlideshow.random != null) PlayFromSlideshow(object, object.ILMSlideshow.random.GetNext());
	else                                    PlayFromSlideshow(object, 0);
}

function StopSlideshow(object)
{
	if (!object.ILMSlideshow.playing) return;
	object.ILMSlideshow.playing = false;

	window.clearTimeout(object.ILMSlideshow.timer);
}

function PauseSlideshow(object)
{
	if (!object.ILMSlideshow.playing) return;
	object.ILMSlideshow.playing = false;

	window.clearTimeout(object.ILMSlideshow.timer);
}

function ResumeSlideshow(object)
{
	PlayFromSlideshow(object, object.ILMSlideshow.index);
}

function PlayFromSlideshow(object, slide_index)
{
	if (object.ILMSlideshow.playing) return;
	object.ILMSlideshow.playing = true;

	if (slide_index < 0 || slide_index >= object.ILMSlideshow.slides.length) slide_index = 0;

	if (object.ILMSlideshow.index != slide_index) {
		DisplayFrame(object, slide_index);
	} else {
		var delay = object.ILMSlideshow.times[object.ILMSlideshow.index];
		object.ILMSlideshow.timer = window.setTimeout("NextSlide("+object.id+");", delay);
	}
}

function BackwardFrameSlideshow(object)
{
	var next_index = GetNextSlideIndex(object, true);
	if (next_index != -1) {
		window.clearTimeout(object.ILMSlideshow.timer);
		DisplayFrame(object, next_index);
	}
}

function ForwardFrameSlideshow(object)
{
	var next_index = GetNextSlideIndex(object, false);
	if (next_index != -1) {
		window.clearTimeout(object.ILMSlideshow.timer);
		DisplayFrame(object, next_index);
	}
}

function GotoFrameSlideshow(object, frame)
{
	if (frame >= 0 && frame < object.ILMSlideshow.slides.length) {
		window.clearTimeout(object.ILMSlideshow.timer);
		DisplayFrame(object, frame);
	}
}

function NextSlide(object)
{
	var next_index = GetNextSlideIndex(object, false);
	if (next_index == -1) StopSlideshow(object);
	else                  DisplayFrame(object, next_index);
}

function DisplayFrame(object, frame_index)
{
	if (object.ILMSlideshow.slides.length == 0) return;
	var from_slide = this.slides[object.ILMSlideshow.index];
	var to_slide   = this.slides[frame_index];
	object.ILMSlideshow.index = frame_index;

	if (to_slide.filters[0] != null)
	{
		to_slide.filters[0].Stop();
		to_slide.filters[0].Apply();
		to_slide.style.zIndex = 1;
		from_slide.style.zIndex = 0;
		to_slide.style.visibility = "visible";
		to_slide.onfilterchange = new Function("this.parentElement.all('"+from_slide.id+"').style.visibility = 'hidden';");
		to_slide.filters[0].Play();
	} else {
		from_slide.style.visibility = "hidden";
		to_slide.style.visibility = "visible";
	}

	if (object.ILMSlideshow.playing) {
		var delay = object.ILMSlideshow.times[object.ILMSlideshow.index];
		object.ILMSlideshow.timer = window.setTimeout("NextSlide("+object.id+");", delay);
	}
}

function GetNextSlideIndex(object, backward)
{
	if (object.ILMSlideshow.random != null) {
//		if (backward) {
//			if (m_random_list.GetIndex() == 0) {
//				// The current pass through the random list if finished, check the repeat count
//				if (m_iRepeatCount++ >= pProperties->m_iRepeat && pProperties->m_iRepeat != -1) {
//					return FALSE;
//				}
//			}
///			m_iCurrentSlide = m_random_list.GetPrevious(); // Random list automatically wraps around
//		} else {
//			// Random slide order - forwards
//			if (m_random_list.GetIndex()>= m_random_list.GetLength()-1) {
//				// The current pass through the random list if finished, check the repeat count
//				if (m_iRepeatCount++ >= pProperties->m_iRepeat && pProperties->m_iRepeat != -1) {
//					return FALSE;
//				}
//			}
//			m_iCurrentSlide = m_random_list.GetNext(); // Random list automatically wraps around
//		}
	} else {
		if (backward) {
			if (object.ILMSlideshow.index <= 0) {
				if (object.ILMSlideshow.repeat_index++ >= object.ILMSlideshow.repeat && 
					object.ILMSlideshow.repeat != -1) {
					return -1;
				} else {
					return object.ILMSlideshow.slides.length - 1;
				}
			} else {
				return object.ILMSlideshow.index - 1;
			}
		} else {
			if (object.ILMSlideshow.index >= object.ILMSlideshow.slides.length - 1) {
				if (object.ILMSlideshow.repeat_index++ >= object.ILMSlideshow.repeat && 
					object.ILMSlideshow.repeat != -1) {
					return -1;
				} else {
					return 0;
				}
			} else {
				return object.ILMSlideshow.index + 1;
			}
		}
	}

	m_iFirstSlide = FALSE;
	return TRUE;

}

function makeArrayImplict()
{
	var count;
	this.length = makeArrayImplict.arguments.length;
	for (count = 0; count < makeArrayImplict.arguments.length; count++) {
		this[count] = makeArrayImplict.arguments[count];
	}
	return (this);
}

function makeSlideShow(object, repeat, autostart, random)
{
	this.object = object;
	this.repeat = repeat;
	this.autostart = autostart;
	//this.random = random;
	this.index = 0;
	this.repeat_index = 0;
	this.playing = false;
	return (this);
}


