function load_doc(document)
{
	var xmlhttp;
	var string = "ajax/";
	string += document;
	
	if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
	}
	else{// code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	xmlhttp.onreadystatechange=function(){
		if (xmlhttp.readyState==4 )//&& xmlhttp.status==200)
		{
			$("#displayDiv").html(xmlhttp.responseText);
		}
	}
	
	xmlhttp.open("GET",string,true);
	xmlhttp.send();
}

/***************** Gallery Controls ***************************************/
//Variables to control the scrolling  and playing functions
var isScrolling = 0;
var isPlaying = 0;
var slideshowID;

function pictureListScrollDown(){
	if(isScrolling) return;
	
	if(Math.abs(parseInt($("#pictureList").css("margin-top"))) < ($("#pictureList").height() - 504)){
		isScrolling = 1;
		$("#pictureList").animate({"margin-top": "-=504px"}, "slow", 
			function(){
				//Animation Complete
				isScrolling = 0;
		});
	}
}

function pictureListScrollUp(){
	if(isScrolling) return;
	
	if(Math.abs(parseInt($("#pictureList").css("margin-top"))) > 0){
		isScrolling = 1;
		$("#pictureList").animate({"margin-top": "+=504px"}, "slow", 
			function(){
				//Animation Complete
				isScrolling = 0;
		});
	}
}

function triggerThumbnailImage(){
	if(isPlaying){
		$('.thumbnailSelected').nextAll('.thumbnailImageDiv').eq(0).trigger('click');
		slideshowID = setTimeout("triggerThumbnailImage()", 3500);
	}
}

function slideshowPlay(){
	
	var thumbnailSelected = 0;
	isPlaying = 1; //activate the slideshow

	$('div.thumbnailImageDiv').each(function(index,element) {
			if($(this).hasClass('thumbnailSelected')){
				thumbnailSelected  = 1;
			}
	});
	
	if(!thumbnailSelected){	//If no thumbnail is selected then we start with the first one
		$('div.thumbnailImageDiv').first().trigger('click');
	}
	
	slideshowID = setTimeout("triggerThumbnailImage()", 3500);
	
}

function slideshowPause(){
	isPlaying = 0;
}


function thumbnailClick(memory){
	
	$('.thumbnailImageDiv').removeClass("thumbnailSelected");
	memory.addClass("thumbnailSelected");
	$('#fullImageTitle').text('');
	
	//image shrinks
	$('#fullImage').fadeOut('slow', function() {
		// Animation complete
		
		$("#fullImage").attr('src',memory.children().attr('src'));
	
		$("#fullImage").css('margin-left', ($("#mainPicture").width()-$("#fullImage").width()+10)/2  + "px");
		
		$("#fullImage").css('margin-top', ($("#mainPicture").height()-$("#fullImage").height()-6)/2 +50 + "px");
		
		if(memory.children().attr('title').search(/(.jpeg|.jpg|.bmp|.png|.gif)/i) == -1){
			$('#fullImageTitle').text(memory.children().attr('title'));
		}
	});
	
	//Animate back
	$('#fullImage').fadeIn('slow', function() {
		//animation complete
	});
}

function fancyboxClosed(){
	isPlaying = 0;
}

function fancyboxStart(){
			
	$.picasa.images('110064573222080587680','5675409833327595073', function(images) {
			var liPicasa;
			
			$.each(images, function(index, element) {
					var divPicasa = $("<div class='thumbnailImageDiv grid_2'></div>");
					//index += 1;
					
					liPicasa = $("<img/>");
					liPicasa.attr('src', element.url);
					liPicasa.attr('title', element.title);
					liPicasa.addClass('thumbnailImage');
					
					if(!(index%3)){
						if(index != 0) $("#pictureList").append('<div class="clear vertical_spacer_20px"></div>');
						divPicasa.addClass('alpha');
					}
					
					else if((index%3) == 2){
						liPicasa.addClass('omega');
					}
					
					divPicasa.click(function(){
						thumbnailClick($(this));
							
					});
					
					divPicasa.append(liPicasa);
					$("#pictureList").append(divPicasa);
			});
	});	
	
	/* Using custom settings */
	
	$("a#inline").fancybox({
		'hideOnContentClick'	: 	false,
		'showCloseButton'	: 	false,
		'transitionIn'		:	'elastic',
		'transitionOut'		:	'elastic',
		'speedIn'			:	500, 
		'speedOut'			:	500,
		'onClosed'			:	function() {
								isPlaying = 0;
							}
	});
	$("a#inline_1").fancybox({
		'hideOnContentClick'	: 	false,
		'showCloseButton'	: 	false,
		'transitionIn'		:	'elastic',
		'transitionOut'		:	'elastic',
		'speedIn'			:	500, 
		'speedOut'			:	500
	});
	$("a#inline_2").fancybox({
		'hideOnContentClick'	: 	false,
		'showCloseButton'	: 	false,
		'transitionIn'		:	'elastic',
		'transitionOut'		:	'elastic',
		'speedIn'			:	500, 
		'speedOut'			:	500
	});
	/*$("a#inline_3").fancybox({
		'hideOnContentClick'	: 	false,
		'showCloseButton'	: 	false,
		'transitionIn'		:	'elastic',
		'transitionOut'		:	'elastic',
		'speedIn'			:	500, 
		'speedOut'			:	500
	});*/
	$("a#inline_4").fancybox({
		'hideOnContentClick'	: 	false,
		'showCloseButton'	: 	false,
		'transitionIn'		:	'elastic',
		'transitionOut'		:	'elastic',
		'speedIn'			:	500, 
		'speedOut'			:	500
	});
	$("a#inline_5").fancybox({
		'hideOnContentClick'	: 	false,
		'showCloseButton'	: 	false,
		'transitionIn'		:	'elastic',
		'transitionOut'		:	'elastic',
		'speedIn'			:	500, 
		'speedOut'			:	500
	});

	/* Apply fancybox to multiple items */
	
	$("a.group").fancybox({
		'transitionIn'	:	'elastic',
		'transitionOut'	:	'elastic',
		'speedIn'		:	600, 
		'speedOut'		:	600, 
		'overlayShow'	:	false
	});
};


