
$(function(){

	//サブナビゲーションの処理
	//$('#nav').droppy();

	//リンク先が外部ならカウンターを呼び出し
	$('a').click(function(){
		if($(this).attr('href').substr(0,7) == "http://"){
			//出勤ページャーのブランク回避
			if($(this).hasClass('att-link')) return true;

			$(this).attr('target', "_blank");
			var img="";
			var url=encodeURIComponent(this.href);
			if($(this).children("img").attr("src")){
				img=$(this).children("img").attr("src");
			}
			else if ($(this).parent("div").attr("class") == "qitem"){
				img=$(this).next("div").css("background-image").replace(/^url|[\"\(\)]/g, '');
			}
			else{
				img=$(this).text();
			}

			$.get("/php/counter.php", {site: "1", url: url, img: img, page: document.URL});
		}
	});

	//携帯メールに送信
	$('a.mobilemail').click(function(){
		//入力ダイアログを表示
		var email = window.prompt("送信先の携帯メールアドレスを入力して下さい。\n（※携帯電話のメールアドレスのみ有効です。）", "");
		var url = $('a.mobilemail').attr("href");
		var title = $('a.mobilemail').attr("title");

		if(email==null){
			return false;
		}

		if(email.match(/([\w\-\.]+)@(docomo|ezweb|[a-z]{1}\.vodafone|softbank|i\.softbank|[a-z]{0,3}\.?pdx)(\.ne)?(\.jp)/)){
			if (!Mail_To_Mobile(email, url, title)){
				window.alert(email+"\nにクーポンURLを送信しました。");
			}
			else{
				window.alert("送信失敗しました。");
			}
		}
		else{
			window.alert('メールアドレスを確認して下さい。\n'+email);
		}
		return false;
	});

	//携帯メールに送信
	$("#officialmail").submit(function(){
		var form = $("#officialmail");

		if(!form.find("input[name='account']").val()){
			window.alert("メールアドレスを入力して下さい。");
			return false;
		}

		var email = form.find("input[name='account']").val()+"@"+form.find("select option:selected").val();
		var url = form.find("input[name='url']").val();
		var title = form.find("input[name='shop']").val();

		if(email.match(/([\w\-\.]+)@(docomo|ezweb|[a-z]{1}\.vodafone|softbank|i\.softbank|[a-z]{0,3}\.?pdx)(\.ne)?(\.jp)$/)){
			if (!Mail_To_Mobile2(email, url, title)){
				window.alert(email+"\nに「"+title+"」のホームページURLを送信しました。");
			}
			else{
				window.alert("送信失敗しました。");
			}
		}
		else{
			window.alert('メールアドレスを確認して下さい。\n'+email);
		}
		return false;
	});

	//コンテンツページのクーポンポップ
	$("#contents-pop1, #contents-pop2").hover(
		function () { $(this).removeClass("semitransparent")},
		function () { $(this).addClass("semitransparent")}
	);

	$(".close").click(function () {
		$(this).parent().hide("slow");
	});

	$("input[name='email']").focus(function(){
		$(this).removeClass("empty");
	});

	$("input[name='email']").blur(function(){
		if($(this).val().length == 0) $(this).addClass("empty");
	});

	$("input[name='email']").keyup(function(){
		if($(this).val().length == 0) {
			$(this).next().attr("disabled","disabled");
		}
		else{
			$(this).next().attr("disabled","");
		}
	});

	//ヘッダバナー
	if($("#slid").length){
		$("#slid").easySlider({
			auto: true,
			controlsShow: true,
			continuous: true,
			pause:      5000
		});
	}

	//店舗ページトピックス表示切替
	var topibox = $("div#topics div.topibox dl");
	$("ul#topics-navi").find("li a").click(function(){
		var className = $(this).parent("li").attr("class");
		topibox.removeAttr("class").addClass(className).children().removeAttr("style");
		return false;
	});

	topibox.find("dt").hover(function(){
		if(!$(this).hasClass("all")) $(this).addClass("hover");
	}, function(){
		$(this).removeClass("hover");
	}).click(function(){
		if(topibox.hasClass("list")){
			$(this).next().next().toggle();
			$(this).next().next().next().toggle();
		}
		else if(topibox.hasClass("text")){
			if($(this).hasClass("all")){
				$(this).removeClass("all");
				$(this).next().next().removeAttr("class").addClass("image");
				$(this).next().next().next().removeAttr("class").addClass("body");
			}
			else {
				$(this).addClass("all");
				$(this).next().next().removeAttr("class").addClass("image-all");
				$(this).next().next().next().removeAttr("class").addClass("body-all");
			}
		}
		return false;
	});

	//店舗ページ在籍表示切替
	if($("div#castlist").length){
		var box = $("div#castlist ul.basic, div#castlist ul.newface, div#castlist ul.attend");
	}

	$("ul#cast-menu li a").live("click", function(){
		var anc = $(this).parent("li").attr("id");
		var attend = false;
		$("div#castlist p").remove();
		box.show();
		$("div#castlist ul#cast-menu li").each(function(){
			menuId = $(this).attr("id");
			if(menuId == anc){
				$(this).replaceWith('<li id="' + menuId +'">'+ $(this).text() +'</li>');
			}
			else{
				$(this).replaceWith('<li id="' + menuId +'"><a href="javascript:void(0);">'+ $(this).text() +'</a></li>');
			}
		});
		
		if(anc == "cast-newface"){
			box.not(".newface").hide();
			if(!box.filter(".newface").length) $("ul#cast-menu").after("<p>新人女性の登録はありません。</p>");
		}
		else if(anc == "cast-attend"){
			box.each(function(){
				if(!$(this).find(".text7").length){
					$(this).hide();
				}
				else{
					attend = true;
				}
			});
			if(!attend) $("ul#cast-menu").after("<p>本日の出勤予定は店舗様HPにてご確認下さい。</p>");
		}


		return false;
	});

	//出勤情報絞込み関連
	$("#attendance-form").submit(function(){
		if ($("input[name='pager_flg']").val() == '0') {
			var action = '/attendance/'+$("#att-day option:selected").val()+'.html';
			$(this).attr("action",action);
			return true;
		}
	});

	$("a.att-link").click(function() {
		post_url = $(this).attr('href');
		$("input[name='pager_flg']").val('1');
		$("#attendance-form").attr("action",post_url);
		$("#attendance-form").submit();
		return false;
	});

	//出勤絞込みの時間調節
	if($("form#attendance-form").length){
		opt_set_today = $("#sub-option select").html();
		opt_set_all = $("#att-time").html();
		$("#attendance-form").initAttendTime();
		$("#att-day").change(function(){
			$("#attendance-form").initAttendTime();
		});
	}

	if($("#mailmaga").length){
		//メルマガ送信先
		mailto = $("a.mm").attr("href");
		if(mailto == "mailto:"){
			mailto = "mailto:entry"+"@"+"fspot.net?subject="+"購読開始";
			$("a.mm").attr("href", mailto).each(function(){
				if($(this).text() == "@"){
					$(this).text("entry"+"@"+"fspot.net");
				}
			});
		}

		//フォームのフォーカス時動作
		adrs_form = $("input#address");
		adrs_form.focus(function(){
			$(this).removeClass("off");
		}).blur(function(){
			if($(this).val() == ""){
				$(this).addClass("off");
			}
		});

		//AJAX通信処理
		$("#mmreg").submit(function(){
			if(!$(this).find("input#address").val()){
				window.alert("メールアドレスを入力して下さい。");
				return false;
			}
			var url = $(this).attr("action");
			var address = $(this).find("input[name='address']").val();
			$.post(url, {"mode": "ajax", "address": address}, function(e){
				if(e){
					window.alert(e);
				}
			}, "text");

			return false;
		});
	}

});

$.fn.extend({
	initAttendTime: function(){
		var date = new Date();
		yy = date.getYear();
		mm = date.getMonth() + 1;
		dd = date.getDate();
		if (yy < 2000) { yy += 1900; }
		if (mm < 10) { mm = "0" + mm; }
		if (dd < 10) { dd = "0" + dd; }
		today = yy+"-"+mm+"-"+dd;
		if($("select#att-day option:selected").val() == today){
			$("#att-time").html(opt_set_today);
		}
		else{
			$("#att-time").html(opt_set_all);
		}
		return false;
	}
});

function Mail_To_Mobile(email, url, title){
	$.post("/php/couponmail.php", {email: email, url: url, title: title}, function(rst){
		return rst;
	});
}


function Mail_To_Mobile2(email, url, title){
	$.post("/php/shopmail.php", {email: email, url: url, title: title}, function(rst){
		return rst;
	});
}



function ClickCount(url, img){
	str = document.URL;
	hashIndex = str.indexOf("#");
	page = str.substr(0, hashIndex);
	$.get("/php/counter.php", {site: "1", url: url, img: img, page: page});
}

function addBookmark(title,url) {
	if (window.sidebar) {
			window.sidebar.addPanel(title, url,"");
	} else if( document.all ) {
			window.external.AddFavorite( url, title);
	} else if( window.opera && window.print ) {
			return true;
	}
}

