

// countdown --------------------------------------------------------------------------------------------------------------------
var dayCount;
var countdownTimer;
function countdownAjax(){
		$.ajax({
			url: "./time.php",
			cache: false,
			success: function(msg){
				var cdHTMLAry = msg.split(",");
				$('#countdown').css("top","-70px");
				$("#timeCount").html(cdHTMLAry[3]);
				countdownInit();
			}
		});
}
function countdownFin(){
	clearTimeout(countdownTimer);
}

function checkCount(){
	var tmpSec = parseInt($(".countdown_section:eq(3)").text())+"";
	while(tmpSec.length < 2){
		tmpSec = "0" + tmpSec;
	}
	var numText = 0;
	numText += parseInt($("#countDay1 img").attr("alt"));
	numText += parseInt($("#countDay2 img").attr("alt"));
	numText += parseInt($("#countHour1 img").attr("alt"));
	numText += parseInt($("#countHour2 img").attr("alt"));
	numText += parseInt($("#countMin1 img").attr("alt"));
	numText += parseInt($("#countMin2 img").attr("alt"));
	if(numText == 0){
		countdownFin();
	}else{
	//----------------------------------------------------------------------------
	if(tmpSec == "00"){
		var min2Alt = $("#countMin2 img").attr("alt");
		if(min2Alt == "0"){
			min2Next = 9;
		}else{
			min2Next = parseInt(min2Alt) - 1;
		}
		$("#countMin2").prepend('<img src="http://web-cache.stream.ne.jp/www11/emi-web/emirocks.com/common/img/num'+min2Next+'.png" alt="'+min2Next+'" width="62" height="1" />');
		$("#countMin2 img:eq(0)").css({"bottom":"auto","top":"0px"});
		$("#countMin2 img:eq(0)").animate({"height":"103px"},1000);
		$("#countMin2 img:eq(1)").animate({"height":"0px"},1000,function(){
			$(this).css({"bottom":"0px","top":"auto"});
			$(this).remove();
			$("#countMin2 img:eq(0)").css({"bottom":"0px","top":"auto"});
		});
		//----------------------------------------------------------------------------
		if(min2Alt == "0"){
				var min1Alt = $("#countMin1 img").attr("alt");
				if(min1Alt == "0"){
					min1Next = 5;
				}else{
					min1Next = parseInt(min1Alt) - 1;
				}
				$("#countMin1").prepend('<img src="http://web-cache.stream.ne.jp/www11/emi-web/emirocks.com/common/img/num'+min1Next+'.png" alt="'+min1Next+'" width="62" height="1" />');
				$("#countMin1 img:eq(0)").css({"bottom":"auto","top":"0px"});
				$("#countMin1 img:eq(0)").animate({"height":"103px"},1000);
				$("#countMin1 img:eq(1)").animate({"height":"0px"},1000,function(){
					$(this).css({"bottom":"0px","top":"auto"});
					$(this).remove();
					$("#countMin1 img:eq(0)").css({"bottom":"0px","top":"auto"});
				});
				//----------------------------------------------------------------------------
				if(min1Alt == "0"){
						var hour2Alt = $("#countHour2 img").attr("alt");
						if(hour2Alt == "0"){
							hour2Next = 9;
							var hour1Chk = $("#countHour1 img").attr("alt");
							if(hour1Chk == "0"){
								hour2Next = 3;
							}
						}else{
							hour2Next = parseInt(hour2Alt) - 1;
						}
						$("#countHour2").prepend('<img src="http://web-cache.stream.ne.jp/www11/emi-web/emirocks.com/common/img/num'+hour2Next+'.png" alt="'+hour2Next+'" width="62" height="1" />');
						$("#countHour2 img:eq(0)").css({"bottom":"auto","top":"0px"});
						$("#countHour2 img:eq(0)").animate({"height":"103px"},1000);
						$("#countHour2 img:eq(1)").animate({"height":"0px"},1000,function(){
							$(this).css({"bottom":"0px","top":"auto"});
							$(this).remove();
							$("#countHour2 img:eq(0)").css({"bottom":"0px","top":"auto"});
						});
						//----------------------------------------------------------------------------
						if(hour2Alt == "0"){
								var hour1Alt = $("#countHour1 img").attr("alt");
								if(hour1Alt == "0"){
									hour1Next = 2;
								}else{
									hour1Next = parseInt(hour1Alt) - 1;
								}
								$("#countHour1").prepend('<img src="http://web-cache.stream.ne.jp/www11/emi-web/emirocks.com/common/img/num'+hour1Next+'.png" alt="'+hour1Next+'" width="62" height="1" />');
								$("#countHour1 img:eq(0)").css({"bottom":"auto","top":"0px"});
								$("#countHour1 img:eq(0)").animate({"height":"103px"},1000);
								$("#countHour1 img:eq(1)").animate({"height":"0px"},1000,function(){
									$(this).css({"bottom":"0px","top":"auto"});
									$(this).remove();
									$("#countHour1 img:eq(0)").css({"bottom":"0px","top":"auto"});
								});
								//----------------------------------------------------------------------------
								if(hour1Alt == "0"){
										var day2Alt = $("#countDay2 img").attr("alt");
										if(day2Alt == "0"){
											day2Next = 9;
										}else{
											day2Next = parseInt(day2Alt) - 1;
										}
										$("#countDay2").prepend('<img src="http://web-cache.stream.ne.jp/www11/emi-web/emirocks.com/common/img/num'+day2Next+'.png" alt="'+day2Next+'" width="62" height="1" />');
										$("#countDay2 img:eq(0)").css({"bottom":"auto","top":"0px"});
										$("#countDay2 img:eq(0)").animate({"height":"103px"},1000);
										$("#countDay2 img:eq(1)").animate({"height":"0px"},1000,function(){
											$(this).css({"bottom":"0px","top":"auto"});
											$(this).remove();
											$("#countDay2 img:eq(0)").css({"bottom":"0px","top":"auto"});
										});
										//----------------------------------------------------------------------------
										if(day2Alt == "0"){
												var day1Alt = $("#countDay1 img").attr("alt");
												if(day1Alt == "0"){
													day1Next = 9;
												}else{
													day1Next = parseInt(day1Alt) - 1;
												}
												$("#countDay1").prepend('<img src="http://web-cache.stream.ne.jp/www11/emi-web/emirocks.com/common/img/num'+day1Next+'.png" alt="'+day1Next+'" width="62" height="1" />');
												$("#countDay1 img:eq(0)").css({"bottom":"auto","top":"0px"});
												$("#countDay1 img:eq(0)").animate({"height":"103px"},1000);
												$("#countDay1 img:eq(1)").animate({"height":"0px"},1000,function(){
													$(this).css({"bottom":"0px","top":"auto"});
													$(this).remove();
													$("#countDay1 img:eq(0)").css({"bottom":"0px","top":"auto"});
												});
												//----------------------------------------------------------------------------
												if(parseInt(day1Alt) < 1){
													$("#countDay0").css("display","none");
													$("#countdownBox").removeClass("countdownBox100Day");
													$("#countdownActive").removeClass("countdownActive100Day");
													$("#homeSlideBox").removeClass("homeSlideBox100Day");
												}
										}
								}
						}
				}
		}
	}
	}
	//$("#debug").html(tmpSec);
	countdownTimer = setTimeout("checkCount()",1000);
}

