var sId = 1, snowBox;
var Snow = function() {
	this.id = sId ++;
	this.type = Math.floor( Math.random() * 8 ) + 1;
	this.direction = 1;
	this.y = (isiPhone()) ? Math.floor( Math.random() * 250 ) : Math.floor( Math.random() * 90 );
	this.x = this.getX(true);

	this.direction = 1;
	snowBox.append("<i id='sId-" + this.id + "' class='star" + this.type + "'></i>");
	this.obj = $("#sId-" + this.id);
	this.width = this.obj.width();
	
	this.obj = $("#sId-" + this.id).css({"top" : this.y, "left" : this.x, "zIndex" : (isiPhone()) ? 6 : Math.floor(Math.random() * 2) + 4, "opacity" : 0});
	
	this.fall(false);
};  

Snow.prototype.fall = function(last) {
	var t = this;
		
	if (!last) {
		this.y += Math.floor( Math.random() * 80 ) + 20;
		this.y = (this.y >= 260 ) ? 260 + Math.floor( Math.random() * 10 ) : this.y;
		this.direction *= -1;
		this.x = this.getX();
		this.obj.animate({
			top: this.y,
			left: this.x,
			opacity: 1
		}, {
    		duration: 3000,
   			specialEasing: {
		    	top: 'linear',
	 	   		left: 'easeInOutQuad'
			}, 
			complete: function() {
				if (t.y <= 200) {
					t.fall();
				}
				else {
					t.fall(true);
				}
			}
		});
	
	}
	else {
		var t = this
		this.y = 270 - Math.floor( Math.random() * 10 );
		this.direction *= -1;
		this.x = this.getX();
		this.obj.animate({
			top: this.y,
			left: this.x,
			opacity: 0
		},
		{
			duration: 3000,
			specialEasing: {
		    	top: 'linear',
	 	   		left: 'easeInOutQuad'
			},  
			complete: function() {
				$("#sId-" + t.id).remove()
			}
		});
	}
};

Snow.prototype.getX = function(first) {
	var R = 155,
		d = 2*R,
		maxX = Math.floor(Math.sqrt(Math.pow(R,2) - Math.pow(R-this.y, 2))),
		//maxX = R - this.y,	
		space = R - maxX + 20,
		//space = R - this.y,
		x, range;

		if (first) {
			range = 320;
			this.x = 0;
		}
		else {
			range = 80;
		}

		x = this.x + this.direction * Math.floor( Math.random() * range );
		
		if ( x < space ) {
			x = space + Math.floor( Math.random() * 10 ) + 10;	
		}
		else if ( x > (d - space)){
			x = d - space - Math.floor( Math.random() * 10 ) - 20;	
			if (!first) x -= this.width
		}
		
		//do {
		//	x = this.x + this.direction * Math.floor( Math.random() * range );
		//	console.log("y:"+this.y,"x:"+x, "maxX:"+maxX, "space:"+space, "2*R-space:"+d)
		//	console.log((x < space) || (x > R - space));
		//} while( (x < space) || (x > (2*R - space)) );
		return x;
		
};
function isiPhone(){
	return (
		(navigator.userAgent.match(/iPhone/i) != null) || (navigator.userAgent.match(/iPod/i) != null || (navigator.userAgent.match(/iPad/i) != null) )
	);
}	

function createSnow () {
	new Snow()	
}
function shakeSnow() {
	for(var i = 1; i <= 80; i++) {
		createSnow();
	}
}	
(function($) {
	$(function() {
		snowBox = $(".starsBox");
		if (!isiPhone()) {
			setInterval(createSnow,100)
			shakeSnow();
			shakeSnow();
		}
		else {
			$(".ballBox__shake").show();
			var myShakeEvent = new Shake();
			myShakeEvent.start();
			myShakeEvent.shakeEventDidOccur = function() {
				shakeSnow();
			}
		}
		
		//createSnow();
		
		var genplan = $("#genplan");
		if (genplan.length > 0) {
		    var so = new SWFObject("swf/map_21.swf", "", "974", "562", "9", "");
			so.addParam("base", "swf");
			so.addParam("wmode", "transparent");
		    so.write("genplan");				
		}		
		
		
	});
})(jQuery);