function cloudStart(){
	var 	new_cloud = $('<img class="floating_cloud"/>');
	var 	negative = 1;
	var	random;
	
	new_cloud.attr('src', 'images/cnc/cloud' + (Math.floor(Math.random()*6) +1) + 'b.png');
	
	//Set the margin top randomly
	new_cloud.offset({ top: Math.random()*250+5 });
	
	//Change the width and the height
	if(Math.random()>0.5)	negative = -1;
	random = Math.random();
	new_cloud.height(65*(1-(0.3*random)*negative));
	new_cloud.width(130*(1-(0.3*random)*negative));
	
	$('#body_div').append(new_cloud);
	
	//Set the animations
	new_cloud.animate({
		left: '-10%',
		}, 50000*(Math.random()+1), 'linear', function() {
		// Animation complete.
		new_cloud.remove();
	});
	
	setTimeout('cloudStart()', 10000*(Math.random()+0.1));

}

var number_of_menuTab;
var index_menuTab = 0;

function continuousFadeIn(){
	if(index_menuTab >= number_of_menuTab) return;
	$('.menuTab').not('.menuTabAppear').first().fadeIn('slow', function() {
			// Animation complete
			$('.menuTab').not('.menuTabAppear').first().addClass('menuTabAppear');		
			continuousFadeIn();
        });
}

function crossStart(){
	isQuoting = 1;
	var left = $(window).width()*0.6;
	var left_text = left - 50 +"px";
	$('#cross').css('margin-left', left_text);
	
	
	$('#cross').animate({
		top: $(window).height()*0.503,
		}, 2000, function() {
		// Animation complete.
		isQuoting = 0;
		//Start the fade in of the selection boxes
		continuousFadeIn();
	});
	
	$('#quotes_from_god').css('margin-left', left+60);
	$('#quotes_from_god').css('top', $(window).height()*0.503-140);
	
}

function toggleVolume(){
	
	if($('#volume').attr('value')==1){
		//alert($('#volume').attr('value'));
		$('#volume').attr('src','images/icons/volume_down_24.png');
		$('#volume').css('background-color','cornsilk');
		$('#volume').attr('value','0');
	}
	else{
		$('#volume').attr('src','images/icons/volume_up_24.png');
		$('#volume').css('background-color','white');	
		$('#volume').attr('value','1');
	}
}
function updateAboutUs(){
}




//JS for the calendars

//Variables for the calendars
var weekday=new Array(7);
weekday[0]="Sunday";
weekday[1]="Monday";
weekday[2]="Tuesday";
weekday[3]="Wednesday";
weekday[4]="Thursday";
weekday[5]="Friday";
weekday[6]="Saturday";

//Functions for the calendars

function calendarSingleEvent(selected){
	$('.calendarSingleEventSelected').removeClass('calendarSingleEventSelected');
	selected.addClass('calendarSingleEventSelected');
	
	var event 	= 	selected.val().substring(11);
	var date	= 	selected.val().substring(0,10);
	
	$('#calendarViewBoxTitle').html(event);
	
	
	//Check if the event is a kairos saturday event
	if(event.match('Kairos Saturday') || event.match('kairos Saturday') || event.match('Kairos saturday') || event.match('kairos saturday')){
		//If it matches that we will not reveal it unless the date is over
		
		//Get the current date 
		var currentDate = new Date();
		
		//Compare to the event date
		var eventDate = new Date();
		eventDate.setFullYear(20+selected.val().substring(8,10), selected.val().substring(0,2)-1, selected.val().substring(3,5));
		
		//alert(date);
		//alert(currentDate);
		//alert(eventDate);
		
		//If the date is less than the eventdate, then we will not show the information
		if(currentDate < eventDate){
			$('#calendarViewBox').html("A full lesson plan for this cell group session will be released on this page once the date has passed.");
			return;
		}
		
	}
	
	//Get the information via ajax from the database and update the preview
	$.post("/index.php/admin_content/get_calendar_one_content", {selected_event_name: event, selected_date: date},
		function(data) {
			$('#calendarViewBox').html(data.event_content);
		},
		"json"
	);
}

