/*
	/00000000000                        /0   0\      000       000
	00000000000/            /00000000  /000 000\    0   0      000
		0000     /00000000  000        000000000   00   00   /000000/
		0000     000   000  000000     000 0 000  000000000    000
		0000     000   000  000        000   000  000   000    000
		000/     00000000/  \00000000  000   000  000   000    000
		
		Page & code by Thomas Renck  http://X-INfERNO.com  © 2010
*/

/* --------------------------------------------	*/
/* JQuery Events								*/
/*												*/
/* --------------------------------------------	*/

//Make sure the page is ready first 
$(document).ready(pageInit);
$(window).unload(pageUnload);
//global
var time_to_sermon = -1

function pageUnload()
{
	//Holder to make sure page ready fires on browser back.
}

function pageInit()
{
	/* //jQuery event setups
	$("a.email").click(function(){
		return false;
	});
	
	$('#dialog').dialog({
        autoOpen: true,
        height: 200,
        width: 200,
        resizable: false,
        modal: true,
        title: 'Check this out'
    });
	 */
	
	//Drop downs
	$(".nav > ul > li").mouseover( function() {
		$("div", this).stop(1,0);
		$("div", this).slideDown(400, function(){ $(this).css({height:'auto'})}); 
	});
	$(".nav > ul > li").mouseout( function() {
		$("div", this).stop(1,0);
		$("div", this).slideUp(400, function(){ $(this).css({height:'auto'})}); 
	});
	
	//News and Twitter buttons change scrollbox content
	$("#twitter_list").hide();
	
	$("#news_button").click( function() {
		this.className="active";
		document.getElementById('twitter_button').className="";
		$("#twitter_list").hide(500);
		$("#news_list").show(500);
		scrollBoxReset();
		return false;
	});
	$("#twitter_button").click( function() {
		this.className="active";
		document.getElementById('news_button').className="";
		$("#news_list").hide(500);
		$("#twitter_list").show(500);
		scrollBoxReset();
		return false;
	});
	
	//Staff thumbnail picker
	$(".staff_thumbs .thumb .img_tmb").mouseover( function() {
		if(whitespaceDone) $(this.parentNode.childNodes[1]).show(150);
	});
	$(".staff_thumbs .thumb .img_tmb").mouseout( function() {
		$(this.parentNode.childNodes[1]).hide(150);
	});
	$(".staff_thumbs .thumb .img_tmb").click( function() {
		if(whitespaceDone) {
			//Hide the popup;
			$(this.parentNode.childNodes[1]).hide(400);
			//Turn off all other active thumbs
			$('.thumb_active').removeClass('thumb_active');
			//Activate this thumb
			$(this.parentNode).addClass('thumb_active');
			//Fill the info with this persons
			$('.staff_bio .heading').html( $(this.parentNode.childNodes[1]).html() );
			$('.staff_rotator').html( $(this.parentNode.childNodes[2]).html() );
			$('.staff_bio .content #scroll_inner').html( $(this.parentNode.childNodes[3]).html() );
			//Reset the scrollbar to top
			scrollBoxReset();
		}
	});

	//Current Series popup
	$(".current_series").mouseover( function() {
		$("#info_popup").fadeIn(100);
	});
	$(".current_series").mousemove( function(evt) {
		//create the top and left numbers for the popup
		var mouseXY = toeMouseXY(evt);
		var iTop = mouseXY['y'] - 5;
		var iLeft = mouseXY['x'] + 30;

		var props = {};
		props['top'] = iTop + 'px';
		props['left'] =  iLeft + 'px';
		$("#info_popup").css(props);
	});
	$(".current_series").mouseout( function() {
		$("#info_popup").fadeOut(100);
	});
	
	//Media table highlight
	$("#media_table td").mouseover( function() {
		//$(this.parentNode).css({backgroundColor:'rgba(128,128,128,0.1)'});
		$(this.parentNode).addClass('active');
	});
	$("#media_table td").mouseout( function() {
		//$(this.parentNode).css({background:'0'});
		$(this.parentNode).removeClass('active');
	});
	
	//Frontpage slideshow
	$('.slideshow').cycle({
		fx: 'fade', 
		speed:1000,
		/*easing: 'backinout',*/
		timeout: 6000,
		pause: 1,
		next: '#nav_next',
		prev: '#nav_prev'
	});
	$('.slideshow_box').mouseover( function() {
		$('.slideshow_box .nav_prev, .slideshow_box .nav_next').stop(1,0);
		$('.slideshow_box .nav_prev, .slideshow_box .nav_next').animate({opacity:1.0},400);
	});
	$('.slideshow_box').mouseout( function() {
		$('.slideshow_box .nav_prev, .slideshow_box .nav_next').stop(1,0);
		$('.slideshow_box .nav_prev, .slideshow_box .nav_next').animate({opacity:0.0},800);
	});
	
	//TEMP STYLE CHOOSER
	$("#style_button_1").click( function() {
		$("#style_link").attr('href','includes/lightstyle.css');
		return false;
	});
	$("#style_button_2").click( function() {
		$("#style_link").attr('href','includes/darkstyle.css');
		return false;
	});
	$("#logo_button_1").click( function() {
		$(".logo").css({background: 'url(sc_images/sandals_church_logo_lt.png) no-repeat'});
		return false;
	});
	$("#logo_button_2").click( function() {
		$(".logo").css({background: 'url(sc_images/sandals_church_logo2_lt.png) no-repeat'});
		return false;
	});
	$("#logo_button_3").click( function() {
		$(".logo").css({background: 'url(sc_images/sandals_church_logo_dk.png) no-repeat'});
		return false;
	});
	$("#logo_button_4").click( function() {
		$(".logo").css({background: 'url(sc_images/sandals_church_logo2_dk.png) no-repeat'});
		return false;
	});
	
	
	if(time_to_sermon != -1)
	{
		countDown(time_to_sermon);	
		$("#popdown").slideDown(1000);
	}
}

