/* 入力チェック by jQuery（郵便番号） ********************************/
function zipCheck (zip) {
	// 8文字または数値以外なら末尾を一文字削除
	if (zip.length > 7 || !zip.match(/^([0-9]*)$/) || zip.charAt(zip.length-1) == '-') {
		$("#zip").val(zip.substr(0, zip.length-1));
		return false;
	} else {
		return true;
	}
}

/* Ajax通信 by jQuery（郵便番号） ********************************/
function zip2addr (zip) {
	if (zip.length != 0) {
		$.ajax({
			type: "GET",
			dataType: "xml",
			url: "/Ajax/Zip2Addr.php",
			data: "key=" + zip,
			timeout: 1000,
			error: function(){
				alert("読み込みに失敗しました");
			},
			success: function(xml){
				var address = $(xml).find("item").find("state").text();
				address += $(xml).find("item").find("address").text();
				$("#address").val(address)
			}
		});
	}
}

/* 同意チェック *************************************************/
$(document).ready(function(){
	if($("#privacy-disagree:checked").val()){
		$("#myForm").hide();
	}
});
function agreecheck() {
	$("#myForm").show();
	
}
function disagreecheck() {
	$("#myForm").hide();
}


//URL Encode (UTF-8)
function encodeURL(str) {
  var character = '';
  var unicode   = '';
  var string    = '';
  var i         = 0;

  for (i = 0; i < str.length; i++) {
    character = str.charAt(i);
    unicode   = str.charCodeAt(i);

    if (character == ' ') {
      string += '+';
    } else {
      if (unicode == 0x2a || unicode == 0x2d || unicode == 0x2e || unicode == 0x5f || ((unicode >= 0x30) && (unicode <= 0x39)) || ((unicode >= 0x41) && (unicode <= 0x5a)) || ((unicode >= 0x61) && (unicode <= 0x7a))) {
        string = string + character;
      } else {
        if ((unicode >= 0x0) && (unicode <= 0x7f)) {
          character   = '0' + unicode.toString(16);
          string += '%' + character.substr(character.length - 2);
        } else if (unicode > 0x1fffff) {
          string += '%' + (oxf0 + ((unicode & 0x1c0000) >> 18)).toString(16);
          string += '%' + (0x80 + ((unicode & 0x3f000) >> 12)).toString(16);
          string += '%' + (0x80 + ((unicode & 0xfc0) >> 6)).toString(16);
          string += '%' + (0x80 + (unicode & 0x3f)).toString(16);
        } else if (unicode > 0x7ff) {
          string += '%' + (0xe0 + ((unicode & 0xf000) >> 12)).toString(16);
          string += '%' + (0x80 + ((unicode & 0xfc0) >> 6)).toString(16);
          string += '%' + (0x80 + (unicode & 0x3f)).toString(16);
        } else {
          string += '%' + (0xc0 + ((unicode & 0x7c0) >> 6)).toString(16);
          string += '%' + (0x80 + (unicode & 0x3f)).toString(16);
        }
      }
    }
  }

  return string;
}

/* ほねみくじ ********************************/
$(function(){
	var delay = 320;
	var rand;
	var re = 0;
	var chance = 0;
	var INTERVAL = 3000;

	$('#TB_window').css('border-color','#BC0000');
	$('#TB_closeWindowButton').css('color','#FFFFFF');
	$('#TB_closeWindowButton:hover').css('text-decoration','underline');

	$('#startbtn').click(function(){
		$('#titleimg').css('display','none');
		rand = Math.floor(Math.random() * 10000)+1;
		if(rand >= 1 && rand <= 25){
			// ほね大賞：0.25%
			re = 8;
			chance = 1;
		}else if(rand >= 26 && rand <= 525){
			// 大吉：5%
			re = 1;
			if(Math.floor(Math.random() * 10) < 7){chance = 1;}
		}else if(rand >= 526 && rand <= 1525){
			// 中吉：10%
			re = 2;
			if(Math.floor(Math.random() * 20) < 7){chance = 1;}
		}else if(rand >= 1526 && rand <= 3525){
			// 小吉：20%
			re = 3;
			if(Math.floor(Math.random() * 30) < 7){chance = 1;}
		}else if(rand >= 3526 && rand <= 6525){
			// 吉：30%
			re = 4;
			if(Math.floor(Math.random() * 30) < 7){chance = 1;}
		}else if(rand >= 6526 && rand <= 8925){
			// 末吉：24%
			re = 5;
			if(Math.floor(Math.random() * 30) < 7){chance = 1;}
		}else if(rand >= 8926 && rand <= 9900){
			// 凶：9.75%
			re = 6;
			if(Math.floor(Math.random() * 50) < 7){chance = 1;}
		}else if(rand >= 9901 && rand <= 10000){
			// 大凶：1%
			re = 7;
			if(Math.floor(Math.random() * 10) < 7){chance = 1;}
		}

		$('#startbtn').css('display','none');

		$('#chance').fadeIn('slow');

		setTimeout(function(){
			$('#endbtn').fadeIn(4000);
		},INTERVAL);
		fortune('#result',delay,1);
	});
	$('#endbtn').click(function(){
		for(i=1;i<7;i++){
			if(i != re){
				$('#result'+i).attr('style','display:none');
			}
		}
		res = '#result'.concat(re);
		$(res).css('font-size','60px');
		$(res).css('font-family','ＭＳ 明朝');
		$(res).css('font-weight','bold');
		if(re == 1 || re == 8){
			$(res).css('color','#897006');
		}else if(re > 1 && re < 6){
			$(res).css('color','#990000');
		}else if(re > 5 && re < 8){
			$(res).css('color','#000000');
		}
		$(res).fadeIn(4000);

		$.post("/fortune.php",{"result":re},function(fdoc){
			$('#fortunedoc').html(fdoc);
			$('#fortunedoc').fadeIn(3000);
			$('#endbtn').attr('style','display:none');
		},"text");

		$.post("/fortune_relogin.php");
	});


});
function fortune(selector_base,delay,ct) {

	if(ct > 0){
		fortune_sub();
	}
	function fortune_sub(){
		var selector = selector_base + ct++;
		$(selector).fadeIn(delay);
		$(selector).fadeOut(delay,fortune_sub);
		if(delay > 100){
		delay = delay - 70;
		}
		if(ct > 6){
			ct = 1;
		}
		$('#endbtn').click(function(){
			ct = 100;
		});
	}
}
$(function(){
	$("#startbtn").hover(
		function(){
			$("#startbtn").attr("src","/img/fortune/button_on.gif");
		},
		function(){
			$("#startbtn").attr("src","/img/fortune/button_off.gif");
	});
});
$(function(){
	$("#endbtn").hover(
		function(){
			$("#endbtn").attr("src","/img/fortune/button_on.gif");
		},
		function(){
			$("#endbtn").attr("src","/img/fortune/button_off.gif");
	});
});


