

/* --------------------------------
	メインビジュアル
-------------------------------- */

$(function() {
	visual.init();
});

//演目シーズン切り替え数字
var season = {
	
	conf: [
		{ start:'2011/11/5', end:'2011/11/11' },
		{ start:'2011/11/12', end:'2011/11/25' },
		{ start:'2011/11/26', end:'2011/12/2' },
		{ start:'2011/12/3', end:'2011/12/16' },
		{ start:'2011/12/17', end:'2012/1/13' },
		{ start:'2012/1/14', end:'2012/1/20' },
		{ start:'2012/1/21', end:'2012/2/17' },
		{ start:'2012/2/18', end:'2012/3/9' },
		{ start:'2012/3/10', end:'2012/3/23' },
		{ start:'2012/3/24', end:'2012/5/11' },
		{ start:'2012/5/12', end:'2012/5/18' }
	],
	
	init: function() {
		var dateObj = new Date();
		var y = dateObj.getFullYear();
		var m = dateObj.getMonth()+1;
		var d = dateObj.getDate();
		var today = (new Date(y+'/'+m+'/'+d).getTime())/1000;
		var length = this.conf.length - 1;
		for(var i=0; i<=this.conf.length-1; i++) {
			var first = (new Date(this.conf[0].start).getTime())/1000;
			var last = (new Date(this.conf[length].end).getTime())/1000;
			var start = (new Date(this.conf[i].start).getTime())/1000;
			var end = (new Date(this.conf[i].end).getTime())/1000;
			if(today < first) { return 0; }
			if((start <= today) && (today <= end)) { return i; }
			if(today > last) { return 0; }
		}
	}
}


//ビジュアル切り替え
var visual = {
	
	conf: {
		containerSelector: '#main_visual', //コンテナ
		introSelector: '#mv_intro', //イントロ
		introCookieName: 'metIntroSkip',
		bgSelector: '#bg_container', //背景
		sectionSelector: '#section_container', //コンテンツ
		navSelector: '#main_visual_nav', //ナビ
		next: '.next a', //右矢印
		prev: '.prev a', //左矢印
		currentClass: 'current', //カレント要素
		duration: 1500, //時間
		easing: 'easeInOutExpo' //フェードの種類
	},
	
	item: [],
	move: [-415,-1295,-2175,-3055,-3935,-4815,-5695,-6575,-7455,-8335,-9215],
	animation: 0,
	
	//初期化
	init: function() {
		var self = this;
		var conf = this.conf;
		
		if ($.ua.isIE) $(conf.sectionSelector).find('.section p.typo').fixPng();
		
		var skip = $.cookie(conf.introCookieName);
		if((skip == null) || (/(^\?|&)intro=1(&|$)/.test(location.search))) {
			$(conf.introSelector).fadeIn(800,function() {
				$(conf.introSelector).find('p').delay(400).fadeIn(800);
				setTimeout(function(){
					$(conf.introSelector).fadeOut(1500, function() {
						$.cookie(conf.introCookieName, 1 ,{ expires:1 }); // cookie
						self.intro();
					});
				}, 3000);
			});
		} else { self.intro(); }
	},
	
	//イントロ
	intro: function() {
		var self = this;
		var conf = this.conf;
		var query = $.getQuery();
		var start;
		
		if(/(^\?|&)season/.test(location.search)) {
			if(query.season == 0) { start = 0; }
			else if(query.season > 11) { start = 0; }
			else { start = query.season - 1; }
		} else {
			start = season.init();
		}
		
		if(start == 'undefined') { start = 0; }	
		var maxNo = $(conf.navSelector).find('ul li').length - 1;
			
		$(conf.navSelector).find('ul li a').eq(start).addClass(conf.currentClass);
		$(conf.bgSelector).find('.section').css({opacity: 0.5});
		$(conf.bgSelector).find('.section').eq(start).css({opacity: 1});
		$(conf.sectionSelector).find('.section p.typo').css({opacity: 0.2});
		$(conf.sectionSelector).find('.section p.typo').eq(start).css({opacity: 1});
		$(conf.sectionSelector).find('.section p.btn').css({opacity: 0.2});
		$(conf.sectionSelector).find('.section p.btn').eq(start).css({opacity: 1});
		
		if(start == 0) { $(conf.prev).hide(); }
		if(start == maxNo) { $(conf.next).hide(); }
		
		var skip = $.cookie('metSlideSkip');
		if((skip == null) || (/(^\?|&)intro=1(&|$)/.test(location.search))) {
			$(conf.bgSelector).stop().animate({ left:self.move[start] },{ duration: conf.duration+1200, easing: 'easeOutQuint' });
			$(conf.sectionSelector).stop().delay(150).animate({ left:self.move[start] },
			{
				duration: conf.duration+1200,
				easing: 'easeOutQuint',
				complete:function() { $(conf.navSelector).delay(100).fadeIn(1000, function() { self.run(start); $.cookie('metSlideSkip', 1 ,{ expires:1 }); }); }
			});
		} else {
			$(conf.bgSelector).css({ left:self.move[start] });
			$(conf.sectionSelector).css({ left:self.move[start] });
			$(conf.navSelector).show();
			self.run(start);
		}
	},
	
	//処理
	run: function(start) {
		var self = this;
		var conf = this.conf;
		var start = start;
		
		$(conf.containerSelector).each(function() {
			var container = $(this);
			var index = self.item.length;
			var maxNo = $(conf.navSelector).find('ul li').length - 1;
			var obj = {
				containerObj: container,
				current: start,
				nav: container.find(conf.navSelector).find('ul li a'),
				bg: container.find(conf.bgSelector).find('.section'),
				section: container.find(conf.sectionSelector).find('.section p.typo'),
				btn: container.find(conf.sectionSelector).find('.section p.btn')
			}
			
			self.item.push(obj);
			
			obj.nav.each(function(i) {
				$(this).click(function() {
					if($(this).attr('class') != conf.currentClass) {
						$(conf.next).hide();
						$(conf.prev).hide();
					}
					if(self.animation == 0) {
						if($(this).attr('class') != conf.currentClass) { self.change(index, i); }
					}
				});
			});
			
			$(conf.next).click(function() {
				$(conf.next).hide();
				$(conf.prev).hide();
				if(self.animation == 0) {
					var i = self.item[index].current + 1;
					var count = i + 1;
					if(count > obj.nav.length) { return; }
					self.change(index, i);
				}
			});
			
			$(conf.prev).click(function() {
				$(conf.next).hide();
				$(conf.prev).hide();
				if(self.animation == 0) {
					var i = self.item[index].current - 1;
					if(i < 0) { return; }
					self.change(index, i);
				}
			});
			
			$(document).keydown(function(e) {
				if (e == null) { keycode = event.keyCode; } //IE
				else { keycode = e.which; } //Mozilla
				if(keycode == 39) { //next
					$(conf.next).hide();
					$(conf.prev).hide();
					if(self.animation == 0) {
						var i = self.item[index].current + 1;
						var count = i + 1;
						if(count > obj.nav.length) { return; }
						self.change(index, i);
					}
				} else if(keycode == 37) { //prev
					$(conf.next).hide();
					$(conf.prev).hide();
					if(self.animation == 0) {
						var i = self.item[index].current - 1;
						if(i < 0) { return; }
						self.change(index, i);
					}
				}
			});
		});
		
	},
	
	//変化処理
	change: function(index, i) {
		var self = this;
		var conf = this.conf;
		var obj = this.item[index];
		var current = obj.current;
		var nav = obj.nav;
		var section = obj.section;
		var bg = obj.bg;
		var btn = obj.btn;
		
		self.animation = 1;
		
		$(conf.bgSelector).stop().animate({ left: self.move[i] },{ duration: conf.duration, easing: conf.easing });
		bg.stop().animate({ opacity: 0.5 },{ duration: conf.duration-200, easing: 'linear' });
		section.stop().animate({ opacity: 0.2 },{ duration: conf.duration-200, easing: 'linear' });
		btn.stop().animate({ opacity: 0.2 },{ duration: conf.duration-200, easing: 'linear' });
		
		$(conf.sectionSelector).stop().delay(100).animate({ left: self.move[i] },
		{
			duration: conf.duration,
			easing: conf.easing,
			complete: function() {
				self.animation = 0;
				if(i != 10) $(conf.next).fadeIn(300);
				if(i != 0) $(conf.prev).fadeIn(300);
				bg.eq(i).stop().animate({ opacity: 1 },{ duration: 300, easing: 'linear' });
				section.eq(i).stop().animate({ opacity: 1 },{ duration: 300, easing: 'linear' });
				btn.eq(i).stop().animate({ opacity: 1 },{ duration: 300, easing: 'linear' });
			}
		});
		nav.removeClass(conf.currentClass);
		nav.eq(i).addClass(conf.currentClass);
		this.item[index].current = i;
	}
		
}



