var slideShowPlaying = true;
var currentSlide = 2;
var timer;
var delay = 5000;
var waitAfterClick = 12000;



function nextSlide()
{
	if(slideShowPlaying){
		switchSliderTo(currentSlide);
		if(currentSlide < 4){
			currentSlide++;
		}else{
			currentSlide = 1;
		}
	}
	timer = setTimeout("nextSlide()",delay);
}

function stopSlideShow(pageId){
	clearTimeout(timer);
	timer = setTimeout("nextSlide()",waitAfterClick);
	currentSlide = pageId;
}

function switchSliderTo(slideId)
{
	for(var i=1; i<=4; i++){
		if(i == slideId){
			document.getElementById('slider_link'+i).style.backgroundPosition = "bottom left";
		}else{
			document.getElementById('slider_link'+i).style.backgroundPosition = "top left";
		}
	}
	new Effect.Move('slider', { x: - (slideId-1) * 929, mode: 'absolute', duration:0.5});
}

function swapServiceTabsTo(slideId)
{
	if(f_scrollTop()+f_clientHeight() < document.body.scrollHeight){
		scrollWindowTo('scrollAnchor1'); 
	}
	for(var i=1; i<=4; i++){
		if(i == slideId){
			document.getElementById('services_tab'+i).style.backgroundPosition = "top left";
		}else{
			document.getElementById('services_tab'+i).style.backgroundPosition = "bottom left";
		}
	}
	new Effect.Morph('services_container', {
  		style: 'position:relative; margin-top:'+ (- (slideId-1) * 371) +';',
  		duration: 0.8 
	});
	//new Effect.Move('services_container', { y: - (slideId-1) * 371, mode: 'absolute', duration:0.5});
}

function scrollWindowTo(objectId)
{
	new Effect.ScrollTo(objectId);
}

function folio_init(numThumbs){
	
	document.getElementById('portfolio_slider').style.width = numThumbs * 188;
	swapFolio(getValue("fid"));
}

function getValue(varname)
{
  // First, we load the URL into a variable
  var url = window.location.href;

  // Next, split the url by the ?
  var qparts = url.split("?");

  // Check that there is a querystring, return "" if not
  if (qparts.length == 0)
  {
    return "";
  }

  // Then find the querystring, everything after the ?
  var query = qparts[1];

  // Split the query string into variables (separates by &s)
  var vars = query.split("&");

  // Initialize the value with "" as default
  var value = "";

  // Iterate through vars, checking each one for varname
  for (i=0;i<vars.length;i++)
  {
    // Split the variable by =, which splits name and value
    var parts = vars[i].split("=");
    
    // Check if the correct variable
    if (parts[0] == varname)
    {
      // Load value into variable
      value = parts[1];

      // End the loop
      break;
    }
  }
  
  // Convert escape code
  value = unescape(value);

  // Convert "+"s to " "s
  value.replace(/\+/g," ");

  // Return the value
  return value;
}

function slideThumbsLeft(numThumbs){
	document.getElementById('portfolio_box_arrow_left').href="javascript:void(0)";
	document.getElementById('portfolio_box_arrow_right').href="javascript:void(0)";
	var folioSlider = document.getElementById('portfolio_slider');
	var curPosition = folioSlider.offsetLeft - 90;
	var sliderWidth = numThumbs * 188;
	
	if(curPosition > -sliderWidth + 769){
		new Effect.Move('portfolio_slider',{x:-188, mode:'relative', duration:0.3});
	}
	
	setTimeout("assignLink('portfolio_box_arrow_right','javascript:slideThumbsLeft("+numThumbs+");')",300);
	setTimeout("assignLink('portfolio_box_arrow_left','javascript:slideThumbsRight("+numThumbs+");')",300);
}

function slideThumbsRight(numThumbs){
	document.getElementById('portfolio_box_arrow_left').href="javascript:void(0)";
	document.getElementById('portfolio_box_arrow_right').href="javascript:void(0)";
	var folioSlider = document.getElementById('portfolio_slider');
	var curPosition = folioSlider.offsetLeft - 90;
	var sliderWidth = numThumbs * 188;
	
	if(curPosition < -100){
		new Effect.Move('portfolio_slider',{x:188, mode:'relative', duration:0.3});
	}
	
	setTimeout("assignLink('portfolio_box_arrow_right','javascript:slideThumbsLeft("+numThumbs+");')",300);
	setTimeout("assignLink('portfolio_box_arrow_left','javascript:slideThumbsRight("+numThumbs+");')",300);
}

function assignLink(link,path){
	document.getElementById(link).href=path;
}

var currentNode = "lahopper";
function swapFolio(newNode){
	scrollWindowTo("scrollAnchor3");
	if(newNode != undefined){
		new Effect.BlindUp("portfolio_box_top_"+currentNode,{duration:0.4});
		new Effect.BlindDown("portfolio_box_top_"+newNode,{duration:0.4, delay:0.4});
		typeOut(newNode);
		currentNode = newNode;
	}
}


var fieldCount = 0;

function typeOut(objectid){
	document.getElementById(objectid).style.display = "none";
	new Effect.Appear(objectid,{duration:3.0});
	window['trigger'+fieldCount] = document.getElementById(objectid+"_link");
	window['trigger'+fieldCount].href = "javascript:void()";
	window['count'+fieldCount] = 0;
	window['sArray'+fieldCount] = new Array();
	window['n_string'+fieldCount] = "";
	window['layer'+fieldCount] = document.getElementById(objectid);
	window['_string'+fieldCount] = window['layer'+fieldCount].innerHTML;
	window['sLen'+fieldCount] = window['_string'+fieldCount].length;
	window['layer'+fieldCount].innerHTML = "";
	nextLetter(objectid,fieldCount);
	fieldCount++;
}

function nextLetter(trigger,varID){
	window['n_string'+varID] += window['_string'+varID].charAt(window['count'+varID]);
	window['layer'+varID].innerHTML = window['n_string'+varID];
	if(window['count'+varID] < window['sLen'+varID] - 1){
		setTimeout(function () {nextLetter(trigger,varID)}, 1);
	}else{
		window['trigger'+varID].href = "javascript:swapFolio('"+trigger+"')";
	}
	window['count'+varID]++;
}


function f_clientWidth() {
	return f_filterResults (
		window.innerWidth ? window.innerWidth : 0,
		document.documentElement ? document.documentElement.clientWidth : 0,
		document.body ? document.body.clientWidth : 0
	);
}
function f_clientHeight() {
	return f_filterResults (
		window.innerHeight ? window.innerHeight : 0,
		document.documentElement ? document.documentElement.clientHeight : 0,
		document.body ? document.body.clientHeight : 0
	);
}
function f_scrollLeft() {
	return f_filterResults (
		window.pageXOffset ? window.pageXOffset : 0,
		document.documentElement ? document.documentElement.scrollLeft : 0,
		document.body ? document.body.scrollLeft : 0
	);
}
function f_scrollTop() {
	return f_filterResults (
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
}
function f_filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}