var its;
var browserName = '';
var browserNew = '';
function its() {
	var n = navigator;
	var ua = ' ' + n.userAgent.toLowerCase();
	this.ie = ua.indexOf('msie') > 0;
	return this;
}
function browserNaming() {
	its = new its();
	if (!document.getElementById) {
		browserNew = false;
	}
	else {
		browserNew = true;
	}
	if (its.ie) {
		browserName = "IE";
	}
}

function initBlocks() {

	var stretchers = document.getElementsByClassName('content'); //div that stretches
	var switches = document.getElementsByClassName('switch'); //a's where I click on
	var toggles = document.getElementsByClassName('display'); //a's where I click on
	var boxes = document.getElementsByClassName("portBox");
	var divs = document.getElementById("pageBody").getElementsByTagName("div");
	
	var myAccordion = new fx.Accordion(
		switches, stretchers, {opacity: true, duration: 400}
	);

	function checkHash(){
		var found = false;
		switches.each(function(h2, i){
			if (window.location.href.indexOf(h2.title) > 0) {
				myAccordion.showThisHideOpen(stretchers[i]);
				found = true;
			}
		});
		return found;
	}
	
	if (!checkHash()) {
		document.getElementById("webDesign").className = "portBox cf open";
		getPage('/chris/portfolio/webDesign.php','webDesignBox','webDesign' );
		myAccordion.showThisHideOpen(stretchers[0]);
	}

    for (var i = 0; i < toggles.length; i++) {
		var numb = i;
		toggles[i].onclick = function(numb) {
			if (this.parentNode.parentNode.className == 'portBox cf open'){
				myAccordion.showThisHideOpen(this.parentNode.parentNode.id +'Box');
				this.parentNode.parentNode.className = "portBox cf closed";
			}
			else {
				//var closeIt =	 alert('nodeN'); 
				if (document.getElementById(this.parentNode.parentNode.id +'Box').getElementsByTagName('div').length == 0) {
					getPage('/chris/portfolio/'+ this.parentNode.parentNode.id +'.php',this.parentNode.parentNode.id +'Box',this.parentNode.parentNode.id );	
				}
				myAccordion.showThisHideOpen(this.parentNode.parentNode.id +'Box');
				for (var i = 0; i < boxes.length; i++) {	
					boxes[i].className = "portBox cf closed"
				}
				this.parentNode.parentNode.className = "portBox cf open";
			}
			return false;
		}
	}
}

function getPage(page,box,section) {
	new ajax (page, {update: $(box), onComplete:updateLinks(section) });
}

function updateLinks(section) {
	if (section == 'webDesign') {
	}
	else if (section == 'threeD') {
		setTimeout ("setThumbs ('threedThumbs','3d')",400)
	}
	else if (section == 'drawigns') {
		setTimeout ("setThumbs ('drawingThumbs','drawings')",400)
	}
	else if (section == 'print') {
		setTimeout ("setThumbs ('printThumbs','print')",400)
	}
	//alert(''+ section +'');
}

function setThumbs (divs,roll) {
	var nuImg = new Array();
	var links = document.getElementById(divs).getElementsByTagName('a');
	var img = document.getElementById(roll+'Img');
	for(i=0; i<links.length; i++) {
		var imgName = links[i].getAttribute('title');
		var nImgSrc = "/chris/portImages/"+ roll +"/"+ imgName +".jpg";
		nuImg[i] = new Image()
		nuImg[i].src = nImgSrc;
		links[i].setAttribute('nImgSrc', nImgSrc);
		links[i].onclick = function () { 
			img.setAttribute('src', this.getAttribute('nImgSrc')); 
			return false;
		}
	}
}


function unblur() {
	this.blur();
}
function getLinksToBlur() {
	if (browserNew) {
		links = document.getElementsByTagName("a");
		for(i=0; i<links.length; i++) {
			links[i].onfocus = unblur
		}
	}
}
if(document.getElementById && document.createTextNode)
{
	window.onload=function()
	{
	initBlocks();
	getLinksToBlur();
	}
}
