$(function(){

	//control button
	$('#control ul li').live('click',function(){
		var c = $(this).attr('class');
		if(c=='login'||c=='recipe'){
			$('#function p.close').show();
			$('#function').css({position:'absolute'});
			$('#function div.inner').hide();
			$(this).parent().parent().siblings('div.'+c).children('div.inner').show();
			return false;
		}
	});

	//function window close
	$('#function p.close').live('click',function(){
		$('#function').css({position:'relative'});
		$('#function div.inner, #function p.close').hide();
		return false;
	});

	//search
	$('#control ul.menu li.search input.words').val('料理名・材料・レシピ番号').css({color:'#f60'}).focus(function(){
		if($(this).val() == '料理名・材料・レシピ番号'){
			$(this).val('').css({color:'#292929'});
		}
	}).blur(function(){
		if($(this).val() == ''){
			$(this).val('料理名・材料・レシピ番号').css({color:'#f60'});
		}
	});

	$('#control ul.menu li.search input.button').click(function(){
		headwordsrch();
		return false;
	});

	//function login checkbox
	$('#autologin').change(function(){
		if($(this).is(':checked')){
			$(this).next('label').addClass('checked');
		}
		else{
			$(this).next('label').removeClass('checked');
		}
	});
	$("#krylogin").submit(function(){
		var msg = {
			m0: "必須入力項目です",
			m7: "半角英数字を入力してください"
		};
		var login = {
			id: "#fmuserid",
			pw: "#fmpass"
		}
		if ($(login.id).val() == "") {
			alert(msg.m0);
			$(login.id).focus();
			return false;
		} else {
			if (!($(login.id).val().match(/^[a-zA-Z0-9]+$/))) {
				alert (msg.m7);
				$(login.id).focus();
				return false;
			}
		}
		if ($(login.pw).val() == "") {
			alert(msg.m0);
			$(login.pw).focus();
			return false;
		}
	});

	if(g_browser.SmartPhone ||g_browser.iPad){
		$('#header2011 ul.menu li').live('click',function(){
			if($(this).hasClass('i5')){}
			else{
				if($(this).children('ul').css('display')=='none'){
					$('#header2011 ul.menu li a').removeClass('hover');
					$('#header2011 ul.menu li ul').hide();
					$(this).children('a:first-child').addClass('hover');
					$('ul',this).slideDown('fast');
				}
				else{
					$(this).children('a:first-child').removeClass('hover');
					$('ul',this).slideUp('fast');
				}
				return false;
			}
		});
		$('#header2011 ul.menu li ul li a').live('click',function(){
			top.window.location.href = $(this).attr('href');
		});
	}
	else{
		initHeaderMenu();
	}

	//setup
	initFontSize();
	smoothScroll();
	bindHover();

});

//header menu
var slideDownUl;
var slideDownTimer;
function initHeaderMenu(){
	$('#header2011 ul.menu li a.first').attr('href','javascript:void(0);');
	$('#header2011 ul.menu li').hover(
		function() {
			var ul = $('ul', this);
			$(this).children('a:first-child').addClass('hover');
			if(ul.attr('class') != 'show') {
				ul.addClass('show');
				slideDownTimer = setTimeout(function() { slideDownMenu(ul) }, 200);
			}
		},
		function() {
			var ul = $('ul', this);
			$(this).children('a:first-child').removeClass('hover');
			ul.slideUp('fast').removeClass('show');
		}
	);
}
	
function slideDownMenu(target) {
	if(target.attr('class') == 'show') {
		target.slideDown('fast').removeClass('show');
	}
}

