// -----------------------------------------------------------------------------------
//
//	TweenInOut v1
//	by Justin Gitlin - http://www.factorylabs.com
//	07/16/07
//	
//	open source thanks to: Lokesh Dhakar (huddletogether.com), Peter-Paul Koch (quirksmode.org), Thomas Fuchs (mir.aculo.us).
//
//	caveats:
//	* Safari v2 has a rendering bug that prevents it from animating floating html elements properly.
//	  A solution is to disable this script for Safari, or to use tables instead of float:left; 
//	* IE might have with this script unless the elements you're animating have a css width defined.
//	* If you already have a css class on an element you want to animate, you can define multiple styles
//	  on an, separated by spaces ( <div class="yourStyle tweenInOut">animate me</div> )
//
// -----------------------------------------------------------------------------------



// customizable anitmation vars
var animationActive = true;				// global disabling of animation if you don't want to see it while testing
var animateClass = '.tweenInOut';		// class name of elements to add to animation queue
var linkKeys = ['plasticsoundsupply','release/'];	// strings to look for in links to decide if it's a normal link that should be turned into an animation-friendly javascript link
var useDisabler = false;					// set to false if you don't want to use a disabler at all
var disablerOpacity = 0;				// opacity of the disabler overlay - between 0-1
var disablerFadeTime = 0;				// speed to fade the disabler - 0 snaps instantly
var introTweenInterval = 200;			// milliseconds in between the intro tweens
var introTweenDuration = 2;				// seconds to fade in the intro tweens
var outroTweenInterval = 100;			// milliseconds in between the outro tweens
var outroTweenDuration = .5;			// seconds to fade out the outro tweens


// global var declaration
var nextLink;							// store link location for outroFinished()
var fadeArr;							// stores array of elements to fade in
var fadeArrRev;							// stores array of elements to fade out
var disablerElement;					// reference to the disabler div



// sets up and initializes intro fade sequence
function init() {
	if( animationActive == true )
	{
		findFadeElements();
		if( fadeArr.length > 0 )
		{
			if( useDisabler == true ) createDisablerLayer();
			prepareFadeElements();
			prepareLinks();
			initIntro();
		}
	}
	else
	{
		showInlineHiddenElements();
	}
}
Event.observe(window, 'load', init, false);

// reset opacity of all fade elements on unload
function unload() {
	killFadeElements();	
}
Event.observe(window, 'unload', unload, false);

// create animation array by finding elements with the defined class
function findFadeElements()
{
	fadeArr = $$(animateClass);
	fadeArrRev = $$(animateClass)._reverse();
}

// set all elements' opacity to 0. other common element manipulation can happen here
function prepareFadeElements()
{
	for( var i = 0; i < fadeArr.length; i++ )
	{
		Element.Methods.setOpacity( $(fadeArr[i]), 0 );
	}
}

// convert all links to javascript links to handle fade-out. *can edit here to add multiple outro paths depending on the destination link
function prepareLinks()
{
	var linkArr = $$('a');

	for( var i = 0; i < linkArr.length; i++ )
	{
		var linkString = '' + linkArr[i].href;
		//alert(linkString + '  ' + linkString.indexOf("/wentworth_kersey") );
		if( linkString.indexOf('release/wentworth_kersey') != -1 && linkString.indexOf('.mp3') == -1 )
		{
			linkArr[i].href = 'javascript:link(\''+ linkArr[i].href +'\');';
		}
		else if( linkArr[i].href == 'http://plasticsoundsupply.com/' || linkArr[i].href == 'http://www.plasticsoundsupply.com/' )
		{
			linkArr[i].href = 'javascript:link(\''+ linkArr[i].href +'\');';
		}
		else if( linkArr[i].href == '../wentworth_kersey_-_o' || linkArr[i].href == 'http://www.cacheflowe.com/' )
		{
			linkArr[i].href = 'javascript:link(\''+ linkArr[i].href +'\');';
		}
	
	}
}

// show elements that have been hidden inline to prevent flashing before smooth into animation
function showInlineHiddenElements()
{
	// show elements that have an inline "display:none;"
	$('content').show();
	//$('footer').show();
}

// kick off intro sequence. *can edit here to have multiple intro paths depending on the user's location
function initIntro()
{
	showInlineHiddenElements();
	// start the sequence of fading in
	sequenceFadeIn();
}

// for some reason stops the back button in firefox from keeping the cleared-out cached version of the page
function killFadeElements()
{
	// set all elements' opacity to 0
	for( var i = 0; i < fadeArr.length; i++ )
	{
		Element.Methods.setOpacity( fadeArr[i], 0 );
	}
}

// sequentially fade elements in on an interval
function sequenceFadeIn()
{
	callback = ( fadeArr.length == 1 ) ? introFinished : null; 
	Effect.AppearTweaked( fadeArr[0], { duration:introTweenDuration, afterFinish: callback } );
	fadeArr.splice(0,1);
	if( fadeArr.length > 0 ) setTimeout( "sequenceFadeIn();", introTweenInterval );
}

