/*
トップページのみで実行
*/
dispatcher('^/$', function () {
	$(function () {
		(function () {
			var span = $('#moreLinkNav .kayacBlog span');
			span.text(span.data().close);
			$('#kayacBlogSection').show();
		})();

		// ranking open
		(function () {
			var groupUnitNewsRank4th10th = $('.groupUnitNewsRank4th10th');
			var link = $('#viewNewsRankMore').find('a');
			var o_msg = link.text();
			var viewNewsRankMore = $('#viewNewsRankMore').toggle(function() {
				groupUnitNewsRank4th10th.stop(true, true).slideDown(function () {
					link.text(link.data().messageClose);
					viewNewsRankMore.addClass('viewNewsRankMoreOpen');
				});
			},
			function() {
				viewNewsRankMore.removeClass('viewNewsRankMoreOpen');
				groupUnitNewsRank4th10th.stop(true, true).slideUp(function () {
					link.text(o_msg);
				});
			});
		})();

		//rotation dice
		(function (win) {
			if ($.browser.smartphone) {
				return;
			};
			if (!$.support.svg) {
				return;
			};
			var wrapper = $('#mediaNewsSection .viewNewsMore, #newsRankSectionInnr');
			var base = wrapper.eq(0);
			var dice = $('#rankingEffect');
			var offset = dice.offset();
			var w = $(win);
			set_rotation_dice();
			function set_rotation_dice () {
				$(window).bind('scroll.rotation_dice resize.rotation_dice', $.throttle(100, function () {
					if (!dice.touchWindow()) {
						return;
					};
					var base_top = (w.scrollTop() + w.height()) - base.offset().top;
					dice.stop().animate({
						'right' : base_top,
						'rotate' : '-=15deg'
					}, 100);
				}));
			};
			$(window).bind('goPageTopStart', function () {
				$(window).unbind('.rotation_dice');
			});
			$(window).bind('goPageTopEnd', function () {
				set_rotation_dice();
			});
		})(window);

		// fukidashi hover
		(function () {
			if ($.browser.smartphone) {
				return;
			};
			if (!$.support.svg) {
				return;
			};
			$('.newsSection').each(function () {
				var section = $(this);
				var effects = section.children('.sectionSelectEffect').children('.effectLine, .balloon');
				var line = effects.eq(0);
				var balloon = effects.eq(1);
				section.find('.unitNews').hover($.throttle(200, function () {
					var self = $(this);
					var pos = self.offset();
					effects.stop(true, true);
					line.fadeIn();
					balloon.show();
					var w_height = $(window).height();
					var w_width = $(window).width();
					var s_height = self.outerHeight();
					var s_width = self.outerWidth();
					line.css({
						'top' : pos.top - (w_height * 2) + (s_height / 2),
						'left' : pos.left - (w_width * 2) + (s_width / 2),
						'height' : w_height * 4,
						'width' : w_width * 4
					});
					balloon.css({
						'top' : pos.top - (s_height * 0.5),
						'left' : pos.left - (s_width * 0.25),
						'height' : s_height * 2,
						'width' : s_width * 1.5
					});
					$(window).bind('scroll.fukidashi_hover', $.throttle(200, function () {
						requestAnimationFrame(function () {
							effects.stop(true, true);
							line.fadeOut();
							balloon.hide();
						});
						$(this).unbind('scroll.fukidashi_hover');
					}));
				}), function () {
					effects.stop(true, true);
					line.fadeOut();
					balloon.hide();
					$(this).unbind('scroll.fukidashi_hover');
				});
			});
		})();

		// fukidashi resize
		(function () {
			if ($.browser.smartphone) {
				return;
			};
			if (!$.support.svg) {
				return;
			};
			$(window).bind('load_data_svg_url_complate', function () {
				$.fn.specialBalloon_resize = function () {
					$('.sectionSelectEffect .balloon svg').each(function () {
						var svg = $(this);
						var svg_base = svg.svgWidth();
						var box_size = (svg.find('.base_size').svgWidth() || 38) / 2;
						var news = svg.closest('.newsSection').find('.news01');
						var base = news.width() - svg_base;
						if (base < 0) {
							base = 0;
						};
						var box_count = Math.ceil(base / box_size);

						var viewBox = svg.attr('viewBox').replace(/(([\d\.]\s){2})([\d\.]+)/, '$1' + (svg_base + base));
						svg.attr('viewBox', viewBox);

						svg.find('.right_use').attr({
							'y' : 0,
							'x' : (box_count * box_size)
						});

						var use_elems = svg.find('use:not(.right_use)');
						var use_ = use_elems.first();
						_(box_count+1).times(function (count) {
							var use_elem = use_.clone();
							use_elem.attr({
								'y' : 0,
								'x' : (count * box_size)
							});
							svg.append(use_elem);
						});
						use_elems.remove();
					});
				};
				$(this).specialBalloon_resize();
				$(window).resize($.throttle(1000, function () {
					$(this).specialBalloon_resize();
				}));
			});
		})();

		// twitter update
		$(window).bind('load', function () {
			$.getJSON('/omake.json', function (data) {
				if (!data.length) {
					return;
				};
				$('#omakeSection').show();
				$(function () {
					var article = $('#omakeSection article');
					update_twitter();

					function update_twitter () {
						update_twitter.current = update_twitter.current || 0
						var res = data[++update_twitter.current] || data[update_twitter.current = 0];
						if (!res) {
							return;
						};
						var omakeSection = $('#omakeSection');
						if (res.created_at) {
							var created_at = res.created_at.replace(/\s\+/, ' UTC+');
                            var d = new Date(created_at).paddingYYYYMMDD();
                            if (d) {
    							omakeSection.find('.time time').text(d);
                            }
						};
						var image = omakeSection.find('article .image').hide();
						var text = res.text.replace(/http:\/\/[-_.!~*'()a-zA-Z0-9;\/?:\@&=+\$,%#]+/g, function (link) {
							if (link.match(/^http:\/\/yfrog.com\//)) {
								image.show().find('img').attr('src', link+':small');
							};
							return '<a href="'+link+'" class="external">'+link+'</a>';
						});
						omakeSection.find('.sentence').html(text).find('a.external').meca('external');
						article.fadeTo(200, 1).delay(8000).fadeTo(200, 0.1, update_twitter);
					};
				});
			});
		});

		// service banner
		$('#specialNewsSection .newsAreaContent').banner_cycle({
			'navNextPrev' : '.navNextPrev li',
			'target' : '.bnrGroup',
			'duration' : 700
		});

		// slide banner(.bnrSlide)
		$('#serviceNewsSection .newsAreaContent>.unitNews').first().banner_cycle({
			'navNextPrev' : '.navNextPrevSlide li',
			'target' : '.bnrSlide',
			'duration' : 700
		});
	});

	$('.bnrGroup li').clickToOpen();
	$('.bnrSlideWrap li').clickToOpen();
});

