var timer = 4;
// four photos 2d array, first param: image file name, second param: alt text
// first photo is declared in html page
var photos = [
    ['main_imageb', 'Trying to get pregnant?'],
    ['main_imagec', 'New giveaway'],
    ['main_imaged', 'Updated: Discipline guide'],
    ['main_imagee', '13 things to love about your teen']
];
// four html links corresponding to the four images
var imageLinks = ['http://www.todaysparent.com/pregnancy/preconception/article.jsp?content=20100222_111819_2676&page=1',
				'http://blogs.todaysparent.com/momslove/add-a-little-bling-to-spring/',
				'http://www.todaysparent.com/lifeasparent/parenting/article.jsp?content=20100217_135432_2124&page=1',
				'http://www.todaysparent.com/teen/behaviordevelopment/article.jsp?content=20100218_113512_1432&page=1'];

var img, addAnchor, count = 0;
var sTime;

function startSlideshow()
{
  img = document.getElementById('slideshow_image');
  addAnchor = document.getElementById('imgLink');
  sTime = window.setTimeout('cueNextSlide()', timer * 900);
}

function nextSlide(nr) {
	var next = new Image;
	next.onload = function() {
    img.src = next.src;
    img.alt = photos[nr][nr];
	addAnchor.setAttribute('href',imageLinks[nr]);

    img.width = next.width;
    img.height = next.height;
	clearInterval(sTime);
    sTime=window.setTimeout('cueNextSlide()', timer * 900);
	setLinkColor(nr);
  };

  next.src = '/images/homepage/' + photos[nr][0] + '.jpg';
}

function pause() {
	clearInterval(sTime);
	document.getElementById('pause').style.color="#6bad04"; 
}

function cueNextSlide()
{
  var next = new Image;

  next.onerror = function()
  {
    alert('Failed to load next image');
  };

  next.onload = function()
  {
    img.src = next.src;
    img.alt = photos[count][1];

    img.width = next.width;
    img.height = next.height;
	addAnchor.setAttribute('href',imageLinks[count]);
    ++count;
	setLinkColor(count-1);
    if ( count== photos.length) { count = 0;  }
	clearInterval(sTime);
    sTime=window.setTimeout('cueNextSlide()', timer * 900);
  };

  next.src = '/images/homepage/' + photos[count][0] + '.jpg';
}

addLoadListener(startSlideshow);

function setLinkColor(nr) {
	var intNr = String(nr);
	switch(intNr) {
		case '0': document.getElementById('zero').style.color="#37b2d3";  // blue
				  document.getElementById('one').style.color="#787878";
				  document.getElementById('two').style.color="#787878";
				  document.getElementById('three').style.color="#787878";
				  document.getElementById('pause').style.color="#787878";
				break;
		case '1': document.getElementById('zero').style.color="#787878";
				  document.getElementById('one').style.color="#37b2d3"; // blue
				  document.getElementById('two').style.color="#787878";
				  document.getElementById('three').style.color="#787878";
				  document.getElementById('pause').style.color="#787878";
				break;
		case '2': document.getElementById('zero').style.color="#787878";
				  document.getElementById('one').style.color="#787878";
				  document.getElementById('two').style.color="#37b2d3";  // blue
				  document.getElementById('three').style.color="#787878";
				  document.getElementById('pause').style.color="#787878";
				break;
		case '3': document.getElementById('zero').style.color="#787878";
				  document.getElementById('one').style.color="#787878";
				  document.getElementById('two').style.color="#787878";
				  document.getElementById('three').style.color="#37b2d3";  // blue
				  document.getElementById('pause').style.color="#787878";
				break;
	}
}

function addLoadListener(fn)
{
  if (typeof window.addEventListener != 'undefined')
  {
    window.addEventListener('load', fn, false);
  }
  else if (typeof window.attachEvent != 'undefined')
  {
    window.attachEvent('onload', fn);
  }
  else
  {
    var oldfn = window.onload;
    if (typeof window.onload != 'function')
    {
      window.onload = fn;
    }
    else
    {
      window.onload = function()
      {
        oldfn();
        fn();
      };
    }
  }
}
