
window.onload=function(){
	// look for movie anchors
	movieListener();
	// look for news items
	/*if (document.getElementById("newsItems")) {
		prepareNewsItems();	
	}*/
	if ((document.getElementById("contentNav")) && (document.getElementById("archiveGrid"))) {
		prepareArchiveHover();
	}
}

//movieListener binds onclick event to anchors with "movie" as a className
function movieListener(){
	if( document.getElementById && document.getElementsByTagName ){	
		var aLinks = document.getElementsByTagName("A");		
		for( var i=0; i < aLinks.length; i++ ){				
			if (isMatch(aLinks[i].className,"movie")) {
				aLinks[i].onclick = function(){
				return showMovie(this);
				};
			
			} else {
				return true;
			}					
		}
	}
}

// prepareArchiveHover loops through all anchors in the contentNav and archiveGrid and applies the hoverMatch function to the mouseover of the element the user hovers over
function prepareArchiveHover() {
	if( document.getElementById && document.getElementsByTagName ){	
		var aLinks = document.getElementById("contentNav").getElementsByTagName("A");		
		for( var i=0; i < aLinks.length; i++ ){							
			aLinks[i].onmouseover = function(){
			return hoverMatch(this,1);
			};							
			aLinks[i].onmouseout = function(){
			return hoverMatch(this,0);
			};							
		}
		aLinks = document.getElementById("archiveGrid").getElementsByTagName("A");		
		for( var i=0; i < aLinks.length; i++ ){							
			aLinks[i].onmouseover = function(){
			return hoverMatch(this,1);
			};							
			aLinks[i].onmouseout = function(){
			return hoverMatch(this,0);
			};					
		}
	}	
}

function hoverMatch(pEl,toggleMode) {
	var aryNavLis = document.getElementById("contentNav").getElementsByTagName("LI");	
	var aryGridLis = document.getElementById("archiveGrid").getElementsByTagName("LI");
	var curZone = getGroupParent(pEl,"UL");
	var curPos = 0;
	
	if (curZone.id == "contentNav") {
		for( var i=0; i < aryNavLis.length; i++ ){		
			if (aryNavLis[i] == pEl.parentNode) {
				curPos = i;
			}
		}	
		aryGridLis[curPos].className = "on";
	}
	if (curZone.id == "archiveGrid") {
		for( var i=0; i < aryGridLis.length; i++ ){		
			if (aryGridLis[i] == pEl.parentNode) {
				curPos = i;
			}
		}		
		aryNavLis[curPos].className = "on";	
	}
	// turn off the li className on mouseOut
	if (toggleMode == 0) {
		for( var i=0; i < aryNavLis.length; i++ ){		
			aryNavLis[i].className = "";
		}
		for( var i=0; i < aryGridLis.length; i++ ){		
			aryGridLis[i].className = "";
		}		
	
	}
}

// prepareNewsItems loops through all anchors in the newsItems list and attaches the toggleNewsItems function to the onclick event
function prepareNewsItems() {
	if( document.getElementById && document.getElementsByTagName ){	
		var aLinks = document.getElementById("newsItems").getElementsByTagName("A");		
		for( var i=0; i < aLinks.length; i++ ){
			if (isMatch(aLinks[i].className,"teaser")) {
				aLinks[i].onclick = function(){
				return toggleNewsItems(this);
				};				
			}
		}
	}	
}

// toggleNewsItems shows the news blurb the user clicked and hides the others
function toggleNewsItems(pEl) {
	var newsItems = document.getElementById("newsItems").getElementsByTagName("A");	
	for( var i=0; i < newsItems.length; i++ ){
		if (isMatch(newsItems[i].className,"teaser")) {
			var theNewsBlurb = getNextNode(newsItems[i]);
			if (newsItems[i] == pEl) {
				newsItems[i].className = "teaser on";
				theNewsBlurb.className = "newsCopy on";
			} else {
				newsItems[i].className = "teaser";
				theNewsBlurb.className = "newsCopy";
			}
		}
	}	
	return false;
}


