var frameinterval=50
var moveright=false
var moveextent=750
var moveincrement=1
var alphaincrement=10
var underleft=0
var overleft=0
var moveoffset=0
var movecurrent=0
var movealternate=0
var fadedirection=0
var currentalpha=100
var val=0
var cury=0
var curx=0
var quotealpha=0
var quotebackgroundalpha=0.5
var quoteaction=0
var quotewait=0
var quoteincrement=5
var quoteholdtime=80
var quotedelay=40
var disablefader=0

/*Changes opacity (shading) of an object.*/
function setalpha(id,percent)
{
	val=Math.max(Math.min(percent,100),0)
	try
	{
		document.getElementById(id).style.opacity=val/100
	}
	catch(e)
	{
	}
		try
	{
		document.getElementById(id).style.MozOpacity=val/100
	}
	catch(e)
	{
	}
	try
	{
		document.getElementById(id).style.filter='alpha(opacity='+val+')'
	}
	catch(e)
	{
	}
}

/*Generates a random number.*/
function rndno(lowerbound,upperbound)
{
	return Math.round(Math.random()*(parseFloat(upperbound)-parseFloat(lowerbound))+parseFloat(lowerbound))
}

function doquote()
{
	if((quotealpha<=quoteincrement)*(quoteaction==0))
	{
		quotealpha=0
		posquote()
		quoteaction=1
	}
	if((quotealpha>=(100-quoteincrement))*(quoteaction==1))
	{
		quotealpha=100
		quoteaction=3
	}
	if(quoteaction>=3)
	{
		if(quoteaction>=(quoteholdtime+2))
		{
			quotewait=0
			quoteaction=0
		}
		else
		{
			quoteaction=quoteaction+1
		}
	}
	if(quoteaction==1)
	{
		if(quotewait<quotedelay)
		{
			quotewait=quotewait+1
		}
		else
		{
			quotealpha=quotealpha+quoteincrement
		}
	}
	if(quoteaction==0)
	{
		quotealpha=quotealpha-quoteincrement
	}
	if(quoteaction<3)
	{
		setquotealpha()
	}
}

/*Sets how opaque the quote and its background are.*/
function setquotealpha()
{
	setalpha('fader_quote_text',quotealpha)
	setalpha('fader_quote_background',eval(quotealpha*quotebackgroundalpha))
}

/*Positions the quote on the fader.*/
var curquote = 0;
var newquote = 0;

function posquote()
{
  do {
    newquote = rndno(1,countquotes());
  } while (curquote == newquote)
	loadquote(newquote);
  curquote = newquote;
	cury=rndno(topy(),bottomy())
	curx=rndno(0,rightx())
	document.getElementById('fader_quote_background').style.top=cpx(cury)
	document.getElementById('fader_quote_background').style.left=cpx(curx)
	document.getElementById('fader_quote_text').style.top=cpx(cury-textdiff())
	document.getElementById('fader_quote_text').style.left=cpx(curx)
}

/*Various functions used in positioning.*/
function topy()
{
	return(-parseInt(document.getElementById('fader_under').clientHeight))
}

function bottomy()
{
	return(-parseInt(document.getElementById('fader_quote_text').clientHeight))
}

function rightx()
{
	return parseInt(document.getElementById('fader_container').clientWidth)-parseInt(document.getElementById('fader_quote_text').clientWidth)
}

function textdiff()
{
	return parseInt(document.getElementById('fader_quote_text').clientHeight)
}

function cpx(value)
{
	return value+'px'
}

/*Fetches quote text.*/
function loadquote(quoteid)
{
	document.getElementById('fader_quote_text').innerHTML=document.getElementById('quote'+quoteid).innerHTML
	document.getElementById('fader_quote_background').innerHTML=document.getElementById('fader_quote_text').innerHTML
}

/*Finds the number of quotes.*/
function countquotes()
{
	var enumquote=1
	var exitvar=0
	do
	{
		try
		{
			if(document.getElementById('quote'+enumquote).id=='quote'+enumquote)
			{
				enumquote=enumquote+1
			}
		}
		catch(e)
		{
			exitvar=1
		}
	}
	while(exitvar==0)
	return(enumquote-1)
}

/*Scrolls the fader.*/
function domove()
{
	moveoffset=document.getElementById('fader_container').offsetWidth
	movecurrent=document.getElementById('fader_over').style.marginLeft
	movecurrent=movecurrent.replace('px','')
	moveoffset=document.getElementById('fader_container').offsetWidth
	if((movecurrent>=-moveincrement)*(moveright==false))
	{
		moveright=true
		fadedirection=2
		movecurrent=0
	}
	if((movecurrent<=-moveextent+moveoffset+moveincrement)*(moveright==true))
	{
		moveright=false
		fadedirection=1
		movecurrent=-moveextent+moveoffset
	}
	if(moveright)
	{
		movecurrent=parseInt(movecurrent)-moveincrement
	}
	else
	{
		movecurrent=parseInt(movecurrent)+moveincrement
	}
	document.getElementById('fader_over').style.marginLeft=movecurrent+'px'
	movealternate=-moveextent-movecurrent+moveoffset
	document.getElementById('fader_under').style.marginLeft=movealternate+'px'
	if(fadedirection==1)
	{
		if(currentalpha<=alphaincrement)
		{
			currentalpha=0
			fadedirection=0
			newImage(true);
		}
		else
		{
			currentalpha=parseInt(currentalpha)-parseInt(alphaincrement)
		}
		setalpha('fader_over',currentalpha)
	}
	if(fadedirection==2)
	{
		if(currentalpha>=100-parseInt(alphaincrement))
		{
			currentalpha=100
			fadedirection=0
			newImage(false);
		}
		else
		{
			currentalpha=parseInt(currentalpha)+parseInt(alphaincrement)
		}
		setalpha('fader_over',currentalpha)
	}
	doquote()
	if(disablefader==1)
	{
	}
	else
	{
		setTimeout('try{domove()} catch (e) {};',frameinterval)
	}
}

var curover = 0;
var curunder = 0;

function newImage(over) {
  if (over) {
    do {
    curover = rndno(1,imagecount);
    } while (curunder == curover);
    document.getElementById('fader_over').src=imageroot+'image'+curover+'.jpg?'
  } else {
    do {
      curunder = rndno(1,imagecount);
    } while (curunder == curover);
    document.getElementById('fader_under').src=imageroot+'image'+curunder+'.jpg?'
  }
}

/*Creates the fader.*/
function createFader()
{
	document.getElementById('fader_quote_background').style.display='block'
	document.getElementById('fader_quote_text').style.display='block'
	newImage(true);
	domove()
}
