// -----------------------------------------------------------------------------------
//
//	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();
		}
	}
}
Event.observe(document, 'dom:loaded', init, false);

Event.observe(window, 'load', initIntroSequence, 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++ )
	{
		fadeArr[i].setOpacity( 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 +'\');';
		}
	
	}
}

// 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 initIntroSequence()
{
	callback = ( fadeArr.length == 1 ) ? introFinished : null; 
	Effect.AppearTweaked( fadeArr[0], { duration:introTweenDuration, afterFinish: callback } );
	fadeArr.splice(0,1);
	if( fadeArr.length > 0 ) setTimeout( "initIntroSequence();", 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;
	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]);
}
