/*  
	JavaScript Document for PregnancyNewark.com Purple Speech Bubble Layout
	Design by: Jacob Barr of iRapture.com
	Development by: Joshua Moor of iRapture.com
	
	Copyright iRapture.com All Rights Reserved
	Last Modified: 7/13/2010
*/

window.onload = function(){
	window.extendedSliderHeight = 150; //final height of sliders
	window.rateOfSliderChange = 20; //number of px to increase/decrease slider height on each animation pass
	window.sliderAnimationDelay = 1; //delay between animation cycles
	window.callSlider = document.getElementById('callMe');
	window.emailSlider = document.getElementById('emailMe');
	window.textSlider = document.getElementById('textMe');
	
	window.bubbleMinHiddenDelay = 1000;//set animation variables
	window.bubbleMaxHiddenDelay = 2500;
	window.bubbleMinDisplayDelay = 3000;
	window.bubbleMaxDisplayDelay = 8000;
	window.bubbleRateofChange = 1; //in 10%
	
	window.bubble1Delay = 2000; //set initial timeouts
	window.bubble2Delay = 5000;
	window.bubble3Delay = 6000;
	
	window.bubble1Opacity = 0;//set initial animation variables
	window.bubble2Opacity = 0;
	window.bubble3Opacity = 0;
	
	window.bubble1 = document.getElementById('speechBubble1');
	window.bubble2 = document.getElementById('speechBubble2');
	window.bubble3 = document.getElementById('speechBubble3');
	
	prepSpeechBubbles();
}

function prepSpeechBubbles(){
	//set initial opacity
	window.bubble1.style.opacity = window.bubble1Opacity/10;
	window.bubble2.style.opacity = window.bubble2Opacity/10;
	window.bubble3.style.opacity = window.bubble3Opacity/10;
	
	//set initial IE opacity
	window.bubble1.style.filter = 'alpha(opacity='+window.bubble1Opacity*10+')';
	window.bubble2.style.filter = 'alpha(opacity='+window.bubble2Opacity*10+')';
	window.bubble3.style.filter = 'alpha(opacity='+window.bubble3Opacity*10+')';
	
	//set initial timeouts
	setTimeout("fadeIn('1')", window.bubble1Delay);
	setTimeout("fadeIn('2')", window.bubble2Delay);
	setTimeout("fadeIn('3')", window.bubble3Delay);
	
	//set initial slogans
	switchSlogan("1");
	switchSlogan("2");
	switchSlogan("3");
	
	//set initial animation delays
	setAnimationDelay("1");
	setAnimationDelay("2");
	setAnimationDelay("3");
}

function setAnimationDelay(targetBubble){
	var animationLength = Math.floor(Math.random() * 50 + 50);
	switch(targetBubble){
		case "1":
			window.bubble1Delay = animationLength;
		break;
		case "2":
			window.bubble2Delay = animationLength;
		break;
		case "3":
			window.bubble3Delay = animationLength;
		break;
	}
}

function fadeIn(targetBubble){
	var bubble, currentOpacity, delay;
	
	switch(targetBubble){//get current values
		case "1":
			bubble = window.bubble1;
			currentOpacity = window.bubble1Opacity;
			delay = window.bubble1Delay;
		break;
		case "2":
			bubble = window.bubble2;
			currentOpacity = window.bubble2Opacity;
			delay = window.bubble2Delay;
		break;
		case "3":
			bubble = window.bubble3;
			currentOpacity = window.bubble3Opacity;
			delay = window.bubble3Delay;
		break;
	}
	
	//make changes
	currentOpacity += window.bubbleRateofChange;
	bubble.style.opacity = currentOpacity/10;
	bubble.style.filter = 'alpha(opacity='+currentOpacity*10+')';
	
	switch(targetBubble){//update current opacity
		case "1":
			window.bubble1Opacity = currentOpacity;
		break;
		case "2":
			window.bubble2Opacity = currentOpacity;
		break;
		case "3":
			window.bubble3Opacity = currentOpacity;
		break;
	}
	
	if(currentOpacity == 10){//setup next timeout
		var displayTime = Math.floor(Math.random() * (window.bubbleMaxDisplayDelay - window.bubbleMinDisplayDelay) + window.bubbleMinDisplayDelay);
		setTimeout("fadeOut('"+targetBubble+"')", displayTime);
	}else{
		setTimeout("fadeIn('"+targetBubble+"')", delay);
	}
	
}

