
//
// CodaEffects.js - (C) 2007 Panic, Inc.
//
// Used to scroll our tabbed view, display our download popup, and display the giant dialog.
// Requires: Effects.js
//
// Not for redistribution.

// Scroll: Setup Scrolling Stuff

var currentCNav = "cNavPL";
var currentSection = "rediscover-channel1"; // The default loaded section on the page
var currentSectionNum = 0;
var tabTag = "";
var paneTag = "-channel1";
var m, c;
var moveArrow = {start:40, end:40};

var c1sections = new Array();
c1sections[0] = "rediscover-channel1";
c1sections[1] = "network-channel1";
c1sections[2] = "learn-channel1";
c1sections[3] = "connect-channel1";
c1sections[4] = "build-channel1";

var c1arrow = new Array();
c1arrow[0] = 40;
c1arrow[1] = 105;
c1arrow[2] = 154;
c1arrow[3] = 203;
c1arrow[4] = 251;

var currentSection2 = "hillsong-channel2"; // The default loaded section on the page
var currentSectionNum2 = 0;
var tabTag2 = "";
var paneTag2 = "-channel2";
var d;
var c2sections = new Array();
c2sections[0] = "hillsong-channel2";
c2sections[1] = "dzschech-channel2";
c2sections[2] = "jhouston-channel2";
c2sections[3] = "esmith-channel2";
c2sections[4] = "cdheath-channel2";
c2sections[5] = "chall-channel2";
c2sections[6] = "jwilson-channel2";
c2sections[7] = "nbeach-channel2";
c2sections[8] = "pvischer-channel2";
c2sections[9] = "icron-channel2";
c2sections[10] = "nortberg-channel2";
c2sections[11] = "gmacdonald-channel2";
c2sections[12] = "mcaliguire-channel2";
c2sections[13] = "smiddlebrook-channel2";
c2sections[14] = "jwilhite-channel2";
c2sections[15] = "rbarton-channel2";
c2sections[16] = "rnoland-channel2";
c2sections[17] = "rscovill-channel2";
c2sections[18] = "sendicott-channel2";
c2sections[19] = "sjackson-channel2";
c2sections[20] = "jsippel-channel2";
c2sections[21] = "nsearcy-channel2";
c2sections[22] = "bwhitemire-channel2";
c2sections[23] = "jhatley-channel2";
c2sections[24] = "ebramlett-channel2";
c2sections[25] = "tbiaggne-channel2";
c2sections[26] = "mlundgren-channel2";
c2sections[27] = "darmentrout-channel2";
c2sections[28] = "tcarson-channel2";
c2sections[29] = "jhorness-channel2";
//c2sections[30] = "jberryman-channel2";

// Scroll the page manually to the position of element "link", passed to us.

function ScrollSection(link, scrollArea, offset)
{

	// Store the last section, and update the current section
	if (currentSection == c1sections[link]) {
		return;
	}
	lastSection = currentSection;
	currentSection = c1sections[link];
	currentSectionNum = link;
	// Change the section highlight.
	// Extract the root section name, and use that to change the background image to 'top', revealing the alt. state


    
	// Get the element we want to scroll, get the position of the element to scroll to
	
	theScroll = document.getElementById(scrollArea);
	position = findElementPos(document.getElementById(currentSection));

	// Get the position of the offset div -- the div at the far left.
	// This is the amount we compensate for when scrolling
	
	if (offset != "") {
		offsetPos = findElementPos(document.getElementById(offset));
		position[0] = position[0] - offsetPos[0];
	}

	scrollStart(theScroll, theScroll.scrollLeft, position[0]);
	// return false;
}


function ScrollSection2(link, scrollArea, offset)
{

	// Store the last section, and update the current section
	if (currentSection2 == c2sections[link]) {
		return;
	}
	lastSection2 = currentSection2;
	currentSection2 = c2sections[link];
	currentSectionNum2 = link;
	// Change the section highlight.
	// Extract the root section name, and use that to change the background image to 'top', revealing the alt. state

    
	// Get the element we want to scroll, get the position of the element to scroll to
	
	theScroll2 = document.getElementById(scrollArea);
	position2 = findElementPos(document.getElementById(currentSection2));

	// Get the position of the offset div -- the div at the far left.
	// This is the amount we compensate for when scrolling
	
	if (offset != "") {
		offsetPos = findElementPos(document.getElementById(offset));
		position2[0] = position2[0] - offsetPos[0];
	}

	scrollStartB(theScroll2, theScroll2.scrollLeft, position2[0]);
	// return false;
}



//
// Animated Scroll Functions
// Scrolls are synchronous -- only one at a time.
//

var scrollanim = {time:0, begin:0, change:0.0, duration:0.0, element:null, timer:null};

function scrollStart(elem, start, end, direction)
{
	//console.log("scrollStart from "+start+" to "+end+" in direction "+direction);

	if (scrollanim.timer != null) {
		clearInterval(scrollanim.timer);
		scrollanim.timer = null;
	}
	scrollanim.time = 0;
	scrollanim.begin = start;
	scrollanim.change = end - start;
	scrollanim.duration = 50;
	scrollanim.element = elem;
	
	scrollanim.timer = setInterval("scrollHorizAnim();", 15);

}


