function HalloweenController() {
	var self = this;
	this.num=5;    //Number of bats
	this.y = 0;
	this.x = 0;
	this.s0 = 0;
	this.s1 = 0.5;
	this.del = 0.1;
	this.y1=new Array();
	this.x1=new Array();
	this.y2=new Array();
	this.x2=new Array();

	this.init = function(){
		for (i=0; i < self.num; i++){
			self.y1[i]=0;
			self.x1[i]=4;
			self.y2[i]=0;
			self.x2[i]=0;
		}

		//var width = ($(window).ie6)?$(window).getScrollWidth()-25:$(window).getScrollWidth();


		$('batsFrame').setStyles({
			'position':'absolute',
			'top':'0px',
			'left':'0px',
			'width':0,
			'height':0,
			'z-index':'99'
		});


		var html = "";
		for (i=0; i < self.num; i++){
			html = html + '<img id= "bats' + i + '" src="' + mod_yooholiday_path + 'halloween/image/bat.gif" />';
		}

		$('batsFrame').setHTML(html);
		for (i=0; i < self.num; i++){
			$('bats' + i).setStyles({
				'position':'absolute',
				'top':'-50px',
				'left':'0px',
				'width':'48px',
				'height':'48px',
				'z-index':'100'
			});
		}

		if($(window).ie){
			$(document.body).addEvent('mousemove', function(e){
				setXYCoords(e, $(window).getHeight());
			});
		}else if($(window).opera){
			$(window).addEvent('mousemove', function(e){
				setXYCoords(e, $(window).innerHeight);
			});
		}else{
			$(window).addEvent('mousemove', function(e){
				setXYCoords(e, $(window).getHeight());
			});
		}

		var timer = follow.periodical(100);
	}

	function setXYCoords(e, height){
		var currentX = e.clientX + $(window).getScrollLeft();
		var currentY = e.clientY + $(window).getScrollTop();
		var borderX = $(window).getWidth() + $(window).getScrollLeft()-($(window).getWidth()/14) - 48;
		var borderY = height + $(window).getScrollTop()-(height/6) - 48;
		self.x = (currentX > borderX)?borderX:currentX;
		self.y = (currentY > borderY)?borderY:currentY;
	}

	function swirl(){

		var yb = $(window).getHeight()/6;
		var xb = $(window).getWidth()/14;

		if($(window).opera)
			yb = $(window).innerHeight/6;

		for (i=0; i < self.num; i++){
			$('bats'+i).setStyles({
				 'top': self.y1[i] + yb * Math.sin(((self.s0) + i * 3.7) / 4) * Math.cos((self.s0 + i * 35) / 20),
				 'left': self.x1[i] + xb * Math.cos(((self.s0) + i * 3.7) / 4) * Math.cos((self.s0 + i *35) / 62)
			 });

		 }
		self.s0 += self.s1;
	}

	var follow = function(){
		self.y1[0]=Math.round(self.y2[0]+=((self.y)-self.y2[0])*self.del);
		self.x1[0]=Math.round(self.x2[0]+=((self.x)-self.x2[0])*self.del);
		for (i=1; i < self.num; i++){
			self.y1[i]=Math.round(self.y2[i]+=(self.y1[i-1]-self.y2[i])*self.del);
			self.x1[i]=Math.round(self.x2[i]+=(self.x1[i-1]-self.x2[i])*self.del);
		}
		swirl();
	}
}


var hc = new HalloweenController();

window.addEvent('domready', function(){
	hc.init();
});