/*
 * (c) SOGDesign 2009
 */
var MovingBox = new Class({
	Implements: [Options],
	options: {
		maxMove: 50,
		startPos: 500,
		inertia: 500,
		transition: Fx.Transitions.Quint.easeOut,
		pollingInterval: 100
	},
	initialize: function(movingElement, boundingElement, mouseEventElement, options) {
		this.movingElement = $(movingElement);
		this.boundingElement = $(boundingElement);
		this.mouseEventElement = $(mouseEventElement);
		this.setOptions(options);
		this.mouseX = 0;
		
		this.movingElement.set('tween', {duration: this.options.inertia, link: 'cancel', transition: this.options.transition});
		
		this.mouseEventElement.addEvent('mousemove', function(e) {
			this.mouseX = e.client.x;
		}.bindWithEvent(this));
		
		this.moveTimer = this.move.periodical(this.options.pollingInterval, this);
	},
	
	move: function() {
		var innerWidth = this.movingElement.getStyle('width').toInt();
		var outerWidth = this.boundingElement.offsetWidth.toInt();
		
		var curPos =  this.movingElement.getStyle('left').toInt();
		var newPos = this.options.startPos + outerWidth / (outerWidth / this.options.maxMove) -
			(this.mouseX / (outerWidth / this.options.maxMove) + (outerWidth - innerWidth) / 2);
		
		this.movingElement.tween('left', curPos, newPos);
	},
	
	stop: function() {
		$clear(this.moveTimer);
		this.mouseEventElement.removeEvents();
	},
	
	start: function() {
		this.stop();
		this.moveTimer = this.move.periodical(this.options.pollingInterval, this);
	}
});

window.addEvent('domready', function() {
    var boxes = $$('.generalMovingBox');
	var curtains = $$('.curtain');
    if(boxes.length > 0 && $('center') != null) {
		
        
    		moxingBox = new MovingBox(boxes[0], $('center'), $$('html')[0], {startPos: 850 - $(boxes[0]).getStyle('width').toInt()});
		
    }
    if(curtains.length > 0 && $('center') != null) {
        moxingBox1 = new MovingBox(curtains[0], $('curtain'), $$('html')[0], {startPos: 100,maxMove:250,inertia:2000});
		moxingBox2 = new MovingBox(curtains[1], $('curtain'), $$('html')[0], {startPos: 650,maxMove:-250,inertia:2000});
    }
});