var h = document.getElementsByTagName('head')[0];
var q = document.createElement('script');
q.src = 'http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js';
q.type = 'text/javascript';
h.appendChild(q);

var bLoaded = false;
var cNav
var cSubNav = cLocalImgs = false;

function mouseOver(obj) {
	if(!bLoaded) return false;
	var sPath = obj.src.substring( 0, obj.src.lastIndexOf('/') +1 );
	var sImg = obj.src.substring(obj.src.lastIndexOf('/') +1, obj.src.lastIndexOf('.'));
	if(sImg.indexOf('_on') == -1) obj.src = sPath + sImg + "_on.gif";
}

function mouseOut(obj) {
	if(!bLoaded) return false;
	var sPath = obj.src.substring( 0, obj.src.lastIndexOf('/') +1 );
	var sImg = obj.src.substring(obj.src.lastIndexOf('/') +1, obj.src.lastIndexOf('.'));
	if(sImg.indexOf('_on') != -1) obj.src = sPath + sImg.replace('_on', '') + ".gif";
}

function attachMouseEvents() {
	for(var n=0; n<cNav.length; n++) {
		// no mouse events on active buttons
		if(cNav[n].src.indexOf('_on')!=-1) continue;
		cNav[n].onmouseover = function() { mouseOver(this) }
		cNav[n].onmouseout = function() { mouseOut(this) }
	}
	
	if(cSubNav) {
		for(var u=0; u<cSubNav.length; u++) {
			// no mouse events on active buttons
			if(cSubNav[u].src.indexOf('_on')!=-1) continue;
			cSubNav[u].onmouseover = function() { mouseOver(this) }
			cSubNav[u].onmouseout = function() { mouseOut(this) }
		}
	}
	
	if(cLocalImgs.length > 0) {
		for(var i=0; i<cLocalImgs.length; i++) {
			// no mouse events on active buttons
			if(cLocalImgs[i].src.indexOf('_on')!=-1) continue;
			cLocalImgs[i].onmouseover = function() { mouseOver(this) }
			cLocalImgs[i].onmouseout = function() { mouseOut(this) }
		}
	}	
}

function cmp(a, b) {
	return b-a;
}

function equalizeColumns() {
	
	oIcon = document.getElementById('icon');
	oWrpContent = document.getElementById('wrp-content');
	oContentPrimary = document.getElementById('content-primary');
	oContentSecondary = document.getElementById('content-secondary');
	aContentHeights = new Array(oContentPrimary.offsetHeight, oContentSecondary.offsetHeight);
	aContentHeights = aContentHeights.sort(cmp);
	if(oIcon.offsetHeight < oWrpContent.offsetHeight) {
		oIcon.style.height = (oWrpContent.offsetHeight - 178) + "px";
	}

}

function init_nextProject() {
	var isPortfolio = $('#belly .company');
	
	if( isPortfolio.size() > 0 ) {
		
		$('<div class="t-right"><a href="#" id="next-project"><img src="../images/nav_nextproject.gif" width="70" height="11" alt="Next Project" class="rollover" /></a></div>').insertAfter('#belly .detail');
		var qNextProject = $('#next-project');
		var qCurrentProject = $('#belly .company').attr('alias');
		var firstHref = '';
		
		$('#content-secondary').find('a').each(
			function(index) {
				var _this = $(this);
				var linkText = _this.text();
				
				// store this for efficient use when we're at the end of this list.  at that point, just start all over again
				if( index == 0 ) {
					firstHref = this.href;
				}
				
				if( linkText == qCurrentProject) {
					var next = _this.parent().next();
					var nextHref = _this.parent().next().find('a').attr('href');

					// if nextHref wasn't defined, it's because there is not next() sibling so use the firstHref istead
					if (typeof nextHref == 'undefined') {
						nextHref = firstHref;
					}
					qNextProject.attr( {href: nextHref} );
				}
			}
		);
	}
}

function init() {

	cNav = document.getElementById('nav').getElementsByTagName('IMG');
	
	if(document.getElementById('subnav')) {
		cSubNav = document.getElementById('subnav').getElementsByTagName('IMG');
	}

	oRollovers = document.getElementById('main').getElementsByTagName('IMG') || false;
	cLocalImgs = new Array();
	for(var i=0; i<oRollovers.length; i++) {
		if(oRollovers[i].className == 'rollover') {
			cLocalImgs.push(oRollovers[i]);
		}
	}

	equalizeColumns();
	attachMouseEvents();
	init_nextProject();
	bLoaded = true;
	
}

window.onload = init;

