/**
 * global vars
 */
var randnum = Math.floor(Math.random()*10);
var textover = false;
var overlayopen = false;
var articlearray = new Array();
var scrollid = 0;
/**
 * easing set
 */
jQuery.easing.quart = function(x,t,b,c,d){
	return -c*((t=t/d-1)*t*t*t-1)+b;
};
/**
 * jQuery function
 */
$(function(){
	/* onload action */
	// sidenavi animate
	$("#updown").stop().css({top:350});
	var starty = $(window).height()-76;
	var lasty = starty+$(document).scrollTop();
	$('#updown').animate({top:lasty},{duration:1000,easing:'quart'});
	/* load file */
	// load json & text animation
	$.ajax({
		url: "http://hikarinitsuite.pictry.jp/json.php",
		type:"get",
		dataType: "json",
		success: function(data, status){
			$("#movetext").html(data['t'+randnum]);
			setInterval(function(){
				if(!textover){
					$('#movetext').animate({left:-1200},
						{duration:4000,easing:'quart',complete:function(){
						randnum = Math.floor(Math.random()*10);
						$(this).css("left", 800).html(data['t'+randnum]).animate({left:0},{duration:2000,easing:'quart',complete:function(){$(this).stop()}});
						}}
					);
				}
			}, 20000);
		},
		error: function(XMLHttpRequest, status, errorThrown){
			$("#movetext").html("error!");
		}
	});
	/* event handler */
	// lead mouse over
	$("#textclip").hover(function(){
		textover = true;
		$('#movetext').stop().css("left", 30).animate({fontSize:"107%"},{duration:300,easing:'quart',complete:function(){
			$(this).animate({left:-1200},{duration:50000,easing:'linear',complete:function(){$(this).css("left",0)}});
		}}).css({"color":"#FFFFFF"});
		$(this).css({"backgroundColor":"#00DADA"});
	},function(){
		textover = false;
		$('#movetext').stop().css("left", 0).animate({fontSize:"68%"},{duration:100,easing:'quart'}).css({"color":"#000000"});
		$(this).css({"backgroundColor":"#FFFFFF"});
	});
	// form forcus
	$('#searchform input').focus(function(){
		$(this).css("background-color", "#00DADA");
	});
	$('#searchform input').blur(function(){
		$(this).css("background-color", "#FFFFFF");
	});
	// submit rollover
	$('#searchform #searchsubmit').mouseover(function(){
		$(this).css("color", "#FFFFFF").css("background-color", "#000000");
	});
	$('#searchform #searchsubmit').mouseout(function(){
		$(this).css("color", "#000000").css("background-color", "#CCCCCC");
	});
	$('#commentform #submit').mouseover(function(){
		$(this).css("color", "#FFFFFF").css("background-color", "#000000");
	});
	$('#commentform #submit').mouseout(function(){
		$(this).css("color", "#000000").css("background-color", "#CCCCCC");
	});
	// sidenavi action
	$('a#naviup').click(function(){
		if(scrollid!=0){
			scrollid--;
		}
		$('html,body').stop();
		$('#updown').stop();
		var movelimit = 0;
		var pageoffset = $("#"+articlearray[scrollid]).offset().top;
		var navitop = $(window).height()-76;
		var navioffset = pageoffset+navitop;
		if($(document).scrollTop()>movelimit){
			$('html,body').animate({scrollTop:pageoffset},{duration:500,easing:'quart'});
			$('#updown').animate({top:navioffset},{duration:1000,easing:'quart'});
		}
	});
	$('a#navidown').click(function(){
		if(scrollid!=articlearray.length-1){
			scrollid++;
		}
		$('html,body').stop();
		$('#updown').stop();
		//var movelimit = $('html,body').attr('offsetHeight')-$(window).height();
		var movelimit = $('body').height()-$(window).height();
		var pageoffset = $('#'+articlearray[scrollid]).offset().top;
		var navitop = $(window).height()-76;
		var navioffset = pageoffset+navitop;
		if($(document).scrollTop()<movelimit){
			$('html,body').animate({scrollTop:pageoffset},{duration:500,easing:'quart'});
			$('#updown').animate({top:navioffset},{duration:1000,easing:'quart'});
		}
	});
	// scroll action
	$(window).scroll(function(){
		$('#updown').stop();
		var navitop = $(window).height()-76;
		var navioffset = navitop+$(document).scrollTop();
		$('#updown').animate({top:navioffset},{duration:1000,easing:'quart'});
	});
	// overlay
	$('a#overlaylink').click(function(){
		if(!overlayopen){
			var pwidth = $(window).width();
			var pheight = $(document).height();
			var pleft = (pwidth/2)-300;
			var cleft = pleft+600;
			overlayopen = true;
			$('body').prepend('<div id="olhikari"></div>');
			$('body').prepend('<div id="plinknavi"></div>');
			$('body').prepend('<div id="naviclose"><a href="javascript:void(0);"> </a></div>');
			$('#olhikari').css({
				"position": "absolute",
				"display": "none",
				"z-index": "9000",
				"width": pwidth,
				"height": pheight,
				"background-color": "#000000"
			});
			$('#plinknavi').css({
				"position": "absolute",
				"z-index": "9001",
				"display": "none",
				"top": "100px",
				"left": pleft,
				"width": "600px",
				"height": "400px",
				"text-align": "left",
				"color": "#000000",
				"background-color": "#FFFFFF"
			});
			$('#naviclose').css({
				"position": "absolute",
				"z-index": "9002",
				"display": "none",
				"top": "105px",
				"left": cleft
			});
			$.ajax({
				url: "/overlay.html",
				cache: false,
				success: function(html){
					$("#plinknavi").append(html);
				}
			});
			$('#olhikari').fadeTo(
				100,
				0.75,
				function(){
					$('#plinknavi').fadeTo(
						800,
						1,
						function(){
							$('#naviclose').fadeTo(800, 1);
						}
					);
				}
			);
			$('#olhikari').click(function(){
				overlayopen = false;
				$('#plinknavi').remove();
				$('#naviclose').remove();
				$(this).remove();
			});
			$('#naviclose').click(function(){
				overlayopen = false;
				$('#plinknavi').remove();
				$('#olhikari').remove();
				$(this).remove();
			});
		}else{
			overlayopen = false;
			$('#olhikari').remove();
		}
	});
});

