if (!window.console) {
	window.console = { log: function() {} };
}

// workaround for Opera 10.50
if (window.CanvasRenderingContext2D
	&& !CanvasRenderingContext2D.prototype.createImageData
	&& window.ImageData) {	
	CanvasRenderingContext2D.prototype.createImageData = function(w,h) {return new ImageData(w,h) };
}

/**
 * @requires jQuery 1.4.2, jQuery Easing Plugin 1.3, jQuery Blend 2.1
 */
jQuery(function($) {

	$(window).bind("unload", $.noop);

	var time = 500;
	var $container = $("#container");
	var $body = $("#container");
	var anchorDeparture = false;
	var href;
	var target;
	var imageSelector = ".content img:not(.special-transition), .content .img-hover";

	var imageHover = function (i, j) {
		var $image = $(this).find("img").first();
		var width = $image.width();
		var height = $image.height();
		var src = $image.attr("src");
		$(this)
			.css({
				display: "block",
				width: width,
				height: height,
				backgroundImage: "url('" + src + "')",
				backgroundPosition: "0 0",
				backgroundRepeat: "no-repeat",
				textIndent: "-9999px"
			})
			.hover(
				function() {
//					console.log("a");
					$(this).css({
						backgroundPosition: "0 " + (height * -1) + "px"
					});
				},
				function() {
//					console.log("b");
					$(this).css({
						backgroundPosition: "0 0"
					});
				}
			)
			.blend(333)
			.unbind("mouseleave")
		;
		$image.css({display: "none"}).remove();
	};

	/**
	 * 別ページに遷移する直前に呼び出されます
	 */
	var departure = function(callback) {
		var len = $(imageSelector).length;
		$($(imageSelector).get().reverse()).each(function(i, j) {
			var delay = i * parseInt(time / len * 1.5);
			$(this).delay(delay).animate({ opacity: 0 } , time);
		});
		$container.animate({ opacity: 0 }, time, "easeInExpo", callback);
	};

	/**
	 * 別ページから遷移した直後に呼び出されます
	 */
	var arrival = function(callback) {
//		console.log("arrival");
		$body.css({ visibility: "visible", cursor: "progress" });
		if (anchorDeparture) {
			$("html, #container").animate({ scrollTop: 0 }, time, "easeInOutCirc", function() {
				anchorDeparture = false;
			});
		}

		$(".img-hover").each(imageHover).blend();
		$container.css({ opacity: 0 });

		$(window).bind("rendercomplete", function() {
			$body.css({ cursor: "auto" });
			$container.css({ opacity: 0 }).animate({ opacity: 1 }, time  * 2, "easeOutExpo", callback);
			var len = $(imageSelector).length;
			$(imageSelector).each(function(i, j) {
				var delay = i * parseInt(time / len * 2.5);
				var delayOffset = 0;
				$(this)
					.css({ opacity: 0 })
					.delay(delayOffset + delay)
					.animate({ opacity: 1 } , time)
				;
			});
			initRollover($);
		});
	};
	
	$("a[href=#header]").live("click", function(event) {
		$("html, #container").animate({
			scrollTop: 0
		}, time, "easeInOutCirc");
		return false;
	});

	$("a[href=#type1]").live("click", function(event) {
		var x = $("div[id=type1]").offset().top;
		$("html, #container").animate({
			scrollTop: x - 10
		}, time, "easeInOutCirc");
		return false;
	});

	$("a[href=#type2]").live("click", function(event) {
		var x = $("div[id=type2]").offset().top;
		$("html, #container").animate({
			scrollTop: x - 10
		}, time, "easeInOutCirc");
		return false;
	});

	$(".lightBoxClose")
		.live("click", function(event) {
			$.fancybox.close();
			return false;
		})
	;

	$("a").live("click", function(event) {
		anchorDeparture = true;
		target = this.target;
		href = this.href;
		if (target == "_blank") {
			window.open(href, null);
			return false;
		}
		departure(function() {
			document.location = href;
		});
		return false;
	});

	$("a:not(.img-hover) img:not(.no-hover)")
		.live("mouseenter", function(event) {
			$(this).stop()
			//	.animate({ opacity: 0 }, 33)
				.animate({ opacity: 0.4 }, 300)
			;
		})
		.live("mouseout", function(event) {
			$(this).stop()
				.animate({ opacity: 1 }, 333)
			;
		})
	;

	$(".box-hover")
		.live("mouseenter", function(data) {
			$(this).stop()
			//	.animate({ opacity: 0 }, 33)
				.animate({ opacity: 0.4 }, 300)
			;
			
		})
		.live("mouseout", function(data) {
			$(this).stop()
				.animate({ opacity: 1 }, 333)
			;
		})
		.css({ cursor: "pointer" })
		.live("click", function(data) {
			$(this).find("a").click();
		});
	;
	$(".box-hover table")
		.live("mouseenter", function(data) {
			$(this).stop()
			//	.animate({ opacity: 0 }, 33)
				.animate({ opacity: 0.4 }, 300)
			;			
		})
		.live("mouseout", function(data) {
			$(this).stop()
				.animate({ opacity: 1 }, 333)
			;
		})
	;

	$(".box-hover table *")
		.live("mouseenter", function(data) {
			$(this).parents("table").stop()
			//	.animate({ opacity: 0 }, 33)
				.animate({ opacity: 0.4 }, 300)
			;			
		})
		.live("mouseout", function(data) {
			$(this).parents("table").stop()
				.animate({ opacity: 1 }, 333)
			;
		})
	;
	
	arrival($.noop);
});

