//JavaScript for Yanke Website

var animating;
function move(pos) {
	if(animating == pos || !animating) {
		pPos = 49 - (179*(pos));
		pic = document.getElementById('picCol');
		if(pic.style.backgroundPosition) {
			curPos = parseInt(pic.style.backgroundPosition.substr(4, pic.style.backgroundPosition.length));
			if(curPos < pPos) {
				for(x=10;x>0;x--){
					if((pPos-curPos)%x==0) {
						newint = x;
						x=0;
					}
				}
			}
			else if(curPos==pPos) {
				return;
			}
			else {
				for(x=10;x>0;x--){
					if((pPos-curPos)%x==0) {
						newint = -x;
						x=0;
					}
				}
			}
			pic.style.backgroundPosition = '0px ' + (curPos + newint) + 'px';
		}
		else if(pic.style.backgroundPositionY) {
			curPos = parseInt(pic.style.backgroundPositionY);
			if(curPos < pPos) {
				for(x=10;x>0;x--){
					if((pPos-curPos)%x==0) {
						newint = x;
						x=0;
					}
				}
			}
			else if(curPos==pPos) {
				return;
			}
			else {
				for(x=10;x>0;x--){
					if((pPos-curPos)%x==0) {
						newint = -x;
						x=0;
					}
				}
			}
			pic.style.backgroundPositionY = (curPos + newint) + 'px';
		}
		else {
			pic.style.backgroundPosition = '0px 0px';
		}
		if(pic.style.backgroundPosition) {
			if(parseInt(pic.style.backgroundPosition.substr(4, pic.style.backgroundPosition.length)) != pPos) {
				animating = pos;
				window.setTimeout('move(' + pos + ')', 10);
			}
			else {
				animating = false;
			}
		}
		else if(pic.style.backgroundPositionY) {
			if(parseInt(pic.style.backgroundPositionY) != pPos) {
				animating = pos;
				window.setTimeout('move(' + pos + ')', 10);
			}
			else {
				animating = false;
			}
		}
	}
	else if(animating) {
		window.setTimeout('move(' + pos + ')', animating*10);
		return;
	}
}

function toggleChild(pNode) {
	topNode = pNode.parentNode.parentNode;
	for(x=0;x<topNode.childNodes.length;x++) {
		if(topNode.childNodes[x].childNodes[0] == pNode) {
			if(topNode.childNodes[x].childNodes[2].className == 'hidden') {
				topNode.childNodes[x].childNodes[2].setAttribute('class', 'shown');
				topNode.childNodes[x].childNodes[2].className = 'shown';
			}
			else {
				topNode.childNodes[x].childNodes[2].setAttribute('class', 'hidden');
				topNode.childNodes[x].childNodes[2].className = 'hidden';
			}
		}
		else {
			topNode.childNodes[x].childNodes[2].setAttribute('class', 'hidden');
			topNode.childNodes[x].childNodes[2].className = 'hidden';
		}
	}
}

function loadData(objString) {
	req = getAJAXObj();
	urlStr = "data2.cfm?label=" + objString;
	req.onreadystatechange = function() {
		if(req.readyState==4) {
			if(req.status==200) {
				var text = req.responseText;
				document.getElementById('mainText').innerHTML = text;
				if(text.indexOf('lightbox') > -1) {
					if(document.getElementById('lightbox') && document.getElementById('overlay')) {
						pNode = document.getElementById('overlay').parentNode;
						pNode.removeChild(document.getElementById('lightbox'));
						pNode.removeChild(document.getElementById('overlay'));
					}
					initLightbox();
				}
			}
		}
	}
	req.open("GET", urlStr, true);
	req.send(null);
	try {
		document.getElementById('iconbar').innerHTML = "";
		if(objString.indexOf("%20") > -1) {
			curArray = eval(objString.replace(/%20/g, "_"));
		}
		else {
			curArray = eval(objString.replace(/\s/g, "_"));
		}
		document.getElementById('iconbar').style.margin = "0px";
		document.getElementById('iconbar').style.width = (curArray.length * 96) + "px";
		document.getElementById('iconbar').style.margin = "auto";
		temp = "";
		for(x=0;x<curArray.length;x++) {
			temp += "<a href='javascript:loadData2(\"" + curArray[x] +"\");setSelected(\"" + curArray[x].replace(/\s/g, "_") + "\");' id='" + curArray[x].replace(/\s/g, "_") + "'><span>" + curArray[x] + "</span></a>";
		}
		document.getElementById('iconbar').innerHTML = temp;
	}
	catch(e) {
		//Do Nothing.  There are no Icons to display
	}
}

