function $object(obj){var tmp='';for(var x in obj)tmp+=x+'='+obj[x]+'\n';return alert(tmp)}

var ScaleView=new Class({
	Implements:[Events,Options],
	options:{
		css:'scaler', //the css-class name you want as a base (default: scaler, scaler-nav, scaler-float, scaler-next, scaler-prev)
		footerHeight:70, //the pixelheight of space without the floating navitation at the bottom for menus
		stopOperaScroll:true, //opera really want to scroll, but we don't. Be aware: this stops ALL scrolling with the wheel, also on other elements
		letterboxVerticals:true //Should vertical images be letterboxed with blank space on the sides? false makes them strech to fit window width
	},
	initialize:function(options){
		this.el=document.getElement('.'+this.options.css).getElement('img');
		this.dimentions=$extend(new Image(),{onload:this.scale.bind(this),src:this.el.src});
		this.links=document.getElements('.'+this.options.css+'-nav a');
		this.view=window.getSize();
		this.scale.periodical(300,this);

		window.addEvents({
			keydown:function(e){if(e.code==32||e.code==39||e.code==40)return $type(e.target.value)=='string'},
			mousewheel:$lambda(!(Browser.Engine.presto&&this.options.stopOperaScroll)),
			resize:this.scale.bind(this)
		});
		
		if(this.links.length){
			this.links.each(function(el){new Image().src=el.addEvent('click',this.goto.pass(el,this)).href},this);
			this.float=new Element('a',{href:'javascript:void(0)','class':this.options.css+'-float'}).inject(document.body);
			this.offset=Hash.map(this.float.addEvent('click',this.swap.bind(this)).getSize(),function(v){return v/2});
			this.goto(this.links[0]);
			document.addEvents({
				mousemove:this.mouse.bind(this),
				keydown:this.swap.bind(this)
			});
		}
	},
	scale:function(){
		if(this.el.complete){
			window.scrollTo(0,0);
			var i=this.dimentions,v=this.view;this.view=window.getSize();
			if(this.view.x!=v.x||this.view.y!=v.y||this.el.src!=i.src||i.w!=i.width||i.h!=i.height){
				$extend(this.dimentions,{src:this.el.src,w:i.width,h:i.height});
				var width=Math.round(this.view.y*(i.width/i.height));
				if((i.width>i.height||!this.options.letterboxVerticals)&&this.view.x>width)width='50%';
				this.el.setStyle('width',width);
			}
		}
	},
	goto:function(el){
		this.links.removeClass('cur');
		this.scale(this.el.src=el.addClass('cur').href);
		return false;
	},
	swap:function(e){
		if(!e.key||e.code==39||e.code==37){
			var next=e.key?e.code==39:this.dir=='next',tot=this.links.length;
			var to=this.links.indexOf(this.links.filter('.cur')[0])+(next?1:-1);
			this.goto(this.links[tot==to?0:(to<0?tot-1:to)]);
		}
	},
	mouse:function(e,dir){
		if((this.view.y-e.client.y)>this.options.footerHeight){
			this.float.style.left=e.client.x-this.offset.x+'px';
			this.float.style.top=e.client.y-this.offset.y+'px';
			dir=e.client.x<(this.view.x/2)?'prev':'next';
		}
		if(this.dir!=dir){
			this.float.style.display=dir?'block':'none';
			if(this.dir=dir)this.float.className=this.options.css+'-float '+this.options.css+'-'+dir;
		}
	}
});

window.addEvent('domready',function(){
	new ScaleView();
});
