// JavaScript Document

// Example:

// simplePreload( '01.gif', '02.gif' ); 

function simplePreload()
{ 
  var args = simplePreload.arguments;

  document.imageArray = new Array(args.length);

  for(var i=0; i<args.length; i++)
  {

    document.imageArray[i] = new Image;

    document.imageArray[i].src = args[i];

  }

}





// Script to rotate images *******************************************************************
// To add images add the filename to the adImages array
adImages = new Array("images/IMG_1411.jpg","images/IMG_1393.jpg","images/IMG_0743.jpg","images/IMG_0750.jpg","images/IMG_0744.jpg","images/IMG_0297.jpg","images/JuniorandRNLI060.jpg","images/IMG_0196.jpg","images/Troutmasters20098.JPG","images/harpsopen20086.jpg","images/Alistair_Ross_2008_4.jpg","images/IMG_0018.jpg","images/harps_open20071.jpg","images/winter_league1.jpg","images/rnli20081.jpg","images/rnli200814.jpg","images/Casting_demo_2008_9.jpg","images/Casting_demo_2008_1.jpg","images/Report_March2009.jpg","images/Report_March20091.jpg","images/IP1.JPG","images/IP2.JPG","images/al_ross_20093.JPG")

compImages = new Array("images/IMG_0003.jpg","images/IMG_0006.jpg","images/IMG_0007.jpg","images/IMG_0008.jpg","images/IMG_0009.jpg","images/IMG_0013.jpg","images/IMG_0016.jpg","images/IMG_0018.jpg","images/IMG_0019.jpg","images/IMG_0022.jpg","images/IMG_0023.jpg","images/IMG_0027.jpg","images/IMG_0030.jpg","images/IMG_0031.jpg","images/IMG_0032.jpg")
//********************************************************************************************

// function takes one argument - the name of the array to be used to store the images.
imgCt = adImages.length
	firstTime = true
	function rotate(){
	
	//alert(imgArr.length)
	
	if (document.images) {
		if(firstTime) {
			thisAd = Math.floor((Math.random() * imgCt))
			firstTime = false
			}
			else{
				thisAd++
					if(thisAd == imgCt) {
					thisAd = 0
					}
				}
				document.myPicture.src=adImages[thisAd]
				setTimeout("rotate()",Math.floor((Math.random() * 13)) * 1000)
				}
			}
			
			
			imgCt2 = compImages.length
	firstTime2 = true
			function rotate2(){
	
	//alert(imgArr.length)
	
	if (document.images) {
		if(firstTime2) {
			thisAd2 = Math.floor((Math.random() * imgCt2))
			firstTime2 = false
			}
			else{
				thisAd2++
					if(thisAd2 == imgCt2) {
					thisAd2 = 0
					}
				}
				document.compPicture.src=compImages[thisAd2]
				setTimeout("rotate2()",Math.floor((Math.random() * 13)) * 1000)
				}
			}
//End rotate images code***********************************************************			
			
//******************************************************************************************			
// Script to give date for countdown to next competitiion or days since last competition

	//initialise a new date to now
	now = new Date()
	today = new Date(now.getFullYear(),now.getMonth(),now.getDate())
	
	
	
	isBefore = true
	
	thisYr = now.getYear()
	if (thisYr < 1900) {
		thisYr = thisYr+1900
	}
	
	//enter the date of the competition here and remember to subtract one from the month number
	//i.e. april 1 2007 is (2007,3,1)
	//******************************************************************************************
	compDate = new Date(2010,5,12)
	//******************************************************************************************
	
	//set isBefore to detwrmine if the comp date is in the future or past
	if ((compDate.getTime() < today.getTime())) {
		isBefore = false
	}

	function dayToDays(inTime) {
		return (inTime.getTime() / (1000 * 60 * 60 * 24))
	}

	//function to work out the number of days between the parameter and the day now
	function daysTill(inDate) {
		if(isBefore)
		{
		return (Math.ceil(dayToDays(inDate) - dayToDays(today)))
		}
		else
		{
		return (Math.ceil(dayToDays(today) - dayToDays(inDate)))
		}
	}
	
	//function to generate content for the writeContent function which is called in the body tag of 
	//the index page. 
	function makeContent()
	{
	var theContent
	if ((compDate.getDate() == now.getDate()) && (compDate.getMonth() == now.getMonth())) 
	{
	theContent = 'There is a competition taking place today'	
	}
	else
	{
	if(isBefore)
	{
	if(daysTill(compDate) == 1)
	{
	theContent = 'Next Competition is Tomorrow'
	}
	else
	{
	theContent = 'Next Competition is in '+ daysTill(compDate) + ' days.'
	}
	}
	else
	{
	if (daysTill(compDate) == 1)
	{
	theContent = 'Last Competition was Yesterday'
	}
	else
	{
	theContent = 'Last competition was ' + daysTill(compDate) + ' days ago.'
	}
	}
	}
	return theContent
	}
	
		
			
//code to display the competition information using node manipulation
//function writes whatever content is passed into it into the pageDiv Div tag on the page
function writeDays(content,id){
	
		var newHead = document.createElement("h3")
		var newContent = newHead.appendChild(document.createTextNode(content))
		var pageDiv = document.getElementById(id)
		pageDiv.appendChild(newContent)
		}
		
//****************End comp Date code ******************************************************