/**
 * custom effect using canvas element
 * @requires jQuery 1.4.2, jQuery Easing Plugin 1.3
 */
(function() {

	var image;
	var context;
	var bufferElement, bufferContext;
	var canvasWidth, canvasHeight;
	var rendered = [];
	var transferTimerId;

	var vars = $.extend($('<div>')[0], {
		whiteFade: 0,
		customAnimate: true,
		updated: true
	});

	var $_fx_step_default = $.fx.step._default;
	$.fx.step._default = function (fx) {
		if (!fx.elem.customAnimate) return $_fx_step_default(fx);
		fx.elem[fx.prop] = fx.now;
		fx.elem.updated = true;
	};
	
	var render = function(current) {
		var imageData = bufferContext.getImageData(0, 0, canvasWidth, canvasHeight);	
		var data = imageData.data;
		for (var x = 0; x < canvasWidth; x++) {
			for (var y = 0; y < canvasHeight; y++) {
				var index = (x + y * canvasWidth) * 4;
				data[index + 0] = data[index + 0] + parseInt((1 - current) * 0xff);
				data[index + 1] = data[index + 1] + parseInt((1 - current) * 0xff);
				data[index + 2] = data[index + 2] + parseInt((1 - current) * 0xff);
			}
		}
		rendered.push(imageData);
		if (current >= 1) {
			$(window).trigger("rendercomplete");
			setTimeout(transfer, 0);
		}
		function transfer() {
			var imageData = rendered.shift();
			context.putImageData(imageData, 0, 0);
			if (rendered.length < 1) {
				return;
			}
			setTimeout(arguments.callee, 1000 / 15);
		}
	};

	jQuery(function($) {

		if (!document.createElement("canvas").getContext) {
//			console.log("this browser doesn't support canvas element!");
			$(window).trigger("rendercomplete");
			return;
		}
		
		bufferElement = document.createElement("canvas");
		bufferContext = bufferElement.getContext("2d");
		
		var $canvas = $("canvas");
		if ($canvas.length < 1) {
			$(window).trigger("rendercomplete");
			return;
		}
		canvasWidth = $canvas.width();
		canvasHeight = $canvas.height();
		context = $canvas[0].getContext("2d");

		var url = $canvas.find("img").first().attr("src");
		image = document.createElement("img");
		
		$(image).bind("load", function(data, event) {
			var imgWidth = this.width;
			var imgHeight = this.height;
	 
			var image = new Image(imgWidth, imgHeight);
			bufferElement.width = imgWidth;
			bufferElement.height = imgHeight;
		
			image.width = canvasWidth;
			image.height = canvasHeight;
			image.src = this.src;
	 
			bufferContext.drawImage(image, 0, 0, imgWidth, imgHeight, 0, 0, canvasWidth, canvasHeight);

			if (!$.isFunction(bufferContext.getImageData)) {
				context.drawImage(image, 0, 0, imgWidth, imgHeight, 0, 0, canvasWidth, canvasHeight);
				$(window).trigger("rendercomplete");
				return;
			}

			var i = 0;
			var step = 15;
			setTimeout(function() {
				if (i <= step) {
					render($.easing.easeOutExpo(0, i, 0, 1, step - 1));
					i++;
					setTimeout(arguments.callee, 0);
				}
			}, 0);
		});

		image.src = url;
	});
})();
