var SLIDETIMER = 3;
var SLIDESPEED = 5;
var SCROLLTIMER = 3;
var SCROLLSPEED = 3;
var STARTINGOPACITY = 40;

// handles section to section scrolling of the content //
function slideContent(id,prefix,timer) {
  var div = document.getElementById(id);
  var slider = div.parentNode;
  clearInterval(slider.timer);
  slider.section = parseInt(id.replace(/\D/g,''));
  slider.target = div.offsetTop;
  slider.style.top = slider.style.top || '0px';
  slider.current = slider.style.top.replace('px','');
  slider.direction = (Math.abs(slider.current) > slider.target) ? 1 : -1;

  var IE6 = (navigator.appVersion.indexOf('MSIE 6.')==-1) ? false : true;
  if (IE6) {
	  slideContentNoAnim(id,prefix,timer);
  }
  else {
	slider.style.opacity = STARTINGOPACITY * .01;
	slider.style.filter = 'alpha(opacity=' + STARTINGOPACITY + ')';
  	slider.timer = setInterval( function() { slideAnimate(slider,prefix,timer) }, SLIDETIMER);
  }
}

// handles section to section scrolling of the content //
function slideContentNoAnim(id,prefix,timer) {
  var div = document.getElementById(id);
  var slider = div.parentNode;
  clearInterval(slider.timer);
  slider.section = parseInt(id.replace(/\D/g,''));
  slider.target = div.offsetTop;
  slider.style.top = (slider.target * -1) + 'px';
}

function slideAnimate(slider,prefix,timer) {
  var curr = Math.abs(slider.current);
  var tar = Math.abs(slider.target);
  var dir = slider.direction;
  if((tar - curr <= SLIDESPEED && dir == -1) || (curr - tar <= SLIDESPEED && dir == 1)) {
    slider.style.top = (slider.target * -1) + 'px';
	slider.style.opacity = 1;
	slider.style.filter = 'alpha(opacity=100)';
    clearInterval(slider.timer);
	if(slider.autoscroll) {
	  setTimeout( function() { autoScroll(slider.id,prefix,timer) }, timer * 1000);
	}
  } else {
	var pos = (dir == 1) ? parseInt(slider.current) + SLIDESPEED : slider.current - SLIDESPEED;
    slider.current = pos;
    slider.style.top = pos + 'px';
  }
}

// handles manual scrolling of the content //
function scrollContent(id,dir) {
  var div = document.getElementById(id);
  clearInterval(div.timer);
  var sections = div.getElementsByTagName('div');
  var length = sections.length;
  var limit;
  if(dir == -1) {
    limit = 0;
  } else {
    if(length > 1) {
      limit = sections[length-1].offsetTop;
    } else {
      limit = sections[length-1].offsetHeight - div.parentNode.offsetHeight + 20;
    }
  }
  div.style.opacity = STARTINGOPACITY * .01;
  div.style.filter = 'alpha(opacity=' + STARTINGOPACITY + ')';
  div.timer = setInterval( function() { scrollAnimate(div,dir,limit) }, SCROLLTIMER);
}

function scrollAnimate(div,dir,limit) {
  div.style.top = div.style.top || '0px';
  var top = div.style.top.replace('px','');
  if(dir == 1) {
	if(limit - Math.abs(top) <= SCROLLSPEED) {
	  cancelScroll(div.id);
	  div.style.top = '-' + limit + 'px';
	} else {
	  div.style.top = top - SCROLLSPEED + 'px';
	}
  } else {
	if(Math.abs(top) - limit <= SCROLLSPEED) {
	  cancelScroll(div.id);
	  div.style.top = limit + 'px';
	} else {
	  div.style.top = parseInt(top) + SCROLLSPEED + 'px';
	}
  }
}

// cancel the scrolling on mouseout //
function cancelScroll(id) {
  var div = document.getElementById(id);
  div.style.opacity = 1;
  div.style.filter = 'alpha(opacity=100)';
  clearTimeout(div.timer);
}

// initiate auto scrolling //
function autoScroll(id,prefix,timer,restart) {
  var div = document.getElementById(id);
  div.autoscroll = (!div.autoscroll && !restart) ? false : true;
  if(div.autoscroll) {
    var sections = div.getElementsByTagName('div');
    var length = sections.length;
    div.section = (div.section && div.section < length) ? div.section + 1 : 1;
    slideContent(prefix + '-' + div.section,prefix,timer);
  }
}

// cancel automatic scrolling //
function cancelAutoScroll(id) {
  var div = document.getElementById(id);
  div.autoscroll = false;
}



/************ ADDED ****************/
var sliderSection = new Array();

function initProjectSliders() {

	for (var type in sliderSection) {
		var section = sliderSection[type];
		section = new Number(section);

		// scroll
		if (type == 'past') {
			section = 'p-project-' + section;
			slideContentNoAnim(section);
			//slideContent(section);
		}
		else if (type == 'current') {
			section = 'c-project-' + section;	
			slideContentNoAnim(section);
		}
	}
}

function projectsUp(type) {
	var sliderID =type + '-projects-slider';
	
	var section = sliderSection[type];
	if (section) {
		section--;
	}

	if (section < 1) { section = 1; }
	
	sliderSection[type] = section;
	
	// scroll
	if (type == 'past') {
		section = 'p-project-' + section;
		slideContent(section);
	}
	else if (type == 'current') {
		section = 'c-project-' + section;
		slideContent(section);		
	}
}