function scrollHorizAnim()
{
	if (scrollanim.time > scrollanim.duration) {
		clearInterval(scrollanim.timer);
		scrollanim.timer = null;
	}
	else {
		move = sineInOut(scrollanim.time, scrollanim.begin, scrollanim.change, scrollanim.duration);
		scrollanim.element.scrollLeft = move;
		scrollanim.time++;
	}
}


//
// Animated Scroll Functions
// Scrolls are synchronous -- only one at a time.
//

var scrollanimB = {time:0, begin:0, change:0.0, duration:0.0, element:null, timer:null};

function scrollStartB(elem, start, end, direction)
{
	//console.log("scrollStart from "+start+" to "+end+" in direction "+direction);

	if (scrollanimB.timer != null) {
		clearInterval(scrollanimB.timer);
		scrollanimB.timer = null;
	}
	scrollanimB.time = 0;
	scrollanimB.begin = start;
	scrollanimB.change = end - start;
	scrollanimB.duration = 50;
	scrollanimB.element = elem;
	
	scrollanimB.timer = setInterval("scrollHorizAnimB();", 15);

}


function scrollHorizAnimB()
{
	if (scrollanimB.time > scrollanimB.duration) {
		clearInterval(scrollanimB.timer);
		scrollanimB.timer = null;
	}
	else {
		moveB = sineInOut(scrollanimB.time, scrollanimB.begin, scrollanimB.change, scrollanimB.duration);
		scrollanimB.element.scrollLeft = moveB;
		scrollanimB.time++;
	}
}


// Utility: Find the Y position of an element on a page. Return Y and X as an array

function findElementPos(elemFind)
{
	var elemX = 0;
	var elemY = 0;
	do {
		elemX += elemFind.offsetLeft;
		elemY += elemFind.offsetTop;
	} while ( elemFind = elemFind.offsetParent )

	//console.log("Found element "+elemFind+" at "+elemY+"/"+elemX);

	return Array(elemX, elemY);
}

function sineInOut(t, b, c, d)
{
	return -c/2 * (Math.cos(Math.PI*t/d) - 1) + b;
}



function MoveArrow(num) {
	clearInterval(m);
	moveArrow.end = c1arrow[num];
	if(moveArrow.end != moveArrow.start)
		m = setInterval('MoveArrowNow()', 15);
}
function MoveArrowNow() {
	if(moveArrow.start == moveArrow.end) {
		clearInterval(m);
	}
	else
	{
		var dif = moveArrow.end - moveArrow.start;
		if(dif > 0)
		{
			moveArrow.start = moveArrow.start + (dif/10 + 1);
			if(moveArrow.start > moveArrow.end)
			{
				moveArrow.start = moveArrow.end;
				clearInterval(m);
			}
		}
		else
		{
			moveArrow.start = moveArrow.start + (dif/10 - 1);
			if(moveArrow.start < moveArrow.end)
			{
				moveArrow.start = moveArrow.end;
				clearInterval(m);
			}
		}
		
		document.getElementById('screen1MenuArrow').style.left = moveArrow.start + "px";
	}
}

function cycleScreen1() {
	c = setInterval('cycleScreen1Now()', 9500);
}
function cycleScreen1Now() {
	currentSectionNum++;
	if(currentSectionNum == 5)
		currentSectionNum = 0;
	ScrollSection(currentSectionNum, "screen1", "rediscover-channel1")
	MoveArrow(currentSectionNum);
}
function clearScreen1() {
	clearInterval(c);
}

function cycleScreen2() {
	d = setInterval('cycleScreen2Now()', 11300);
}
function cycleScreen2Now() {
	currentSectionNum2++;
	if(currentSectionNum2 == c2sections.length)
		currentSectionNum2 = 0;
	ScrollSection2(currentSectionNum2, "screen2", "hillsong-channel2")
}
function clearScreen2() {
	clearInterval(d);
}

function channel2Next() {
	currentSectionNum2++;
	if(currentSectionNum2 == c2sections.length)
		currentSectionNum2 = 0;
	ScrollSection2(currentSectionNum2, "screen2", "hillsong-channel2")
}

function channel2Prev() {
	currentSectionNum2--;
	if(currentSectionNum2 < 0)
		currentSectionNum2 = c2sections.length - 1;
	ScrollSection2(currentSectionNum2, "screen2", "hillsong-channel2")
}

function changeClass(id, newClass) {
	document.getElementById(id).className = newClass;
}

function turnOn(object) {
	object.className = "on";
}
function turnOff(object) {
	//object.className = "";
}

function leaveOn(id) {
	if(id != currentCNav)
	{
		document.getElementById(id).className = "on";
		document.getElementById(currentCNav).className = "";
		currentCNav = id;
	}
}

function toggleBio(anchor, divID) {
	var div = document.getElementById(divID);
	if(div.className == 'hide')
	{
		div.className = 'show';
		anchor.className = 'fullBioOpen';
	}
	else
	{
		div.className = 'hide';
		anchor.className = 'fullBio';
	}
}

function toggleFAQ(faq, block) {
	if(faq.className == "faq")
	{
		faq.className = "faqOn";
		document.getElementById('faq'+block).className = "faqBlockOn";
	}
	else
	{
		faq.className = "faq";
		document.getElementById('faq'+block).className = "faqBlock";
	}
}

function clickclear(thisfield, defaulttext) {
	if (thisfield.value == defaulttext) {
	thisfield.value = "";
	}
}
function clickrecall(thisfield, defaulttext) {
	if (thisfield.value == "") {
	thisfield.value = defaulttext;
	}
}