/* --------------------------------------------	*/
/* Other Javascript Functions					*/
/*												*/
/* --------------------------------------------	*/ 

//Global variables
var dragObject = null;
var mouseY = null;
var mouseOvershoot = 0;
var whitespaceDone = 0;

//Live stream countdown function
function countDown(time_till)
{	
	//Are we done right now?
	if(time_till==0)
	{
		document.getElementById("pd_header").innerHTML="We're streaming live right now!";
		return 0;
	}
 	var dat = new Date();
	dat.setTime(time_till*1000);
	
	var hours = dat.getUTCHours();
	var mins =  dat.getUTCMinutes();
	var secs =  dat.getUTCSeconds();
	
	//Add leading zeros
	if(hours<10) hours = "0" + hours;
	if(mins<10) mins = "0" + mins;
	if(secs<10) secs = "0" + secs;
	
	var count_down = "00:" + hours + ":" + mins + ":" + secs +"!"; 
	
	var elem = document.getElementById("ls_countdown");
	if(elem != null && elem != 'null')
		elem.innerHTML = count_down;
	
	time_till = time_till-1;
	
	setTimeout(function(){ countDown(time_till) }, 1000); 
	return 0;
}

//Whitespace cleaner for node compliance!!
function cleanNodes() { cleanWhitespace(document.getElementById('main')); whitespaceDone = 1; }
var notWhitespace = /\S/;
function cleanWhitespace(node) /*by Kor*/
{
	for (var x = 0; x < node.childNodes.length; x++) 
	{
		var childNode = node.childNodes[x]
		if ((childNode.nodeType == 3)&&(!notWhitespace.test(childNode.nodeValue))) 
		{
			// that is, if it's a whitespace text node
			node.removeChild(node.childNodes[x])
			x--
		}
		if (childNode.nodeType == 1) 
		{
			// elements can have text child nodes of their own
			cleanWhitespace(childNode)
		}
	}
}

//Setup for mouse events
document.onmousemove = mouseMove;
document.onmouseup = (function() {
	if(dragObject) 
	{
		dragObject.className="scrollkey"; 
		dragObject=null; 
		mouseOvershoot=0; 
	}
});

/*--- Setup for on page load ---*/
function pageLoad()
{
	
}

/*--- Make the current element the drag object ---*/
function scroll(elem)
{
	//debugBox = document.getElementById('debug');
	//debugBox.innerHTML = Math.random();
	
	if(!elem.style.top) { elem.style.top="0px"; }
	dragObject = elem;
	
	dragObject.className="scrollkey_active";
	
	//Deny selections
	document.body.focus();
	return false;
}

/*--- Mouse Move event handler for dragging a scrollkey ---*/ 
function mouseMove(evt)
{
	debugBox = document.getElementById('debug');
	//debugBox.innerHTML = mouseOvershoot;
	
	if(dragObject && mouseY)
	{
		//Calculate mouseDeltaY
		var mouseDeltaY = toeMouseXY(evt)['y'] - mouseY;
		var newBoxTop = parseInt(dragObject.style.top) + mouseDeltaY;
		
		//Get elements we need
		var contentElem = dragObject.parentNode.parentNode.childNodes[2];
		var contentHeight = contentElem.childNodes[0].offsetHeight;
		
		//alert(contentElem.className);
		
		//Get the element sizes
		var contentBoxHeight = contentElem.offsetHeight;
		var keyHeight = dragObject.offsetHeight;
		var scrollbarHeight = dragObject.parentNode.offsetHeight;
		var maxBoxTop = scrollbarHeight - keyHeight;
		
		if(newBoxTop<=0 || mouseOvershoot<0)	//Don't get too close to the sun
		{
			mouseOvershoot+=mouseDeltaY;
			if(mouseDeltaY>0 && mouseOvershoot>0) { mouseOvershoot=0; }
			newBoxTop=0;
		}
		else if(newBoxTop>=maxBoxTop || mouseOvershoot>0)		//Don't dig too deep
		{
			mouseOvershoot+= mouseDeltaY;
			if(mouseDeltaY<0 && mouseOvershoot<0) { mouseOvershoot=0; }
			newBoxTop=maxBoxTop;
		}
		else
			mouseOvershoot=0;
		
		//Move the scroll key
		dragObject.style.top=newBoxTop + "px";
		
		//Scroll the actual box
		contentElem.scrollTop = (newBoxTop*(contentHeight-contentBoxHeight))/maxBoxTop;
		
		//Deselect the text in IE
		if($.browser.msie)
			document.selection.empty();
		}
		
	mouseY = toeMouseXY(evt)['y'];
}
/*--- Scrollbox reset (set to top) ---*/
function scrollBoxReset()
{
	//Get the elements
	key = document.getElementById('scrollkey');
	contentElem = key.parentNode.parentNode.childNodes[2];
	//Okay now set them to top.
	key.style.top=0;
	contentElem.scrollTop = 0;
}

/*--- HERE's the HTML for the scroll box ---//

<div class="[scroll_box]">
	<div class="[heading]">
		<!--Box Heading Text-->
	</div>
	<div class="[scrollbar]">
		<div id="scrollkey" class="[scrollkey]" onMouseDown="return scroll(this);"></div>
	</div>
	<div class="[content]">
		<div id="[scroll_inner]">
			<!--START CONTENT! -->

			<!--END CONTENT! -->
		</div>
	</div>
</div>

-------------------------------------------*/