/* --------------------------------
	最新情報
-------------------------------- */

$(function() {
	topics.init();
});

var topics = {
	
	date: [],
	title: [],
	href: [],
	time: [],
	
	//初期化
	init: function() {
		this.rss();
	},
	
	//RSS読み込み
	rss: function() {
		var self = this;
		var today = new Date();
		$.getJSON('php/index.php', function(data){
			$('.loading').remove();
			$.each(data.feed.entry, function(i, e) {
				var dateObj = e.published.$t;
				var dateList = dateObj.split('T');
				var dateText = dateList[0].replace(/[-]/g,'.');
				var timeText = dateList[0].replace(/[-]/g,'/');
				var timeObj = new Date(timeText);
				var time = timeObj.getTime();
				self.date.push(dateText);
				self.time.push(time);
				
				var title = e.title.$t;
				self.title.push(title);
								
				var href = e.link[2].href;
				self.href.push(href);
			});
			
			var str = '<ul class="clearfix">';
			for(var i=0; i<=9; i++) {
				var oldDay = new Date(self.time[i]);
				var d = (today - oldDay) / (1000 * 24 * 3600);
				str += '<li class="clearfix">';
				str += '<div class="date"><p>'+self.date[i]+'</p></div>';
				if(d <= 8) { str += '<div class="title"><p><a href="'+self.href[i]+'" target="_blank">'+self.title[i]+'<span class="new"><img src="common/img/ico_new.gif" alt="NEW" width="30" height="14" /></span></a></p></div>'; }
				else { str += '<div class="title"><p><a href="'+self.href[i]+'" target="_blank">'+self.title[i]+'</a></p></div>'; }
				str += '</li>';
			}
			str += '</ul>';
			$('div#feed').append(str);
			
			$('div#feed ul li:even').addClass('even');
			$('div#feed ul li:odd').addClass('odd');
			
		});	
	}
	
}





