/*--------------------------------------------------------------------------------------------- mb_rotate_linked_images.js 
	
	Created by Matthew Brewer on January 20, 2007
	http://www.macfanatic.net
	matt@macfanatic.net
	
	You may use this software for any purpose, but there isn't any warranty provided and I'm
	not responsible for anything that it could do to you or your loved ones.
	
	That being said, this is a simple script that swaps images in and out on my website.  I
	have setup three objects to represent all of the images and the links those images point 
	to, so as they are swapped out on the page, the links always match up to the images.
	
	There is code available online similar to do this, but I wanted to tackle some javascript
	for myself and this is my solution.  If you have any questions you can email me.
	
---------------------------------------------------------------------------------------------------------------------------------------*/


// Define my custom object
function LinkedPictures() {
	this.pics = new Array();
	this.links = new Array();
	this.Index = 0;
}


// Setup the objects that will hold arrays and links for each placeholder in document
var left_pics = new LinkedPictures();
var middle_pics = new LinkedPictures();
var right_pics = new LinkedPictures();
var speed = 9000;	// Regular interval to swap the images out with
var target;		// Will just be used as a pointer


function LoadImages(pathToImages) {
	
	/* Setup the images in the left holder */
	left_pics.pics[0] = new Image();
	left_pics.pics[0].src =  pathToImages + "left/Healthy-Mac.jpg";
	left_pics.links[0] = "http://www.macfanatic.net/blog/2006/07/29/keeping-your-mac-healthy/";
	
	left_pics.pics[1] = new Image();
	left_pics.pics[1].src = pathToImages + "left/Disco-Interview.jpg";
	left_pics.links[1] = "http://macfanatic.net/blog/2006/11/06/interview-with-disco-developer-austin-sarner/";
	
	left_pics.pics[2] = new Image();
	left_pics.pics[2].src = pathToImages + "left/Viewer-2.1-Released.jpg";
	left_pics.links[2] = "http://www.macfanatic.net/blog/viewer/";

	//left_pics.pics[3] = new Image();
	//left_pics.pics[3].src = pathToImages + "left/MC-iPhone-ObjC2.0-Article.jpg";
	//left_pics.links[3] = "http://www.maccompanion.com/documents/freeissues/2007/February2007.pdf";
	
	
	/* Setup images in middle */
	middle_pics.pics[0] = new Image();
	middle_pics.pics[0].src = pathToImages + "middle/Billings-2-Review.jpg";
	middle_pics.links[0] = "http://www.macfanatic.net/blog/2007/02/03/billings-2-review/";
	
	middle_pics.pics[1] = new Image();
	middle_pics.pics[1].src = pathToImages + "middle/macComanion-XCode-Article.jpg";
	middle_pics.links[1] = "http://www.maccompanion.com/archives/December2006/Columns/GeekSpeak.htm";
	
	middle_pics.pics[2] = new Image();
	middle_pics.pics[2].src = pathToImages + "middle/Hardware-Roundup.jpg";
	middle_pics.links[2] = "http://www.macfanatic.net/blog/2007/01/21/hardware-roundup-ifire-lanyard-headphones-and-casio-exilim-camera/";
	
	middle_pics.pics[3] = new Image();
	middle_pics.pics[3].src = pathToImages + "middle/Macworld-2007.jpg";
	middle_pics.links[3] = "http://www.macfanatic.net/blog/2007/01/13/macworld-2007-podcast/";
	
	
	/* Setup images on right */
	right_pics.pics[0] = new Image();
	right_pics.pics[0].src = pathToImages + "right/Instant-Messaging-On-The-Mac.jpg";
	right_pics.links[0] = "http://www.macfanatic.net/blog/2007/01/27/instant-messaging-on-the-mac/"; 
	
	right_pics.pics[1] = new Image();
	right_pics.pics[1].src = pathToImages + "right/CSSEdit-2-Review.jpg";
	right_pics.links[1] = "http://www.macfanatic.net/blog/2006/11/08/cssedit-2-review/";
	
	right_pics.pics[2] = new Image();
	right_pics.pics[2].src = pathToImages + "right/Schoolhouse-1.1-Review.jpg";
	right_pics.links[2] = "http://macfanatic.net/blog/2006/12/17/schoolhouse-review/";
	
	right_pics.pics[3] = new Image();
	right_pics.pics[3].src = pathToImages + "right/Crossover-DVDShrink.jpg";
	right_pics.links[3] = "http://macfanatic.net/blog/2006/09/02/crossover-and-boot-camp-review/";
				
	// Set timer to keep going and change them
	SetTimer();
}

