globalSwitch = "off";

function loadDesc() {
	
	// Make the first item of Specialists Array appear
	var array = getElementsByClassName(document, 'div', 'profileBlurb');
	for (var i=0; i<array.length; i++) {
		if (i == 0) {
			array[i].style.display='block';
		}else{
			array[i].style.display='none';
		}
	}
	
	// Truncate Text
	for (var i=0; i<array.length; i++) {
		var profileDesc
		profileDesc = array[i].getElementsByTagName("div")[1].innerHTML;		
		array[i].getElementsByTagName("div")[1].innerHTML = profileDesc;
	}	
	
}


function changePic(ID) {
	var profilePicSrc = document.getElementById('profilePic').src;		
	var array = getElementsByClassName(document, 'div', 'profileBlurb');	
	
	for (var i=0; i<array.length; i++) {
		if (i == ID) {
			array[i].style.display='block';
			profilePicSrc = array[i].getElementsByTagName("img")[0].src;
		}else{
			array[i].style.display='none';
		}
	}
	
	if(globalSwitch == "off"){
		globalSwitch = "on";
		opacity('profilePic',100,0,300);
		window.setTimeout("opacity('profilePic',0,100,300)",298);
		window.setTimeout("document.getElementById('profilePic').src='"+profilePicSrc+"'",300);	
		window.setTimeout("globalSwitch = 'off'",500);
	}
	
}

// START OPACITY FUNCTION
function opacity(id, opacStart, opacEnd, millisec) {
    //speed for each frame
    var speed = Math.round(millisec / 100);
    var timer = 0;

    //determine the direction for the blending, if start and end are the same nothing happens
    if(opacStart > opacEnd) {
        for(i = opacStart; i >= opacEnd; i--) {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    } else if(opacStart < opacEnd) {
        for(i = opacStart; i <= opacEnd; i++)
            {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    }
}
function changeOpac(opacity, id) {
    var object = document.getElementById(id).style;
    object.opacity = (opacity / 100);
    object.MozOpacity = (opacity / 100);
    object.KhtmlOpacity = (opacity / 100);
    object.filter = "alpha(opacity=" + opacity + ")";
} 
// END OPACITY FUNCTION





function addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    } else {
        window.onload = function() {
            oldonload();
            func();
        }
    }
}

function moveSlideshow(elementID,final_x,final_y,interval) {
    if (!document.getElementById) return false;
    
    // if the element does not exist we have nothing to do
    if (!document.getElementById(elementID)) return false;
    var elem = document.getElementById(elementID);
    
    // the slideshow events stack up and the animation is not smooth anymore
    if (elem.movement) {
        clearTimeout(elem.movement);
    }
    
    // current slideshow position
    var xpos = parseInt(elem.style.left);
    var ypos = parseInt(elem.style.top);
    if (xpos == final_x && ypos == final_y) {
        return true;
    }
    
    // restrict moving to white area
    if (final_x <= -elem.max_x) {
        final_x = -elem.max_x;
    }
    if (final_x > 0) {
	    final_x = 0;
    }
    
    // animation bit (taken from the book DOM Scripting by Jeremy Keith)
    if (xpos < final_x) {
        var dist = Math.ceil((final_x - xpos)/10);
        xpos = xpos + dist;
    }
    if (xpos > final_x) {
        var dist = Math.ceil((xpos - final_x)/10);
        xpos = xpos - dist;
    }
  
    // again, restrict showing white area
    if (xpos <= -elem.max_x) {
	    xpos = -elem.max_x;
    }
    if (xpos > 0) {
	    xpos = 0;
    }
    
    // fix the elements position
    elem.style.left = xpos + "px";
    elem.style.top = ypos + "px";
    
    // and set up the event again after an interval
    var repeat = "moveSlideshow('"+elementID+"',"+final_x+","+final_y+","+interval+")";
    elem.movement = setTimeout(repeat,interval);
}

function prepareSlideshow() {
	loadDesc();
	// first lets make sure the browser understands the DOM methods we will be using
  	if (!document.getElementsByTagName) return false;
  	if (!document.getElementById) return false;
  	
	// Make sure the elements exist
  	if (!document.getElementById("slideshow")) return false;
  	var slideshow = document.getElementById("slideshow");
  	var wrapper = document.getElementById("slideshow_wrapper");
  	wrapper.style.overflow = "hidden";
  	
  	// prepare the navigation bit we will be using
  	// left
  	var navigation = document.createElement("ul");
  	navigation.setAttribute("id", "slider_navigation");
  	var li = document.createElement("li");
  	var scroll_left = document.createElement("a");
  	scroll_left.setAttribute("id", "scroll_left");
  	scroll_left.href ="#";
  	var text = document.createTextNode("Left");
  	scroll_left.appendChild(text);
  	li.appendChild(scroll_left);
  	navigation.appendChild(li);
  	slideshow.insertBefore(navigation, wrapper);
  	
  	//right
  	var li = document.createElement("li");
  	var scroll_right = document.createElement("a");
  	scroll_right.setAttribute("id", "scroll_right");
  	scroll_right.href ="#";
  	var text = document.createTextNode("Right");
  	scroll_right.appendChild(text);
  	li.appendChild(scroll_right);
  	navigation.appendChild(li);
  	slideshow.insertBefore(navigation, wrapper);
	
	var slideshow_set = document.getElementById("slideshow_set");
	slideshow_set.style.top = 0+"px";
	slideshow_set.style.left = 0+"px";
	
	// to get the max y position of the gallery image track we need to count all
	// the li items and multiply that number with 130
	var li = slideshow_set.getElementsByTagName("li");
	slideshow_set.max_x = ((li.length-5) * 70) -70;
	slideshow_set.max_y = li.length * 130;
	
	// need the width of the gallery so that they do not scroll vertical
	var width = li.length * 70;
	slideshow_set.style.width = width + "px";
	
	// Attach onmouseover event for left
  	scroll_left.onclick = function() {
		// get the current position of the gallery element
		var slideshow_set = document.getElementById("slideshow_set");
		var x = parseInt(slideshow_set.style.left);
		if (x % 70 == 0) {
    		moveSlideshow("slideshow_set",x+70,0,10);
		}
		return false;
	}
	
	// Attach onmouseover event for right
  	scroll_right.onclick = function() {
		// get the current position of the gallery element
		var slideshow_set = document.getElementById("slideshow_set");
		var x = parseInt(slideshow_set.style.left);
    	if (x % 70 == 0) {
    		moveSlideshow("slideshow_set",x-70,0,10);
		}
		return false;
	}
}
addLoadEvent(prepareSlideshow);
