window.addEvent('domready', preload);
var StateManager = EXANIMO.managers.StateManager;
StateManager.initialize();
StateManager.defaultStateID='home';
StateManager.onstatechange = function(e){
if(e.id == 'home'){loadhome();return false;}
if(e.id == 'about'){loadabout();return false;}
else{getimg(e.id);return false;}
}
function preload(){
centersize();
loadhome();
	window.addEvent('resize',centersize);
	$('journalspace').tween('height',250);
	$('paintingspace').tween('height',236);

	
	$$('.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',236);	
				}
			
				
			});
		});
	});
	load();

}
function load(){
journalcall();
thumbscall('sketches');
thumbscall('print');
thumbscall('painting');
thumbscall('sculpture');
thumbscall('illust');
thumbscall('design');
}
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');
		});
	}});
journalajax.send();
}
function loadhome(){
var home = new Element('div',{'id':'home','class':'paper'});
var foot =  new Element('div',{'class':'paperfoot'});
$('content').adopt(home);
$('content').getLast().load('home.html').set('tween', {transition:'quad:out',duration:1000,onComplete:function(){$('content').getFirst().dispose();
}}).tween('top',0).adopt(foot);
}

function loadabout(){
var about = new Element('div',{'id':'about','class':'paper'});
var foot =  new Element('div',{'class':'paperfoot'});
$('content').adopt(about);
$('content').getLast().load('about.html').set('tween', {transition:'quad:out',duration:1000,onComplete:function(){$('content').getFirst().dispose();
}}).tween('top',0).adopt(foot);
}
function centersize(){
//var ws = window.getSize().y
//$('center').tween('height',ws+'px');
}

function thumbscall(type){
function hazefade(e){e.tween('opacity',0);}
function hazeunfade(e){e.tween('opacity',1);}
var thumbajax = new Request({url:'getthumbs.php', onComplete:function(response){$(type+'space').set('html',response);$$('.haze').each(function(h){h.set('tween',{duration:200});h.addEvents({'mouseenter':hazefade.pass(h),'mouseleave':hazeunfade.pass(h)})});},data:'type='+type});
thumbajax.send();
}


var i=0;
function getimg(number){
i++;
var imgajax = new Request({url:'getimage.php',data:'number='+number,method:'post', onComplete: function(response){
var paper = new Element('div',{'id':'paper'+i,'html':response,'class':'paper'});
var foot =  new Element('div',{'class':'paperfoot'});
paper.injectInside('content');
$('paper'+i).set('tween', {transition:'quad:out',duration:1000,onComplete:function(){$('content').getFirst().dispose();}}).tween('top',0).adopt(foot);
}})
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});
}
function mdmail(){
var mail = $('mailform').toQueryString();
var mailajax = new Request({url:'mail.php',data:mail,onComplete:function(response){alert(response);}});
mailajax.send();
}