addLoadEvent(prepareGallery);
addLoadEvent(prepareImageGalleries);

function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	}
	else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

function prepareGallery() {
  if (document.getElementById && document.getElementsByTagName) {
    if (document.getElementById("gallery")) {
      var gallery = document.getElementById("gallery");
      var links = gallery.getElementsByTagName("a");
      for (var i=0; i < links.length; i++) {
        links[i].onclick = function() {
          return showPic(this);
        }
      }
    }
  }
}


function showPic(whichpic){ 
	if (document.getElementById) { 
		document.getElementById('placeholder').src = whichpic.href; 
		if (whichpic.title) { 
			document.getElementById('featured_project').childNodes[0].nodeValue = whichpic.title; 
		} else { 
			document.getElementById('featured_project').childNodes[0].nodeValue = whichpic.childNodes[0].nodeValue; 
		} 
		return false; 
	} else { 
		return true; 
	} 
}


function prepareImageGalleries(){
	var inlineNavigations = getElementsByClassName( 'inlineNavigation' ,'DL', document);
		var theNavigation;
		for(var x=0;x<inlineNavigations.length;x++){	
			//		contentTabNavigations[x].childNodes[0].outerHTML = contentTabNavigations[x].childNodes[0].outerHTML	+ '<div class="clearsides">&nbsp;</div>'; // A Browser Display Fix
			/*alert(inlineNavigations[x].childNodes.length);
			inlineNavigations[x].childNodes[3].childNodes[0].getElementsByTagName('li');*/
			theTabList=inlineNavigations[x].getElementsByTagName('li');
			for(var y=0;y<theTabList.length;y++){
				
				theTabList[y].childNodes[0].onclick = function(){
					theTargetId = this.href.substring(this.href.indexOf('#') + 1, this.href.length);
					inlineNavigationClickTab(theTargetId);
					window.scrollTo(0,0);
					return false;
				}
				if(theTabList[y].className.indexOf('selected')>=0){
					theTargetId = theTabList[y].childNodes[0].href.substring(theTabList[y].childNodes[0].href.indexOf('#') + 1, theTabList[y].childNodes[0].href.length);						
					showImageGallery(theTargetId);		
				}
			}
		}				
	}

function showImageGallery(theTargetId){
		var inlineNavigationLink = document.getElementsByTagName('A');
		var inlineNavigationContainer;
		for(var x=0;x<inlineNavigationLink.length;x++){
			
			if(inlineNavigationLink[x].href.substring(inlineNavigationLink[x].href.indexOf('#') + 1, inlineNavigationLink[x].href.length)==theTargetId){		
				inlineNavigationContainer = inlineNavigationLink[x];
				while(!(inlineNavigationContainer.tagName == 'DL' && inlineNavigationContainer.className == 'inlineNavigation')){
					inlineNavigationContainer = inlineNavigationContainer.parentNode;
				}				
			}
		}
		theTargetContentContainer = inlineNavigationContainer.id.substring(0,inlineNavigationContainer.id.length-'inlineNavigation'.length);
		theContentContainer = document.getElementById(theTargetContentContainer+'gallery');

		var theContentContainerItems =  getElementsByClassName('contentItem', 'dd' , theContentContainer);
				
				for(var z=0;z<theContentContainerItems.length;z++){

				if(theContentContainerItems[z].id== theTargetId){
					theContentContainerItems[z].style.display="block";
				}else{
					theContentContainerItems[z].style.display="none";
				}
			}
}

function inlineNavigationSelectTab(theTargetId){		
		var inlineNavigationLink = document.getElementsByTagName('A');
		var inlineNavigationContainer;
		for(var x=0;x<inlineNavigationLink.length;x++){
			
			if(inlineNavigationLink[x].href.substring(inlineNavigationLink[x].href.indexOf('#') + 1, inlineNavigationLink[x].href.length)==theTargetId){		
				inlineNavigationContainer = inlineNavigationLink[x];
				
				while(!(inlineNavigationContainer.tagName == 'DL' && inlineNavigationContainer.className == 'inlineNavigation')){
					inlineNavigationContainer = inlineNavigationContainer.parentNode;
				}				
			}
		}
var inlineNavigationLi = inlineNavigationContainer.getElementsByTagName('LI');
		for(var x=0;x<inlineNavigationLi.length;x++){
			
			if(inlineNavigationLi[x].childNodes[0].href.substring(inlineNavigationLi[x].childNodes[0].href.indexOf('#') + 1, inlineNavigationLi[x].childNodes[0].href.length)==theTargetId){
				inlineNavigationLi[x].className = 'selected';
				
			}
			else{
				
				inlineNavigationLi[x].className ='notselected';
			}
		}
	}

	function inlineNavigationClickTab(theTargetId){
		inlineNavigationSelectTab(theTargetId);
		showImageGallery(theTargetId);
	}

/* third party js functions... credit goes to whoever made them first ;) */
function getElementsByClassName(className, tag, elm){
		var testClass = new RegExp("(^|\\s)" + className + "(\\s|$)");
		var tag = tag || "*";
		var elm = elm || document;
		var elements = (tag == "*" && elm.all)? elm.all : elm.getElementsByTagName(tag);
		var returnElements = [];
		var current;
		var length = elements.length;
		for(var i=0; i<length; i++){
			current = elements[i];
			if(testClass.test(current.className)){
				returnElements.push(current);
			}
		}
		return returnElements;
	}
	
	function addEvent( obj, type, fn ) { 
		if ( obj.attachEvent ) { 
			obj['e'+type+fn] = fn; 
			obj[type+fn] = function(){obj['e'+type+fn]( window.event );} 
			obj.attachEvent( 'on'+type, obj[type+fn] ); 
		} else 
		obj.addEventListener( type, fn, false ); 
	} 

	function removeEvent( obj, type, fn ){ 
		if ( obj.detachEvent ){ 
			obj.detachEvent( 'on'+type, obj[type+fn] ); 
			obj[type+fn] = null; 
		}
		else 
			obj.removeEventListener( type, fn, false ); 
	}
		function isClassNamePresent(theObject, theClassName){		
		var alreadyPresent = false;	
		if(theObject.className.length > 0){
			var theClassNames = theObject.className.split(' ');
			for(var x=0;x<theClassNames.length;x++){
				if(theClassNames[x] == theClassName)					
					alreadyPresent = true;
			}
		}
		return alreadyPresent;		
	}

	function removeClassName(theObject, theClassName){
		if(!(theObject.className.length == 0) && isClassNamePresent(theObject, theClassName)){
			var theClassNames = theObject.className.split(' ');
			var theNewArray = new Array();
			for(var x=0;x<theClassNames.length;x++){
				if(!(theClassNames[x] == theClassName))	
					theNewArray.push(theClassNames[x]);		
					theObject.className = theNewArray.join(' ');
					if(theObject.className.length == 0)
						theObject.removeAttribute('className');
			}
		}
	}

	function addClassName(objObject, strClassName){	
		if(objObject.className.length == 0){
			objObject.className = strClassName;
		}
		else{
			if(!isClassNamePresent(objObject, strClassName)){
				var strClassNames = objObject.className.split(' ');
				strClassNames.push(strClassName);
				objObject.className = strClassNames.join(' ');
			}
		}		
	}	