var memorialStarted=0, pictureCounter=0, nonZeroingPictureCounter=0, totalTimeLeft=15000000;
var myPictureList = [];

$(document).ready(function () {
getPictureArray(myPictureList); //Array is passed by reference, so it is updated.
});



jQuery.fn.fadeIn = function(speed, callback) { 
    return this.animate({opacity: 'show'}, speed, function() { 
        if (jQuery.browser.msie)  
            this.style.removeAttribute('filter');  
        if (jQuery.isFunction(callback)) 
            callback();  
    }); 
};
 
jQuery.fn.fadeOut = function(speed, callback) { 
    return this.animate({opacity: 'hide'}, speed, function() { 
        if (jQuery.browser.msie)  
            this.style.removeAttribute('filter');  
        if (jQuery.isFunction(callback)) 
            callback();  
    });
}; 
 
jQuery.fn.fadeTo = function(speed,to,callback) { 
    return this.animate({opacity: to}, speed, function() { 
        if (to == 1 && jQuery.browser.msie)  
            this.style.removeAttribute('filter');  
        if (jQuery.isFunction(callback)) 
            callback();  
    }); 
}; 

function DetectEnterPressed(e) {
	var characterCode
	if(e && e.which){ // NN4 specific code
		e = e
		characterCode = e.which
	}
	else {
		e = event
		characterCode = e.keyCode // IE specific code
	}
	if (characterCode == 13) enterDogName(); // Enter key is 13
	else return false
}

function startMemorial() {
	
	//Call the memorial immediately to get started.
		getNewPicture();
	//Set the memorial to get a new picture every 5000 milliseconds (5 seconds)
	  $(document).everyTime(5000, "memorial", function() {
	    getNewPicture();
	  });
}

function getNewPicture()
 {
 	if (memorialStarted == 0) {
		memorialStarted = 1;
		$("#donateButton").fadeIn("slow");
		$("#donateButton").css("visibility","visible");
	}
 	 var prevID = $("#lastid").val();
	$("#memorialColCenter").css("visibility","hidden");
	$("#memorialColCenter").empty();
    $("#memorialColCenter").load("getPicture.php?id=" + myPictureList[pictureCounter], function() {
	$("#memorialColCenter").fadeIn("slow");
	$("#memorialColCenter").css("visibility","visible");
	
	$("#memorial_lefttext").empty();
	$("#memorial_lefttext").append(pictureCounter);
	$("#memorial_lefttext").fadeIn("slow");
	
	
	
	
	
	var memorialTimeDays = Math.floor(totalTimeLeft / (60*60*24));
	var remainder = totalTimeLeft % (60*60*24);
	var memorialTimeHours = Math.floor(remainder / (60*60));
	var remainder = remainder % (60*60);
	var memorialTimeMinutes = Math.floor(remainder / (60));
	var remainder = remainder % (60);
	var memorialTimeSeconds = Math.floor(remainder);
	totalTimeLeft = totalTimeLeft - 5;
	$("#memorial_righttext").empty();
	$("#memorial_righttext").append(memorialTimeDays + " Days <br />" + memorialTimeHours + " Hours <br />" + memorialTimeMinutes + " Minutes <br />" + memorialTimeSeconds + " Seconds <br />" + " <br /> until 3 million pictures are displayed.");
	$("#memorial_righttext").fadeIn("slow");
	
	
});

	//SIMPLE COUNTERS
	if (pictureCounter>=myPictureList.length-1)	{pictureCounter=0;}
	else {pictureCounter++;}
	nonZeroingPictureCounter++;
	
}
 
  function enterDogName()
  {
	//var getSex = $("input[@name=sex]:checked").val();
  	var dogName = $('#dogname').val();
  	if (dogName)
	{
		$("#memorialHideText").css("visibility","visible");
		$("#memorialHideText").fadeIn("slow");
		$('.dogNameMemorial').empty();
		$('.dogNameMemorial').append(" " + dogName + " ");
		//$('.dogSexMemorial').empty();
		//$('.dogSexMemorial').append(" " + getSex + " ");
	}
	else {
		$('#memorialErrorDisplay').empty();
		$('#memorialErrorDisplay').append("Please Enter A Name & Sex");
	}
}
 
function getPictureArray()
 {
 	var tempArray = new Array ();
	
 	//The purpose of this function is to ge t an array of all the IDs stored in the database for each picture from a PHP file that is accessing the database.
	$.get("getPictureArray.php", 
  function(data){
	  tempArray = data.split(","); //The IDs are all separated by commas, so populate the array by splitting it along commas.
	  myPictureList=tempArray;
	 
  });
}