function switchSlogan(targetBubble){
	var bubbleText, bubble;
	switch(targetBubble){//update current opacity
		case "1":
			bubble = window.bubble1;
			bubbleText= Array("<p style='font-size:30px; padding:30px 10px 0px 0px;'>could I be<br/>pregnant?</p>", "<p style='font-size:30px; padding:30px 10px 0px 0px;'>could I be<br/>pregnant?</p>", "<p style='font-size:24px; padding:15px 10px 0px 0px;'>how can I<br/>tell if I<br/>have an STD?</p>","<p style='font-size:24px; padding:30px 10px 0px 0px;'>how can I<br/>get through<br/>this?</p>");
		break;
		case "2":
			bubble = window.bubble2;
			bubbleText= Array("<p style='font-size:30px; letter-spacing:2px; margin-top:-5px;'>I had plans.</p>", "<p style='font-size:28px; letter-spacing:2px; margin-top:-5px;'>can I finish school?</p>","<p style='font-size:30px; letter-spacing:2px; margin-top:-5px;'>am i pregnant?</p>");
		break;
		case "3":
			bubble = window.bubble3;
			bubbleText= Array("<p style='font-size:24px; letter-spacing:3px; margin:10px 0px 0px 30px;'>what should I do?</p>", "<p style='font-size:24px; letter-spacing:3px; margin:10px 0px 0px 30px;'>I can't tell anyone.</p>", "<p style='font-size:24px; letter-spacing:3px; margin:10px 0px 0px 30px;'>what should I do?</p>", "<p style='font-size:24px; letter-spacing:3px; margin-left:20px;'>am I really pregnant?</p>", "<p style='font-size:20px; letter-spacing:3px; margin-left:50px;'>what are the risks<br/>with abortion?</p>");
		break;
	}
	var randomSelection = Math.floor(Math.random()*bubbleText.length);
	bubble.innerHTML = bubbleText[randomSelection];
}

function fadeOut(targetBubble){
	var bubble, currentOpacity, delay;
	
	switch(targetBubble){//get current values
		case "1":
			bubble = window.bubble1;
			currentOpacity = window.bubble1Opacity;
			delay = window.bubble1Delay;
		break;
		case "2":
			bubble = window.bubble2;
			currentOpacity = window.bubble2Opacity;
			delay = window.bubble2Delay;
		break;
		case "3":
			bubble = window.bubble3;
			currentOpacity = window.bubble3Opacity;
			delay = window.bubble3Delay;
		break;
	}
	
	//make changes
	currentOpacity -= window.bubbleRateofChange;
	bubble.style.opacity = currentOpacity/10;
	bubble.style.filter = 'alpha(opacity='+currentOpacity*10+')';
	
	switch(targetBubble){//update current opacity
		case "1":
			window.bubble1Opacity = currentOpacity;
		break;
		case "2":
			window.bubble2Opacity = currentOpacity;
		break;
		case "3":
			window.bubble3Opacity = currentOpacity;
		break;
	}
	
	if(currentOpacity == 0){
		switchSlogan(targetBubble);
		setAnimationDelay(targetBubble);
		var displayTime = Math.floor(Math.random() * (window.bubbleMaxDisplayDelay - window.bubbleMinDisplayDelay) + window.bubbleMinDisplayDelay);
		setTimeout("fadeIn('"+targetBubble+"')", displayTime);
	}else{
		setTimeout("fadeOut('"+targetBubble+"')", delay);
	}
}

