/**
 * All code protected by United States copyright law and may not be reproduced, distributed, transmitted, displayed, published or broadcast without the prior written permission of Christian.tv. 
 * All material contained are the copyrighted property of the company unless otherwise specified in association with a particular piece of material. No code owned, operated, licensed, or controlled by us may be copied, reproduced, republished, uploaded, posted, transmitted, or distributed in any way, without express permission. 
 * In the event you purchase software from Christian.tv, the software, including any files, images incorporated in or generated by the software, and data accompanying the software (collectively, the "Software") are licensed to you by us or third-party licensors for your use only. 
 * We do not transfer title to the Software to you. We (or our third-party licensors) retain full and complete title to the Software and all intellectual property rights therein. You may not redistribute, sell, auction, decompile, reverse engineer, disassemble, or otherwise reduce the Software to a human-readable form.
*/
// JavaScript Document
var timer = new Array(null, null, null);
var theScroll = new Array();
var content = new Array();

var currentIndex = 0;
var timerInterval = 15;
var timerDelay = 800;

var arraypos = 0;
var no_end = 0;

var autoDelay1 = 60*1000;
var autoDelay2 = 60*1000;

var autoTimer = null;

function add_content(text){
	//alert(text);
	if(arraypos < content.length){
	}
	else{
		content.push(text);
		arraypos++;
	}
	
}

function write_content(a1, a2, a3, a4, a5){

	if(a1) timerInterval = a1;
	if(a2) timerDelay = a2;

	no_end = ( a3 ? 1 : 0 );

	if(a4) autoDelay1 = a4*1000;
	if(a5) autoDelay2 = a5*1000;

	var k = 0;

	var str = '';


	 str += '<TABLE ALIGN="left" BORDER="0" CELLSPACING=0 CELLPADDING=0 WIDTH="261" HEIGHT="479">';
	 str += '';
	 str += '<tr><td>';

	 str += '<DIV id="frame" style="overflow: hidden; margin: 0 auto; width: 261px; position: relative; display:inline;height:458px;">';
	 str += '<TABLE ALIGN="left" BORDER=0 CELLSPACING=0 CELLPADDING=0 WIDTH="261" HEIGHT="458">';

	for (var i = 1; i < 4; i++) {

		var space = ( i == 3 ? '150' : '154' );

		 str += '<TR ALIGN="left" VALIGN="top" style="height:'+space+'px;"><TD>';
		 str += '<DIV id="scroller-'+i+'" style="width: 261px; margin: 0 auto; overflow: hidden;">';
		 str += '<DIV id="content-'+i+'" style="height: 150px; width: 5250px;">';

		for (var j = 1; j < 4; j++) {
			 str += '<DIV id="item_'+j+'-'+i+'" style="width: 261px; margin: 0px; float: left; height: 150px;">';
			 str += content[k++];
			 str += '</DIV>';
		}

		 str += '</DIV>';
		 str += '</DIV>';
		 str += '</TD></TR>';

	}

	 str += '</TABLE>';
	 str += '</DIV>';
	 str += '</td></tr>';

	 str += '';
	 str += '<tr><td><div style="width:20px; width:261px; margin:0; position: relative;  display:inline;">';
	 str += '<TABLE ALIGN="left" BORDER="0" CELLSPACING=0 CELLPADDING=0 HEIGHT="20" WIDTH="261" style="postion: relative; text-align:center;  background: #003C58" ><TR ALIGN="middle" VALIGN="middle"><TD><a href="javascript:ScrollArrow(\'left\',\'scroller\',\'item_1\',1);" ><img src="/img/navleft.jpg" style="border:0" height="21" alt="left scroll arrow" /></a></TD><TD>';
	 str += '<TABLE ID="nav-1" ALIGN="center" BORDER="0" CELLSPACING="0" CELLPADDING="0" HEIGHT="10" WIDTH="10" style="border:solid 0px white; background-color:white"><TR><TD></TD></TR></TABLE></TD><TD>';
	 str += '<TABLE ID="nav-2" ALIGN="center" BORDER="0" CELLSPACING="0" CELLPADDING="0" HEIGHT="10" WIDTH="10" style="border:solid 0px white; background-color:white"><TR><TD></TD></TR></TABLE></TD><TD>';
	 str += '<TABLE ID="nav-3" ALIGN="center" BORDER="0" CELLSPACING="0" CELLPADDING="0" HEIGHT="10" WIDTH="10" style="border:solid 0px white; background-color:white"><TR><TD></TD></TR></TABLE></TD><TD>';
	 str += '<TABLE ID="nav-4" ALIGN="center" BORDER="0" CELLSPACING="0" CELLPADDING="0" HEIGHT="10" WIDTH="10" style="border:solid 2px #669999;"><TR><TD></TD></TR></TABLE></TD><TD>';
	 str += '<TABLE ID="nav-5" ALIGN="center" BORDER="0" CELLSPACING="0" CELLPADDING="0" HEIGHT="10" WIDTH="10" style="border:solid 2px #669999;"><TR><TD></TD></TR></TABLE></TD><TD>';
	 str += '<TABLE ID="nav-6" ALIGN="center" BORDER="0" CELLSPACING="0" CELLPADDING="0" HEIGHT="10" WIDTH="10" style="border:solid 2px #669999;"><TR><TD></TD></TR></TABLE></TD><TD>';
	 str += '<TABLE ID="nav-7" ALIGN="center" BORDER="0" CELLSPACING="0" CELLPADDING="0" HEIGHT="10" WIDTH="10" style="border:solid 2px #669999;"><TR><TD></TD></TR></TABLE></TD><TD>';
	 str += '<TABLE ID="nav-8" ALIGN="center" BORDER="0" CELLSPACING="0" CELLPADDING="0" HEIGHT="10" WIDTH="10" style="border:solid 2px #669999;"><TR><TD></TD></TR></TABLE></TD><TD>';
	 str += '<TABLE ID="nav-9" ALIGN="center" BORDER="0" CELLSPACING="0" CELLPADDING="0" HEIGHT="10" WIDTH="10" style="border:solid 2px #669999;"><TR><TD></TD></TR></TABLE></TD><TD><a href="javascript:ScrollArrow(\'right\',\'scroller\',\'item_1\',1);" ><img src="/img/navright.jpg" style="border:0" height="21" alt="right scroll arrow" /></a></TD></TR></TABLE>';
	 str += '</div></td></tr>';
	 str += '';
	 str += '</table>';

	document.write(str);

	autoTimer = setTimeout('AutoScrollIt();', autoDelay1);

}


