function $object(obj){var tmp='';for(var x in obj)tmp+=x+'='+obj[x]+'\n';return alert(tmp)}
function $E(tag,el){return $(el||document).getElement(tag)}

window.addEvents({
	domready:function(){
		db=$E('body');
		if(!Cookie.read('full')){
			var w=screen.availWidth,h=screen.availHeight,nh=Math.round((w/4)*3),nw=Math.round((h/3)*4);
			if(nh>h)w=nw;
			else if(nw>w)h=nh;
	
			if(document.all)window.resizeTo(w,h);
			else if(window.outerHeight<h||window.outerWidth<w){window.outerHeight=h;window.outerWidth=w}

			try{
				var c=Math.round((screen.availWidth-w)/2);
				window.moveTo(0,c);
				window.screenY=window.screenTop=0;
				window.screenX=window.screenLeft=c;
			}catch(e){}
			Cookie.write('full','true');
		}
		if(Layout.bg=$E('img#cont')){Layout.fix();setInterval(Layout.fix,300)}
		if(Layout.menu=$('menu')){
			Layout.menu.set('opacity',0).set('tween',{property:'opacity',duration:200});
			db.setStyle('cursor','pointer').oncontextmenu=function(e){return !!e.target.tagName.match(/(input|textarea)/gi)};
			if(Layout.form().photos=$E('ol'))Layout.gallery();
		}
		db.addEvents({
			keyup:function(e){$try(function(){db.scrollTo(0,0)})},
			click:function(e){
				var e=new Event(e),el=Layout.menu,trg=$(e.target),tag=trg.get('tag');
				if(el.style.opacity>0)return el.set('opacity',0);
				if(tag!='a'&&tag!='textarea'&&tag!='input'&&!trg.getParent('a')){
					var win=window.getSize(),siz=el.getSize();
					el.setStyles({
						left:(e.page.x+siz.x)>win.x?win.x-siz.x:e.page.x-20,
						top:(e.page.y+siz.y)>win.y?win.y-siz.y:e.page.y-20
					}).tween('opacity',0.7);
				}
			}		
		});
		window.addEvent('scroll',function(){db.scrollTo(0,0)});		
	}
});

Layout={
	doc:{x:0,y:0},
	fix:function(){
		if(Layout.bg.complete&&(db.offsetWidth!=Layout.doc.x||db.offsetHeight!=Layout.doc.y||Layout.fix.src!=Layout.bg.src)){
			var bg=Layout.bg.getSize();
			Layout.doc={x:db.offsetWidth,y:db.offsetHeight};
			Layout.fix.src==Layout.bg.src||$extend(Layout.fix,{p:bg.x/bg.y,src:Layout.bg.src});
			Layout.bg.style.width=((bg.y=Layout.doc.x>(bg.x=Math.round(Layout.doc.y*Layout.fix.p)))?Layout.doc.x:bg.x)+'px';
			Layout.bg.style.minWidth=Layout.bg.style.minHeight=0;
		}
	},
	gallery:function(e){
		Layout.photos.addEvent('click',Layout.swap);
		Layout.float=$('float').addEvent('click',Layout.act);
		document.addEvents({mousemove:Layout.mouse,keydown:Layout.act});
	},
	mouse:function(e){
		var w=db.offsetWidth/3,x=e.client.x,y=db.offsetHeight-e.client.y,el=Layout.float,m=Layout.menu;
		if(m.style.opacity>0){
			var t=m.style.top.toInt(),b=t+m.offsetHeight,l=m.style.left.toInt(),r=l+m.offsetWidth,my=e.client.y;
			if(my>t&&my<b&&x>l&&x<r)return el.className=el.style.display='none';
		}
		if(y>50&&(x<w||x>(w+w))){
			if(x<w){if(el.className!='prv'){el.className='prv';el.style.display='block'}}
			else if(el.className!='nxt'){el.className='nxt';el.style.display='block'}
			with(el.style){left=x-15+'px';top=e.client.y-8+'px'}
		}
		else el.className=el.style.display='none';
	},
	act:function(e){if(!e.key||(e.code==37||e.code==39))Layout.swap(e.stop().key?e.code==39:Layout.float.className=='nxt')},
	swap:function(e,cur){
		(cur=$E('.cur',Layout.photos).removeClass('cur')).getFirst().blur();
		if(this==Layout.photos)el=e.stop().target.getElement('img').getParent('li');
		else (el=cur[e?'getNext':'getPrevious']()||Layout.photos[e?'getFirst':'getLast']()).getFirst().focus();
		if($('menu').style.opacity>0)$('menu').set('opacity',0);
		if($('text'))$('text').set('html',el.getElement('span').innerHTML);
		Layout.fix(Layout.bg.src=el.addClass('cur').getElement('img').src);
	},
	form:function(){
		$$('input[type="text"],textarea').forEach(function(el){
			el.set('pre_value',el.value).addEvents({
				'focus':function(){if(this.setStyle('background','').value==this.get('pre_value'))this.value=''},
				'blur':function(){if(!this.value)this.value=this.get('pre_value')}
			});
		});
		$$('form').addEvent('submit',function(e,fail){
			this.getElements('input[type="text"],textarea').forEach(function(el){
				if(!el.value||el.value==el.get('pre_value'))fail=el.setStyle('background','#633');
				else el.setStyle('background','');
			});
			return !fail;
		});
		return Layout;
	}
}