//swap movie clips   
function showMovie(pEl){

	var movieName = pEl.href;
	var strTitle = pEl.title;
	var movieContentID = "";
	var i = 0;
	var movieHolders = document.getElementById("movieCol").getElementsByTagName("DIV");
	
	//loop through all movieHolders and hide to ensure only one is shown at a time	
	for( i=0; i < movieHolders.length; i++ ){			
		if (isMatch(movieHolders[i].className,"movieHolder")) {
			movieHolders[i].style.display="none";
		}
	}
	
	
	//map the title to an movieHolder id	
	switch(strTitle) {
	
		case "Rick and Steve Teaser":
		
			movieContentID = "rickandsteve";		
		
			break;    
	
		case "Rick and Steve Promo":
		
			movieContentID = "rickandsteve";		
		
			break;    
	
		case "Keeping Score":
		
			movieContentID = "keepingscore";
	
			break;
		
		default:
		
			movieContentID = "";
	
	}
	
	//weird exception here, this link was somehow causing the movieListener script to stop looping
	if (movieName == "http://www.videodesignsoftware.com/") {
		window.open("http://www.videodesignsoftware.com/");
	} else {
	
		
		//write the title to above the movie clip
		document.getElementById(movieContentID).getElementsByTagName("H2")[0].innerHTML = strTitle;
		
		//if the link clicked was in the videoMenu, need to set that li to "current"
		if (pEl.parentNode.tagName == "LI") {
			var theVideoMenu = getGroupParent(pEl,"UL");
			var theVideoMenuLi = pEl.parentNode;
			var theVideoMenuEls = theVideoMenu.getElementsByTagName("LI");
			for(i=0; i < theVideoMenuEls.length; i++ ) {
				if (theVideoMenuEls[i] == theVideoMenuLi) {
					theVideoMenuEls[i].className = "current";
				} else {
					theVideoMenuEls[i].className = "";
				}			
			}
		}
		
		//set the quicktime movie
		var strQtime = "<OBJECT CLASSID=\"clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B\" WIDTH=\"320\" HEIGHT=\"256\" CODEBASE=\"http://www.apple.com/qtactivex/qtplugin.cab\"><PARAM name=\"SRC\" id=\"qtime_obj_src\" VALUE=\"" + movieName + "\"><PARAM name=\"AUTOPLAY\" VALUE=\"false\"><PARAM name=\"CONTROLLER\" VALUE=\"true\"><EMBED id=\"qtime_emb_src\" SRC=\"" + movieName + "\" WIDTH=\"320\" HEIGHT=\"256\" AUTOPLAY=\"false\" CONTROLLER=\"true\" PLUGINSPAGE=\"http://www.apple.com/quicktime/download/\"></EMBED></OBJECT>";
		
		
		//write quicktime movie to holder div
		document.getElementById(movieContentID).getElementsByTagName("DIV")[1].innerHTML = strQtime;
		
		//show the movie and it's info
		document.getElementById(movieContentID).style.display="block";
	}
	
	//don't follow the anchor's href
	return false;

}

/* HELPERS */
function isMatch(pEl,pMatch){
	if ((pEl.match(pMatch))==pMatch) {
		return true;
	} else {
		return false
	}	
}

function getNextNode(pNode) {
	var nextSib = pNode.nextSibling;
	while (nextSib.nodeType != 1) {
		nextSib = nextSib.nextSibling;
	}	
	return nextSib;
}

function getChildByTag(pNode, theTagName) {
	var theChildren = pNode.childNodes;
	var theChosenOne = "";	
	for (i=0; i<theChildren.length; i++) {
		if (theChildren[i].tagName == theTagName) {
			theCosenOne = 	theChildren[i];
		}
	}	
	return theCosenOne;
}

function getGroupParent(pNode,strTagName) {
	var groupParent = pNode.parentNode;
	while (groupParent.tagName != strTagName.toUpperCase()) {
		groupParent = groupParent.parentNode;
	}	
	return groupParent;
}

/* jQuery  */
$(document).ready(function() {
	
	/*
	
	Home page still viewer
	
	*/

    $("#view-stills a").hover(
      function () {
		  // update the stills image holder
			$("#stills")[0].src = this.href;
			
			$(this).addClass("on");
      }, 
      function () {
			// remove onstate from all the anchors
			$("#view-stills a").removeClass("on");
      }
    );

	/*
	
	DISABLE STILL VIEWER LINKS
	
	*/
	$("#view-stills a").click(function(){
		return false;
	});




});