function AutoScrollIt(){

	ScrollArrow('right','scroller','item_1', 0);

	autoTimer = setTimeout('AutoScrollIt();', autoDelay2);

}

function ScrollSection(nextIndex, scrollArea, offset, bool)
{

	// Store the last section, and update the current section

	if (currentIndex == nextIndex) {
		return;
	}
	lastIndex = currentIndex;
	currentIndex = nextIndex;


	for(var i = 1; i < 4; i++){
		// Get the element we want to scroll, get the position of the element to scroll to

		theScroll[i-1] = document.getElementById(scrollArea+'-'+i);
		position = findElementPos(document.getElementById('item_'+(currentIndex+1)+'-'+i));

		// 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+'-'+i));
			position[0] = position[0] - offsetPos[0];
		}

		if(bool && no_end){
			timer[i-1] = setTimeout('scrollStart('+position[0]+', "horiz", '+(i-1)+', '+lastIndex+', '+currentIndex+');', 0);
		}
		else{
			timer[i-1] = setTimeout('scrollStart('+position[0]+', "horiz", '+(i-1)+', '+lastIndex+', '+currentIndex+');', (i-1)*timerDelay);
		}

		// return false;

	}


}

// Scroll the page using the arrows

function ScrollArrow(direction, scrollArea, offset, clearAuto) {

	if(clearAuto) clearTimeout(autoTimer);

	toolbarNames = new Array('item_1','item_2','item_3');

	var bool = 0;

	// Now iterate through our array of tab names, find matches, and determine where to go.

	for (var i = 0; i < toolbarNames.length; i++) {
		if (i == currentIndex) {
			if (direction == "left") {
				if (i - 1 < 0) {
					nextIndex = toolbarNames.length - 1;
					bool =1;
				} else {
					nextIndex = i - 1;
				}
			} else {
				if ((i + 1) > (toolbarNames.length - 1)) {
					nextIndex = 0;
					bool =1;
				} else {
					nextIndex = i + 1;
				}
			}
		}
	}
	
	// Go to the section name!
		ScrollSection(nextIndex, scrollArea, offset, bool);

	if(clearAuto) autoTimer = setTimeout('AutoScrollIt();', autoDelay1);

}



