var curDisp;
var curIndex = 0;
var numEntries = 5;
var myInterval;
var stop = false;
var blogWindow;
var iassWindow;
var fadeInterval;
var isSlideChanging = false;
var isFading = false;
var currentOpacity = 0.0;

function rotate() {
  curIndex = ((curIndex + 1) % numEntries);
  if(!stop)
  {
    swap("s" + (curIndex + 1));
  }
}

function setFirst(id) {
  curDisp = id;
  myInterval = setInterval("rotate()",5000);
}

function changeEntry(id) {
  if(!stop) {
	stop = true;
	clearInterval(myInterval);
	clearInterval(fadeInterval);
  }
  swap(id);
}

function swap(id) {
  if(curDisp == id) return;
  isSlideChanging = true;
  var temp = ge('cover');
  temp.style.display = "block";
  temp.style.visibility = "visible";
  fadeInterval = setInterval("changeOpacity(0.050, '"+ id + "');", 850/20);
  //changeSlide(id);
}

function changeSlide(id) {
  var entry;
  entry = ge(id);
  entry.style.display = "block";
  entry.style.visibility = "visible";
  ge(id + "-a").className = "sel";
  entry = ge(curDisp);
  entry.style.display = "none";
  entry.style.visibility = "hidden";
  ge(curDisp + "-a").className = "";
  curDisp = id;
  fadeInterval = setInterval("changeOpacity(-0.050);", 850/20);
}

function ge(id) {
  return document.getElementById(id);
}

function openBlog() {
  if(blogWindow == null || blogWindow.closed) 
    blogWindow = window.open('http://www.valleyzen.com/', 'popup', '');
  blogWindow.focus();
}

function openIASS() {
  if(iassWindow == null || iassWindow.closed) 
    iassWindow = window.open('http://www.inasinglestroke.com/', 'popup2', '');
  iassWindow.focus();
}

function changeOpacity(val, id) {
  currentOpacity += val;
  if(currentOpacity <= 0.0) currentOpacity = 0.0;
  else if(currentOpacity >= 1.0) currentOpacity = 1.0;
  setOp(val);
  if(currentOpacity >= 1.0) {
	clearInterval(fadeInterval);
	changeSlide(id);
  }
  else if(currentOpacity <= 0.0) {
     clearInterval(fadeInterval);
     var temp = ge('cover');
     temp.style.display = "none";
     temp.style.visibility = "hidden";
     isSlideChanging = false;
  }
}

function setOp(val) {
   var elem = ge('cover');
   if(val == 1.0) val = 0.999;
   elem.style.opacity = '' + currentOpacity;
   elem.style.MozOpacity = '' + currentOpacity;
   elem.style.KhtmlOpacity = '' + currentOpacity;
   elem.style.filter = "alpha(opacity=" + (currentOpacity * 100) + ")";
}



function initPopups() {
  var links = document.getElementsByTagName('a');
  for(var i = 0; i < links.length; i++) {
    if(links[i].className.indexOf('p-link') != -1) {
      links[i].onclick = function()
      {
	  popupLink(this.getAttribute('href'));
        return false;
      }
    }
  }
}

function popupLink(url) {
  if(blogWindow == null || blogWindow.closed) 
    blogWindow = window.open(url, 'popup', '');
  blogWindow.focus();
}



  /* Wrapper function for the onload handler (can't pass in the parameter, so
   * I'll pass it in here.
   */
  function ssGo(){
    setFirst('s1');
  }

  function addLoadEvent(fn) {
    var curFn = window.onload;
    window.onload = (typeof window.onload != 'function') ? fn : function(){curFn();fn();}
  }
  
  addLoadEvent(ssGo);
  addLoadEvent(initPopups);