// (c) Copyright 2012, The Software Ranch LLC and Morpho-Graphics LLC. All rights reserved

function getElement(elementName)
{
	// Get the element.
	return document.getElementById(elementName);
}

function resetElement(element)
{
	// Reset the mouse events.
	element.onselectstart = function () { return false; }	// ie
	element.onmousedown = function () { return false; } 	// everyone else
	element.onmousemove = null;
	element.onmouseup = null;

	// Resize to zero.
	element.style.top = 0 + "px";
	element.style.left = 0 + "px";
	element.style.height = 0 + "px";
	element.style.width = 0 + "px";
	element.innerHTML = ""
	
	// Reset the class name.
	element.className = "drawing";
}

function initializeElements()
{
	initElements($('div.intro'));
	initElements($('div.title'));
	initElements($('div.subTitle'));

	initElements($('div.prompt'));
	initElements($('div.image'));
	initElements($('div.drawingBase'));
}

function initElements(items)
{
	$.each(items, function(index, item)
	{
		// Reset the mouse events.
		item.onselectstart = function () { return false; }	// ie
		item.onmousedown = function() {return false;}		// everyone else
		item.onmousemove = null;
		item.onmouseup = null;
	})
}

function moveElement(element, moveX, moveY)
{
	if (element) 
	{
		element.style.left = moveX + "px";
		element.style.top = moveY + "px";
	}
}
		
function moveElementRelative(element, moveX, moveY)
{
	if (element) 
	{
		element.style.left = (parseInt(element.style.left) + moveX) + "px";
		element.style.top = (parseInt(element.style.top) + moveY) + "px";
	}
}

var curImageRot = 0;
var curMirror = 0;
var curInvert = 0;

function clearRotation(element) 
{
	curImageRot = 0;
	curMirror = 0;
	curInvert = 0;
	applyFilter(element, curImageRot, curMirror, curInvert);
}

function rotateLeft(element)
{
	curImageRot -= 1;
	if (curImageRot < 0)
	{
		curImageRot = 3;
	}
	applyFilter(element, curImageRot, curMirror, curInvert);
}

function rotateRight(element)
{
	curImageRot += 1;
	if (curImageRot > 3)
	{
		curImageRot = 0;
	}
	applyFilter(element, curImageRot, curMirror, curInvert);
}

function flipVert(element)
{
	curMirror++;
	if (curMirror > 1)
	{
		curMirror = 0;
	}
	applyFilter(element, curImageRot, curMirror, curInvert);
}

function applyFilter(element, rotation, mirror, invert)
{
	if (document.all)
	{
		// apply the filter, but only for IE.
		element.style.filter = "progid:DXImageTransform.Microsoft.BasicImage()";
		element.filters.item("DXImageTransform.Microsoft.BasicImage").rotation = rotation;
		element.filters.item("DXImageTransform.Microsoft.BasicImage").mirror = mirror;
		element.filters.item("DXImageTransform.Microsoft.BasicImage").invert = invert;
	}
	else
	{
		// TODO: Figure out how to do this in FireFox.
		// Use SVG
	}
}

var dop = 180 / Math.PI;

function degToRad(x) 
{ 
	return (x / dop); 
}

function radToDeg(x) 
{ 
	return (x * dop); 
}

