var currentDate = null;
var today = new Date();

var lastDate = null;  // For pagination

$(document).ajaxError(function(){
    if (window.console && window.console.error) {
        console.error(arguments);
    }
});


$(document).ready(function(){

	// Read current date from page
	y = Date.parse($('#Devo-Date').text());
	// Set current date 
	currentDate = new Date();
	currentDate.setTime = y;


});

function changeDevo(direction){

var audioCutoffDate = new Date();
audioCutoffDate.setFullYear(2009,9,31); // 9 is October (zero-based array)


if (direction != ''){

//$('#spinner').show();

$('#spinner').css('visibility','visible');

// Create date objects
prevDate = new Date();

// Copy current over to previous
prevDate = currentDate;
// Adjust day
if(direction == 'up'){
	
	// Don't go higher then today

	if (currentDate >= today){
		$('#spinner').css('visibility','hidden');
		return;
	}
	
	prevDate.setDate(currentDate.getDate()+1);	

}else{


	prevDate.setDate(currentDate.getDate()-1);


}

}else
{
prevDate = new Date();	
}

var dataString = 'action=GetDevotional&keyword='+ prevDate.toDateString();
$.ajax({
      type: "GET",
      url: "/ajax/ajax.cfm",
      data: dataString,
	  dataType: "xml",
      success: function(result) {

	devotitle = $(result).find("title").text();
	devodate = $(result).find("date").text();
	devoverse = $(result).find("verse").text();	
	devotext = $(result).find("text").text();
	devogroup = $(result).find("group").text();
	month = $(result).find("month").text();
    day = $(result).find("day").text();
    year = $(result).find("year").text();
	
	// Correct day
	twodigitday = day;
	if (day.length == 1){
		twodigitday = '0' + day;
	}
	
	// Correct month
	twodigitmonth = month;
	if (month.length == 1){
		twodigitmonth = '0' + month;
	}
	if (devotitle !=''){

		//Update persistant object
		currentDate = prevDate;
		
		$('#Devo').fadeTo('fast', 0, function(){
	
		$('#Devo-Date').html(devodate);
		$('#Devo-Title').html(devotitle);
		$('#Devo-Verse').html(devoverse);
		$('#Devo-Text').html(devotext);
		$('#Devo-Discuss').html(devogroup);	
		$('#Devo').fadeTo('fast',1);
		$('#spinner').css('visibility','hidden');

		// Show only if after cutoff date
		if (audioCutoffDate < currentDate){
			$('#DOTG').show();
			CreateFlowPlayerMP3('otg'+year+twodigitmonth+twodigitday,'devo');
		}else{
			$('#DOTG').hide();	
		}
		
		
		  });
	}else{
				$('#Devo-Date').html('');
				$('#Devo-Title').html('');
				$('#Devo-Verse').html('');
				$('#Devo-Text').html('We apologize, but this devotional is not available at this time.<br /><br /> Please try clicking the "PREVIOUS" button.');
				$('#Devo-Discuss').html('');	
				$('#spinner').css('visibility','hidden');
		}
	
	
	}

	  
	   });

}

function getDevo(keyword){

var audioCutoffDate = new Date();
audioCutoffDate.setFullYear(2009,9,31); // 9 is October (zero-based array)


//$('#spinner').show();
$('#spinner').css('visibility','visible');

var dataString = 'action=GetDevotional&keyword='+ keyword;
$.ajax({
      type: "GET",
      url: "/ajax/ajax.cfm",
      data: dataString,
	  dataType: "xml",
      success: function(result) {

	devotitle = $(result).find("title").text();
	devodate = $(result).find("date").text();
	devoverse = $(result).find("verse").text();	
	devotext = $(result).find("text").text();
	devogroup = $(result).find("group").text();
	month = $(result).find("month").text();
    day = $(result).find("day").text();
    year = $(result).find("year").text();
	
	// Correct day
	twodigitday = day;
	if (day.length == 1){
		twodigitday = '0' + day;
	}
	
	// Correct month
	twodigitmonth = month;
	if (month.length == 1){
		twodigitmonth = '0' + month;
	}
	
	
		if (devotitle !=''){
	
			//Update persistant object
			currentDate.setMonth(month-1);
			currentDate.setDate(day);
			currentDate.setFullYear(year);
	
			
			$('#Devo').fadeTo('fast', 0, function(){
		
			$('#Devo-Date').html(devodate);
			$('#Devo-Title').html(devotitle);
			$('#Devo-Verse').html(devoverse);
			$('#Devo-Text').html(devotext);
			$('#Devo-Discuss').html(devogroup);	
			$('#Devo').fadeTo('fast',1);
			$('#spinner').css('visibility','hidden');
			
			// Show only if after cutoff date
			if (audioCutoffDate < currentDate){
				$('#DOTG').show();
				CreateFlowPlayerMP3('otg'+year+twodigitmonth+twodigitday,'devo');
			}else{
				$('#DOTG').hide();	
			}
			
			  });
		}else{
				$('#Devo-Date').html('');
				$('#Devo-Title').html('');
				$('#Devo-Verse').html('');
				$('#Devo-Text').html('We apologize, but this devotional is not available at this time.<br /><br /> Please try clicking the "PREVIOUS" button.');
				$('#Devo-Discuss').html('');	
				$('#spinner').css('visibility','hidden');
		}
	 

	}

	
	   });


}




/*
function getDevoTitles(keyword){

var dataString = 'action=GetDevotionalTitles&keyword='+ keyword;

$('#spinner').css('visibility','visible');

$.ajax({
      type: "GET",
      url: "/ajax/ajax.cfm",
      data: dataString,
	  dataType: "xml",
      success: function(result) {getDevoTitlesHandler(result,keyword);},
	  timeout: 1000,
	  error: function(XMLHttpRequest, textStatus, errorThrown){alert("Error code: " + errorThrown + "\nStatus: " + textStatus);}
	 
	   });

}

function getDevoTitlesHandler(result,keyword){
$('devo',result).each(function(i){
	devotitle = $(this).find("title").text();
	devodate =  $(this).find("date").text();
	lastDate = $(this).find("linkdate").text()
	$('#devoList').append('<a href="dailydevotion.cfm?keyword=' + lastDate + '">' + devodate + '</a> ' + devotitle + '<br />');
	
});


$('#devoMore').html('<a href="javascript:getMoreDevoTitles(\'' + lastDate + '\',\'' + keyword + '\')">Load More</a>');

$('#spinner').css('visibility','hidden');
}
*/

function getMoreDevoTitles(lastDate,keyword){

$('#spinner').css('visibility','visible');
//keyword = (typeof keyword == 'undefined') ? '' : keyword;
//lastDate = (typeof lastDate == 'undefined') ? '' : lastDate;

var dataString = 'action=GetMoreDevotionalTitles&keyword='+ keyword + '&lastDate=' + lastDate;

$.ajax({
      type: "GET",
      url: "/ajax/ajax.cfm",
      data: dataString,
	  dataType: "xml",
      success: function(result) {
			
			$('devo',result).each(function(i){
					   
				devotitle = $(this).find("title").text();
				devodate =  $(this).find("date").text();
				lastDate = $(this).find("linkdate").text()
				$('#devoList').append('<a href="dailydevotion.cfm?keyword=' + lastDate + '">' + devodate + '</a> ' + devotitle + '<br />');
	
			});


			$('#devoMore').html('<a href="javascript:getMoreDevoTitles(\'' + lastDate + '\',\'' + keyword + '\')"><img src="/images/LoadMore.jpg" width="135" height="40" alt="Load More" /></a>');

	$('#spinner').css('visibility','hidden');
	}


	   });

}