function projectsDown(type) {
	var sliderID =type + '-projects-slider';
	
	// get number of sections
	var slider = document.getElementById(sliderID);
	var projects = slider.childNodes;
	
	var num = 0;
	
	for (var i = 0; i < projects.length; i++) {
		var project = projects[i];
		if (project.nodeType == 1 && project.tagName.toLowerCase() == 'div') {
			num++;
		}
	}
	
	// check for current section
	var section = sliderSection[type];
	if (!section) {
		section = 1
	}
	section++;

	// make sure new section doesn't extend past end
	if (section > num) {
		section = num;	
	}
	
	sliderSection[type] = section;
	
	// scroll
	if (type == 'past') {
		section = 'p-project-' + section;
		slideContent(section);
	}
	else if (type == 'current') {
		section = 'c-project-' + section;
		slideContent(section);		
	}
}

function updateLink(alink) {
	for (var type in sliderSection) {
		var section = sliderSection[type];
		section = new Number(section);
		alink.href = alink.href + '&slp_' + type + '=' + section;
	}
}

/*******************************************/

function initAboutSliders() {

	for (var type in sliderSection) {
		var section = sliderSection[type];
		section = new Number(section);

		// scroll
		if (type == 'journalism-about-slider') {
			section = 'journalism-' + section;
			//slideContentNoAnim(section);		
			slideContentNoAnim(section);
		}
		else if (type == 'influences-about-slider') {
			section = 'influences-' + section;
			//slideContentNoAnim(section);		
			slideContentNoAnim(section);
		}
		else if (type == 'books-about-slider') {
			section = 'books-' + section;
			//slideContentNoAnim(section);		
			slideContentNoAnim(section);
		}
	}
}

function aboutUp(type) {
	var sliderID = type + '-about-slider';
	
	var section = sliderSection[sliderID];
	if (section) {
		section--;
	}

	if (section < 1) { section = 1; }
	
	sliderSection[sliderID] = section;
		
	// scroll
	section = type + '-' + section;
	slideContent(section);
}

function aboutDown(type) {

	var sliderID = type + '-about-slider';
	
	// get number of sections
	var slider = document.getElementById(sliderID);
	var items = slider.getElementsByTagName('div');
	var num = items.length;

	// check for current section
	var section = sliderSection[sliderID];
	if (!section) {
		section = 1
	}
	section++;
	
	// make sure new section doesn't extend past end
	if (section > num) {
		section =  num;	
	}
	
	sliderSection[sliderID] = section;

	section = type + "-" + section;
	slideContent(section);
}
/*
function journalismUp() {
	var sliderID = type + 'journalism-slider';
	
	var section = sliderSection[sliderID];
	if (section) {
		section--;
	}

	if (section < 1) { section = 1; }
	
	sliderSection[sliderID] = section;
		
	// scroll
	section = 'journalism-' + section;
	slideContent(section);
}

function journalismDown() {
	var sliderID ='journalism-slider';
	
	// get number of sections
	var slider = document.getElementById(sliderID);
	var items = slider.getElementsByTagName('div');
	var num = items.length;

	// check for current section
	var section = sliderSection[sliderID];
	if (!section) {
		section = 1
	}
	section++;
	
	// make sure new section doesn't extend past end
	if (section > num) {
		section =  num;	
	}
	
	sliderSection[sliderID] = section;

	section = "journalism-" + section;
	slideContent(section);
}


function influencesUp() {
	var sliderID ='influences-slider';
	
	var section = sliderSection[sliderID];
	if (section) {
		section--;
	}

	if (section < 1) { section = 1; }
	
	sliderSection[sliderID] = section;
		
	// scroll
	section = 'influences-' + section;
	slideContent(section);
}

function influencesDown() {
	var sliderID ='influences-slider';
	
	// get number of sections
	var slider = document.getElementById(sliderID);
	var items = slider.getElementsByTagName('div');
	var num = items.length;

	// check for current section
	var section = sliderSection[sliderID];
	if (!section) {
		section = 1
	}
	section++;
	
	// make sure new section doesn't extend past end
	if (section > num) {
		section =  num;	
	}
	
	sliderSection[sliderID] = section;

	section = "influences-" + section;
	slideContent(section);
}


function booksUp() {
	var sliderID ='books-slider';
	
	var section = sliderSection[sliderID];
	if (section) {
		section--;
	}

	if (section < 1) { section = 1; }
	
	sliderSection[sliderID] = section;
		
	// scroll
	section = 'books-' + section;
	slideContent(section);
}

function booksDown() {
	var sliderID ='books-slider';
	
	// get number of sections
	var slider = document.getElementById(sliderID);
	var items = slider.getElementsByTagName('div');
	var num = items.length;

	// check for current section
	var section = sliderSection[sliderID];
	if (!section) {
		section = 1
	}
	section++;
	
	// make sure new section doesn't extend past end
	if (section > num) {
		section =  num;	
	}
	
	sliderSection[sliderID] = section;

	section = "books-" + section;
	slideContent(section);
}*/