var d;

var itemsPerPage = 5;
var aniFramsSec = 24;
var aniSpeed = 0.6;



/* === Home BottomNav === */

//whole set at a time
//var shiftX = -965;
//var pages = Math.ceil(slideITEMS/itemsPerPage);
//var timingDelay = 0.2;	// seconds
//var aniSpeed = 2.0;		// seconds

//one item at a time
var shiftX = -193;
var pages;
var slideTimeDelay = 0;	// seconds
var ani1;
var curSLIDE = 1;
var curShift;
var currentX = 0;
var currentY = 0;
var shiftY = 0;

function shiftGallery(blnLeftRight){
    if(!d || !ani1) buildAnimation();
	// for clarity, a LEFT arrow button (<<) will trigger a right direction movement i.e: 1 (left) = images move to the left
	// blnLeftRight: 1 = left, 0 = right
	
	curShift = blnLeftRight;
	
	//ani1.addDebug('CUR'+currentX, curSLIDE);
	if(ani1.curSTEP==0 || ani1.curSTEP == ani1.aniSTEPS){
		if(blnLeftRight){
			//if (currentX>shiftX*(pages-1)) {
			if ( curSLIDE > slideITEMS ) {
				// so this is only if the 1st item is the left-most being displayed
				//		- its time to jump back to the beginning
				ani1.setX(0);
				currentX = 0;
				curSLIDE = 1;
			}
				
			ani1.curSTEP = 0;
			ani1.posStart(currentX,currentY);
			ani1.posStop(currentX+shiftX,shiftY,rollingShift,slideTimeDelay);
			//ani1.setEase("inout",1);
			currentX = currentX + shiftX;
			
			//ani1.stopCMD = function(){rollingShift();}
			ani1.animate();
			curSLIDE++;
			
		}else{
			if(curSLIDE<=1){
				shiftSet = (slideITEMS)*shiftX;
				ani1.setX( shiftSet );
				currentX = shiftSet;
				curSLIDE = slideITEMS+1;
				//ani1.addDebug("SHIFT SET", shiftSet);
				//ani1.addDebug(curSLIDE, slideITEMS);
			}
			ani1.curSTEP = 0;
			ani1.posStart(currentX,currentY);
			ani1.posStop(currentX-shiftX,shiftY,rollingShift,slideTimeDelay);
			//ani1.setEase("inout",1);
			currentX = currentX - shiftX;
			
			//ani1.stopCMD = function(){rollingShift();}
			ani1.animate();
			curSLIDE--;
		}
	}else{
		ani1.stopCMD = rollingShift;
	}
}

function rollingShift(){
	shiftGallery(curShift);
}

function stopShift(){
	ani1.stopCMD = 0;
}

function buildAnimation(){
	if(!d)d=document;
	
	var obj1 = d.getElementById('aniBlock');
	//obj1.style.position="relative";
	ani1 = new objectAnimation(obj1, 1, aniFramsSec);
	//ani1.setDebug();
}

/* === Home Slideshow === */

var sld1;  //fadeOut and swap
var sld2;  //fadeIn and delay
var slides = [];

Array.prototype.next = function(){
	if(this.current==this.length || this.current==undefined) this.current=0;

	ret = this[this.current];
	this.current++;
	return ret;
}

function triggerSlideShow(){
	if(!d || !sld1)
	{
		loadSlideImages(); //should be built on page;
		buildSlideShow();
	}
}

