
/**
 * @package		PageBreak2 Content Plugin
 * @version		$Id: pagebreak2.js 12 2008-12-01 16:17:17Z tcp $
 * @copyright	Copyright (C) 2008 Green Mountain Information Technology and Consulting. All rights reserved.
 * @author		Toby Patterson (tcp@gmitc.biz)
 * @license		Mixed
 */

window.addEvent('domready', function() {
	
	// Create variables (in this case two arrays) representing our bubbles and pages
	var tocLinks = $$('.toclick');
	var articlePages = $$('.articlePage');

	// Set bubbles opacity to zero so they're hidden initially and toggle visibility on for their container	
	
	// Add our events to the pages
	tocLinks.each(function(el, i) {
	
		if (i) articlePages[i].style.display = 'none';
		
		el.showPage = function()
		{
			$$('.pb2active').removeClass('pb2active');
			this.addClass('pb2active');
			
			for(var j = 0; j < articlePages.length; j++)
			{
				var opacity = articlePages[j].getStyle('opacity');
				var display = articlePages[j].getStyle('display');
				var delay	= $chk(pageBreak2TransitionDelay) ? pageBreak2TransitionDelay : 200;
				
				if(j!=i)
				{
					// Hide the currently displayed part
					if (display == 'block') {
						
						// Start fading the page out
						articlePages[j].effect('opacity', {duration: delay, transition: Fx.Transitions.Expo.easeOut}).start(1,0);
						
						// Turn off the display after a second
						(function(){
							this.setStyle('display', 'none');
						}).delay(delay, articlePages[j]);
					}

				} else {
					
					// Show the desired page
					if ( display == 'none' ) {
						
						
						
						// After a second, show the new page and start the transition
						(function(){
							
							this.setStyle('opacity', 0);
							this.setStyle('display', '');
							
							this.effect('opacity', {duration: delay, transition: Fx.Transitions.Expo.easeIn}).start(0,1);
						}).delay(delay, articlePages[j]);
					}
				}
			}

			return false;
		}
		
		el.addEvents({
			'click': el.showPage
		});
		
	});
	
	var hash = window.location.hash;
	hash = hash.replace(/^\W+/,'');
	if ( ! hash ) { hash = 'pb2start' };

	var link = $(hash+'-link');
	if ( link )
	{
		link.showPage();
	}
	
});