function ChangeLeftImages() {
	
	/* Deal with the left hall of fame holder */
	left_pics.Index++;
	if ( left_pics.Index >= left_pics.pics.length ) {
		left_pics.Index = 0;
	}
	
	if ( document.images.left_hallOfFame ) {
		target = document.images.left_hallOfFame;
	}
	
	if ( document.all && document.getElementById("left_hallOfFame") ) {
		target = document.getElementById("left_hallOfFame");
	}
		
	// Change the image itself
	if ( target != null ) {
		target.src = left_pics.pics[left_pics.Index].src;
	}
	
	// Change the link itself
	target = document.getElementById("left_link");
	target.href = left_pics.links[left_pics.Index];
	
}
	
function ChangeMiddleImages() {	
	/* Deal with the middle images */
	middle_pics.Index++;
	if ( middle_pics.Index >= middle_pics.pics.length ) {
		middle_pics.Index = 0;
	}
	
	if ( document.images.middle_hallOfFame ) {
		target = document.images.middle_hallOfFame;
	}
	
	if ( document.all && document.getElementById("middle_hallOfFame") ) {
		target = document.getElementById("middle_hallOfFame");
	}
		
	// Change the image itself
	if ( target != null ) {
		target.src = middle_pics.pics[middle_pics.Index].src;
	}
	
	// Change the link itself
	target = document.getElementById("middle_link");
	target.href = middle_pics.links[middle_pics.Index];
	
}
	
function ChangeRightImages() {	
	/* Deal with far right images */
	right_pics.Index++;
	if ( right_pics.Index >= right_pics.pics.length ) {
		right_pics.Index = 0;
	}
	
	if ( document.images.right_hallOfFame ) {
		target = document.images.right_hallOfFame;
	}
	
	if ( document.all && document.getElementById("right_hallOfFame") ) {
		target = document.getElementById("right_hallOfFame");
	}
		
	// Change the image itself
	if ( target != null ) {
		target.src = right_pics.pics[right_pics.Index].src;
	}
	
	// Change the link itself
	target = document.getElementById("right_link");
	target.href = right_pics.links[right_pics.Index];
}


function SetTimerForMiddle() {
	setInterval("ChangeMiddleImages()", speed);
}

function SetTimerForRight() {
	setInterval("ChangeRightImages()", speed);
}

function setTimerForLeft() {
	setInterval("ChangeLeftImages()", speed);
}

function SetTimer() {
	
	// Place the image at array[0] in the space (I don't have an image there in the HTML source)
	InitializeImage("left");
	InitializeImage("middle");
	InitializeImage("right");
	
	// Setup the left images to start rotating every 5 seconds
	setTimeout("setTimerForLeft()", 0);
	
	// Give the left images a head start, we'll wait 2 seconds before calling a setInterval for the middle
	setTimeout("SetTimerForMiddle()", 3000);
	
	// Gave the others head start, now start this one, same concept as above
	setTimeout("SetTimerForRight()", 6000);
}

function InitializeImage(position) {
	var elementId;
	var linkElementId;
	var code;
	
	if ( position == "left" ) {
		elementId = "left_hallOfFame";
		linkElementId = "left_link";
		code = 1;
	} else if ( position == "middle" ) {
		elementId = "middle_hallOfFame";
		linkElementId = "middle_link";
		code = 2;
	} else if ( position == "right" ) {
		elementId = "right_hallOfFame";
		linkElementId = "right_link";
		code = 3;
	}
		
	if ( code == 1 ) {
		if ( document.images.left_hallOfFame ) {
			target = document.images.left_hallOfFame;
		}
		if ( document.all && document.getElementById(elementId) ) {
			target = document.getElementById(elementId);
		}
	} else if ( code == 2 ) {
		if ( document.images.middle_hallOfFame ) {
			target = document.images.middle_hallOfFame;
		}
		if ( document.all && document.getElementById(elementId) ) {
			target = document.getElementById(elementId);
		}
	} else if ( code == 3 ) {
		if ( document.images.right_hallOfFame ) {
			target = document.images.right_hallOfFame;
		}
		if ( document.all && document.getElementById(elementId) ) {
			target = document.getElementById(elementId);
		}
	}
		
	// Set the intial images up
	if ( target != null ) {
		if ( code == 1 ) {
			target.src = left_pics.pics[0].src;
			target = document.getElementById(linkElementId);	// Switch target to point to href
			target.href = left_pics.links[0];
		} else if ( code == 2 ) {
			target.src = middle_pics.pics[0].src;
			target = document.getElementById(linkElementId);	// Switch target to point to href
			target.href = middle_pics.links[0];
		} else if ( code == 3 ) {
			target.src = right_pics.pics[0].src;
			target = document.getElementById(linkElementId);	// Switch target to point to href
			target.href = right_pics.links[0];
		}
	}
}