function countdownInit(){
	
	if($("#countDay0").length > 0){
		$("#countdownBox").addClass("countdownBox100Day");
		$("#countdownActive").addClass("countdownActive100Day");
		$("#homeSlideBox").addClass("homeSlideBox100Day");
	}
	
	var liftoffTime = new Date();
	var baseSec = liftoffTime.getTime();
	var targetSec = baseSec + (parseInt($("#timeCount").html())*1000);
	liftoffTime.setTime(targetSec);
	$('#timeCount').countdown({until:liftoffTime, compact:false, format:'DHMS', description:''});
	
	checkCount();
	
}



//ナンバー画像をプリロード
jQuery.preloadImages = function(){
	for(var i = 0; i<arguments.length; i++){
		jQuery("<img>").attr("src", arguments[i]);
	}
}
$.preloadImages("http://web-cache.stream.ne.jp/www11/emi-web/emirocks.com/common/img/num0.png", "http://web-cache.stream.ne.jp/www11/emi-web/emirocks.com/common/img/num1.png", "http://web-cache.stream.ne.jp/www11/emi-web/emirocks.com/common/img/num2.png", "http://web-cache.stream.ne.jp/www11/emi-web/emirocks.com/common/img/num3.png", "http://web-cache.stream.ne.jp/www11/emi-web/emirocks.com/common/img/num4.png", "http://web-cache.stream.ne.jp/www11/emi-web/emirocks.com/common/img/num5.png", "http://web-cache.stream.ne.jp/www11/emi-web/emirocks.com/common/img/num6.png", "http://web-cache.stream.ne.jp/www11/emi-web/emirocks.com/common/img/num7.png", "http://web-cache.stream.ne.jp/www11/emi-web/emirocks.com/common/img/num8.png", "http://web-cache.stream.ne.jp/www11/emi-web/emirocks.com/common/img/num9.png");






