/*

.: Description:
-----------------------
Function to display the view switcher div. Animated show/hide functionality

.: Declarations:
-----------------------
setTimeout augmentation to send multiple arguments through a standard setTimeout call... */

var _st = window.setTimeout;

window.setTimeout = function(fRef, mDelay) 
{ 
	if(typeof fRef == "function")
	{  
		var argu = Array.prototype.slice.call(arguments,2); 
		var f = (function(){ fRef.apply(null, argu); }); 
		return _st(f, mDelay); 
	} 
	return _st(fRef,mDelay);
}

/*

.: Usage:
-----------------------
It has been designed for use with the report page viewSwitcher element.
To show the view switcher from a link would be as follows:

<a href="javascript:reveal('viewSwitcher01');">Change</a>
The div would need to be written in HTML beneath the link.

The numbers would need to be written incrementally so that it targets a specific element.

There should also be another div of the same class written below the table

.: The Script:
-----------------------

*/

var showing;
var animCount = 0;
var animTimer;


function getPage(pageToGet){
var target=document.getElementById('galleryThumbs');


// CALCULATE CURRENT POSITION
if(target.style.left == "" || target.style.left == NaN || target.style.left == undefined || target.style.left == "undefined"){
	target.style.left = 0;
}
var currentPos = parseInt(target.style.left);
// SET INCREMENTAL VALUE (435)
var pageWidth = 435;
// CALCULATE OFFSET WIDTH OF TARGET
var totWidth = target.offsetWidth;
// ESTABLISH TOTAL NUMBER OF POSSIBLE PAGES
var totPages = Math.ceil(totWidth/pageWidth);
// ESTABLISH CURRENT PAGE(BASED ON CURRENT POS AND INCREMENTAL VALUE)
var currentPage = Math.floor(-currentPos / pageWidth);
// ESTABLISH FURTHEST POINT THE ELEMENT CAN MOVE (TOTAL PAGES - 1)
var furthestPos = pageWidth * (totPages -1);
// CALCULATE NEXT PAGE
var nextPage = currentPage + 1;
// CALCULATE POSITION OF NEXT PAGE


var goToPos = (pageToGet-1) * pageWidth;

if((pageToGet-1) < currentPage){
	// PREV BUTTON PRESSED (IN EFFECT...)
	showing = true;
}else{
	// NEXT PAGE...
	showing = false;
}


/* DEBUG:
alert('PAGE WIDTH: '+pageWidth+'\nTOTAL WIDTH: '+totWidth+'\nPAGES: '+totPages+'\nCURRENT PAGE: '+currentPage+'\nFURTHEST POSITION: '+furthestPos+'\nNEXT PAGE: '+nextPage+'\nGO TO: '+goToPos);
*/


// IF GO TO POS IS LOWER THAN FURTHEST POSSIBLE POS, ANIMATE. ELSE DO NOTHING
goToPos = -goToPos;

/* DEBUG:
alert('ANIMATING TO: '+goToPos);
*/

animate(target, 'left', currentPos, goToPos,'')	;


}


function nextPage(targetElement){
//var target=document.getElementById(targetElement);
var target=document.getElementById('galleryThumbs');

// CALCULATE CURRENT POSITION
if(target.style.left == "" || target.style.left == NaN || target.style.left == undefined || target.style.left == "undefined"){
	target.style.left = 0;
}
var currentPos = parseInt(target.style.left);
// SET INCREMENTAL VALUE (435)
var pageWidth = 435;
// CALCULATE OFFSET WIDTH OF TARGET
var totWidth = target.offsetWidth;
// ESTABLISH TOTAL NUMBER OF POSSIBLE PAGES
var totPages = Math.ceil(totWidth/pageWidth);
// ESTABLISH CURRENT PAGE(BASED ON CURRENT POS AND INCREMENTAL VALUE)
var currentPage = Math.floor(-currentPos / pageWidth);
// ESTABLISH FURTHEST POINT THE ELEMENT CAN MOVE (TOTAL PAGES - 1)
var furthestPos = pageWidth * (totPages -1);
// CALCULATE NEXT PAGE
var nextPage = currentPage + 1;
// CALCULATE POSITION OF NEXT PAGE
var goToPos = nextPage * pageWidth;

/* DEBUG
alert('PAGE WIDTH: '+pageWidth+'\nTOTAL WIDTH: '+totWidth+'\nPAGES: '+totPages+'\nCURRENT PAGE: '+currentPage+'\nFURTHEST POSITION: '+furthestPos+'\nNEXT PAGE: '+nextPage+'\nGO TO: '+goToPos);
*/

// IF GO TO POS IS LOWER THAN FURTHEST POSSIBLE POS, ANIMATE. ELSE DO NOTHING
if(nextPage <= (totPages-1)){
	goToPos = -goToPos;
	
	/* DEBUG
	alert('ANIMATING TO: '+goToPos);
	*/
	
	showing = false;
	animate(target, 'left', currentPos, goToPos,'')	;
}else{
	alert('No more pictures!');
}

}