function loadData2(objString) {
	req = getAJAXObj();
	urlStr = "data2.cfm?label=" + objString;
	req.onreadystatechange = function() {
		if(req.readyState==4) {
			if(req.status==200) {
				var text = req.responseText;
				if(parseInt(objString) > 1900) {
					document.getElementById('mainText').innerHTML = "<u>" + objString + "</u><br><br>" + text;
				}
				else {
					document.getElementById('mainText').innerHTML = text;
				}
				if(text.indexOf('lightbox') > -1) {
					initLightbox();
				}
			}
		}
	}
	req.open("GET", urlStr, true);
	req.send(null);
}

function setSelected(objID) {
	iBar = document.getElementById('iconbar');
	if(iBar.hasChildNodes()) {
		for(x=0;x<iBar.childNodes.length;x++) {
			if(iBar.childNodes[x].id != objID) {
				iBar.childNodes[x].className = "";	
			}
			else {
				iBar.childNodes[x].className = "selected";
			}
		}
	}
}

function getAJAXObj() {
	var request;
	var ua = navigator.userAgent.toLowerCase();
	if (!window.ActiveXObject)
		request = new XMLHttpRequest();
	else if (ua.indexOf('msie 5') == -1)
		request = new ActiveXObject("Msxml2.XMLHTTP");
	else
		request = new ActiveXObject("Microsoft.XMLHTTP");
	return request;
}

var timer;
var animating;
function startTheSlide(amt) {
	if(!animating) {
		animating = true;
		timer = window.setInterval("slide(" + amt + ")", 10);
	}
}
var lastIndex;
function slide(amt) {
	numImages=0;
	for(k=0;k<document.getElementById('tBar').childNodes.length;k++) {
		if(document.getElementById('tBar').childNodes[k].nodeName == 'IMG') {
			numImages = numImages+1;
		}
	}
	for(i=0;i<document.getElementById('tBar').childNodes.length;i++) {
		if(document.getElementById('tBar').childNodes[i].nodeName == 'IMG') {
			if(parseInt(document.getElementById('tBar').childNodes[i].style.left)) {
				if(lastIndex > i && amt > 0) {
					if(parseInt(document.getElementById('tBar').childNodes[i].offsetLeft) >=250) {
						curLeft = parseInt(document.getElementById('tBar').childNodes[lastIndex].style.left);
						difference = 250-curLeft;
						slide(difference);
						i = document.getElementById('tBar').childNodes.length;
					}
					else {
						
						moveElement(i);
					}
				}
				else if(lastIndex > i && amt < 0) {
					
					if(parseInt(document.getElementById('tBar').childNodes[lastIndex].offsetLeft) <=250) {
						curLeft = parseInt(document.getElementById('tBar').childNodes[lastIndex].style.left);
						offsetLen = (numImages*100)-350;
						difference = -offsetLen-curLeft;
						slide(difference);
						i = document.getElementById('tBar').childNodes.length;
					}
					else {
						moveElement(i);
					}
				}
				else {
						moveElement(i);
				}
				if(i != document.getElementById('tBar').childNodes.length)lastIndex = i;
			}
			else {
				document.getElementById('tBar').childNodes[i].style.left = amt + "px";
			}
		}
	}
	animating = false;
	function moveElement(curIndex) {
		curLeft = parseInt(document.getElementById('tBar').childNodes[curIndex].style.left);
		document.getElementById('tBar').childNodes[curIndex].style.left = (curLeft+amt) + "px";
		if(parseInt(document.getElementById('tBar').childNodes[curIndex].offsetLeft) > 200 && parseInt(document.getElementById('tBar').childNodes[curIndex].offsetLeft) < 300) {
			document.getElementById('timelineCaption').innerHTML = document.getElementById('tBar').childNodes[curIndex].alt;
		}
	}
}

function endTheSlide() {
	animating = false;
	window.clearInterval(timer);
}

function goTimeline() {
	loadData2(document.getElementById('timelineCaption').innerHTML);
}

function loadTimeline() {
	document.getElementById('iconbar').style.width = "auto";
	tReq = getAJAXObj();
	tReq.onreadystatechange = function() {
		if(tReq.readyState == 4) {
			if(tReq.status == 200) {
				htmlString = tReq.responseText;
				foot = document.getElementById('iconbar');
				foot.innerHTML = foot.innerHTML + htmlString;
				slide(250);
			}
		}
	}
	tReq.open("GET", "data.cfm", true);
	tReq.send(null);
}

