var scrolling=0,
    notesTop=0,
    lyricsTop=0,
    notesTravel=0,
    lyricsTravel=0,
    notesStop=0,
    lyricsStop=0,
    scrollTimer;

window.addEvent('domready', function(){
	
	if ($('showwordsover')) {
		$('showwordsover').addEvent('click', function(e){
			showOverlay('wordsover');
			if (notesTravel==0) {
				scrollTimer = wordsScroll.periodical(25);
				var notesHeight = $('wordsnotes').getSize().y;
				var lyricsHeight = $('wordslyrics').getSize().y;
				var windowHeight = window.getSize().y;
				notesTravel=Math.round(notesHeight/300);
			    lyricsTravel=Math.round(lyricsHeight/300);
			    notesStop=Math.round((windowHeight/2)-notesHeight);
			    lyricsStop=Math.round((windowHeight/2)-lyricsHeight);
			}
			e.stop();
		});
		
		$('wordsoverbg').addEvent('click', function(e){
			hideOverlay('wordsover');
			e.stop();
		});
		
		$('closewords').addEvent('click', function(e){
			hideOverlay('wordsover');
			e.stop();
		});
		
		if (Browser.Engine.trident4) {
			fixIe6Fixed('wordsover');
			window.addEvent('scroll', fixIe6Fixed.bind(this,'wordsover'));
			$('wordscover').set('html','<img src="images/PLcover.jpg" alt="Power+Light" id="plcover" /><br /><a href="#" id="scrollup"><img src="images/scroll.gif" alt="scroll" width="11" height="42"/></a><br /><a href="#" id="closewords"><img src="images/close.gif" alt="close" width="11" height="34"/></a><br /><a href="#" id="scrolldown"><img src="images/scroll.gif" alt="scroll" width="11" height="42"/></a>');
			$('closewords').addEvent('click', function(e){
				hideOverlay('wordsover');
				e.stop();
			});
		}
	}
	
	$('scrollup').addEvent('mouseover', function(){
		scrolling = 1;
	});
	$('scrolldown').addEvent('mouseover', function(){
		scrolling = -1;
	});
	$('scrollup').addEvent('mouseout', function(){
		scrolling = 0;
	});	
	$('scrolldown').addEvent('mouseout', function(){
		scrolling = 0;
	});
	$('scrollup').addEvent('click', function(e){
		e.stop();
	});
	$('scrolldown').addEvent('click', function(e){
		e.stop();
	});

});

function showOverlay(whichOverlay) {
	$(whichOverlay).setStyle('display','block');
	$(whichOverlay + 'bg').get('tween', {property:'opacity',duration:125}).start(0,0.85).chain(function(){
		$(whichOverlay + 'content').get('tween', {property:'opacity',duration:350}).start(0,1);
	});
}

function hideOverlay(whichOverlay) {
	$(whichOverlay + 'content').get('tween', {property:'opacity',duration:350}).start(1,0);
	$(whichOverlay + 'bg').get('tween', {property:'opacity',duration:125}).start(0.85,0).chain(function(){
		$(whichOverlay).setStyle('display','none');
	});
}

function fixIe6Fixed(whichOverlay) {
	$(whichOverlay).setStyles({
		'position': 'absolute',
		'overflow': 'hidden',
		'top': window.getScroll().y+'px',
		'left': window.getScroll().x+'px',
		'width': '100%',
		'height': '100%'
	});
}

function wordsScroll() {
	if (scrolling) {
		if ((scrolling < 0 && notesTop > notesStop) || (scrolling > 0 && notesTop < 0)) {
			notesTop = notesTop+(notesTravel*scrolling);
			$('wordsnotes').setStyle('top',notesTop);	
		}
		if ((scrolling < 0 && lyricsTop > lyricsStop) || (scrolling > 0 && lyricsTop < 0)) {
			lyricsTop = lyricsTop+(lyricsTravel*scrolling);
			$('wordslyrics').setStyle('top',lyricsTop);
		}
	}
}