
//-------------------------------------------------
// include the required JavaScripts....
//-------------------------------------------------
var head = document.getElementsByTagName('head')[0];	

script = document.createElement('script');
script.src = 'http://www.ventrella.com/WorldWovenWeb/Constants.js';
script.type = 'text/javascript';
head.appendChild(script)

var MILLISECONDS_PER_ANIMATION_STEP	= 50;


//-------------------------------------------------------
// Here is the Weaver class constructor
//-------------------------------------------------------
function Weaver()
{
	//--------------------------------------
	// private members
	//--------------------------------------
	var crawler		= null;
	var crawling	= false;
	var timer		= 0;
	var x			= 0;
	var y			= 0;
	var index		= -1;
	var style		= -1;
	var startTime	= -1;
	
	//-------------------------------------------------------
	this.initialize = function( crawlIndex, crawlStyle )
	{
		index = crawlIndex;
		style = crawlStyle;
		
		startTime = crawlIndex * CRAWL_DURATION;

		console.log( "style = " + style );
		console.log( "GLOBAL_CRAWL_DURATION = " + GLOBAL_CRAWL_DURATION );
		console.log( "startTime = " + startTime );

				
		var body = document.getElementsByTagName( "body" )[0];
	
		crawler = document.createElement( "div" );
		crawler.style.position	= "absolute";
		crawler.style.left = x;
		crawler.style.top  = y;
		crawler.innerHTML = "";
		
		body.appendChild( crawler );
	
		this.arrangeElementsAccordingToBrowserWidthAndHeight();
	
		clearTimeout ( timer );	
		timer = setTimeout( "weaver.updateAnimation()", MILLISECONDS_PER_ANIMATION_STEP );
		
	}//-------------------------------------------------------------------------------------------------------------




	//-------------------------------------------------------
	this.arrangeElementsAccordingToBrowserWidthAndHeight = function()
	{
		browserWidth	= document.body.clientWidth;
		browserHeight	= document.body.clientHeight;
	
	}//----------------------------------------------------------------------


	
	//----------------------------------------------------------------------
	this.updateAnimation = function()
	{
		//------------------------------------------------------------
		// reset the timer so it keeps on tickin'
		//------------------------------------------------------------
		timer = setTimeout( "weaver.updateAnimation()", MILLISECONDS_PER_ANIMATION_STEP );
		
		var date = new Date();
		var milliseconds = date.getTime();

		var globalCrawlTime = milliseconds % GLOBAL_CRAWL_DURATION;
		var crawlTime = globalCrawlTime - startTime;
		
		console.log( "globalCrawlTime = " + globalCrawlTime + ", crawlTime = " + crawlTime );

		if ( ( crawlTime > 0 ) && ( crawlTime < CRAWL_DURATION ) )
		{
			if ( ! crawling ) 
			{
				crawling = true;
				crawler.innerHTML = "<img src = 'http://www.ventrella.com/WorldWovenWeb/body.png'>";
			}			
		}
		else
		{
			if ( crawling ) 
			{
				crawling = false;
				crawler.innerHTML = "";
				x = 0;
				y = 0;
			}
		}
		
		if ( crawling )
		{
			x += 2;
			y += 2;
			crawler.style.left	= x;
			crawler.style.top	= y;
		}
		
	}//----------------------------------------------------------------------
	
	
	//---------------------------------------------------------------
	// buncho get methods...
	//---------------------------------------------------------------
	this.getTitle		= function() { return title;		}
	this.getAuthor		= function() { return author;		}
	this.getCurrentPage	= function() { return currentPage;	}
	this.getNumPages	= function() { return numPages;		}
	this.getWidth 		= function() { return width;		}
	this.getHeight		= function() { return height;		}
	this.getXPosition	= function() { return x;			}
	this.getYPosition	= function() { return y;			}


} //---------------------------------------------------------------------------------
 //---------------  END of Weaver class constructor ----------------------------------
//---------------------------------------------------------------------------------

var weaver = new Weaver();


function initializeWeaver( index, crawlStyle )
{
	weaver.initialize( index, crawlStyle );
}