function buildSlideShow(){
	if(!d)d=document;
	var picDelay = 2.6; //seconds
	//var picTransSpeed = 0.5; //seconds - second arg of objectAnimation class
	
	var obj1 = d.getElementById('content');
	//obj1.style.position="relative";
	sld1 = new objectAnimation(obj1, 0.4, aniFramsSec);
	sld1.stopDelay = 0;
	sld1.fadeSet(100,0);
	//sld1.setDebug();
	sld1.stopCMD = function(){
		sld1.aniOBJ.style.backgroundImage = "url(" + slides.next() + ")";
		sld2.reset();
		sld2.animate();
	}
	
	sld2 = new objectAnimation(obj1, 0.5, aniFramsSec);
	sld2.stopDelay = 0;
	sld2.fadeSet(0,50);
	//sld2.setDebug();
	sld2.stopCMD = function(){
		sld3.reset();
		sld3.animate();
	}
	
	sld3 = new objectAnimation(obj1, 0.3, aniFramsSec);
	sld3.stopDelay = picDelay;
	sld3.fadeSet(50,100);
	//sld2.setDebug();
	sld3.stopCMD = function(){
		sld1.reset();
		sld1.animate();
	}
	
	setTimeout(sld1.animate,(picDelay*1000));
	
}





/* === Gallery Nav === */

var galMainIMG;
function galImgOver(newSRC,newW,newH){
	if(!d) buildGalAnimation();
	{
		if(!galMainIMG) galMainIMG = d.getElementById("galMainIMG");
		
		galMainIMG.style.display = 'none';
		galMainIMG.src = newSRC;
		galMainIMG.width = newW;
		galMainIMG.height = newH;
		galMainIMG.style.display = '';
		
	}
}


var galCurrentY = 0;
var galShiftY = -160;
var galPages;
var galTimingDelay = 0;	// seconds

var gal1;
var galCurSLIDE = 1;
var galCurShift;

function galShiftGallery(blnUpDown){
    if(!d || !gal1) buildGalAnimation();
	
	galCurShift = blnUpDown;
	
	//gal1.addDebug('CUR'+galCurrentY, galCurSLIDE);
	
	if(blnUpDown){
		if ( galCurSLIDE > galITEMS ) {
			// so this is only if the 1st item is the left-most being displayed
			//		- its time to jump back to the beginning
			gal1.setY(0);
			galCurrentY = 0;
			galCurSLIDE = 1;
		}
		
		gal1.curSTEP = 0;
		gal1.posStart(0,galCurrentY);
		galCurrentY = galCurrentY + galShiftY;
		gal1.posStop(0,galCurrentY,galRollingShift,0);
		
		gal1.animate();
		//galCurSLIDE += itemsPerPage;
		galCurSLIDE++;
		
		//gal1.addDebug(galCurSLIDE,galCurrentY);
	
	}else{
		if(galCurSLIDE<=1){
			shiftSet = (galITEMS)*galShiftY;
			gal1.setY( shiftSet );
			galCurrentY = shiftSet;
			galCurSLIDE = galITEMS+1;
			//gal1.addDebug("SHIFT SET", shiftSet);
			//gal1.addDebug(curSLIDE, slideITEMS);
		}
		
		gal1.curSTEP = 0;
		gal1.posStart(0,galCurrentY);
		galCurrentY = galCurrentY - galShiftY;
		gal1.posStop(0,galCurrentY,galRollingShift,0);
		
		gal1.animate();
		galCurSLIDE--;
		
		//gal1.addDebug(galCurSLIDE,galCurrentY);
	}
}

function galRollingShift(){
	galShiftGallery(galCurShift);
}

function galStopShift(){
	gal1.stopCMD = 0;
}

function buildGalAnimation(){
	if(!d)d=document;
	
	var obj1 = d.getElementById('aniGalBlock');
	//obj1.style.position="relative";
	gal1 = new objectAnimation(obj1, aniSpeed, aniFramsSec);
	//gal1.setDebug();
}

