
FloatBanner = null;

function TFloatBanner(){
	this.bannerFloatStop = false;
	this.deltax = 170;
	this.deltay = 90;

	this.sites = [];
	//this.sites[0] = ['/banners/240x350_plato_rozprodazh.swf', null, 240, 350, false];

	if(this.sites.length == 0) return;
	this.sitebanner = this.sites[Math.round(Math.random() * (this.sites.length - 1))];

	this.width = this.sitebanner[2] || 240;
	this.height = this.sitebanner[3] || 340;
	this.moving = this.sitebanner[4] || false;

	this.init = function(){
		var close_btn = new Element('a', {'class': 'banner_close_button'}).setStyle({cursor: 'pointer'}).update('закрыть');

		if(this.sitebanner[1] === null){
			var float_ban = new SWFObject(this.sitebanner[0], "fbanner", this.width, this.height, "8", "#FFFFFF");
			float_ban.addParam("wmode", "transparent");
			float_ban.write("bloatbanner");
		}
		else{
			var float_banner = new Element('img', {src: this.sitebanner[0], width: this.width, height: this.height}).setStyle({cursor: 'pointer'});
			float_banner.observe('click', this.close.bindAsEventListener(this));
			$('bloatbanner').insert({bottom: float_banner});
		}

		close_btn.observe('click', this.stop.bindAsEventListener(this));
		$('bloatbanner').insert({top: close_btn});

		this.go();
	}

	this.go = function(){
		var left = parseInt((document.viewport.getWidth() - this.width) / 2);
		var top = parseInt((document.viewport.getHeight() - this.height) / 2);

		$('bloatbanner').setStyle({position: 'absolute', left: left + 'px', top: top + 'px'});
		if(this.moving) this.float();
	}

	this.float = function(x){
		if(this.bannerFloatStop == false){
			x = x || 180;

			var left = parseInt((document.viewport.getWidth() - 240) / 2 + (Math.cos((Math.PI * x) / 180) * this.deltax));
			var top = parseInt((document.viewport.getHeight() - 320 - 100) / 2 + (Math.sin((Math.PI * x) / 180) * this.deltay));
			$('bloatbanner').setStyle({left: left + 'px', top: top + 'px'});
	
			this.float.bind(this).delay(.04, x + 1);
		}
	}

	this.stop = function(){
		this.bannerFloatStop = true;
		$('bloatbanner').remove();
	}

	this.close = function(){
		if(this.sitebanner[1].length)
			window.location = this.sitebanner[1];
		else
			$('bloatbanner').remove();
	}

	var loc = document.location.toString();
	if(loc.match(/.+(\/ru\/?|\/ua\/?)$/i) || loc.match(/http:\/\/(www\.)?plato.ua\/?$/i)){
		this.init();
	}
	else{
		$('bloatbanner').remove();
	}
}

//------------------------------------------------------------------------------

function newWindow(url, w, h){
	var x = parseInt((window.screen.width - w) / 2);
	var y = parseInt((window.screen.height - h - 50) / 2);

	window.open(url, "newWindow", "width=" + w + ", height=" + h + ", left=" + x + ", top=" + y + ", scrollbars=0");

	return false;
}

Event.observe(window, 'load', function(){
    var is_active = false;
	
	$$('img[rel="menu"]').each(function(el, i){
		var new_src = el.src.substring(0, el.src.length - 4) + '_a' + el.src.substring(el.src.length - 4);

		new Element('img', {src: new_src});
		el.writeAttribute('oldsrc', el.src);

		el.observe('mouseover', swapHoverImage.bindAsEventListener(null, new_src));
		el.observe('mouseout', swapHoverImage.bindAsEventListener(null, el.readAttribute('oldsrc')));
	});
});

function swapHoverImage(e, src){
	e.element().writeAttribute('src', src);	
	e.stop();
}

function randomBanner(){
	var banners = ['1.gif', '2.gif', '3.gif', '4.gif'];
	document.write('<img src="/banners/' + banners[Math.round(Math.random() * (banners.length - 1))] + '" width="260" height="220">');
}
