var sortTop;
var imgOffset = 100;
var hashed = '';

var resizeImage = function() {
	if($('screenholder'))
	{
		var myImg = $('screenholder').getElement('img');
		
		var oldY = myImg.getSize().y;
		var oldX = myImg.getSize().x;
		var newY = $(document.body).getSize().y-(imgOffset+10);
		var newX = Math.ceil((newY*oldX)/oldY);
		var imgTop = $(document.body).getScroll().y+Math.ceil(imgOffset/2);
		
		if(newX > ($(document.body).getSize().x-Math.ceil(imgOffset/2)))
		{
			newX = $(document.body).getSize().x-Math.ceil(imgOffset/2);
			newY = Math.ceil((newX*oldY)/oldX);
			imgTop = Math.ceil(($(document.body).getSize().y-newY)/2)+$(document.body).getScroll().y;
		}
		myImg.morph({
			'height': newY,
			'width': newX,
			'margin-top': imgTop,
			'opacity':1
		});
	}
}

window.addEvent('domready', function() {

if(window.location.hash != '')
{
	hashed = window.location.hash.substr(1);
}

if($('sortbar'))
{
	sortTop = $('sortbar').getPosition().y;
	var sortSpace = new Element('div', {
		'html':'&nbsp;',
		'id':'sortbar_spacer'
	});
	$('sortbar').setStyle('position', 'absolute');
	$('sortbar').setStyle('top', sortTop);
	$('sortbar').setStyle('margin', 0);
	$('sortbar').set('tween', {'duration':350});
	sortSpace.inject($('sortbar'), 'before');
}

if($('listing'))
{
	$('listing').getElements('td .more .toggle').each(function(tog){
		var myTd = tog.getParent('td');
		var myImg = myTd.getElement('img');
		var togDup = tog.clone();
		tog.dispose();
		var fadeDiv = new Element('div', {
			'class':'infofade',
			'styles':{
				'top':(myImg.getPosition().y+myImg.getSize().y-10),
				'left':(myImg.getPosition().x-$('page').getPosition().x),
				'width':(myTd.getElement('.more').getSize().x),
				'opacity':'0'
			},
			'html':'<div class="arrow"></div>'
		});
		myTd.getElement('.more').dispose();
		fadeDiv.set('tween', {'duration':250});
		togDup.inject(fadeDiv);
		togDup.set('html', '<em>Click image to enlarge.</em>'+togDup.get('html'));
		fadeDiv.inject(myTd);
		myImg.addEvent('mouseenter', function(e){
			if((e.client.y+$(document.body).getScroll().y) > ($(document.body).getScrollSize().y/2))
			{
				if(!this.getParent('td').getElement('.infofade').hasClass('bot'))
				{
					this.getParent('td').getElement('.infofade').addClass('bot');
					this.getParent('td').getElement('.infofade').getElement('.arrow').setStyle('top', this.getParent('td').getElement('.infofade').getSize().y-10);
				}
				this.getParent('td').getElement('.infofade').setStyle('top', this.getPosition().y-this.getParent('td').getElement('.infofade').getSize().y+4);
			}
			else
			{
				if(this.getParent('td').getElement('.infofade').hasClass('bot'))
				{
					this.getParent('td').getElement('.infofade').getElement('.arrow').setStyle('top', 0);
					this.getParent('td').getElement('.infofade').removeClass('bot');
				}
				var myImg = this.getParent('td').getElement('img');
				this.getParent('td').getElement('.infofade').setStyle('top', myImg.getPosition().y+myImg.getSize().y-10);
			}
			this.getParent('td').getElement('.infofade').setStyle('left', this.getPosition().x-$('page').getPosition().x);
			this.getParent('td').getElement('.infofade').tween('opacity', 1);
		});
		myImg.addEvent('mouseleave', function(e){
			this.getParent('td').getElement('.infofade').setStyle('left', this.getPosition().x-$('page').getPosition().x);
			this.getParent('td').getElement('.infofade').tween('opacity', 0);
		});
	});
	
	$('listing').getElements('a.thumb').each(function(thumb){
		thumb.addEvent('click', function(e){
			e.stop();
			this.blur();
			if(!$('screen'))
			{
				var scrDiv = new Element('div', {
					'id':'screen'
				});
				scrDiv.setStyle('opacity', 0);
				scrDiv.setStyle('height', window.getScrollSize().y);
				scrDiv.inject($(document.body));
				
				var holdDiv = new Element('div', {
					'id':'screenholder'
				});
				holdDiv.setStyle('height', window.getScrollSize().y);
				holdDiv.inject($(document.body));
				
				holdDiv.addEvent('click', function() {
					var myFx = new Fx.Tween($('screenholder'), {'duration':250});
					myFx.start('opacity', 0).chain(
						function() {
							$('screenholder').dispose();
							var myFx2 = new Fx.Tween($('screen'), {'duration':250});
							myFx2.start('opacity', 0).chain(
								function() {
									$('screen').dispose();
								}
							);
						}
					);
				});
				
				var myThumb = this;
				var myFx = new Fx.Tween(scrDiv, {'duration':250});
				myFx.start('opacity', .8).chain(
					function() {
						var myImg = myThumb.getElement('img').clone();
						myImg.setStyles({
							'border':'4px solid #ffffff',
							'opacity':0,
							'margin-top': ($(document.body).getScroll().y+Math.ceil(imgOffset/2))
						});
						myImg.set('morph', {
							'duration':250
						});
						myImg.removeProperties('height', 'width');
						
						var oldY = myImg.getSize().y;
						var oldX = myImg.getSize().x;
						var newY = $(document.body).getSize().y-(imgOffset+10);
						var newX = Math.ceil((newY*oldX)/oldY);
						var imgTop = $(document.body).getScroll().y+Math.ceil(imgOffset/2);
						myImg.setStyles({
							'height': newY,
							'width': newX,
							'margin-top': imgTop
						});
						oldY = myImg.getSize().y;
						oldX = myImg.getSize().x;
						newY = $(document.body).getSize().y-(imgOffset+10);
						newX = Math.ceil((newY*oldX)/oldY);
						
						if(newX > ($(document.body).getSize().x-Math.ceil(imgOffset/2)))
						{
							newX = $(document.body).getSize().x-Math.ceil(imgOffset/2);
							newY = Math.ceil((newX*oldY)/oldX);
							imgTop = Math.ceil(($(document.body).getSize().y-newY)/2)+$(document.body).getScroll().y;
						}
						myImg.inject($('screenholder'));
						resizeImage();
					}
				);
				
			}
		});
	});
}

if($('content'))
{
	$('content').getElements('ul').each(function(myUl){
		var newClear = new Element('div', {'class':'clear'});
		newClear.inject(myUl, 'after');
	});
}

});

window.addEvent('resize', function() {
	resizeImage();
	if($('screen'))
	{
		$('screen').setStyle('height', window.getScrollSize().y);
	}
});

window.addEvent('scroll', function() {
	
resizeImage();
	
if($('sortbar'))
{
	var sortBar = $('sortbar');
	if($(document.body).getScroll().y >= sortTop)
	{
		$('sortbar').tween('top', $(document.body).getScroll().y);
	}
	else
	{
		$('sortbar').tween('top', sortTop);
	}
}

});

window.addEvent('load', function(){
	if(hashed != '')
	{
		if(hashed != '')
		{
			if($(hashed))
			{				
				var blinker = function(){
					$(hashed).setStyle('background-color', '#FDFAEE');
					$(hashed).getNext('.desc').setStyle('background-color', '#FDFAEE');
				
					var tempF = function(){
						var myFx1 = new Fx.Tween($(hashed), {'duration':2000});
						var myFx2 = new Fx.Tween($(hashed).getNext('.desc'), {'duration':2000});
						myFx1.start('background-color', '#ffffff');
						myFx2.start('background-color', '#ffffff');
					}
					tempF.delay(1000);
				}
				blinker.delay(500);
			}
		}
	}
});