// Scroll the page using the arrows

function ScrollArrowSingle(direction, scrollArea, offset, clearAuto, level) {

	if(clearAuto) clearTimeout(autoTimer);

	toolbarNames = new Array('item_1','item_2','item_3');

	var bool = 0;

	// Now iterate through our array of tab names, find matches, and determine where to go.

	for (var i = 0; i < toolbarNames.length; i++) {
		if (i == currentIndex) {
			if (direction == "left") {
				if (i - 1 < 0) {
					nextIndex = toolbarNames.length - 1;
					bool =1;
				} else {
					nextIndex = i - 1;
				}
			} else {
				if ((i + 1) > (toolbarNames.length - 1)) {
					nextIndex = 0;
					bool =1;
				} else {
					nextIndex = i + 1;
				}
			}
		}
	}
	
	// Go to the section name!
		ScrollLevel(nextIndex, scrollArea, offset, bool, level);

	if(clearAuto) autoTimer = setTimeout('AutoScrollIt();', autoDelay1);

}


//
// Animated Scroll Functions
// Scrolls are synchronous -- only one at a time.
//

var scrollanim = new Array();

scrollanim[0] = {time:0, begin:0, change:0.0, duration:0.0, element:null, timer:null};
scrollanim[1] = {time:0, begin:0, change:0.0, duration:0.0, element:null, timer:null};
scrollanim[2] = {time:0, begin:0, change:0.0, duration:0.0, element:null, timer:null};

function scrollStart(end, direction, index, startIndex, endIndex)
{
//	alert(index+' '+ startIndex+' '+ endIndex);

    var navBox1 = 'nav-' + ((startIndex*3)+(index+1));
	document.getElementById(navBox1).style.backgroundColor = '';
    document.getElementById(navBox1).style.border = 'solid 2px #669999';

	elem = theScroll[index];
	start = theScroll[index].scrollLeft;



	if (scrollanim[index].timer != null) {
		clearInterval(scrollanim[index].timer);
		scrollanim[index].timer = null;
	}
	scrollanim[index].time = 0;
	scrollanim[index].begin = start;
	scrollanim[index].change = end - start;
	scrollanim[index].duration = 25;
	scrollanim[index].element = elem;
	
	scrollanim[index].timer = setInterval("scrollHorizAnim("+index+","+endIndex+");", timerInterval);
}


function scrollHorizAnim(index, navIndex)
{

	if (scrollanim[index].time > scrollanim[index].duration) {
		clearInterval(scrollanim[index].timer);
		scrollanim[index].timer = null;

		var navBox2 = 'nav-' + ((navIndex*3)+(index+1));
		document.getElementById(navBox2).style.backgroundColor = 'white';
//		document.getElementById(navBox2).style.borderWidth = '0px';
    	document.getElementById(navBox2).style.border = 'solid 0px #669999';
	}
	else {
		move = sineInOut(scrollanim[index].time, scrollanim[index].begin, scrollanim[index].change, scrollanim[index].duration);
		scrollanim[index].element.scrollLeft = move;
		scrollanim[index].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 )

	return Array(elemX, elemY);
}


function sineInOut(t, b, c, d)
{
	return -c/2 * (Math.cos(Math.PI*t/d) - 1) + b;
}
