// YEAH!

var archiveSlide;

window.addEvent('domready', function()
{
	if (document.body)
	{
		setRandomBackgroundImage();
	}
	
	initNavigationEventHandler();
	
	getProjects('undefined'); // By omitting the order parameter the default (date descending) project list will be returned.
});

window.addEvent('resize',function(e)
{
	//setRandomBackgroundImage(); // Not so good idea...
});

function initImageDetailsEventHandler()
{
	$('newImage').getElements('a').addEvent('mouseover', function(e)
	{
		if ($(this.id)) $(this.id).setStyle('text-decoration', 'underline');
	});
	$('newImage').getElements('a').addEvent('mouseout', function(e)
	{
		if ($(this.id)) $(this.id).setStyle('text-decoration', 'none');
	});

	$('newImage').getElements('a').addEvent('click', function(e)
	{
		if (this.id.substring(0,4)=='hide')
		{
			hideWorkDetails();
			showProjectDetails(this.id.substring(4));
		}
		else if (this.id.substring(0,4)=='prev')
		{
			showNextPrev(this.id.substring(4));
		}
		else if (this.id.substring(0,4)=='next')
		{
			showNextPrev(this.id.substring(4));
		}
	});	
}

function initTextDetailsEventHandler()
{
	$('newText').getElements('a').addEvent('mouseover', function(e)
	{
		if ($(this.id)) $(this.id).setStyle('text-decoration', 'underline');
	});
	$('newText').getElements('a').addEvent('mouseout', function(e)
	{
		if ($(this.id)) $(this.id).setStyle('text-decoration', 'none');
	});
	
	$('newText').getElements('a').addEvent('click', function(e)
	{
		if (this.id.substring(0,4)=='clnt')
		{
			hideProjectDetails();
			hideWorkDetails();
			filterOn('client',this.id.substring(4),1);
		}
		else if (this.id.substring(0,4)=='tpfc')
		{
			hideProjectDetails();
			hideWorkDetails();
			filterOn('typeface',this.id.substring(4),1);
		}
		else if (this.id.substring(0,4)=='tech')
		{
			hideProjectDetails();
			hideWorkDetails();
			filterOn('technique',this.id.substring(4),1);
		}
		else if (this.id.substring(0,4)=='mtrl')
		{
			hideProjectDetails();
			hideWorkDetails();
			filterOn('material',this.id.substring(4),1);
		}
		else if (this.id.substring(0,4)=='dscp')
		{
			hideProjectDetails();
			hideWorkDetails();
			filterOn('discipline',this.id.substring(4),1);
		}
	});
}

function setRandomBackgroundImage()
{
	// Always append milliseconds in order to force getting the damn page...
	document.body.style.backgroundImage = 'url(ajax/background.php?width='+window.getWidth()+'&height='+window.getHeight()+'&refresh='+new Date().getTime()+')';
}

function initNavigationEventHandler()
{
	$('navigation').getElements('a').addEvent('mouseover', function(e)
	{
		if ($(this.id)) $(this.id).setStyle('text-decoration', 'underline');
	});
	
	$('navigation').getElements('a').addEvent('mouseout', function(e)
	{
		if ($(this.id)) $(this.id).setStyle('text-decoration', 'none');
	});
		
	$('navigation').getElements('a').addEvent('click', function(e)
	{
		hideProjectDetails();
		hideWorkDetails();
		
		setRandomBackgroundImage(); 
		
		if (this.id=='title')
		{
			getProjects('title');
		}	
		if (this.id=='date')
		{
			getProjects('date');
		}
		if (this.id=='medium')
		{
			getProjects('media');
		}
		if (this.id=='images')
		{
			viewAs('images');
		}
		if (this.id=='text')
		{
			viewAs('text');
		}
		if (this.id=='news')
		{
			showNews();
		}
		if (this.id=='about')
		{
			showAbout();
		}
		if (this.id=='contact')
		{
			var url='ajax/contact.php';
			new Request.HTML({url: url, method: 'get', onComplete: showContactComplete, update: $('dynamic')}).send();
		}
	});

}

function viewAs(view)
{
	var url='ajax/projects.php';
	new Request.HTML({url: url, data: 'show='+view, method: 'get', onComplete: initContentEventHandler, update: $('dynamic')}).send();
}

function getProjects(order)
{
	var url='ajax/projects.php';
	new Request.HTML({url: url, data: 'order='+order, method: 'get', onComplete: initContentEventHandler, update: $('dynamic')}).send();
}

function filterOn(key, value)
{
	var url='ajax/projects.php';
	new Request.HTML({url: url, data: key+''+value, method: 'get', onComplete: initContentEventHandler, update: $('dynamic')}).send();
}

// Hmmm... makes me think of Daphne...
// If index is undefined this results in both the archive as the news DIVs to be rendered
function showNews(index)
{
	var url='ajax/news.php';
	
	if (index==undefined)
	{
		new Request.HTML({url: url, data: 'index='+index, method: 'get', onComplete: initContentEventHandler, update: $('dynamic')}).send();
	}
	else
	{
		new Request.HTML({url: url, data: 'index='+index, method: 'get', update: $('newsitem')}).send();
	}
}

function showAbout()
{
	var url='ajax/about.php';
	new Request.HTML({url: url, method: 'get', onComplete: showAboutComplete, update: $('dynamic')}).send();
}

function showAboutComplete()
{
	initContentEventHandler();
}

function changeLabel()
{
	if ($('more').innerHTML.substring(0,4)=='more')
	{
		$('more').innerHTML='&laquo; less';
	}
	else
	{
		$('more').innerHTML='more &raquo;';
	}
}

