////////////////////////////////////////////////////////////////
// Variables
////////////////////////////////////////////////////////////////
step = new Array();
stepSlide = new Array();
crumb = new Array();


////////////////////////////////////////////////////////////////
// Functions
////////////////////////////////////////////////////////////////
function initSlideSteps(stepID, stepCookieID) { 
	// Current Step
	var currentStep = getCurrentStep(stepCookieID);
	
	// Steps
	if (steps = $$('#' + stepID + ' .step')) { 	
		step[stepID] = new Array();
		stepSlide[stepID] = new Array();
		slideCurrent = 0; // will turn to 1 some where in the loop below.  If 1, then begin first slide (at the end of this loop)
		
		// Loop Steps
		steps.each(
		function(stepObj, stepIndex) { 
			// Update Step Array
			step[stepID][stepIndex] = stepObj;
			

			////////////////////////////////////////////////////////////////
			// Next Buttons
			////////////////////////////////////////////////////////////////
			if (stepNext = stepObj.getElements('.next')) { 
				// Loop Nexts
				stepNext.each(
				function(stepNextObj, stepNextIndex) { 
					// Update Step['next'] Array
					step[stepID][stepIndex]['next'] = stepNextObj;
				
					// Step['next'] onClick
					stepNextObj.addEvent(
						'click', 
						function(e) {
							stepSwitch(stepIndex + 1, stepID, stepCookieID);
						}
					);

				}
				);
			}



			////////////////////////////////////////////////////////////////
			// Back Buttons
			////////////////////////////////////////////////////////////////
			if (stepBack = stepObj.getElements('.back')) { 
				// Loop Backs
				stepBack.each(
				function(stepBackObj, stepBackIndex) { 
					// Update Step['back'] Array
					step[stepID][stepIndex]['back'] = stepBackObj;
				
					// Step['back'] onClick
					stepBackObj.addEvent(
						'click', 
						function(e) {
							stepSwitch(stepIndex - 1, stepID, stepCookieID);
						}
					);
					
				}
				);
			}



			////////////////////////////////////////////////////////////////
			// Setup Slides(s)
			////////////////////////////////////////////////////////////////
			stepSlide[stepID][stepIndex] = 
			new Fx.Slide(
				stepObj, {
					duration: 350,
					// mode: 'horizontal',
					transition: Fx.Transitions.Pow.easeOut
				}
			);
			
			// Hide Slide
			hideSlide(stepID, stepIndex);
			
			// CSS Visibility ON
			stepObj.setStyle('visibility', 'visible');
			
			
			////////////////////////////////////////////////////////////////
			// Initialize Current Step
			////////////////////////////////////////////////////////////////
			if (currentStep == stepIndex) slideCurrent = 1;
		}
		);
		
		// Toggle Current Step
		if (slideCurrent) { 
			toggleSlide(stepID, currentStep);
		}
	}
}

function hideSlide(stepID, stepIndex) { 
	if (typeof hideSlideBegin == 'function') hideSlideBegin(stepID, stepIndex);
	stepSlide[stepID][stepIndex].hide();
	if (typeof hideSlideEnd == 'function') hideSlideEnd(stepID, stepIndex);
}

function toggleSlide(stepID, stepIndex) { 
	if (typeof toggleSlideBegin == 'function') toggleSlideBegin(stepID, stepIndex); 
	stepSlide[stepID][stepIndex].toggle().chain(function() {
		if (typeof toggleSlideEnd == 'function') {
			toggleSlideEnd(stepID, stepIndex);
		}
	});
}

function stepSwitch(skipStep, stepID, stepCookieID) { 
	// Current Step
	var currentStep = getCurrentStep(stepCookieID);

	// Hide all Step Slides
	stepSlide[stepID].each(
	function(stepObj, stepIndex) { 
		// Hide Slide
		hideSlide(stepID, stepIndex);
	}
	);

	// Toggle Current Step Slide
	toggleSlide(stepID, skipStep);
	
	// Update Cookie (from skipStep)
	setCookie(stepCookieID, skipStep, 1);
}

function initCrumbSteps(crumbID, stepID, stepCookieID) { 
	// Current Step
	var currentStep = getCurrentStep(stepCookieID);
	
	// Bread Crumbs
	crumb[crumbID] = $$('#'+crumbID+' .page');

	// Tips
	crumb[crumbID]['tip'] = $$('#'+crumbID+' .page .note');

	// Anchor Links
	crumb[crumbID]['anchor'] = $$('#'+crumbID+' .page a');
	
	// Loop Anchors
	crumb[crumbID]['anchor'].each(
	function(anchorObj, anchorIndex) { 

		anchorObj.addEvent(
			'click', 
			function(e) {
				crumbStepSwitch(crumbID, stepID, anchorIndex, stepCookieID);
				stepSwitch(anchorIndex, stepID, stepCookieID)
			}
		);
	}
	);

	// Initialize Crumbs
	crumb[crumbID].each(
	function(crumbObj, crumbIndex) { 
		// Current Step
		if (crumbIndex == currentStep) { 
			crumb[crumbID][currentStep].addClass('current');
		}
	}
	);

	// Steps
	step[stepID].each(
	function(stepObj, stepIndex) { 
		// Update step['next'] onClick Event
		if (step[stepID][stepIndex]['next'] != null) { 
			step[stepID][stepIndex]['next'].addEvent(
				'click', 
				function(e) {
					crumbStepSwitch(crumbID, stepID, (stepIndex + 1), stepCookieID);
				}
			);
		}

		// Update step['back'] onClick Event
		if (step[stepID][stepIndex]['back'] != null) { 
			step[stepID][stepIndex]['back'].addEvent(
				'click', 
				function(e) {
					crumbStepSwitch(crumbID, stepID, (stepIndex - 1), stepCookieID);
				}
			);
		}
	}
	);

}

function crumbStepSwitch(crumbID, stepID, skipStep, stepCookieID) { 
	// Current Step
	for (c = 0; c < crumb[crumbID].length; c++) { 
		crumb[crumbID][c].removeClass('current');
	}

	// Toggle Crumb
	crumb[crumbID][skipStep].addClass('current');
}

function initContinueSlides(stepID, stepCookieID) { 
	var currentStep = getCurrentStep(stepCookieID);
	
	// Continues
	if (continueSlides = $$('#' + stepID + ' .continueSlide')) { 	
		continueSlide[stepID] = new Array();
		
		// Loop Continues (start off hidden);
		continueSlides.each(
		function(continueObj, continueIndex) { 
			// Update Step Array
			continueSlide[stepID][continueIndex] = 
			new Fx.Slide(
				continueObj,
				{
					duration: 350,
					// mode: 'horizontal',
					transition: Fx.Transitions.Pow.easeOut
				}
			).hide();
			
			// If current step, then toggle Continue Slide
			/*
			if (continueIndex == currentStep) { 
				continueSlide[stepID][continueIndex].toggle();
			}
			*/
		}
		);
	}
}

function getCurrentStep(stepCookieID) { 
	var currentStep = getCookie(stepCookieID);
	if (currentStep == '') { 
		currentStep = 0;
		
		// Update Current Step Cookie
		setCookie(stepCookieID, currentStep, 1);
	}
	else { 
		// Sometimes it's 'undefined' (this isn't normal)
		if (currentStep == 'undefined') { 
			currentStep = 0;
			
			// Update Current Step Cookie
			setCookie(stepCookieID, currentStep, 1);
		}

		currentStep = (currentStep * 1);
	}

	if (currentStep < 0) currentStep = 0;
	
	return currentStep;
}
