/*
 * jDropper
 *
 * Date: 2008/08/23
 * @author hisasann
 * @version 0.2
 *
 * Tested with jQuery 1.2.6
 *    On FF 3.0, Opera 9.5 and Safari 3.1 on Mac OS X.
 *       IE6.0, IE7.0, FF 3.0, Opera 9.5 and Safari 3.1 on Windows.
 *
 * Copyright (c) 2008 hisasann (hisasann.com/housetect/)
 * Open source under the BSD License. 
 *
 */
(function($){
	var maxlength = 0;
	var opts = null;

	$.jDropper = function(options){
		opts = options;
		remover(false);

		$("<div id='jdropper-frame'></div>")
			.css({
				overflow: "hidden",
				width: getWindowSize().width + "px",
				height: getWindowSize().height + "px",
				position: "absolute",
				top: getScroll().y + "px",
				left: getScroll().x + "px",
				backgroundColor: "#000000",
				opacity: 0,
				zIndex: 5000
			})
			.appendTo("body");

		$("<div id='jdropper-wrap'></div>")
			.css({
				overflow: "hidden",
				width: getWindowSize().width + "px",
				height: getWindowSize().height + "px",
				position: "absolute",
				top: getScroll().y + "px",
				left: getScroll().x + "px",
				zIndex: 5001,
				cursor: (opts.game ? "pointer" : "default")
			})
			.appendTo("body");

		if((opts.animation == "repeat" || opts.animation == "rain") && !opts.game){
			$("<img src='http://lab.hisasann.com/jdropper/img/stop.png' />")
				.css({
					position: "absolute",
					top: "5px",
					left: $(window).width() - 165 + "px",
					zIndex: 5010,
					cursor: "pointer"
				})
				.click(function() {
					remover(true);
				})
				.appendTo('#jdropper-wrap');
		}

		if(typeof opts.easingRandom == "boolean")
			selectType = easingSelecter();
		else if(typeof opts.easingRandom == "string")
			selectType = opts.easingRandom;
		else
			selectType = "easeOutBounce";

		var dropWidth = (opts.width ? opts.width : 10);
		var posiWidth = 0;
		var winWidth = $(window).width();
		var mathWidth = 0;
		for(var i=0; i<10000; i++){
			mathWidth += dropWidth;
			if(winWidth <= mathWidth){
				maxlength = i;
				break;
			}

			if(opts.images){
				$("<img />")
					.attr("id", "drop"+i)
					.attr('src', imageSelecter())
					.css({
						display: "block",
						float: "left",
						position: "absolute",
						top: "0px",
						left: posiWidth + "px",
						width: (opts.width ? opts.width : 10) + "px",
						height: (opts.height ? opts.height : 40) + "px",
						zIndex: 5002
					})
					.mouseover(function(){
						if(opts.game)
							gameOver();
					})
					.appendTo("#jdropper-wrap");
			}else{
				$("<div></div>")
					.attr("id", "drop"+i)
					.css({
						display: "block",
						float: "left",
						position: "absolute",
						top: "0px",
						left: posiWidth + "px",
						width: (opts.width ? opts.width : 10) + "px",
						height: (opts.height ? opts.height : 40) + "px",
						backgroundColor: colorSelecter(),
						zIndex: 5002
					})
					.mouseover(function(){
						if(opts.game)
							gameOver();
					})
					.appendTo("#jdropper-wrap");
			}

			animeDown("#drop"+i);
			posiWidth += dropWidth;
		}

		if(!opts.repeat){
			var interval = setInterval(function (){
					if(count >= maxlength){
						clearInterval(interval);
						remover(false);
					}
				}
			, 500);
		}
	}
	
	function gameOver(){
		for(var i=0; i<maxlength; i++){
			$("#drop" + i).stop();
		}
		$("#jdropper-wrap").fadeOut("slow", function(){
			$("#jdropper-wrap").remove();
		});

		if(!$("#gameover").get(0)){
			$("<img src='http://lab.hisasann.com/jdropper/img/gameover.png' width='308px' height='120px' />")
				.attr("id", "gameover")
				.css({
					"cursor": "pointer",
					"position": "absolute",
					"opacity": "0",
					"zIndex": 5020
			 	})
				.click(function() {
					$("#jdropper-frame").remove();
					$(this).remove();
					$.jDropper(opts);
				})
				.appendTo("body");
		}
		$("#jdropper-frame")
			.click(function() {
				$("#jdropper-frame").remove();
				$("#gameover").remove();
			});
		var left = Math.floor((getWindowSize().width - $("#gameover").width()) / 2) + getScroll().x;
		var top  = Math.floor((getWindowSize().height - $("#gameover").height()) / 2) + getScroll().y;
		$("#gameover")
			.css({
				"top": top + "px",
				"left": left + "px"
			 })
			.animate({opacity: "1"},{queue: true, duration: (opts.duration ? opts.duration : 3000), easing: "swing", complete: function(){}});
	}

	var selectTpe = null;
	var count = 0;
	function animeDown(objStr){
		var rand = Math.random(5) * 2000;
		setTimeout(function(){
			$(objStr).animate(
				{top: "+=" + getWindowSize().height + "px"},
				{queue: true, duration: (opts.duration ? opts.duration : 3000), easing: selectType, complete: function(){
					if(opts.animation == "rain")
						setTimeout(function(){animeDown2(objStr)}, 10);
					else if(opts.animation == "repeat"){
						animeUp("#" + $(this).get(0).id);
					}
					else{
						count++;
					}
				}}
			);
		}, rand);
	}

	function animeDown2(objStr){
		var rand = Math.random(5) * 2000;
		$(objStr).css("top", 0);
		setTimeout(function(){
			$(objStr).animate(
				{top: "+=" + getWindowSize().height + "px"},
				{queue: true, duration: (opts.duration ? opts.duration : 3000), easing: selectType, complete: function(){
					animeDown2("#" + $(this).get(0).id);
				}}
			);
		}, rand);
	}

	function animeUp(objStr){
		var rand = Math.random(5) * 2000;
		setTimeout(function(){
			$(objStr).animate(
				{top: "-=" + getWindowSize().height + "px"},
				{queue: true, duration: (opts.duration ? opts.duration : 3000), easing: selectType, complete: function(){
					animeDown("#" + $(this).get(0).id);
				}}
			);
		}, rand);
	}
	
	function getWindowSize(){
		return {
			height: (jQuery.browser.opera ? 
					document.documentElement.clientHeight : $(window).height()),
			width: 		(jQuery.browser.opera ? 
							document.documentElement.clientWidth : $(window).width())
		}
	}

	function imageSelecter(){
		var images = opts.images;
		var rand = Math.floor(Math.random() * images.length);
		return (images[rand] == undefined ? images[0] : images[rand]);
	}

	function colorSelecter(){
		var colorType = [
			"#CC42F2",
			"#FF9900",
			"#8F0005",
			"#DCFE00",
			"#80A785",
			"#D90000",
			"#5C3C13",
			"#271A19",
			"#FFFF33",
			"#847281",
			"#88AE99",
			"#FF2462",
			"#4EEA62",
			"#DBEDE5",
			"#FDBEA6",
			"#F62F80",
			"#6100FE",
			"#33FF66",
			"#E747BF"
		];
		var rand = Math.floor(Math.random() * colorType.length);
		return (colorType[rand] == undefined ? colorType[0] : colorType[rand]);
	}

	function easingSelecter(){
		var animeType = [
			"easeInQuad",
			"easeOutQuad",
			"easeInOutQuad",
			"easeInCubic",
			"easeOutCubic",
			"easeInOutCubic",
			"easeInQuart",
			"easeOutQuart",
			"easeInOutQuart",
			"easeInQuint",
			"easeOutCubic",
			"easeOutQuint",
			"easeInOutQuint",
			"easeInSine",
			"easeOutSine",
			"easeInOutSine",
			"easeInExpo",
			"easeOutExpo",
			"easeInOutExpo",
			"easeInCirc",
			"easeOutCirc",
			"easeInOutCirc",
			"easeInElastic",
			"easeOutElastic",
			"easeInOutElastic",
			"easeInBack",
			"easeOutBack",
			"easeInOutBack",
			"easeInBounce",
			"easeOutBounce",
			"easeInOutBounce"
		];
		var rand = Math.floor(Math.random() * animeType.length);
		return (animeType[rand] == undefined ? animeType[0] : animeType[rand]);
	}
	
	function remover(isAnimate) {
		if(isAnimate){
			for(var i=0; i<maxlength; i++){
				$("#drop" + i).stop();
			}
			$("#jdropper-wrap").fadeOut("slow", function(){
				$("#jdropper-wrap").remove();
				$("#jdropper-frame").remove();
			});
		}else{
			for(var i=0; i<maxlength; i++){
				$("#drop" + i).stop();
			}
			$("#jdropper-wrap").remove();
			$("#jdropper-frame").remove();
		}
	}

	function getScroll(){
	   return {
	      x: document.body.scrollLeft || document.documentElement.scrollLeft,
	      y: document.body.scrollTop  || document.documentElement.scrollTop
	   };
	};
})(jQuery);