function previewCalendar(){
	updateCalendar();
	setTimeout("$('#inline_2').triggerHandler('click')",100);
}

function updateCalendar(){
	//This function will update the calendar view using ajax before it is sent to the user
	//Get the list of events from the database
		$.post("/index.php/admin_content/get_calendar_all_content", 
		function(data) {
			var counter = 1;
			var array_data = data.string.split("#");
			
			//Clear the tags
			for(var i = 1; i<10; i++){
				var tag = "#calendar0"+i+"Tag";
				$(tag).html('');
			}
			for(var i = 10; i<13; i++){
				var tag = "#calendar"+i+"Tag";
				$(tag).html('');
			}
			
			//Update the tags with the fields
			while(counter <array_data.length){
				var month = array_data[counter].substring(0,2);
				var day = array_data[counter].substring(3,5);
				var year = array_data[counter].substring(8,10);
				var date = new Date();
				date.setFullYear(20+year,month-1,day);
				var writtenDate = day + '/' + month + '/' + year;
				var event = array_data[counter].substring(11);
				var tagDetails = weekday[date.getDay()];
				
				var new_event = $('<p class="calendarSingleEvent" onclick="calendarSingleEvent($(this));"></p>').html(event);
				new_event.val(array_data[counter]);
				
				//$('#calendar'+month+'Tag').append(new_event);
				//Find if there is already a tag for the date of the event
				if(!($('div[value='+ "'" + writtenDate + "'" + ']').length)){
			
					//Create a new div with the date
					var newDateTag = $('<div class="dateTags"></div>').html(writtenDate + ": " + tagDetails);
					newDateTag.attr('value',writtenDate);
					$('#calendar'+month+'Tag').append(newDateTag);
					$('#calendar'+month+'Tag').append($('<div class="vertical_spacer_20px"></div>'));
					
				}
				
				//Add to the event to this div
				$('div[value='+ "'" + writtenDate + "'" + ']').append(new_event);
				
				counter++;
			}
		},
		"json");
}




// Quoting when you click the cross
var isQuoting = 0;
var quotes_from_the_bible = new Array();
quotes_from_the_bible[0] = "Love must be sincere. Hate what is evil; cling to what is good.";
quotes_from_the_bible[1] = "And now these three remain: faith, hope and love. But the greatest of these is love.";
quotes_from_the_bible[2] = "Be completely humble and gentle; be patient, bearing with one another in love.";
quotes_from_the_bible[3] = "Above all, love each other deeply, because love covers over a multitude of sins.";
quotes_from_the_bible[4] = "Greater love has no one than this, that he lay down his life for his friends.";
quotes_from_the_bible[5] = "Hatred stirs up dissension, but love covers over all wrongs.";
quotes_from_the_bible[6] = "A friend loves at all times, and a brother is born for adversity";
quotes_from_the_bible[7] = "Two are better than one.";
quotes_from_the_bible[8] = "There is no fear in love; but perfect love casts out fear";
quotes_from_the_bible[9] = "Love bears all things, believes all things, hopes all things, endures all things. Love never ends.";




/**************************** On ready jquery *******************************************/
jQuery(document).ready(function($) {		
		
	fancyboxStart();
	
	//cloudStart();
	
	crossStart();
	
	$(window).resize(function() {
		var left = $(window).width()*0.6 -50 + "px"
		var top = $(window).height()*0.503 + "px";
		$('#cross').css('margin-left', left);
		$('#cross').css('top', top);
		
		$('#quotes_from_god').css('margin-left', $(window).width()*0.6+60);
		$('#quotes_from_god').css('top', $(window).height()*0.503-140);
	});
	
	$('#cross').bind('click', function() {
		if(!isQuoting){
			isQuoting = 1;
			$('#quotes_from_god').text(quotes_from_the_bible[Math.floor(Math.random()*10)]).fadeIn('slow').delay(3000).fadeOut('slow', function(){
					//animation complete
					isQuoting = 0;
			});
		}
	});
	
	$('#calendarSelectionMenuAccordion').accordion({
		autoHeight: false,
		collapsible: true,
		active: false
	});
	
});

