/* BERENS VECTOR BASED LAYER MAXIMIZER
  -------------------------------------
  (This will work... ...this WILL work!)
  
  pseudo code
  -----------
  *Pull the vars and sort em out
  *using long forgotten trig, write an algorithm for acceleration and deceleration
   from a 2D start point, to a 2D end point.
   
*/
   
//SUPERGLOBALS
var CURRENTFRAME = 1;
var FINISHFLAG = false;  //Set when all animations have finished.
var noofIterations,posVectorAngle,sizeVectorAngle,stripedXpos,stripedYpos,stripedWidth,stripedHeight,Framerate;
var temporaryHTMLHolder = new Array(); //String holder to hold html data that is replaced.

var LayerOb = new Object();
var  posIterations, sizeIterations = new Array();

//Globals
Framerate = 35; 			//Time in milliseconds for the framerate of the animation (1000/40 = 25fps).
noofIterations = 25; 		// number of frames for the overall maximisation
		
function Maximize (id,xposFin,yposFin,widthFin,heightFin) {

		LayerOb = document.getElementById(id);		
		
		var xpos = LayerOb.style.left;
		var ypos = LayerOb.style.top;
		var width = LayerOb.style.width;
		var height = LayerOb.style.height;

		// Convert all strings to numbers.
		 stripedXpos = ConvertStringToNumber(xpos);
		 stripedYpos = ConvertStringToNumber(ypos);
		 stripedWidth = ConvertStringToNumber(width);
		 stripedHeight = ConvertStringToNumber(height);
		
		//Use pythag to get the hypotenuse vector length from start to end
		var posVectorLength = Math.sqrt((Math.pow((xposFin-stripedXpos),2) + Math.pow((yposFin-stripedYpos),2)));
		// calculate the angle of the vector, making adjustments where necessary
		var Eastings = ((xposFin - stripedXpos) >= 0) ? true : false ;  //These are direction flags
		var Northings = ((yposFin - stripedYpos) >= 0) ? true : false ;
		if (Eastings && Northings) posVectorAngle = Math.atan((yposFin - stripedYpos) / (xposFin - stripedXpos));
		else if (Eastings && !Northings) posVectorAngle = Math.atan((yposFin - stripedYpos) / (xposFin - stripedXpos)) + DegtoRad(360);
		else if (!Eastings && Northings) posVectorAngle = Math.atan((yposFin - stripedYpos) / (xposFin - stripedXpos)) + DegtoRad(180);
		else if (!Eastings && !Northings) posVectorAngle = Math.atan((yposFin - stripedYpos) / (xposFin - stripedXpos)) + DegtoRad(180);
				
		//Use pythag to get the hypotenuse SIZE vector from start to end
		var sizeVectorLength = Math.sqrt((Math.pow((widthFin-stripedWidth),2) + Math.pow((heightFin-stripedHeight),2)));
		var Fattings = ((widthFin - stripedWidth) >= 0) ? true : false ;
		var Tallings = ((heightFin - stripedHeight) >= 0) ? true : false ;
		if (Fattings && Tallings) sizeVectorAngle = Math.atan((heightFin - stripedHeight) / (widthFin - stripedWidth));
		else if (Fattings && !Tallings) sizeVectorAngle = Math.atan((heightFin - stripedHeight) / (widthFin - stripedWidth)) + DegtoRad(360);
		else if (!Fattings && Tallings) sizeVectorAngle = Math.atan((heightFin - stripedHeight) / (widthFin - stripedWidth)) + DegtoRad(180);
		else if (!Fattings && !Tallings) sizeVectorAngle = Math.atan((heightFin - stripedHeight) / (widthFin - stripedWidth)) + DegtoRad(180);
		var sizeVectorAngleDeg = RadtoDeg(sizeVectorAngle);
		
		posIterations = GenerateSineCurve(noofIterations,posVectorLength);
		sizeIterations = GenerateSineCurve(noofIterations,sizeVectorLength); 

		AnimatePosition();
		// AnimateSize();  This is now called by Animate Position to stop clashes.
		
} //->eND OF Maximize