function loadSecurity() {
	document.getElementById('iconbar').innerHTML = "";
	document.getElementById('iconbar').style.margin = "0px";
	document.getElementById('iconbar').style.width = 550 + "px";
	document.getElementById('iconbar').style.margin = "auto";
	temp = "";
	curArray = new Array(1);
	curArray[0] = 'C-TPAT';
	curArray[1] = 'FAST';
	curArray[2] = 'Terminals';
	for(x=0;x<curArray.length;x++) {
		temp += "<a href='javascript:loadData2(\"" + curArray[x] +"\");setSelected(\"" + curArray[x].replace(/\s/g, "_") + "\");' id='" + curArray[x].replace(/\s/g, "_") + "'></a>";
	}
	document.getElementById('iconbar').innerHTML = temp;
}

function getSelected() {
	/* This section needs a lot of work still.  Not quite functional.
	Sort of works in FF and IE, but differenctly.  I need to get it working
	correctly across all browsers.
	*/
	execStr = "";
	if(document.getElementById('selectedElement')) {
		el = document.getElementById('selectedElement');
		if(el.getAttribute('href')) {
			if(el.getAttribute('href').indexOf('http://') > -1) {
				functionString = el.getAttribute('href').substring(19);
			}
			else {
				functionString = el.getAttribute('href');	
			}
			eval(functionString);
		}
		if(el.getAttribute('onclick')) {
			onclk = el.getAttribute('onclick');
			newStr = onclk.replace("this", "document.getElementById('selectedElement')");
			eval(newStr);	
		}
		if(el.getAttribute('class')) {
			curClass = el.getAttribute('class');
			if(curClass != 'menuItem') {
				curClass = curClass.replace(/%20/g, " ");
				menuItems = document.getElementById('leftCol').getElementsByTagName('a');
				//menuItems = document.getElementsByClassName('menuItem');
				for(x=0;x<menuItems.length;x++) {
					if(menuItems[x].getAttribute('class') == 'menuItem') {
						//alert(menuItems[x].nodeName + " - " + menuItems[x].innerHTML + " - " + curClass);
						if(menuItems[x].innerHTML == curClass) {
							if(menuItems[x].getAttribute('onclick')) {
								toggleChild(menuItems[x]);
								execStr = execStr + menuItems[x].innerHTML + " was toggled.  ";
							}
							else {
								toggleChild(curParent);
								execStr = execStr + curParent.innerHTML + " was toggled.  ";
							}
						}
						else if(menuItems[x].getAttribute('onclick')) {
							curParent = menuItems[x];
						}
					}
				}
				//toggleChild(document.getElementById(curClass));	
			}
		}
	}
	//alert(execStr);
		/*alert(element.className);
		if(element.className) {
			alert('2');
				//parent = element.className;
				parent = element.class;
			alert('3');
			if(parent!='menuItem')alert(parent);
		}
		
		urlStr = element.getAttribute('href');
		eval(urlStr);
		if(element.hasAttribute) {
			if(element.hasAttribute('onclick')) {
				onclk = element.getAttribute('onclick');
				newStr = onclk.replace("this", "document.getElementById('selectedElement')");
				//alert("Going to: " + urlStr + ".  And then running " + newStr);
				eval(newStr);
			}
			else if(element.getAttribute('onclick')) {
				onclk = element.getAttribute('onclick').value;
				newStr = onclk.replace("this", "document.getElementById('selectedElement')");
				eval(newStr);
			}
		}
		else {
			if(element.parentNode.parentNode.parentNode.firstChild.id == 'header') {
				//alert('This should probably do something else.');
				for(i=0;i<document.getElementById('leftCol').childNodes[1].childNodes.length;i++) {
					curElement = document.getElementById('leftCol').childNodes[1].childNodes[i];
					if(curElement.firstChild.firstChild.value == element.className) {
						toggleChild(curElement.firstChild);
						//alert(curElement.firstChild.firstChild.value);
					}
				}
			}
			else {
				p=0;
				if(element.parentNode.parentNode.parentNode.firstChild) {
					//alert(element.parentNode.parentNode.parentNode.firstChild.id);
				}
				else {
					if(element.parentNode) {
						p = 1;	
						if(element.parentNode.parentNode) {
							p++;
							if(element.parentNode.parentNode.parentNode) {
								p++;
								if(element.parentNode.parentNode.parentNode.childNodes) {
									//alert(element.parentNode.parentNode.parentNode.childNodes.length);
								}
							}
						}
					}
					//alert(p);
					toggleChild(element.parentNode.parentNode.parentNode.childNodes[0]);	
				}
			}
		}
	}
	*/
}