
function formatTitle(title) {

	title = title.toUpperCase();
	title = title.replace( /-/g, ' ' );
	title.replace( '/', '' );

	return 'NewFinds | ' + title;
}



function handleChange(event) {
	var path = event.path;
	
	
	if( path == "/" ){
		hideModal();
		hideOverlay();
		SWFAddress.setTitle( formatTitle( 'Work') );
		return;
	}
	// add trailing slash if needed
	if (path.substr(path.length - 1) != '/') {
		path += '/';
	}

	if( path != '/' ){

		pageTracker._trackPageview(path);

		
		var arr = path.split( '/' );
		var title = arr[ arr.length-2 ];
		
		SWFAddress.setTitle( formatTitle( title ) );
		path = path.replace( '//', '/' );
		new Ajax.Updater( 'ajax', prefix + path, 
		{asynchronous:true, evalScripts:true, requestHeaders:['X-Update', 'ajax']} );
		showModal();
	}else{
		hideModal();
		hideOverlay();
	}
}

SWFAddress.addEventListener(SWFAddressEvent.CHANGE, handleChange);


/////// rollovers for the grid view //////


function over(divId){
	var div = document.getElementById( divId );
	div.style.background="#93bdbd";
}
function out(divId){
	var div = document.getElementById( divId );
	div.style.background="#fff";
}


////////// panel functions /////////

function showModal(){
	//showOverlay();
	document.getElementById( 'spinner' ).style.display = "";
	
	if( document.getElementById( 'modal' ).style.display == "none"){
		new Effect.Appear("modal", {duration: .6 });
	}
}

function hideModal(){
	//	SWFAddress.setTitle( 'NewFinds | Work' );
	//	SWFAddress.setValue( '' );
	new Effect.Fade("modal", {duration: .2});	
	document.getElementById( 'spinner' ).style.display = "none";
}


function showDetails(){
	if( document.getElementById( 'description' ).style.display == "none" ){
		var ajax = document.getElementById( 'ajax' );
		new Effect.Appear( 'description', {duration:.4,
			//,
			//afterUpdate: function callback(obj) {	
			//	ajax.style.marginTop = -(ajax.clientHeight/2)+"px";
			//} 
			});
		document.getElementById( 'images').style.display = 'none';
	}

	document.getElementById( "imagesLink").style.background = "#666666";
	document.getElementById( "imagesLink").style.color = "#cccccc";
	document.getElementById( "detailsLink").style.color = "#333333";
	document.getElementById( "detailsLink").style.background = "#FFFFFF";
}

function showImages(){
	if( document.getElementById( 'images' ).style.display == "none" ){

		var ajax = document.getElementById( 'ajax');
		document.getElementById( 'description' ).style.display = "none";
		new Effect.Appear( 'images', {duration:.4,
			//, 
		//	afterUpdate: function callback(obj) {	
		////		ajax.style.marginTop = -(ajax.clientHeight/2)+"px";
		//	}
			});
		}

		document.getElementById( "detailsLink").style.background = "#666666";
		document.getElementById( "detailsLink").style.color = "#cccccc";
		document.getElementById( "imagesLink").style.color = "#333333";
		document.getElementById( "imagesLink").style.background = "#FFFFFF";
	}

	function showOverlay(){
		var ajax = document.getElementById( 'ajax' );
		var anIn = false;
		
		if( ajax.style.display == "none" ){
			anIn = true;
		}
		
		ajax.style.display 	= 	"block";
		var overlayHeight	= 	ajax.clientHeight;
		var offset 			= 	( overlayHeight ) / 2;
		ajax.style.display	=	"none";

		if( offset < 0 ){
			offset = 0;
		}

		ajax.style.marginTop		= 	-offset + "px";
		if( anIn ){
			new Effect.Appear("ajax", { duration: .6 });
		}else{
			ajax.style.display = "block";
		}
		document.getElementById( 'spinner' ).style.display = "none";
	}

	function hideOverlay(){

		if( document.getElementById( 'ajax' ).style.display != 'none' ){
			hideModal();
			new Effect.Fade("ajax", { duration: .6 });
			document.getElementById( 'spinner' ).style.display = "none";
		}
	}

	////////////////////////// UTILITIES ////////////////////////////////////

	function getMaxScroll() {
		return document.documentElement.scrollHeight;
	}

	function getWinSize(){
		return document.documentElement.clientHeight;
	}
	
	function clearField( field, defaultVal ){
		if( field.value == defaultVal ){
			field.value = "";
		}
	}

	function getScrollOffset(){

		if (navigator.appName == "Microsoft Internet Explorer"){
			var offset = document.body.scrollTop;
		}else{
			offset = window.pageYOffset;
		}

		return offset;

	}