//sets target heights for sliders in preperation for animation
function switchSlider(sliderName){
	switch(sliderName){
		case "callMe":
			window.callSliderTargetHeight = window.extendedSliderHeight;
			window.emailSliderTargetHeight = 0;
			window.textSliderTargetHeight = 0;
		break;
		case "emailMe":
			window.callSliderTargetHeight = 0;
			window.emailSliderTargetHeight = window.extendedSliderHeight;
			window.textSliderTargetHeight = 0;
		break;
		case "textMe":
			window.callSliderTargetHeight = 0;
			window.emailSliderTargetHeight = 0;
			window.textSliderTargetHeight = window.extendedSliderHeight;
		break;
	}
	
	animateSliders();
}

function animateSliders(){
	clearTimeout(window.animationTimer);
	
	var continueAnimation = false;
	//Check current heights
	var currentCallSliderHeight = window.callSlider.offsetHeight;
	var currentEmailSliderHeight = window.emailSlider.offsetHeight;
	var currentTextSliderHeight = window.textSlider.offsetHeight;
	var newHeight = 0;
	
	if(currentCallSliderHeight != window.callSliderTargetHeight){//check to see if the slider is animating
		if(currentCallSliderHeight < window.callSliderTargetHeight){//check to see if the slider is expanding or contracting
			newHeight = currentCallSliderHeight + window.rateOfSliderChange;//set new height
			if(newHeight > window.callSliderTargetHeight){//check new height to see if is within bounds
				newHeight = window.callSliderTargetHeight;//if not adjust
			}
		}else{
			newHeight = currentCallSliderHeight - window.rateOfSliderChange;
			if(newHeight < window.callSliderTargetHeight){
				newHeight = window.callSliderTargetHeight;
			}
		}
		
		if(newHeight != window.callSliderTargetHeight){//check to see if this slide needs to animate anymore
			continueAnimation = true;
		}
		
		window.callSlider.style.height = newHeight + "px"; //set new height to slider
	}
	
	if(currentEmailSliderHeight != window.emailSliderTargetHeight){
		if(currentEmailSliderHeight < window.emailSliderTargetHeight){//check to see if the slider is expanding or contracting
			newHeight = currentEmailSliderHeight + window.rateOfSliderChange;//set new height
			if(newHeight > window.emailSliderTargetHeight){//check new height to see if is within bounds
				newHeight = window.emailSliderTargetHeight;//if not adjust
			}
		}else{
			newHeight = currentEmailSliderHeight - window.rateOfSliderChange;
			if(newHeight < window.emailSliderTargetHeight){
				newHeight = window.emailSliderTargetHeight;
			}
		}
		
		if(newHeight != window.emailSliderTargetHeight){//check to see if this slide needs to animate anymore
			continueAnimation = true;
		}
		
		window.emailSlider.style.height = newHeight + "px"; //set new height to slider
	}
	
	if(currentTextSliderHeight != window.textSliderTargetHeight){
		if(currentTextSliderHeight < window.textSliderTargetHeight){//check to see if the slider is expanding or contracting
			newHeight = currentTextSliderHeight + window.rateOfSliderChange;//set new height
			if(newHeight > window.textSliderTargetHeight){//check new height to see if is within bounds
				newHeight = window.textSliderTargetHeight;//if not adjust
			}
		}else{
			newHeight = currentTextSliderHeight - window.rateOfSliderChange;
			if(newHeight < window.textSliderTargetHeight){
				newHeight = window.textSliderTargetHeight;
			}
		}
		
		if(newHeight != window.textSliderTargetHeight){//check to see if this slide needs to animate anymore
			continueAnimation = true;
		}
		
		window.textSlider.style.height = newHeight + "px"; //set new height to slider
	}
	
	if(continueAnimation){//if animation needs to continue do so
		window.animationTimer = setTimeout("animateSliders()", window.sliderAnimationDelay);
	}
}