function initContentEventHandler()
{
	$('content').getElements('a').addEvent('click', function(e)
	{  
		if (this.id.substring(0,4)=='show' || this.id.substring(0,4)=='pict' || this.id.substring(0,4)=='year' || this.id.substring(0,4)=='medi')
		{
			showProjectDetails(this.id.substring(4));
		}
		else if (this.id.substring(0,4)=='news')
		{
			showNews(this.id.substring(4));	
		}
		else if (this.id.substring(0,4)=='more')
		{
			showMore(e);
		}
	});
	
	// Extend hover for the entire row

	$('content').getElements('a').addEvent('mouseover', function(e)

	{
		if (this.id.substring(0,4)=='show' || this.id.substring(0,4)=='year' || this.id.substring(0,4)=='medi')
		{
			var index=this.id.substring(4);

			$('show'+index).setStyle('text-decoration', 'underline');
			$('medi'+index).setStyle('text-decoration', 'underline');
			$('year'+index).setStyle('text-decoration', 'underline');
		}
	});

	$('content').getElements('a').addEvent('mouseout', function(e)
	{
		if (this.id.substring(0,4)=='show' || this.id.substring(0,4)=='year' || this.id.substring(0,4)=='medi')
		{
			var index=this.id.substring(4);
			
			$('show'+index).setStyle('text-decoration', 'none');
			$('medi'+index).setStyle('text-decoration', 'none');
			$('year'+index).setStyle('text-decoration', 'none');
		}
	});
}

function showMore(e)
{
	$('test').setStyle('display', 'block');
	
	if(!archiveSlide)
	{
		archiveSlide = new Fx.Slide('test', {onComplete: changeLabel}).hide();
	}
	
	e.stop();
	archiveSlide.toggle();
}

function showNextPrev(index)
{
	var url='ajax/worksimg.php';
	new Request.HTML({url: url, data: 'work='+index, method: 'get', onComplete: initImageDetailsEventHandler, update: $('newImage')}).send();
	
	var url='ajax/workstext.php';
	new Request.HTML({url: url, data: 'work='+index, method: 'get', onComplete: initTextDetailsEventHandler, update: $('newText')}).send();
}

function showWorkDetails(index)
{
	var url='ajax/worksimg.php';
	new Request.HTML({url: url, data: 'work='+index, method: 'get', onComplete: initImageDetailsEventHandler, update: $('newImage')}).send();
	
	var url='ajax/workstext.php';
	new Request.HTML({url: url, data: 'work='+index, method: 'get', onComplete: initTextDetailsEventHandler, update: $('newText')}).send();
		
	$('projectWorks').setStyle('display','none');
	$('newImage').setStyle('display', 'block');
	$('newText').setStyle('display', 'block');
}

function initProjectImageDetailsEventHandler()
{
	$('projectWorks').getElements('a').addEvent('click', function(e)
	{
		if (this.id.substring(0,4)=='pict')
		{
			showWorkDetails(this.id.substring(4));
		}
		if (this.id.substring(0,4)=='hide')
		{
			hideProjectDetails();
		}
	});
}

function initProjectTextDetailsEventHandler()
{
	$('newText').getElements('a').addEvent('mouseover', function(e)
	{
		if ($(this.id)) $(this.id).setStyle('text-decoration', 'underline');
	});
	$('newText').getElements('a').addEvent('mouseout', function(e)
	{
		if ($(this.id)) $(this.id).setStyle('text-decoration', 'none');
	});
	
	$('newText').getElements('a').addEvent('click', function(e)
	{
		if (this.id.substring(0,4)=='clnt')
		{
			hideProjectDetails();
			filterOn('client',this.id.substring(4),1);
		}
		else if (this.id.substring(0,4)=='tpfc')
		{
			hideProjectDetails();
			filterOn('typeface',this.id.substring(4),1);
		}
		else if (this.id.substring(0,4)=='tech')
		{
			hideProjectDetails();
			filterOn('technique',this.id.substring(4),1);
		}
		else if (this.id.substring(0,4)=='mtrl')
		{
			hideProjectDetails();
			filterOn('material',this.id.substring(4),1);
		}
		else if (this.id.substring(0,4)=='dscp')
		{
			hideProjectDetails();
			filterOn('discipline',this.id.substring(4),1);
		}
	});
}

function showProjectDetails(index)
{
	// Load contents for first image, then load the details text, at the end show them both
	
	hideWorkDetails();
	
	var url='ajax/projectsimg.php';
	new Request.HTML({url: url, data: 'project='+index, method: 'get', onComplete: initProjectImageDetailsEventHandler, update: $('projectWorks')}).send();
	
	var url='ajax/projectstext.php';
	new Request.HTML({url: url, data: 'project='+index, method: 'get', onComplete: initProjectTextDetailsEventHandler, update: $('newText')}).send();
		
	$('projectWorks').setStyle('display', 'block');
	$('newText').setStyle('display', 'block');
}

function hideProjectDetails()
{
	$('projectWorks').setStyle('display','none');
	$('projectWorks').innerHTML='';

	$('newText').setStyle('display','none');
	$('newText').innerHTML='';
}

function hideWorkDetails()
{
	$('newImage').setStyle('display','none');
	$('newImage').innerHTML='';

	$('newText').setStyle('display','none');
	$('newText').innerHTML='';
}

function showContactComplete()
{
	var myAccordion = new Accordion($('accordion'), 'a.category', 'div.element', 
	{
		display: -1,
		alwaysHide: true,
		onActive: function(toggler, element)
		{
			toggler.innerHTML='&laquo; '+toggler.innerHTML.substring(0,toggler.innerHTML.length-1);
		},
		onBackground: function(toggler, element)
		{
			if (toggler.innerHTML.substring(0,1).charCodeAt(0)==171) // <<
			{
				toggler.innerHTML=toggler.innerHTML.substring(2) + ' &raquo;';
			}
		}
	});
}