// sequentially fade elements out on an interval
function sequenceFadeOut()
{
	callback = ( fadeArrRev.length == 1 ) ? outroFinished : null; 
	Effect.FadeNoCollapse( fadeArrRev[0], { duration:outroTweenDuration, afterFinish: callback } );
	fadeArrRev.splice(0,1);
	if( fadeArrRev.length > 0 ) setTimeout( "sequenceFadeOut();", outroTweenInterval );
}

// store link location for after we're faded out & start the exit sequence
function link( url )
{
	nextLink = url;
	if( useDisabler == true ) showDisabler();
	sequenceFadeOut();
}

// commands for when the page has animated in
function introFinished()
{
	if( useDisabler == true ) hideDisabler();
}

// navigate to the link that was stored onClick
function outroFinished()
{
	window.location = nextLink;
}

// modified version of scriptaculous Effect.Fade() to prevent collapsing of elements when tween is finished
Effect.FadeNoCollapse = function(element) {
  element = $(element);
  var oldOpacity = element.getInlineOpacity();
  var options = Object.extend({
  from: element.getOpacity() || 1.0,
  to:   0.0,
  afterFinishInternal: function(effect) { 
    if(effect.options.to!=0) return;
    effect.element.setStyle({opacity: oldOpacity}); 
	Element.Methods.setOpacity( effect.element, 0 );
  }}, arguments[1] || {});
  return new Effect.Opacity(element,options);
}

// modified version of scriptaculous Effect.Appear() to prevent rerendering problems in Safari
Effect.AppearTweaked = function(element) {
  element = $(element);
  var options = Object.extend({
  from: (element.getStyle('display') == 'none' ? 0.0 : element.getOpacity() || 0.0),
  to:   1.0,
  afterFinishInternal: function(effect) {
    //effect.element.forceRerendering();	// force Safari to render floated elements properly
	effect.element.setOpacity(effect.options.to).show(); 
  },
  beforeSetup: function(effect) {
    effect.element.setOpacity(effect.options.from).show(); 
  }}, arguments[1] || {});
  return new Effect.Opacity(element,options);
}



// disabler code

// insert div just before the closing body tag to create the disabler layer
function createDisablerLayer()
{
	// insert div as the last element 
	var objBody = document.getElementsByTagName("body").item(0);
	//var objBody = $('body');
	disablerElement = document.createElement("div");
	disablerElement.setAttribute('id','disabler');
	objBody.appendChild( disablerElement );
	
	// make disabler invisible
	Element.Methods.setOpacity( disablerElement, disablerOpacity );

	// make sure the disabler stretches across the whole screen
	var arrayPageSize = getPageSize();
	Element.setHeight('disabler', arrayPageSize[1]);
}

// get rid of the disabler once the page is all the way in
function hideDisabler()
{
	// fade or snap opacity
	if( disablerFadeTime == 0 )
	{
		Element.Methods.setOpacity( disablerElement, 0 );
		$(disablerElement).hide();
	}
	else
	{
		Effect.FadeNoCollapse( disablerElement, { from:disablerOpacity, to:0, duration: disablerFadeTime, afterFinish: function(){ $(disablerElement).hide(); } } );
	}
}

// get rid of the disabler once the page is all the way in
function showDisabler()
{
	// disable
	$( disablerElement ).show();
	
	//make sure disabler is sized to current screen dimensions
	var arrayPageSize = getPageSize();
	Element.setWidth( disablerElement, arrayPageSize[0] );
	Element.setHeight( disablerElement, arrayPageSize[1] );
	
	// fade or snap opacity
	if( disablerFadeTime == 0 )
	{
		Element.Methods.setOpacity( disablerElement, disablerOpacity );
	}
	else
	{
		//Element.Methods.setOpacity( disablerElement, .2 );
		Effect.AppearTweaked( disablerElement, { from:0, to:disablerOpacity, duration: disablerFadeTime } );
	}
}


// utility functions

//	Additional methods for Element added by SU, Couloir
//	- further additions by Lokesh Dhakar (huddletogether.com)
//
Object.extend(Element, {
	getWidth: function(element) {
	   	element = $(element);
	   	return element.offsetWidth; 
	},
	setWidth: function(element,w) {
	   	element = $(element);
    	element.style.width = w +"px";
	},
	setHeight: function(element,h) {
   		element = $(element);
    	element.style.height = h +"px";
	},
	setTop: function(element,t) {
	   	element = $(element);
    	element.style.top = t +"px";
	},
	setSrc: function(element,src) {
    	element = $(element);
    	element.src = src; 
	},
	setHref: function(element,href) {
    	element = $(element);
    	element.href = href; 
	},
	setInnerHTML: function(element,content) {
		element = $(element);
		element.innerHTML = content;
	},
	removeDimensions: function(element){
	   	element = $(element);
	   	element.style.width = '';
		element.style.height = '';
	},
	removeOnclick: function(element){
	   	element = $(element);
	   	element.onclick = function(){}
	},
	setCursor: function(element, cursor){
		element = $(element);
		element.style.cursor = cursor;
	}
});

// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
//
function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}
