/* =====================================================================
*
*    slideshow.js
*
* =================================================================== */
var SEYA_SS = function()
{
	var srcs;

	// スクロールスピードを算出する際の基本となる高さ
	var BASE_HEIGHT = 1000;
	var BASE_SCROLLING_TIME = 15000;
	var FADE_IN_TIME = 500;
	var FADE_OUT_TIME = 2000;
	var IMG_AREA_ID = "SlideShowImage";
	var IMG_CONTAINER_ID = "top-main-image-container";
	var CATCHCOPY_ID = "Catchcopy";
	var CATCHCOPY_LINK_ID = "CatchcopyLink";
	var CATCHCOPY_CONTAINER_ID = "TopCatchcopyContainer";

	var img, imgContainer;
	var catchcopy, chatchcopyLink, catchcopyContainer;
	var playId = 0, catchcopyId = 0;
	var imageContainerSize = 0;
	var targetImage, targetImageSize, halfHeight;

	var init = function()
	{
		$(document).ready(function()
		{
			img = $("#" + IMG_AREA_ID);
			imgContainer = $("#" + IMG_CONTAINER_ID);
			catchcopy = $("#" + CATCHCOPY_ID);
			catchcopyLink = $("#" + CATCHCOPY_LINK_ID);
			catchcopyContainer = $("#" + CATCHCOPY_CONTAINER_ID);

			//jQuery.event.add(window, "load", function(){}
			$(window).load(function()
			{
				targetImage = new Image();
				$(targetImage).bind('load', loadedImage);

				$.ajax({
					type: "GET",
					url: "/conf/seya.xml",
					dataType: "xml",
					success: function(xml) { _parseXML(xml); }
				});
			});

			catchcopyContainer.hide();
		});
	}();

	function _parseXML(xml)
	{
		srcs = [];
		$(xml).find("slide").each(function()
		{
			var obj = {"name": $(this).text(), "src": $(this).attr("url")};
			srcs.push(obj);
		});

		catchcopies = [];
		$(xml).find("catchcopy").each(function()
		{
			var obj = {"catchcopy": $(this).text(), "url": $(this).attr("url"), "linktext": $(this).attr("label")};
			catchcopies.push(obj);
		});

		imageContainerSize = _getContainerSize();
		img.append(targetImage);
		img.css({"opacity":0, "overflow":"hidden", "width" : imageContainerSize.width, "height" : imageContainerSize.height});
		catchcopyContainer.css({opacity:'0'});
		catchcopyContainer.show();

		execute();
	}

	function execute()
	{
		changeImage();
	}

	function changeImage()
	{
		targetImage.alt = srcs[playId].name;
		targetImage.src = srcs[playId].src;
	}

	function loadedImage()
	{
		targetImageSize = { width: targetImage.width, height: targetImage.height };

		displayCatchcopy();
	}

	function displayCatchcopy()
	{
		var currentCatchcopy = catchcopies[catchcopyId];
		catchcopy.text(currentCatchcopy.catchcopy);
		catchcopyLink.text(currentCatchcopy.linktext);
		catchcopyLink.attr("href", currentCatchcopy.url);

		setScrollArea();
	}

	function setScrollArea()
	{
		halfHeight = targetImageSize['height'] - imageContainerSize.height;
		img.animate({scrollTop:halfHeight}, 1, "linear", fi);
	}

	function fi()
	{
		img.animate({opacity:1}, FADE_IN_TIME, "", scrollStart);
	}

	function scrollStart()
	{
		catchcopyContainer.animate({opacity:1}, FADE_IN_TIME);
		var scrollingTime = getScrollingTime();
		img.animate({scrollTop:0}, scrollingTime, "linear", fo);
	}

	function fo()
	{
		catchcopyContainer.animate({opacity:0}, FADE_IN_TIME);
		img.animate({opacity:0}, FADE_OUT_TIME, "linear", nextSlide);
	}

	function nextSlide()
	{
		playId = _getNextImageId();
		catchcopyId = _getNextCatchcopyId();

		execute();
	}

	function getScrollingTime(height)
	{
		return BASE_SCROLLING_TIME * (targetImageSize['height'] / BASE_HEIGHT);
	}

	function getImageTrueSize(image)
	{
		var run, mem, w, h, key = "actual";

		// for Firefox, Safari, Google Chrome
		if ("naturalWidth" in image)
		{
			return { width:  image.naturalWidth,
					 height: image.naturalHeight };
		}

		if ("src" in image)
		{
			if (image[key] && image[key].src === image.src)
			{
				return image[key];
			}

			if (document.uniqueID)
			{
				run = image.runtimeStyle;
				mem = { w: run.width, h: run.height }; // keep runtimeStyle
				run.width  = "auto"; // override
				run.height = "auto";
				w = image.width;
				h = image.height;
				run.width  = mem.w; // restore
				run.height = mem.h;
			}
			else
			{
				mem = { w: image.width, h: image.height }; // keep current style
				image.removeAttribute("width");
				image.removeAttribute("height");
				w = image.width;
				h = image.height;
				image.width  = mem.w; // restore
				image.height = mem.h;
			}

			return image[key] = { width: w, height: h, src: image.src }; // bond
		}

		// HTMLCanvasElement
		return { width: image.width, height: image.height };
	}

	function _getNextImageId()
	{
		var nId = playId + 1;
		return (nId >= srcs.length) ? 0 : nId;
	}

	function _getNextCatchcopyId()
	{
		var nId = catchcopyId + 1;
		return (nId >= catchcopies.length) ? 0 : nId;
	}

	function _getContainerSize()
	{
		var w = imgContainer.width();
		var h = imgContainer.height();

		return {width: w, height: h};
	}
}();

jQuery.extend(jQuery.easing,
{
	easeInQuart: function (x, t, b, c, d)
	{
		return c*(t/=d)*t*t*t + b;
	},
	easeInOutQuint: function (x, t, b, c, d)
	{
		if ((t/=d/2) < 1) return c/2*t*t*t*t*t + b;
		return c/2*((t-=2)*t*t*t*t + 2) + b;
	}
});
