/**
* class Controller
* 
*/
var Controller = Class.create({
	initialize: function(parent, w, h) {
	
		this.id = '' + new Date().getTime() + Math.round(Math.random()*1000);
		this.parent = parent;
		this.width = w;
		this.height = h;
		
		this.wrapper = this.parent.appendChild($(Builder.node('div',{id:'cw_' + this.id}))); // to position absolute
		this.wrapper.absolutize();
		this.wrapper.setStyle({
			'left':'0px',
			'top':'0px',
			'width':w + 'px',
			'height':h + 'px',
			'overflow':'hidden'
		});
		this.innerwrapper = this.wrapper.appendChild($(Builder.node('div',{id:'ciw_' + this.id}))); // to position absolute
		this.innerwrapper.absolutize();
		this.innerwrapper.setStyle({
			'left':'0px',
			'top':'0px',
			'height':h + 'px'
		});
		this.incrementalWidth = 0;
		this.incrementalHeight = 0;
		this.duration = 1;
		this.buttons = $A();
	},
	
	moveLeft: function() {
		new Effect.Move(this.innerwrapper, {
			x:(((this.innerwrapper.positionedOffset().left-100)<(this.width-this.incrementalWidth))?(this.width-this.incrementalWidth):(this.innerwrapper.positionedOffset().left-100)),
			y:0,
			mode:'absolute',
			queue:{
				position:'end',
				scope:this.id,
				limit: 2
			}
		});
	},
	
	moveRight: function() {
		new Effect.Move(this.innerwrapper, {
			x:(((this.innerwrapper.positionedOffset().left+100)>0)?0:(this.innerwrapper.positionedOffset().left+100)),
			y:0,
			mode:'absolute',
			queue:{
				position:'end',
				scope:this.id,
				limit: 2
			}
		});
	},
	
	addItem: function(src, callback) {
		this.buttons.push(new Button(this.innerwrapper, src, callback));
		Event.observe(this.buttons.last().image, "load", this.arrangeItems.bind(this));
	},
	
	arrangeItems: function() {
		this.incrementalWidth = 0;
		this.buttons.each(function(s) {
			s.setSize(this.width,this.height);
			s.setPosition(this.incrementalWidth,0);
			this.incrementalWidth += s.getWidth()+10;
			this.innerwrapper.setStyle({
				'width':(this.incrementalWidth) + 'px',
				'height':this.height
			});
		}.bind(this));
		this.incrementalWidth -= 10;
	},
	
	setDuration: function(duration) {
		this.duration = duration;
	},

	setSize: function(w, h) {
		// find smallest factor
		var factW = w/this.image.getWidth();
		var factH = h/this.image.getHeight();
		var factor = factW>factH?factH:factW;
		if (factor<1) {
			this.image.setStyle({
				'width': (this.image.width * factor) + 'px',
				'height': (this.image.height * factor) + 'px'});
		}
	},
	
	getWidth: function() {
		return this.width;
	},
	
	getHeight: function() {
		return this.height;
	},
	
	setPosition: function(x, y) {
		this.wrapper.setStyle({
			'left':x + 'px',
			'top':y + 'px'
		});
	}	
});