//fontsize
function initFontSize() {
	var FONT_SIZE_TYPE = ['/2011/common/css/fontsize/small.css', '/2011/common/css/fontsize/medium.css', '/2011/common/css/fontsize/large.css'];
	var COOK_KEY = 'eHealthyFontSize';
	var currentFontIndex = cookF.get(COOK_KEY);
	if(currentFontIndex != '') $('#changeFontsize').attr('href', FONT_SIZE_TYPE[currentFontIndex]);
	
	var fontSizeHTML = '<ul>';
	fontSizeHTML += '<li class="minus"><a href="#minus">−</a></li>';
	fontSizeHTML += '<li class="plus"><a href="#plus">＋</a></li>';
	fontSizeHTML += '</ul>';
	$('#control ul li.fontsize').append(fontSizeHTML);
	var minus = $('#control ul li.fontsize ul li.minus a');
	var plus = $('#control ul li.fontsize ul li.plus a');
	
	//クリック時
	var click = function(e) {
		if(e.data.type == 'minus') {
			currentFontIndex--;
			if(currentFontIndex <= 0) {
				minus.addClass('disabled');
				minus.unbind('click', click);
				currentFontIndex = 0;
			} else if(currentFontIndex == FONT_SIZE_TYPE.length - 2) {
				plus.removeClass('disabled');
				plus.bind('click', {type: 'plus'}, click);
			}
		} else if(e.data.type == 'plus') {
			currentFontIndex++;
			if(currentFontIndex >= FONT_SIZE_TYPE.length - 1) {
				plus.addClass('disabled');
				plus.unbind('click', click);
				currentFontIndex = FONT_SIZE_TYPE.length - 1;
			} else if(currentFontIndex == 1) {
				minus.removeClass('disabled');
				minus.bind('click', {type: 'minus'}, click);
			}
		}
		$('#changeFontsize').attr('href', FONT_SIZE_TYPE[currentFontIndex]);
		cookF.set(COOK_KEY, currentFontIndex, {expires:365});
		return false;
	}
	
	minus.bind('click', {type: 'minus'}, click);
	plus.bind('click', {type: 'plus'}, click);
	
	if(currentFontIndex == 0) {
		minus.addClass('disabled');
		minus.unbind('click', click);
	}
	if(currentFontIndex == FONT_SIZE_TYPE.length - 1) {
		plus.addClass('disabled');
		plus.unbind('click', click);
	}
}

//login
function checkKey(e, pm) {
	var key;
	if(document.all)	key = e.keyCode;
	else if(document.getElementById)	key = (e.keyCode)? e.keyCode: e.charCode;
	else if(document.layers)	key = e.which;
	
	if (key == 13) {
		if (pm == 0)	searchMenu();
		else if(pm == 1)	krylogin();
		return true;
	}
	else {
		return false;
	}
}

function krylogin() {
	this.document.KryLogin.target="_top";
	this.document.KryLogin.todo.value = "exec";
	this.document.KryLogin.action="/recipe-webapp/KryLoginServ.php";
	this.document.KryLogin.submit();
}

//search
function headwordsrch () {
	var wsrch = document.wordsrch;
	var srchword = wsrch.srchword.value;
	var srchpgm;
	
	// 入力内容で検索タイプを判断
	var stype = srchword.match(/^\d{1,5}$/);
	var ztype = false;
	ztype = srchword.match(/^[０-９]{1,5}$/);
	
	if (stype || ztype) {
		srchpgm = "/recipe-webapp/IpnSgRcpServ.php?menuCd=";
		srchpgm += replaceHankakuNum(srchword);
		wsrch.target="_top";
		wsrch.action = srchpgm;
		wsrch.submit();
	} else {
		srchpgm = "/recipe-webapp/IpnMenuNameGrepServ.php";
		wsrch.todo.value = "exec";
		wsrch.MENUKEYWORD.value = srchword;
		wsrch.target="_top";
		wsrch.action = srchpgm;
		wsrch.submit();
	}
}

function replaceHankakuNum(str){ //半角数字変換
	var replacenum = new Array("1","2","3","4","5","6","7","8","9","0")
	var num = new Array("１","２","３","４","５","６","７","８","９","０");
	for(i=0; i<str.length;i++){
		for(x = 0; x < num.length; x++){
			if(num[x] == str.charAt(i)){
				str = str.replace(num[x],replacenum[x]);
			}
		}
	}
	return str;
}


