// JavaScript Document
// Par Class, parralax layering
// Build by Nils Corven from E-Mark

var Par = new Class({
	initialize: function (element) {
		this.element = element;
		this.items = this.element.getChildren();
		this.length = this.items.length;
		this.is_chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
		
		this.center = this.old = this.point = {
			x: this.element.getSize().x / 2,
			y: this.element.getSize().y / 2
		};
		
		this.draw.periodical(1000 / 24, this);
	},
	
	draw: function () {
		if(this.old.x == this.point.x && this.old.y == this.point.y) return;
		
		this.old = this.point;
		
		//var myX = Math.round(-1*(this.point.x-450)/60);
		//var myY = Math.round(-1*((event.page.y - pos.y)-300)/90);
		//Swiff.remote(myFlash, 'setParralaxToFlash', myX, 0);
			
		var lengths = {
			x: this.center.x - this.point.x,
			y: this.center.y - this.point.y
		};
		var angle = Math.atan2(lengths.y, lengths.x);
		var step = {
			x: Math.cos(angle) * (lengths.y / Math.sin(angle) / this.length) * 0.1,
			y: Math.sin(angle) * (lengths.y / Math.sin(angle) / this.length) * 0.05
		};
		this.items.each(function (item, index) {
			if((item.id != "flash_menu") && (item.id != "site_content") && (item.id != "supermarkets")) {
				if(this.is_chrome) {
					item.setStyles({
						left: 450 + (step.x * (index+1)) - item.getSize().x / 2,
						top: 300 + (step.y * (index+1)) - item.getSize().y / 2
					});
				} else {
					item.setStyles({
						left: this.center.x + (step.x * (index+1)) - item.getSize().x / 2,
						top: this.center.y + (step.y * (index+1)) - item.getSize().y / 2
					});
				}
			}
		}.bind(this));
	}
});
