var StateManager = EXANIMO.managers.StateManager;
StateManager.defaultStateID='home';
StateManager.onstatechange = function(e)
{
var target = e.id;
getimg(target);
}
function preload(){
StateManager.initialize();
var state = document.location.href.split('#')[1] || StateManager.defaultStateID;
if (state == '' || state == 'home'){
	var newest = 9;
	getimg(newest);}
	$('journalspace').tween('height',250);
	$$('.lnav').each(function(e){
		e.addEvent('click',function(){
			$$('.lnav').each(function(n){
				if(n.id != e.id){
					n.getNext().set('tween',{wait:false}).tween('height',0);
					e.getNext().set('tween',{wait:false}).tween('height',250);	
				}
			
				
			});
		});
	});
		$$('.rnav').each(function(e){
		e.addEvent('click',function(){
			$$('.rnav').each(function(n){
				if(n.id != e.id){
					n.getNext().set('tween',{wait:false}).tween('height',0);
					e.getNext().set('tween',{wait:false}).tween('height',250);	
				}
			
				
			});
		});
	});
	load();
}
function load(){
journalcall();
thumbscall('sketches');
thumbscall('print');
thumbscall('painting');
thumbscall('papers');
/*thumbscall('sculpture');*/
thumbscall('illust');
scrollspace($('aboutspace'));
}
function journalcall(){
var journalajax = new Request({url:'getjournal.php',onComplete:function(response){
	$('journalspace').set('html',response);
	$('journalspace').getElements('a').each(function(e){
		e.setProperty('target','about.blank');
		});
	scrollspace($('journalspace'));
	}});
journalajax.send();
}
function scrollspace(space){
var scroller = new Element('div',{'class':'scroller'});
var scrollbar = new Element('div',{'class':'scrollbar'});
var scrollbay = new Element('div',{'class':'scrollbay'});
scrollbay.clone().adopt(space.getChildren()).inject(space);
var size = space.getFirst().getFirst().getSize().y;
var spacesize = 250;
if(size<spacesize)size = spacesize;
var ratio = spacesize/size;
var scrollratio = (size-spacesize)/100;
space.adopt(scrollbar).scrollTo(0,0);
scroller.inject(space.getLast()).set({'styles':{'height':ratio*spacesize}});
if(size>spacesize){
	new Slider(space.getLast(),space.getLast().getFirst(),{'mode':'vertical','onChange':function(pos){space.getFirst().scrollTo(0,pos*scrollratio)}}).set(0);
}
}
function thumbscall(type){
var space = $(type+'space');
var thumbajax = new Request({url:'getthumbs.php', onComplete:function(response){
space.set('html',response);
if($('home') && type == 'painting'){$(space).getElement('.thumb').fireEvent('click');}
scrollspace(space);
},data:'type='+type});
thumbajax.send();
}
function getimg(number){
	var query = 'getimage.php';
	var paper = new Element('div',{'class':'paper','id':'paper'});
	var temp = new Element('div',{'id':'temp'});
	temp.inject($('content'),'after').adopt($('content').getChildren()).set(
		'tween',{
			onComplete:function(){
				window.scrollTo(0,0);
				$('temp').dispose();	
			}
		}).tween('top',-2000);
	var imgajax = new Request({
		url:query,
		data:'number='+number,
		method:'post',
		onComplete: function(response){
			paper.inject('content').set({'html':response});
			if($('i').complete){$('paper').tween('top',0);}
			else{$('i').addEvent('load',function(){$('paper').tween('top',0);});}
			if($('type')){
				var type = $('type').get('class');
				$(type).fireEvent('click');
			}
			else{
				$('painting').fireEvent('click');
			}
		}
	});
	imgajax.send();
}
function closeit(){
$('dark').dispose();
$('iph').dispose();
$('il').dispose();
}
function pop(img){
var ilw=$('i').getProperty('pw');
var ilh=$('i').getProperty('ph');
var i = $('i').getCoordinates(document.body);
var darken = new Element('div',{'id':'dark'});
document.body.adopt(darken);
var idiv = new Element('div',{'id':'idiv'});
var iph = new Element('img',{'id':'iph','src':'images/mdupload/small/'+img});
var il = new Element('img',{'id':'il','src':'images/mdupload/large/'+img})
$('dark').addEvent('click',closeit).set('opacity',0).setStyle('background-image','url(images/black.png)').tween('opacity',1);
document.body.adopt(iph).adopt(il);
$('iph').setStyles({'top':i.top,'left':i.left}).addEvent('click',closeit);
$('il').setStyles({'top':i.top,'left':i.left}).addEvent('click',closeit);
var isize = $('iph').getSize();
var wsize = window.getSize();
var mx = 20;
if (wsize.x>ilw){mx = ((wsize.x-ilw)/2)};
var imorph = new Fx.Morph('iph',{transition:'quad:in:out',duration:1000, onComplete:function(){$('il').setStyles({'left':mx,'visibility':'visible'});}});
imorph.start({'width':ilw,'height':ilh,'left':mx});
}
window.addEvent('domready', preload);