//--------------------------------------------------------------------------------------------------------------------- 

function Restore (id,xposFin,yposFin,widthFin,heightFin) 
{
		var LayerOb = document.getElementById(id);
		
		LayerOb.style.left = xposFin+'px';
		LayerOb.style.top = yposFin+'px';
		LayerOb.style.width = widthFin+'px';
		LayerOb.style.height = heightFin+'px';
	
}
	

//--------------------------------------------------------------------------------------------------------------------- 

function replaceInnerHTML (id) 
{
	if (document.getElementById) {
		
		if(FINISHFLAG == false) { setTimeout(('replaceInnerHTML("'+id+'")'),500); } //Do nothing till anim finished.
		
		else {
		FINISHFLAG = false;
		var replacy = '<iframe src="realW_submittal.php?noun=' + id + '" width="575" height="575" scrolling="Auto" frameborder="0"></iframe>';
		var innerOb = document.getElementById(id);
		
		temporaryHTMLHolder[id] = innerOb.innerHTML;
		
		innerOb.innerHTML = replacy;
		}
		
		
	}
}
	

//--------------------------------------------------------------------------------------------------------------------- 

function restoreInnerHTML (id) 
{
	if (document.getElementById) {
		var innerOb = document.getElementById(id);
		
		innerOb.innerHTML = temporaryHTMLHolder[id] ;
	}
}
	

//--------------------------------------------------------------------------------------------------------------------- 

function AnimatePosition() 
{
			if (CURRENTFRAME<= noofIterations)
			{
				var xposy = Math.round(Math.cos(posVectorAngle) * posIterations[CURRENTFRAME]);
				var yposy = Math.round(Math.sin(posVectorAngle) * posIterations[CURRENTFRAME]);
				
				var resultingX = stripedXpos + xposy;
				var resultingY = stripedYpos + yposy;
				
				LayerOb.style.left = resultingX+'px';
				LayerOb.style.top = resultingY+'px';
				CURRENTFRAME++;
				setTimeout("AnimatePosition()",Framerate);
			} else 
			{
				CURRENTFRAME = 1;
				AnimateSize();
			}
}
	
//---------------------------------------------------------------------------------------------------------------------

function AnimateSize() 
{
			if (CURRENTFRAME<= noofIterations)
			{
				var xposy = Math.round(Math.cos(sizeVectorAngle) * sizeIterations[CURRENTFRAME]);
				var yposy = Math.round(Math.sin(sizeVectorAngle) * sizeIterations[CURRENTFRAME]);
				
				var resultingWidth = stripedWidth + xposy;
				var resultingHeight = stripedHeight + yposy;
				
				LayerOb.style.width = resultingWidth+'px';
				LayerOb.style.height = resultingHeight+'px';
				CURRENTFRAME++;
				setTimeout("AnimateSize()",Framerate);
			} else 
			{
				CURRENTFRAME = 1;
				FINISHFLAG = true;
			}
}
	
//---------------------------------------------------------------------------------------------------------------------
	
	function GenerateSineCurve (noofIterations, totalDistance) 
	{
		var VectorLengths = new Array(noofIterations);
		var i;
		
		VectorLengths[0] = 0;
		for (i=1; i<=noofIterations; i++) 
		{
			//Time for Bare to remember his trig/
			//This equation will fill an array with values for the distance to travel for each iteration of the journey.
			VectorLengths[i] = totalDistance * (Math.sin(DegtoRad((i/noofIterations)*90)));  //Generates an array 
		}
		return VectorLengths;
	}
	
//---------------------------------------------------------------------------------------------------------------------
	
	function ConvertStringToNumber (stringy)
	{
		return Math.round(stringy.match(/\d+/)); //Searches the string for any numerical digits and returns them
	}
	
//---------------------------------------------------------------------------------------------------------------------
	
	function RadtoDeg (radians) 
	{
		return ((radians/Math.PI) * 180);
	}
	
//---------------------------------------------------------------------------------------------------------------------
	
	function DegtoRad (degrees) 
	{
		return ((degrees/180) * Math.PI);
	}