/*

_______________________________________
|   A N I M A T I O N     C L A S S   |
ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ
BY:
 ____________________________
|                           /
|  Ninja Monkey   apr'08   /
|____  ___________________/
    / /_     _   ____  __
   / // \   / | / /|| / /
  / ///\ \ //||/ / ||/ /
 / / \\/ /// |  /  |  /
/ /   \_/ Ż  ŻŻŻ   / /
ŻŻ                / /
                  ŻŻ

Main Class:
	objectAnimation		[reference to object] - [durration is seconds] - [frames per second]

Properties:
	NAME:			Value Source:		Description:
	ŻŻŻŻŻ			ŻŻŻŻŻŻŻŻŻŻŻŻ		ŻŻŻŻŻŻŻŻŻŻŻŻŻŻ
	aniOBJ			class ARG			reference to object being animated
	aniDUR			class ARG			seconds for animation durration (start position to stop position)
	aniFPS			class ARG			defines the frames per second for the animation
	aniRATE			internal STATIC		milliseconds between frames (setTimeout arg)
	aniSTEPS		internal STATIC		quantity of steps (frames) the animation will use
	curSTEP			internal (count)	current step of the animation process.

	M O T I O N    V A R S :
	motion			internal BLN		if posStart or posStop is used - will render   M O T I O N.
	startX, startY	method ARG			class.posStart:	X/Y - left/top starting position (absolute)
	startCMD		method ARG			class.posStart:	function or command to call when animation starts
	stopX, stopY	method ARG			class.posStop:	X/Y - left/top finishing position (absolute)
	stopCMP			method ARG			class.posStop:	function or command to call when animation finishes
	curX, curY		internal (math)		current animation step positioning

	F A D E    V A R S :
	fade			internal BLN		if fadeSet method is used, this is set to TRUE, triggering fade rendering.
	startFade		method ARG			value from 0 to 100 determining opacity (percentage: 0 = clear; 100 = solid)
	stopFade		method ARG			value from 0 to 100 determining opacity (percentage: 0 = clear; 100 = solid)
	setFADE			internal Funcion	used to define reference to object's opacity property - browser dependant (IE 4+, NS 6+, FF, Moz)

	timingACTION	internal (ref)		referance to current animation setTimeout call

Methods:
	posStart		[X position] - [Y position] - [function to execute]
										cleaner way of separating all the required variables. Defines animation presets.
					X Posit				pixels to position from LEFT
					Y Posit				pixels to position from TOP
					Function			a function or string of commands to execute on START of animation

	posStop			[X position] - [Y position] - [function to execute] - [stopCommand Delay]
										cleaner way of separating all the required variables. Defines animation presets.
					X Posit				pixels to position from LEFT
					Y Posit				pixels to position from TOP
					Function			a function or string of commands to execute on FINISH of animation
					Delay				seconds after FINISH to wait before executing stopFUNCTION

	fadeSet			[start Opacity] - [stop Opacity]
					start Opacity		value from 0 to 100 determining opacity (percentage: 0 = clear; 100 = solid)
					stop Opacity		value from 0 to 100 determining opacity (percentage: 0 = clear; 100 = solid)

	animate			Triggers the animation process

*/

