
var PATH = '/pool-gallery/';

function Box() {
	this.w = 180;
	this.h = 140;
	this.x = 10;
	this.y = $(window).height() - this.h - 50; // csak az alsó sávban mozoghato

	this.direction = 0;
	this.speed = 100;

	this.collisionDistance = 10;

	this.el = $('<div><div id="pool-gallery-flash"></div></div>').css({
		position: 'absolute',
		width: this.w,
		height: this.h,
		'z-index': 999999
	});

	$('body').append(this.el);
	
	this.el.click(function() {
		console.log('click');
	});

	this.moveTo = function(x, y) {
		this.x = x;
		this.y = y;

		return this.el.css({left: this.x, top: this.y});
	};

	this.moveBy = function(x, y) {
		this.x += x;
		this.y += y;

		return this.el.css({left: this.x, top: this.y});
	};

	this.collision = function() {
		sw = $(window).width();
		sh = $(window).height();

		x = this.x - $(document).scrollLeft();
		y = this.y - $(document).scrollTop();

		// nincs véletlenszerűség mert mert akkor kieesne az alsó sávból
		// rand = Math.round(Math.random() * 60 - 30);
		rand = 0;

		if ((d = y - this.collisionDistance) < 0) {
			this.direction = -this.direction + rand;
			this.moveBy(0, -d);
		}

		else if ((d = (sw - this.collisionDistance) - (x + this.w)) < 0) {
			this.direction = 180 - this.direction + rand;
			this.moveBy(d, 0);
		}

		else if ((d = (sh - this.collisionDistance) - (y + this.h)) < 0) {
			this.direction = -this.direction + rand;
			this.moveBy(0, d);
		}

		else if ((d = x - this.collisionDistance) < 0) {
			this.direction = 180 - this.direction + rand;
			this.moveBy(-d, 0);
		}
	};

	this.step = function(interval) {
		this.collision();

		factor = this.speed / (1000 / interval);
		dx = Math.cos(this.direction * (Math.PI / 180)) * factor;
		dy = Math.sin(this.direction * (Math.PI / 180)) * factor;

		this.moveBy(dx, dy);
	};

	this.moveTo(this.x, this.y);
}

$(document).ready(function() {
	if ( /kilépés/i.test($('#col1').text()) )
		return;
	
	var interval = 20,
		box = new Box(),

		scrollLeft = 0,
		scrollTop = 0;

	$(document).scroll(function() {
		sl = $(document).scrollLeft();
		st = $(document).scrollTop();

		box.moveBy(sl - scrollLeft, st - scrollTop);

		scrollLeft = sl;
		scrollTop = st;
	});

	swfobject.embedSWF(
		PATH + 'cu3er.swf',
		'pool-gallery-flash',
		box.w,
		box.h,
		'10.0.0',
		'expressinstall.swf',
		{ // flash variables
//			xml: PATH + 'config.xml.php?' + Math.random()
			xml: PATH + 'hirdetes.xml'
		},
		{ // flash object params
			wmode: 'transparent'
		},
		{ // html attributes
			id: 'cu3er',
			name: 'cu3er'
		}
	);

	setInterval(function() {
		box.step(interval);
	}, interval);
});