// main visual --------------------------------------------------------------------------------------------------------------------
function initMainVisual(){
	
	// home animation ----------------------------------------------------
	$('#visual img').fadeIn(1200);
	$('#visual').cycle({
		speed: 700,
		timeout: 3600,
		easing: 'easeOutSine',
		fx: 'fade',
		random:1
	});
	
}




// flick banner --------------------------------------------------------------------------------------------------------------------
var timerMsec = 4000;
var fl;
var insfs;
var tmpLeftPos;
var ssflg = false;
var flickableTimer;
function startFlickableTimer(){
	clearTimeout(flickableTimer);
	ssflg = true;
	flickableTimer = setTimeout("initFlickableTimer()", timerMsec);
}
function initFlickableTimer(){
	if (ssflg == true){
		if(insfs.page < insfs.pageLength){
			fl.flickSimple('nextPage', 1);
		}else{
			fl.flickSimple('goTo', 1);
		}
	}
}
function initFlickableBanner(){
	
	fl = $('#homeSlides');
	
	var flItemLen = fl.find("li").length;
	var flItemWid = fl.find("li").width();
	var flTotalWid = flItemLen * flItemWid;
	var flOuterWid = $("#homeSlides").width();
	if(flOuterWid < flTotalWid){
		$("#homeSlidePrevNext").css("display", "block");
	}
	fl.find("ul").css("width", flTotalWid + "px");
	
	var flPerPage = 3;
	var flPageMax = flItemLen - flPerPage + 1;
	
	if(flPerPage > flItemLen){
		$("#homeSlides").css({"position":"relative","left":"0px","margin":"0px auto","width":flTotalWid+"px"});
	}
	
	$("#homeSlideBox").append('<ul id="homeSlidePosition"></ul>');
	for(i=0; i<flPageMax; i++){
		$("#homeSlidePosition").append('<li>' + (i+1) + '</li>');
		if(i==0){
			$("#homeSlidePosition li:eq(0)").addClass("active");
		}
	}
	
	fl.flickSimple({
		snap: 'first',
		onChange: function(){
			startFlickableTimer();
			$("#homeSlidePosition li").removeClass("active");
			$("#homeSlidePosition li:eq("+(insfs.page-1)+")").addClass("active");
		}
	});
	
	$('#homeSlidePrevNext .prev').click(function(){
		if(insfs.page != 1){
			fl.flickSimple('prevPage', 1);
		}else{
			//fl.flickSimple('goTo', insfs.pageLength+1);
		}
		ssflg = false;
		setTimeout(function(){ ssflg = true; }, timerMsec * 0.9);
		startFlickableTimer();
		return false;
	});

	$('#homeSlidePrevNext .next').click(function(){
		if(insfs.page < flPageMax){
			fl.flickSimple('nextPage', 1);
		}else{
			//fl.flickSimple('goTo', 1);
		}
		ssflg = false;
		setTimeout(function(){ ssflg = true; }, timerMsec * 0.9);
		startFlickableTimer();
		return false;
	});
	
	$('#homeSlides').bind('touchstart', function(){
		ssflg = false;
	});
	
	$('#homeSlides').bind('touchend', function(){
		setTimeout(function(){ ssflg = true; }, timerMsec * 0.9);
		startFlickableTimer();
	});
	
	$('#homeSlides').hover(
		function(){
			ssflg = false;
		},
		function(){
			ssflg = true;
			startFlickableTimer();
		}
	);
	
	insfs = fl.flickSimple();
	
	startFlickableTimer();
	
}




// tab menu --------------------------------------------------------------------------------------------------------------------
function initTabMenu(){
	$(".homeNewsSec").css("display","none");
	$(".homeNewsSec:eq(0)").css("display","block");
	//$("#homeNewsBox .tabMenu li:eq(0)").addClass("active");
	$("#homeNewsBox .tabMenu a").click(function(){
		$("#homeNewsBox .tabMenu li").removeClass("active");
		$(this).closest("li").addClass("active");
		var tmpHashStr = $(this).attr("href").split("#")[1];
		$(".homeNewsSec").css("display","none");
		$("#"+tmpHashStr).css("display","block");
		return false;
	});
}






$(function() {
	
	initMainVisual();
	
	countdownAjax();
	
	initTabMenu();
	
});

$(window).load(function(){
	
	initFlickableBanner();
	
});



// delay
$.fn.delay = function(time, callback){
    // Empty function:
    jQuery.fx.step.delay = function(){};
    // Return meaningless animation, (will be added to queue)
    return this.animate({delay:1}, time, callback);
}