$(function(){
	$("form").submit(function() {
		$(":submit", this).css("display","none");
	}); 
});

$(function(){
	$("#fortune-link").attr('style','display:inline');
	$("#before-loading").attr('style','display:none');
});

/*
	Twitter風ページ上部アラート
*/
$(function () {
	var $alert = $('#head-alert');
	if($alert.length)
	{
		var alerttimer = window.setTimeout(function () {
			$alert.trigger('click');
		}, 10000);
		$alert.animate({height: '80px'}, 400)
		$(".close-alert").click(function () {
			window.clearTimeout(alerttimer);
			$alert.animate({height: '0'}, 400);
		});
	}
});
$(function () {
	$('#cookie-check').click(function(){
		if($(this).attr('checked')){
			$('#btn').html('<input type="submit" name="sub" value="閉じる" class="close-alert">');
		}else{
			$('#btn').html('<input type="button" name="sub" value="閉じる" class="close-alert">');
		}
	});
});

$(function(){
	$('.president').hover(function(){
		$(this).attr('style', 'background-image:url("/img/footer/president_on.png")');
		$('#up_to_top_msg').attr('style', 'display:inline;float:right;');
	}, function(){
		$(this).attr('style', 'background-image:url("/img/footer/president_off.png")');
		$('#up_to_top_msg').attr('style', 'display:none');
	});
	$('.president').click(function(){
	$('html,body').animate({ scrollTop: 0 }, 'slow');
	});
});

//******* カウントダウン *******
var liftoffTime_sinkyu = new Date("2011/2/27 09:00:00");
var liftoffTime_jusei = new Date("2011/3/6 09:00:00");
//liftoffTime.setDate(liftoffTime.getDate() + 5);


$(document).ready(function() {
	$('#countdown_jusei').countdown({ until: liftoffTime_jusei,
	layout: "あと<span class='cntday'> {d<}{dn} </span>日 と<br />{d>}{h<}{hn} 時間 {h>}{m<}{mnn} 分 {m>}{snn} 秒です。",
	format: "dhms",
	description: ""
	});
	$('#countdown_sinkyu').countdown({ until: liftoffTime_sinkyu,
	layout: "あと<span class='cntday'> {d<}{dn} </span>日 と<br />{d>}{h<}{hn} 時間 {h>}{m<}{mnn} 分 {m>}{snn} 秒です。",
	format: "dhms",
	description: ""
	});
	$('#countdown').countdown({ until: liftoffTime_jusei,
	layout: "あと<span class='cntday'> {d<}{dn} </span>日 と<br />{d>}{h<}{hn} 時間 {h>}{m<}{mnn} 分 {m>}{snn} 秒です。",
	format: "dhms",
	description: ""
	});
});

/**** ソーシャルブックマーク群 *******/
$( document ).ready( function (){
	$( '#sharebox' ).scrollFollow();
});

/**** rollover *******/
jQuery(function($) {
    var postfix = '_on';
    $('.btn').not('[src*="'+ postfix +'."]').each(function() {
        var img = $(this);
        var src = img.attr('src');
        var src_on = src.substr(0, src.lastIndexOf('.'))
                   + postfix
                   + src.substring(src.lastIndexOf('.'));
        $('<img>').attr('src', src_on);
        img.hover(function() {
            img.attr('src', src_on);
        }, function() {
            img.attr('src', src);
        });
    });
});