function objectAnimation(obj, dur, fps){
	var oa = this;
	oa.aniOBJ = obj;
	oa.aniDUR = dur;
	oa.aniFPS = fps;
	oa.aniRATE = 1000/oa.aniFPS;
	oa.aniSTEPS = Math.floor(oa.aniDUR*oa.aniFPS);
	oa.curSTEP = 0;
	oa.stopDelay = 0;
	oa.timingACTION = "";
	oa.curX = "";
	oa.curY = "";
	oa.finishCMD = "";
	oa.startCMD = "";
	oa.motion = false;
	oa.ease = false;
	oa.easeP = 1;
	oa.fade = false;
	oa.debug = false;

	oa.animate = function(){
		if(oa.timingACTION) clearTimeout(oa.timingACTION);

		// check for startCommand
		if(oa.curSTEP==0) {
			if (typeof(oa.startCMD)=='function'){
				oa.startCMD();
			}else if (typeof(oa.startCMD)=='string'){
				eval(oa.startCMD);
			}
		}

		if(oa.aniOBJ){
			// motion path animation
			if (oa.motion) {
				if (!(oa.ease===false)) {
					switch(oa.ease){
                     	case "in":
                     		oa.setX(oa.easeIn(oa.startX,oa.stopX));
							oa.setY(oa.easeIn(oa.startY,oa.stopY));
						case "out":
                            oa.setX(oa.easeOut(oa.startX,oa.stopX));
							oa.setY(oa.easeOut(oa.startY,oa.stopY));
						case "inout":
							oa.setX(oa.easeInOut(oa.startX,oa.stopX));
							oa.setY(oa.easeInOut(oa.startY,oa.stopY));
					}
				}else{
					//oa.addDebug(oa.linearRender(oa.startX,oa.stopX),oa.linearRender(oa.startY,oa.stopY));
					oa.setX(oa.linearRender(oa.startX,oa.stopX));
					oa.setY(oa.linearRender(oa.startY,oa.stopY));
				}
			}

			// fade animation
			if (oa.fade) {
				if (!(oa.ease===false)) {
					switch(oa.ease){
                     	case "in":
							oa.setFADE = oa.easeIn(oa.startFade,oa.stopFade);
						case "out":
							oa.setFADE = oa.easeOut(oa.startFade,oa.stopFade);
						case "inout":
							oa.setFADE = oa.easeInOut(oa.startFade,oa.stopFade);
					}
				}else{
					oa.setFADE(oa.linearRender(oa.startFade,oa.stopFade));
				}
			}
		}
		
		// handle steps and check for stopCommand
		if (oa.curSTEP < oa.aniSTEPS) {
			oa.timingACTION = setTimeout(function(){oa.animate()},oa.aniRATE);
			oa.curSTEP++;
		}else{
			if (typeof(oa.stopCMD)=='function'){
				stopRate = oa.stopDelay*oa.aniFPS*oa.aniRATE;
				//if (oa.stopDelay>0 && oa.stopDelay < 1) stopRate = oa.stopDelay + stopRate;
				//if (oa.stopDelay==0) stopRate = oa.aniFPS*oa.aniRATE;
				oa.timingACTION = setTimeout(oa.stopCMD, stopRate );
			}else if (typeof(oa.stopCMD)=='string'){
				oa.timingACTION = setTimeout("eval("+oa.stopCMD+")",  stopRate );
			}
		}
	}
}

objectAnimation.prototype.reset = function(){
	//this.setX(this.startX);
	//this.setY(this.startY);
	this.curSTEP = 0;
}

objectAnimation.prototype.setEase = function(typ, power){
	// -1:ease-out, 1:ease-in, 0:ease-In-Out
	this.ease = typ;
	this.easeP = power;
}

objectAnimation.prototype.linearRender = function(start,stop){
	return start + ( ((stop - start)/this.aniSTEPS) * this.curSTEP );
}

objectAnimation.prototype.easeInOut = function(start,stop){
	if (this.curSTEP < this.aniSTEPS/2) result = start + 2*stop*this.curSTEP*this.curSTEP/(this.aniSTEPS*this.aniSTEPS);
	var delta = this.curSTEP - this.aniSTEPS/2;
	result = start + -2*stop*delta*delta/(this.aniSTEPS*this.aniSTEPS) + 2*stop*delta/this.aniSTEPS + stop/2;

    //impliment power here

	return result;
}

objectAnimation.prototype.easeIn = function(start,stop){
	result = start +    stop * (this.curSTEP/=this.aniSTEPS) * this.curSTEP;

    //impliment power here

	return result;
}

objectAnimation.prototype.easeOut = function(start,stop){
	result = start + -stop*this.curSTEP*this.curSTEP/(this.aniSTEPS*this.aniSTEPS) + 2*stop*this.curSTEP/this.aniSTEPS;

    //impliment power here

	return result;
}

