// <![CDATA[
var flashMovie;
var iscollapsed = true;
var hmod = 38;
var so;
var activeChild = null;

/* thanks to http://hossgifford.com and of course, geoff stearns, for pointing me in the right direction for the resize script */
function resizeSWF(){
	var h = parseInt(flashMovie.style.height);
	nHeight = h + hmod;
	iscollapsed = false;
	flashMovie.style.height = h+'px';
}

function showProject( element ) {
	if( activeChild != null ){
		activeChild.removeClass( "active" );
		activeChild.addClass( "visited" );
	}
	
	activeChild = element;
	activeChild.addClass('active');
	activeChild.removeClass('visited');
	
	flashMovie.showProject( element.index );
	return false;
}

function showProjImg( pindex, imgindex) {
  		flashMovie.showProjImg( pindex, imgindex );
}

function setActiveChild( element ){
	activeChild = element;
	activeChild.addClass('active');
}

function writeFlashGallery( startIndex ){
	var soh = 448;
	var sow = 800;
	so = new SWFObject("media/gallery.swf", "flashviewer", sow, soh, "8", "#ffffff",true);
	so.addVariable("xmlPath", "xml/data.xml");
	so.addVariable('imageheight', 420 );
	so.addParam("allowScriptAccess", "always");
	so.addParam('allowscale',false);
	so.addParam('scale','noscale');
	so.addParam('align','TL');
	so.addParam('salign','TL');
	so.addParam('wmode','transparent');
	so.addVariable('startIndex', startIndex );
	so.addParam('roundedMask',true);
	so.write("flashcontent");
	flashMovie = document.getElementById('flashviewer');	
}

function makeMultiCol(listElement,itemsper,colwidth,margin,rowheight){
	var itemspercol = itemsper;
	if(typeof listElement == 'string') listElement = document.getElementById(listElement);
	var items = listElement.getElementsByTagName('li');
	count = items.length;
	var col = 0;
	// just in case you ever need to know....
	// var cols = Math.ceil(count/itemsper);
	for(var i = 0; i < count; i++){
		var item = items[i];
		if(i > 0 && i % itemspercol == 0){
			item.style.marginTop = -(itemspercol*rowheight) + "px";
			col++;
		}
		var xoffset = (margin+colwidth)*col;
		item.style.width = colwidth+"px"
		item.style.marginLeft = xoffset +"px";
	}
	var licap = document.createElement('li');
	licap.style.clear = "both";
	licap.style.border = "none";
	listElement.appendChild(licap);
}

function writeEmailString(container){
	if(typeof container == 'string') container = $(container);
	var emailString = String.fromCharCode(60,97,32,104,114,101,102,61,34,109,97,105,108,116,111,58,105,110,102,111,64,109,97,100,101,111,102,112,101,111,112,108,101,46,111,114,103,34,62,105,110,102,111,64,109,97,100,101,111,102,112,101,111,112,108,101,46,111,114,103,60,47,97,62);
	container.innerHTML = emailString;
}

window.addEvent('domready', function(){
		var links = $$("#projectlist li a");
		links.each( function( element, i ){
			element.index = i; 
			element.addEvent( 'click', showProject.bind( this , element ) );
		});
		var startIndex = ( getQueryParamValue("proj") )? getQueryParamValue("proj").split(":")[0] : 0;
		writeFlashGallery( startIndex );
		setActiveChild( links[startIndex] );
		makeMultiCol('projectlist',6,200,0,21);
		writeEmailString('maillink');
});