function prevPage(targetElement){
//var target=document.getElementById(targetElement);
var target=document.getElementById('galleryThumbs');

// CALCULATE CURRENT POSITION
if(target.style.left == "" || target.style.left == NaN || target.style.left == undefined || target.style.left == "undefined"){
	target.style.left = 0;
}
var currentPos = parseInt(target.style.left);
// SET INCREMENTAL VALUE (435)
var pageWidth = 435;
// CALCULATE OFFSET WIDTH OF TARGET
var totWidth = target.offsetWidth;
// ESTABLISH TOTAL NUMBER OF POSSIBLE PAGES
var totPages = Math.ceil(totWidth/pageWidth);
// ESTABLISH CURRENT PAGE(BASED ON CURRENT POS AND INCREMENTAL VALUE)
var currentPage = Math.floor(-currentPos / pageWidth);
// ESTABLISH FURTHEST POINT THE ELEMENT CAN MOVE (TOTAL PAGES - 1)
var furthestPos = pageWidth * (totPages -1);
// CALCULATE NEXT PAGE
var nextPage = currentPage - 1;
// CALCULATE POSITION OF NEXT PAGE
var goToPos = nextPage * pageWidth;

/* DEBUG
alert('PAGE WIDTH: '+pageWidth+'\nTOTAL WIDTH: '+totWidth+'\nPAGES: '+totPages+'\nCURRENT PAGE: '+currentPage+'\nFURTHEST POSITION: '+furthestPos+'\nNEXT PAGE: '+nextPage+'\nGO TO: '+goToPos);
*/

// IF GO TO POS IS LOWER THAN FURTHEST POSSIBLE POS, ANIMATE. ELSE DO NOTHING
if(nextPage >= 0){
	goToPos = -goToPos;
	/* DEBUG
	alert('ANIMATING TO: '+goToPos);
	*/
	showing = true;
	animate(target, 'left', currentPos, goToPos,'')	;
}else{
	alert('No previous pictures!');
}



//animate(target, 'left', -435, 0,'')	;	
}


function reveal(targetElement)
{
	if(animTimer!=undefined){
		clearTimeout(animTimer);
		animCount++;
		//alert("ANIMCOUNT: "+animCount);
	}else{
		//alert("ANIMCOUNT: "+animCount);
		animCount++;
	}
	var target=document.getElementById(targetElement);
	var initHeight = parseInt(target.style.minHeight);
	
	
	if(initHeight == ''){
		initHeight = 0;
	}
	if(!target.style.height){
		
		var heightOfMask = initHeight;
	}else{
		var heightOfMask = parseInt(target.style.height);
	}
	var heightOfBox = calcHeight(target);
	if(heightOfMask > initHeight && showing==true){
		var targetId = target.id;
		var callBack = document.getElementById(targetId+"_callBack");
		callBack.innerHTML = "More...";
		showing = false;
		animate(target,'height', heightOfBox,initHeight, '');		
		
	}else{
		var targetId = target.id;
		var callBack = document.getElementById(targetId+"_callBack");
		callBack.innerHTML = "Hide...";
		showing = true;
		animate(target,'height', initHeight, heightOfBox,  '');		
		
	}
		
}

function calcHeight(targetElement){
	targChildren = targetElement.getElementsByTagName("div");
	targChild = targChildren[0];
    targChild.style.height = "auto";
	targHeight = targChild.offsetHeight;
	targHeight = parseInt(targHeight);
	return targHeight;
}
	

function show(target){
	heightOfBox = calcHeight(target);
	target.style.display = 'block';
	animate(target,'height', 0, heightOfBox, 'block');
	
}

function hide(target){
	heightOfBox = calcHeight(target);
	target.style.d
	isplay = 'block';
	animate(target,'height', heightOfBox, 0, 'none');
	
}

function animate(target, param, startVal, stopVal, endState){
	clearTimeout(animTimer);
	//alert("ANIMATING...");
	var currentVal = parseInt(target.style[param]);
	//var difference = stopVal - currentVal;
	if (target.style[param] == "" ||target.style[param] == "NaN" || target.style[param] == NaN){
			//alert("no style for height");
			currentVal = startVal;
	}
	
	if (currentVal != stopVal){
		//alert("currentVal is: "+currentVal+"\n\rstopVal is: "+stopVal);
		if(showing == true){
			var difference = stopVal - currentVal;
			var easedPos = Math.floor(((20/100)*difference));
			if(easedPos < 1){
				easedPos = 1;
			}
			//alert(easedPos);
			target.style[param] = currentVal + easedPos + "px";
			//setTimeout(animate,2,target,'height',startVal, stopVal,'',showing);
		}else{
			var difference = currentVal - stopVal;
			var easedPos = Math.floor(((20/100)*difference));
			if(easedPos < 1){
				easedPos = 1;
			}
			//alert(easedPos);
			target.style[param] = (currentVal - easedPos) + "px";
			//setTimeout(animate,2,target,'height',startVal, stopVal,'',showing);
		}
		
		animTimer = setTimeout(animate,1,target,param,startVal, stopVal,'');
		
		
		
		/*else{			
			currentVal = currentVal - 1; 
			target.style[param] = currentVal + "px";
			setTimeout(animate,3,target,'height',startVal, stopVal);
		}*/
	}else{
		clearTimeout(animTimer);
	}
}