/* // OLD EASE FN
objectAnimation.prototype.easeInOut = function(start,stop){
	// power: 0 to -1 = ease-out, 0 to 1 = ease-in, 1 = linear
	//if (power==0) power = (this.curSTEP/this.aniSTEPS)-(this.curSTEP^2/this.aniSTEPS^2);

	lcSTEP = (this.curSTEP==0)? 1:this.curSTEP;
	if (power==0){
		power = -2*lcSTEP/this.aniSTEPS + 1;
	}else{
		power = lcSTEP/this.aniSTEPS;
	}
	deltaV = 1;
	delta  = (stop - start);
	if(delta<0){
		delta *= -1;
		deltaV = -1;
	}
    result = deltaV * (start + Math.pow( delta ,  power ));
	if(start!=stop){
		this.addDebug(start+" + "+(Math.round(delta*1000)/1000)+"^"+(Math.round(power*1000)/1000), (Math.round(result*1000)/1000));
	}

	return result;
}
*/

objectAnimation.prototype.fadeSet = function(stF,endF){
	var oa = this;
	oa.startFade = stF;
	oa.stopFade = endF;
	
	// Transparancy Capacity Check:
	if(document.all)
	{
		oa.fade = true;
		oa.setFADE = function(val)
		{
			oa.aniOBJ.style.filter = 'alpha(opacity = ' + val + ')';
		}
		oa.addDebug("StyFiltOpac", "x");
	}else{
		oa.fade = true;
		oa.setFADE = function(val)
		{
			oa.aniOBJ.style.opacity = (val/100);
		}
		oa.addDebug("Opac", "x");
	}
}

objectAnimation.prototype.posStart = function(posX,posY,fCall){
	this.startX = posX;
	this.startY = posY;
	this.motion = true;
	
	//this.addDebug("START X", "START Y");
	//this.addDebug(posX, posY);
	
	this.setX = function(val){
		this.aniOBJ.style.left=val+"px";
	}

	this.setY = function(val){
		this.aniOBJ.style.top=val+"px";
	}

	if (typeof(fCall)=='function' || typeof(fCall)=='string'){
		this.startCMD = fCall;
	}else{
		this.startCMD = null;
	}
}

objectAnimation.prototype.posStop = function(posX,posY,fCall,delay){
	this.stopX = posX;
	this.stopY = posY;
	this.motion = true;

	//this.addDebug("STOP X", "STOP Y");
	//this.addDebug(posX, posY);

	if (typeof(fCall)=='function' || typeof(fCall)=='string'){
		this.stopCMD = fCall;
	}else{
		this.stopCMD = null;
	}

	if (typeof(delay)=='number'){
		this.stopDelay = delay;
	}else{
		this.stopDelay = 0;
	}
}

objectAnimation.prototype.setDebug = function(){
	if(!d) d = document;
	
	if(!this.debug){
		lcTABLE = d.createElement('table');
		d.body.appendChild(lcTABLE);
		this.debug = lcTABLE;
		
		this.debug.style.position = 'absolute';
		this.debug.style.top = '10px';
		this.debug.style.right = '10px';
		this.debug.style.zIndex = '1000';
		this.debug.style.border = '3px solid blue';
		this.debug.style.padding = '10px';
		this.debug.style.backgroundColor = '#005';
		this.debug.style.opacity = '0.6';
		this.debug.style.filter = 'alpha(opacity=60)';
		this.debug.style.height = '600px';
		this.debug.style.overflowY = 'scroll';
	}
	this.addDebug('ANIMATION DEBUGGER','RESULTS');
}

objectAnimation.prototype.addDebug = function(str,str2){
	if(this.debug){
		lcTR = d.createElement('tr');
		lcTD1 = d.createElement('td');
		lcTD2 = d.createElement('td');
		lcTD1t = d.createTextNode(str);
		lcTD2t = d.createTextNode(str2);

		lcTD1.appendChild(lcTD1t);
		lcTD2.appendChild(lcTD2t);
		lcTR.appendChild(lcTD1);
		lcTR.appendChild(lcTD2);
		this.debug.appendChild(lcTR);
	}
}
/*
objectAnimation.prototype.runCmd = function(cmd){
	if(cmd){
		switch (typeof(cmd)){
			case 'function':
				cmd();
				return true;
				break;

			case 'string':
				eval(cmd);
				return true;
				break;

			default:
				return false;
		}
	}
}
*/
