/* ============================================================
新着情報
============================================================ */
$(document).ready(function() {
	jQuery.extend( jQuery.easing,
	{
		easeInQuad: function (x, t, b, c, d) {
			return c*(t/=d)*t + b;
		},
		easeOutQuad: function (x, t, b, c, d) {
			return -c *(t/=d)*(t-2) + b;
		},
		easeInOutQuad: function (x, t, b, c, d) {
			if ((t/=d/2) < 1) return c/2*t*t + b;
			return -c/2 * ((--t)*(t-2) - 1) + b;
		},
		easeInExpo: function (x, t, b, c, d) {
			return (t==0) ? b : c * Math.pow(2, 10 * (t/d - 1)) + b;
		},
		easeOutExpo: function (x, t, b, c, d) {
			return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b;
		},
		easeInOutExpo: function (x, t, b, c, d) {
			if (t==0) return b;
			if (t==d) return b+c;
			if ((t/=d/2) < 1) return c/2 * Math.pow(2, 10 * (t - 1)) + b;
			return c/2 * (-Math.pow(2, -10 * --t) + 2) + b;
		}
	});

	var domIndexInfo = "#index_information";
	var domIndexInfoTab = ".index_tab_select li";
	var domIndexInfoDetail = "#index_tab_detail";
	var classTabActive = "index_tab_act";
	var urlRSS = "/rss/top.xml";
	var autoRepeat = {
		counter: 8000,
		timer: null
	};
	var infoBlock, infoTabs, infoContent, categoryLabels, articleList = {}, current, next, statusTabActive = -1;
	var categories = {
		news: {
			name: "サイト更新情報",
			format: function(article) { 
				return "<div class='index_detail_news'><ul><li><span class='index_tab_date'>" + formatDate(article.pubDate) + "</span><p><span class='link02'><a href='" + article.link + "' target='" + article.target + "'>" + article.title + "</a></span></p></li></ul></div>";
			}
		},
		campaign: {
			name: "キャンペーン情報",
			format: function(article) {
				return "<div class='index_detail_camp'><p class='link02'><a href='" + article.link + "' target='" + article.target + "'>" + article.title + "</a></p></div>"
			}
		},
		new_production: {
			name: "新商品情報",
			format: function(article) {
				return "<div class='index_detail_newprod'><p class='link02'><a href='" + article.link + "' target='" + article.target + "'>" + article.title + "</a><span>" + formatDate(article.pubDate) + "発売</span></p></div>"
			}
		},
		cm: {
			name: "CM・パブリシティ情報（CM）",
			format: function(article) {
				return "<div class='index_detail_cm'><p class='link02'><a href='" + article.link + "' target='" + article.target + "'>" + article.title + "</a><span>公開日： " + formatDate(article.pubDate) + "</span></p></div>"
			}
		},
		publicity: {
			name: "CM・パブリシティ情報（雑誌広告）",
			format: function(article) {
				return "<div class='index_detail_cm'><p class='link02'><a href='" + article.link + "' target='" + article.target + "'>" + article.title + "</a></p></div>"
			}
		},
		info: {
			name: "メナードからのお知らせ",
			format: function(article) {
				return "<div class='index_detail_info'><p class='link02'><a href='" + article.link + "' target='" + article.target + "'>" + article.title + "</a></p></div>"
			}
		}
	};

	function selectTab(label) {
		var categoryIndex = 0;
		for (var i = 0 ; i < categoryLabels.length ; i++) {
			if (label == categoryLabels[i]) {
				categoryIndex = i;
			}
		}
		infoTabs.each(function(i, v) {
			if (i == categoryIndex) {
				$(this).addClass(classTabActive);
			} else {
				$(this).removeClass(classTabActive);
			}
		});
	}

	function restartAutoRepeat(label) {
		clearTimeout(autoRepeat.timer);
		setArticles(label, 0);
	}

	function formatDate(timeGMT) {
		var date = new Date(timeGMT);
		var year = date.getYear();
		year += (year >= 1900 ? 0 : 1900);
		var month = date.getMonth() + 1;
		var day = date.getDate();
		return formattedDate = year + "年" + month + "月" + day + "日";
	}

	function setArticles(category, index) {
		$(domIndexInfoDetail).find(".current").removeClass("current").addClass("next");
		current.category = next.category;
		current.index = next.index;
		next.category = category;
		next.index = index;

		var nextArticle = articleList[next.category][next.index];
		$( generateArticle(nextArticle) ).appendTo(domIndexInfoDetail).addClass("current").css({
			"margin-left": 72,
			"opacity": 0
		}).delay(800).animate({
			"margin-left": 42,
			"opacity": 1
		}, {
			duration: 1500,
			easing: "easeOutExpo"
		});
		$(domIndexInfoDetail).find(".next").stop().	css({
			position: "absolute",
			top: "12px",
			left: 0
		}).animate({
			"margin-left": 20,
			"opacity": 0
		}, {
			duration: 400,
			easing: "easeOutQuad",
			complete: function() {
				$(this).remove();
			}
		});
		autoRepeat.timer = setTimeout(function() {
			var nextIndex = getNextArticleIndex();
			setArticles(nextIndex.category, nextIndex.index);
		}, autoRepeat.counter);
	}

	function generateArticle(article) {
		if (article.category == categories.news.name) {
			return categories.news.format(article);
		}
		if (article.category == categories.campaign.name) {
			return categories.campaign.format(article);
		}
		if (article.category == categories.new_production.name) {
			return categories.new_production.format(article);
		}
		if (article.category == categories.cm.name) {
			return categories.cm.format(article);
		}
		if (article.category == categories.publicity.name) {
			return categories.publicity.format(article);
		}
		if (article.category == categories.info.name) {
			return categories.info.format(article);
		}
	}

	function getNextArticleIndex() {
		var index = next.index;
		var category = next.category;
		var categoryIndex = 0;
		for (var i = 0 ; i < categoryLabels.length ; i++) {
			if (category == categoryLabels[i]) {
				categoryIndex = i;
			}
		}
		var totalCurrentCategory = articleList[category].length;
		if (totalCurrentCategory - 1 == index) {
			category = categoryLabels[(categoryIndex + 1) % categoryLabels.length];
			selectTab(category);
			return {
				category: category,
				index: 0
			};
		} else {
			return {
				category: category,
				index: index + 1
			};
		}
	}

	function parseXML(xml) {
		var initCategoryCommon = "";
		$(xml).find("item").each(function(i, v) {
			var title = $(this).find("title");
			title = (title.length ? title.text() : "");
			var link = $(this).find("link");
			link = (link.length ? link.text() : "");
			var desc = $(this).find("description");
			desc = (desc.length ? desc.text() : "");
			var category = $(this).find("category");
			category = (category.length ? category.text() : "");
			var target = $(this).find("target");
			target = (target.length ? target.text() : "");
			var pubDate = $(this).find("pubDate");
			pubDate = (pubDate.length ? pubDate.text() : "");
			var timeGMT = Math.floor( new Date(pubDate).getTime() );

			var categoryCommon = "";
			for (var j = 0 ; j < categoryLabels.length ; j++) {
				var re = new RegExp("^(" + categoryLabels[j] + ")");
				if (category.match(re)) {
					categoryCommon = categoryLabels[j];
					if (!articleList.hasOwnProperty(categoryCommon)) {
						articleList[categoryCommon] = [];
					}
				}
			}
			articleList[categoryCommon].push({
				title: title,
				link: link,
				desc: desc,
				pubDate: pubDate,
				timeGMT: timeGMT,
				category: category,
				target: target
			});
			if (i == 0) {
				initCategoryCommon = categoryCommon;
			}
		});
		// init settings
		if (statusTabActive < 0) {
			next = { category: initCategoryCommon, index: 0 };
			current = { category: "", index: 0 };
			selectTab(initCategoryCommon);
		} else {
			next = { category: categoryLabels[statusTabActive], index: 0 };
			current = { category: "", index: 0 };
		}
		setArticles(next.category, next.index);

		for (var key in articleList) {
			if (articleList.hasOwnProperty(key)) {
				articleList[key].sort(function(a, b) {
					if (a.hasOwnProperty("timeGMT")) {
						if (b.hasOwnProperty("timeGMT")) {
							return a["timeGMT"] < b["timeGMT"] ? 1 : -1;
						}
					}
					return 0;
				});
			}
		}

		for (var key in articleList) {
			articleList[key] = articleList[key].slice(0, 3);
		}
	}

	function initialize() {
		infoBlock = $(domIndexInfo);
		if (infoBlock.length) {
			var categoryIndex = 0;
			categoryLabels = [];
			infoTabs = infoBlock.find(domIndexInfoTab);
			infoTabs.each(function(i, v) {
				var li = this;
				if ($(this).hasClass(classTabActive)) {
					statusTabActive = i;
				}
				categoryLabels[categoryIndex++] = $(this).find("a").text();
				var link = $(this).find("a").eq(0);
				link.click(function(e) {
					infoTabs.each(function() {
						if (this === li) {
							$(this).addClass(classTabActive);
						} else {
							$(this).removeClass(classTabActive);
						}
					});
					restartAutoRepeat($(li).find("a").text());
					e.preventDefault();
					return false;
				});
			});
		}
		$.ajax({
			type: "GET",
			url: urlRSS,
			dataType: "xml",
			success: parseXML
		});
	}

	initialize();
});

