/* ===========================================
 * Bitoki Life
 * -------------------------------------------
 * JS Library
 * -------------------------------------------
 * 文字コード UTF-8
=========================================== */

var j$ = jQuery.noConflict();

/* ---------------------------------------- */


/* Flash
=========================================== */

function drawSwf(id, url, w, h){
	var version = "8";
	var express = "";
	var flashvars = {};
	var attributes = {};
	var params = {
		quality: "high",
		menu: "false"
	};

	swfobject.embedSWF(url, id, w, h, version, express, flashvars, params, attributes);
}


/* FontSize
=========================================== */

var fsize = {
	getCookie : function(){
		fsize.c = j$.cookie("bitoki_fsize");
		if(!fsize.c) fsize.c = "fs_m";
		fsize.setCookie();
	},
	setCookie : function(){
		var s = "/common/css/font/" + fsize.c + ".css";
		j$("#fsize").attr("href", s);
		j$.cookie("bitoki_fsize", fsize.c, { expires:365, path:'/' });
	},
	setInit : function(){
		j$("#fontNavi>li>img:gt(0)").each(function(){
			j$(this).click(function(){
				fsize.set(j$(this).parent().attr("id"));
				return false;
			});
			j$(this).mouseover(function(){
				j$(this).css("cursor", "pointer");
			});
		});
		fsize.setImg();
	},
	setImg : function(){
		j$("#fontNavi>li>img:gt(0)").each(function(){
			if(j$(this).parent().attr("id") == fsize.c){
				j$(this).addClass("ro_stay");
				this.src = this.src.replace("_n.", "_o.");
			} else {
				j$(this).removeClass("ro_stay");
				this.src = this.src.replace("_o.", "_n.");
			}
		});
	},
	set : function(id){
		if(id != fsize.c){
			fsize.c = id;
			fsize.setCookie();
			fsize.setImg();
		}
	}
}
fsize.getCookie();


/* RollOver
=========================================== */

var rollover = {
	setInit : function(){
		rollover.p = new Object();
		j$("img[src*='_n.'],input[src*='_n.']").each(function(){
			var n = this.src;
			var o = n.replace("_n.", "_o.");
			if(!rollover.p[n]){
				rollover.p[n] = new Image();
				rollover.p[n].src = n;
				rollover.p[o] = new Image();
				rollover.p[o].src = o;
			}
			if(!j$(this).hasClass("ro_exc")){
				j$(this).hover(function(){
					if(!j$(this).hasClass("ro_stay") && this.src.lastIndexOf("_n.") > -1) this.src = rollover.p[o].src;
				}, function(){
					if(!j$(this).hasClass("ro_stay") && this.src.lastIndexOf("_o.") > -1) this.src = rollover.p[n].src;
				});
			}
		});
	}
}


/* BITOKIST
=========================================== */

var bitokist = {
	flatHeights : function(){
		j$("#content>.list_box>.wrap").each(function(){
			var sets = [], temp = [];
			j$('>.inner_box', this).each(function(i){
				temp.push(this);
				if(i % 4 == 3){
					sets.push(temp);
					temp = [];
				}
			});
			if(temp.length) sets.push(temp);
			j$.each(sets, function(){
				j$(this).flatHeights();
			});
		});
	},
	switchHat : function(){
		var t = j$("#side ul.switchDetail");
		j$(t).hide();
		j$("#side p.btn_close_open").click(function(){
			if(j$(this).hasClass("hat")){
				j$(">span", this).text("BITOKISTをもっと見る");
			} else {
				j$(">span", this).text("閉じる");
			}
			j$(t).slideToggle("hat");
			j$(this).toggleClass("hat");
		});
	},
	searchOdd : function(){
		j$("#content>.search_box").each(function(){
			j$(">div:odd", this).addClass('odd');
		});
	}
}


/* AnimeScroll
=========================================== */

j$.easing.quart = function(x, t, b, c, d){
	return -c * ((t=t/d-1)*t*t*t - 1) + b;
};

var aniscroll = {
	setInit : function(){
		j$('a[href*=#]').click(function(){
			if(location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname){
				var $target = j$(this.hash);
				$target = $target.length && $target || j$('[name='+this.hash.slice(1)+']');
				if($target.length){
					var targetOffset = $target.offset().top;
					var targetTag = navigator.appName.match(/Opera/)? "html" : "html,body";
					j$(targetTag).animate({scrollTop: targetOffset}, 'quart');
					return false;
				}
			}
		});
	}
}


/* ===========================================
 * START
=========================================== */

j$(document).ready(function(){
	var p = location.pathname;

	// RollOver
	rollover.setInit();

	// FontSize
	fsize.setInit();

	// BITOKIST
	if(p.indexOf("/bitokist/") == 0 || p.indexOf("/mt/") == 0){
		bitokist.flatHeights();
		bitokist.switchHat();
		bitokist.searchOdd();
	}

	// Form
	j$("form .aform-note").each(function(){
		j$(this).html(j$(this).text().split("　　").join("<br />"));
	});
	j$("form input[type=submit]").each(function(){
		if(j$(this).attr("value") == "確認する") j$(this).attr("value", "入力内容の確認");
	});

	// Special Shine
	if(p.indexOf("/special/shine/") == 0 || p.indexOf("/anti-ageing/") == 0){
		j$("h3.trigger>a").each(function(){
			var h = j$(this).attr("href");
			j$(h).hide();
			j$(this).attr("href", "").click(function(){
				j$(h).slideToggle("fast");
				var s = j$(">img", this).attr("src");
				var s = (s.lastIndexOf("_o.") > -1)? s.replace("_o.", "_s.") : s.replace("_s.", "_o.");
				j$(">img", this).attr("src", s);
				return false;
			});
		});
	}

	// AnimeScroll
	aniscroll.setInit();

});

