/*
 * Thickbox 3.1 - One Box To Rule Them All.
 * By Cody Lindley (http://www.codylindley.com)
 * Copyright (c) 2007 cody lindley
 * Licensed under the MIT License: http://www.opensource.org/licenses/mit-license.php
*/

var tb_WindowHeight = 0;

/*!!!!!!!!!!!!!!!!! edit below this line at your own risk !!!!!!!!!!!!!!!!!!!!!!!*/

//on page load call tb_init
$(document).ready(function(){
	tb_init('a.thickbox, area.thickbox, input.thickbox');//pass where to apply thickbox

	// modify H.takahashi >>>>>>>>>>>>>
//	imgLoader = new Image();// preload image
//	imgLoader.src = tb_pathToImage;

	// do at once
	$("*").removeClass("thickbox");
	// <<<<<<<<<<<<< modify H.takahashi
});

//add thickbox to href & area elements that have a class of .thickbox
function tb_init(domChunk){
	$(domChunk).click(function(){
	// modify H.takahashi >>>>>>>>>>>>>
	ajax_scrolling(false);
	// <<<<<<<<<<<<< modify H.takahashi

	var t = this.title || this.name || null;
	var a = this.href || this.alt;
	var g = this.rel || false;
	tb_show(t,a,g);
	this.blur();
	return false;
	});
}

function tb_show(caption, url, imageGroup) {//function called when the user clicks on a thickbox link
	var isInitTab = false;

	var logout = url.indexOf('Logout');
	var logoutBln=false;
	if(logout!=-1){
		logoutBln = true;
	}

	try {
		// modify H.takahashi >>>>>>>>>>>>>
		// safari4以降で、ファイルアップロードの場合は、強制的にモーダル化
		if (-1!=navigator.userAgent.toLowerCase().indexOf('safari')
		 && -1==navigator.userAgent.toLowerCase().indexOf('chrome')
		 && 500 < parseInt(jQuery.browser.version)) {	// Safari4のバージョンは500とか
			if (-1!=url.indexOf("upload_image.html")) {
				url += "&modal=true";
			}
		}

		var queryString = url.replace(/^[^\?]+\??/,'');
		var params = tb_parseQuery( queryString );

		var tb_pathToImage = "/img/assist/parts/loadingAnimation.gif";
		//var tb_closeBtn = "<img border='0' src='"+get_adjust_path(params["lvl"]) + "/img/assist/btn/close_btn.gif' width='69' height='23' alt='close' />";

		var tb_closeBtn="";
		if(params["lvl"]==3||params["lvl"]==2){
			tb_closeBtn = "<img border='0' src='../../img/assist/btn/close_btn.gif' width='69' height='23' alt='close' />";
		}else{
			tb_closeBtn = "<img border='0' src='../img/assist/btn/close_btn.gif' width='69' height='23' alt='close' />";
		}

		tb_pathToImage = get_adjust_path(params["lvl"]) + tb_pathToImage;
		imgLoader = new Image();// preload image
		imgLoader.src = tb_pathToImage;
		// <<<<<<<<<<<<< modify H.takahashi

		if (typeof document.body.style.maxHeight === "undefined") {//if IE 6
			$("body","html").css({height: "100%", width: "100%"});
			$("html").css("overflow","hidden");
			if (document.getElementById("TB_HideSelect") === null) {//iframe to hide select elements in ie6
//				$("body").append("<iframe id='TB_HideSelect'></iframe><div id='TB_overlay'></div><div id='TB_window'></div>");
				$("body").append("<iframe id='TB_HideSelect' src='javascript:false'></iframe><div id='TB_overlay'></div><div id='TB_window'></div>");
				$("#TB_overlay").click(tb_remove);
			}
		}else{//all others
			if(document.getElementById("TB_overlay") === null){
				$("body").append("<div id='TB_overlay'></div><div id='TB_window'></div>");
				$("#TB_overlay").click(tb_remove);
			}
		}

		if(tb_detectMacXFF()){
			$("#TB_overlay").addClass("TB_overlayMacFFBGHack");//use png overlay so hide flash
		}else{
			$("#TB_overlay").addClass("TB_overlayBG");//use background and opacity
			if(logoutBln){
				$("#TB_overlay").click(function(){
					var href = "../../index.php";
					location.href=href;
					//location.reload();
				});
			}
		}

		if(caption===null){caption="";}
		$("body").append("<div id='TB_load'><img src='"+imgLoader.src+"' /></div>");//add loader to the page
		$('#TB_load').show();//show loader

		// modify H.takahashi >>>>>>>>>>>>>
		if (!url) {
			url = "";
		}
		// <<<<<<<<<<<<< modify H.takahashi

		var baseURL;
	   if(url.indexOf("?")!==-1){ //ff there is a query string involved
			baseURL = url.substr(0, url.indexOf("?"));
	   }else{
	   		baseURL = url;
	   }

	   var urlString = /\.jpg$|\.jpeg$|\.png$|\.gif$|\.bmp$/;
	   var urlType = baseURL.toLowerCase().match(urlString);

		if(urlType == '.jpg' || urlType == '.jpeg' || urlType == '.png' || urlType == '.gif' || urlType == '.bmp'){//code to show images

			TB_PrevCaption = "";
			TB_PrevURL = "";
			TB_PrevHTML = "";
			TB_NextCaption = "";
			TB_NextURL = "";
			TB_NextHTML = "";
			TB_imageCount = "";
			TB_FoundURL = false;
			TB_FoundCount = 0;
			if(imageGroup){
				// modify H.takahashi >>>>>>>>>>>>>
				TB_TempArray = $("a[rel="+imageGroup+"]").get();

				TB_TempArray.push( TB_TempArray[0] );
				TB_TempArray.unshift( TB_TempArray[ TB_TempArray.length-2 ] );

//				TB_TempArray = $("a[@rel="+imageGroup+"]").get();
				for (TB_Counter = 1; ((TB_Counter < TB_TempArray.length) && (TB_NextHTML === "")); TB_Counter++) {
				// <<<<<<<<<<<<< modify H.takahashi
					var urlTypeTemp = TB_TempArray[TB_Counter].href.toLowerCase().match(urlString);
					if (!(TB_TempArray[TB_Counter].href == url)) {
						if (TB_FoundURL) {
							TB_NextCaption = TB_TempArray[TB_Counter].title;
							TB_NextURL = TB_TempArray[TB_Counter].href;
							// modify H.takahashi >>>>>>>>>>>>>
							TB_NextHTML = "<span id='TB_next'><a href='#'><img border='0' src='"+get_adjust_path(params["lvl"])+"/img/assist/btn/ahead_btn.png'/></a></span>";
//							TB_NextHTML = "<span id='TB_next'>&nbsp;&nbsp;<a href='#'>Next &gt;</a></span>";

							if (1 == TB_FoundCount) {
								TB_PrevCaption = TB_TempArray[0].title;
								TB_PrevURL = TB_TempArray[0].href;
								TB_PrevHTML = "<span id='TB_prev'><a href='#'><img border='0' src='"+get_adjust_path(params["lvl"])+"/img/assist/btn/back_btn.png'/></a></span>";
							}
							// <<<<<<<<<<<<< modify H.takahashi
						} else {
							TB_PrevCaption = TB_TempArray[TB_Counter].title;
							TB_PrevURL = TB_TempArray[TB_Counter].href;
							// modify H.takahashi >>>>>>>>>>>>>
							TB_PrevHTML = "<span id='TB_prev'><a href='#'><img border='0' src='"+get_adjust_path(params["lvl"])+"/img/assist/btn/back_btn.png'/></a></span>";
//							TB_PrevHTML = "<span id='TB_prev'>&nbsp;&nbsp;<a href='#'>&lt; Prev</a></span>";
							// <<<<<<<<<<<<< modify H.takahashi
						}
					} else {
						TB_FoundURL = true;
						// modify H.takahashi >>>>>>>>>>>>>
						TB_imageCount = " (" + (TB_Counter + 1-1) +"/"+ (TB_TempArray.length-2) + ")";
						TB_FoundCount = TB_Counter;
//						TB_imageCount = "Image " + (TB_Counter + 1) +" of "+ (TB_TempArray.length);
						// <<<<<<<<<<<<< modify H.takahashi
					}
				}
			}

			imgPreloader = new Image();
			imgPreloader.onload = function(){
			imgPreloader.onload = null;

			// Resizing large images - orginal by Christian Montoya edited by me.
			var pagesize = tb_getPageSize();
			var x = pagesize[0] - 150;
			var y = pagesize[1] - 150;
			// modify H.takahashi >>>>>>>>>>>>>
//			var imageWidth = imgPreloader.width+5;
			var imageWidth = 640;//imgPreloader.width;
			var imageHeight = imgPreloader.height;
//			if (imageWidth > x) {
//				imageHeight = imageHeight * (x / imageWidth);
//				imageWidth = x;
//				if (imageHeight > y) {
//					imageWidth = imageWidth * (y / imageHeight);
//					imageHeight = y;
//				}
//			} else if (imageHeight > y) {
//				imageWidth = imageWidth * (y / imageHeight);
//				imageHeight = y;
//				if (imageWidth > x) {
//					imageHeight = imageHeight * (x / imageWidth);
//					imageWidth = x;
//				}
//			}
			// <<<<<<<<<<<<< modify H.takahashi
			// End Resizing

			TB_WIDTH = imageWidth + 30;
			TB_HEIGHT = imageHeight + 60;

			// modify H.takahashi >>>>>>>>>>>>>
			var tmpPrev = TB_PrevHTML;
			var tmpNext = TB_NextHTML;
			var tmpClose= "<a href='#' id='TB_closeWindowButton'>"+tb_closeBtn+"</a>";
			var tmpTitle= "<div id='TB_title'><div id='TB_ajaxWindowTitle' class='title_text'>"+caption+TB_imageCount+"</div><div id='TB_closeAjaxWindow'>"+tmpClose+"</div></div>";
			var tmpBody = "<a href='' id='TB_ImageOff' title='Close'>";
			tmpBody += "<img id='TB_Image' src='"+url+"' width='"+imageWidth+"' height='"+imageHeight+"' alt='"+caption+"' align='middle'/></a>";

			var tmpAll = tmpTitle;
			tmpAll += "<div id='image_bottom'><div id='image_frame'><div id='image_main'>";
			tmpAll += tmpBody + tmpPrev + tmpNext;
			tmpAll += "</div></div></div>";

			$("#TB_window").append(tmpAll);
//			$("#TB_window").append(tmpBody + tmpTitle + tmpClose);
//			$("#TB_window").append("<a href='' id='TB_ImageOff' title='Close'><img id='TB_Image' src='"+url+"' width='"+imageWidth+"' height='"+imageHeight+"' alt='"+caption+"'/></a>" + "<div id='TB_caption'>"+caption+"<div id='TB_secondLine'>" + TB_imageCount + TB_PrevHTML + TB_NextHTML + "</div></div><div id='TB_closeWindow'><a href='#' id='TB_closeWindowButton' title='Close'>close</a> or Esc Key</div>");

			$("#TB_window *").each(function () {
				adjust_image_path(this, params["lvl"]);
			});
			// <<<<<<<<<<<<< modify H.takahashi
			if(logoutBln){
				$("#TB_closeWindowButton").attr('href','/');
				/*$("#TB_closeWindowButton").click(function(){
					location.reload();
				});*/
			}else{
				$("#TB_closeWindowButton").click(tb_remove);
			}

			if (!(TB_PrevHTML === "")) {
				function goPrev(){
					if($(document).unbind("click",goPrev)){$(document).unbind("click",goPrev);}
					$("#TB_window").remove();
					$("body").append("<div id='TB_window'></div>");
					tb_show(TB_PrevCaption, TB_PrevURL, imageGroup);
					return false;
				}
				$("#TB_prev").click(goPrev);
			}

			if (!(TB_NextHTML === "")) {
				function goNext(){
					$("#TB_window").remove();
					$("body").append("<div id='TB_window'></div>");
					tb_show(TB_NextCaption, TB_NextURL, imageGroup);
					return false;
				}
				$("#TB_next").click(goNext);

			}

			document.onkeydown = function(e){
				if (e == null) { // ie
					keycode = event.keyCode;
				} else { // mozilla
					keycode = e.which;
				}
				if(keycode == 27){ // close
					tb_remove();
				} else if(keycode == 190){ // display previous image
					if(!(TB_NextHTML == "")){
						document.onkeydown = "";
						goNext();
					}
				} else if(keycode == 188){ // display next image
					if(!(TB_PrevHTML == "")){
						document.onkeydown = "";
						goPrev();
					}
				}
			};

			tb_position();
			$("#TB_load").remove();
			$("#TB_ImageOff").click(tb_remove);
			$("#TB_window").css("display","block"); //for safari using css instead of show
//			$("#TB_window").css({display:"block"}); //for safari using css instead of show
			};

			imgPreloader.src = url;
		}else{//code to show html
			// modify H.takahashi >>>>>>>>>>>>>
			if (jQuery.browser.msie && "javascript:" == url) { // ie
				url = "";
			}

			if (!url || -1 == url.indexOf('?', 0)) {
				url = url + '?noparam';
			}

//			var queryString = url.replace(/^[^\?]+\??/,'');
//			var params = tb_parseQuery( queryString );

			TB_WIDTH = (params['width']*1) + 30 || 848 //defaults to 630 if no paramaters were added to URL
			TB_HEIGHT = 500; //defaults to 440 if no paramaters were added to URL
//			TB_WIDTH = (params['width']*1) + 30 || 630; //defaults to 630 if no paramaters were added to URL
//			TB_HEIGHT = (params['height']*1) + 40 || 440; //defaults to 440 if no paramaters were added to URL
			// <<<<<<<<<<<<< modify H.takahashi

			ajaxContentW = TB_WIDTH - 30;
			ajaxContentH = TB_HEIGHT - 45;

			if(url.indexOf('TB_iframe') != -1){// either iframe or ajax window
				urlNoQuery = url.split('TB_');
				$("#TB_iframeContent").remove();
				if(params['modal'] != "true"){//iframe no modal
					// modify H.takahashi >>>>>>>>>>>>>
					$("#TB_window").append("<div id='TB_title840' class='clear'><div id='TB_ajaxWindowTitle' class='title_text'>"+caption+"</div><div id='TB_closeAjaxWindow'><a href='#' id='TB_closeWindowButton' title='Close'>"+tb_closeBtn+"</a></div></div><iframe frameborder='0' hspace='0' src='"+urlNoQuery[0]+"' id='TB_iframeContent' name='TB_iframeContent"+Math.round(Math.random()*1000)+"' onload='tb_showIframe()' style='width:"+(ajaxContentW + 29)+"px;height:"+(ajaxContentH + 17)+"px;' > </iframe>");
//					$("#TB_window").append("<div id='TB_title'><div id='TB_ajaxWindowTitle'>"+caption+"</div><div id='TB_closeAjaxWindow'><a href='#' id='TB_closeWindowButton' title='Close'>close</a> or Esc Key</div></div><iframe frameborder='0' hspace='0' src='"+urlNoQuery[0]+"' id='TB_iframeContent' name='TB_iframeContent"+Math.round(Math.random()*1000)+"' onload='tb_showIframe()' style='width:"+(ajaxContentW + 29)+"px;height:"+(ajaxContentH + 17)+"px;' > </iframe>");
					// <<<<<<<<<<<<< modify H.takahashi
				}else{//iframe modal
					$("#TB_overlay").unbind();
					$("#TB_window").append("<iframe frameborder='0' hspace='0' src='"+urlNoQuery[0]+"' id='TB_iframeContent' name='TB_iframeContent"+Math.round(Math.random()*1000)+"' onload='tb_showIframe();' style='width:"+(ajaxContentW + 29)+"px;height:"+(ajaxContentH + 17)+"px;'> </iframe>");
				}
			}else{// not an iframe, ajax
				if($("#TB_window").css("display") != "block"){
					// modify H.takahashi >>>>>>>>>>>>>
					var tmpClose = "<a href='#' id='TB_closeWindowButton'>"+tb_closeBtn+"</a>";
					var tmpTitle = "<div id='TB_title840' class='clear'><div id='TB_ajaxWindowTitle' class='title_text'>"+caption+"</div><div id='TB_closeAjaxWindow'>"+tmpClose+"</div></div>";
					var tmpBody = "<div id='TB_ajaxContent' style='width:"+ajaxContentW+"px; padding:0;'></div>";
					// <<<<<<<<<<<<< modify H.takahashi

					if(params['modal'] != "true"){//ajax no modal
						// modify H.takahashi >>>>>>>>>>>>>

						$("#TB_window").append(tmpTitle+tmpBody);
//						$("#TB_window").append("<div id='TB_title'><div id='TB_ajaxWindowTitle'>"+caption+"</div><div id='TB_closeAjaxWindow'><a href='#' id='TB_closeWindowButton'>close</a> or Esc Key</div></div><div id='TB_ajaxContent' style='width:"+ajaxContentW+"px;height:"+ajaxContentH+"px'></div>");
						// <<<<<<<<<<<<< modify H.takahashi
					}else{//ajax modal
						$("#TB_overlay").unbind();
						// modify H.takahashi >>>>>>>>>>>>>
						$("#TB_window").append(tmpTitle+tmpBody);
//						$("#TB_window").append("<div id='TB_ajaxContent' class='TB_modal' style='width:"+ajaxContentW+"px;height:"+ajaxContentH+"px;'></div>");
						// <<<<<<<<<<<<< modify H.takahashi
					}
				}else{//this means the window is already up, we are just loading new content via ajax
					$("#TB_ajaxContent")[0].style.width = ajaxContentW +"px";
					$("#TB_ajaxContent")[0].style.height = ajaxContentH +"px";
					$("#TB_ajaxContent")[0].scrollTop = 0;
					$("#TB_ajaxWindowTitle").html(caption);
				}
			}

			if(logoutBln){
				$("#TB_closeWindowButton").attr('href','/');
				/*$("#TB_closeWindowButton").click(function(){
					location.reload();
				});*/
			}else{
				$("#TB_closeWindowButton").click(tb_remove);
			}
			// modify H.takahashi >>>>>>>>>>>>>
			$("#TB_closeWindowButton2").click(tb_remove);
			// <<<<<<<<<<<<< modify H.takahashi

			if(url.indexOf('TB_inline') != -1){
				$("#TB_ajaxContent").append($('#' + params['inlineId']).children());
				$("#TB_window").unload(function () {
					$('#' + params['inlineId']).append( $("#TB_ajaxContent").children() ); // move elements back when you're finished
				});
				tb_position();
				$("#TB_load").remove();
				$("#TB_window").css("display","block");
//				$("#TB_window").css({display:"block"});
			}else if(url.indexOf('TB_iframe') != -1){
				tb_position();
				if($.browser.safari){//safari needs help because it will not fire iframe onload
					$("#TB_load").remove();
					$("#TB_window").css("display","block");
//					$("#TB_window").css({display:"block"});
				}
			}else{
				$("#TB_ajaxContent").load(url += "&random=" + (new Date().getTime()),function(data){//to do a post change this load method
					tb_position();
					$("#TB_load").remove();
					tb_init("#TB_ajaxContent a.thickbox");

					// modify H.takahashi >>>>>>>>>>>>>
					$("#TB_window").css("display","block");
//					$("#TB_window").css({display:"block"});

					// set title text
					$("#TB_ajaxWindowTitle").text($("#TB_WindowTitle").text());

					if(logoutBln){
						/*$("#TB_closeWindowButton").click(function(){
							location.reload();
						});*/
						$("#TB_closeWindowButton").attr('href','/');
					}

					var level = params["lvl"];
					var adjustPath = get_adjust_path(level);

					if (params["get_daishi"]) {
						get_daishi_list(level,"");
					}
					if (params["view_image"]) {
						get_image_data(level);
					}
					if (params["view_send_image"]) {
						get_send_image_data(level,params["session_id"],params["dempo_id"]);
					}
					if (params["init_address"]) {
						init_select_address(level);
						if(params["addr_type"]==5){
							$("#TB_ajaxWindowTitle").text("お申込者の住所を選択してください");
						}
					}
					if (params["init_tab"]) {
						init_sample_message_tab(level);
					}
					if (params["init_tab_okuyami"]) {
						init_sample_message_tab_okuyami(level);
					}
					if (params["init_upload"]) {
						init_upload_image(params['PARM_PROCESS_FLG'],level);
					}
					if (params["init_addr"]) {
						init_addr_list(params["init_addr"], level);
					}
					if (params["init_otodoke"]) {
						init_otodoke(level);
						if(params["nextDenpoNO"]>0){
							$("#TB_ajaxWindowTitle").text(params["nextDenpoNO"]+'通目の電報の情報を入力してください');
						}
					}
					if (-1 != url.indexOf("select_flower.html")) {
						var processflg = "";
						if(null != params['PARM_PROCESS_FLG'])
						{
							processflg = params['PARM_PROCESS_FLG'];
						}
						init_select_flower(level,params['PARM_ITEM_ID_SYS'] ,processflg);
					}
					if (-1 != url.indexOf("guide_flower.html")) {
						init_guide_flower(level);
					}
					if (-1 != url.indexOf("help01.html")) {
						init_help01(level);
					}
					if (-1 != url.indexOf("corporate_logout.html")) {
						init_corp_logout(level);
					}
					if (-1 != url.indexOf("corporate_password.html")) {
						init_corp_passwd(level);
					}

					if (!params["np"]) {
						process_per_ua();
					}

					$("#TB_ajaxContent *").each(function () {
						adjust_image_path(this, params["lvl"]);
					});
					// <<<<<<<<<<<<< modify H.takahashi
				});
			}
		}

		if(!params['modal']){
			document.onkeyup = function(e){
				if (e == null) { // ie
					keycode = event.keyCode;
				} else { // mozilla
					keycode = e.which;
				}
				if(keycode == 27){ // close
					tb_remove();
				}
			};
		}
	} catch(e) {
		//nothing here
	}
}

//helper functions below
function tb_showIframe(){
	$("#TB_load").remove();
	$("#TB_window").css("display","block");
//	$("#TB_window").css({display:"block"});
}

function tb_remove() {
 	$("#TB_imageOff").unbind("click");
	$("#TB_closeWindowButton").unbind("click");
	$("#TB_window").fadeOut("fast",function(){
		$('#TB_window,#TB_overlay,#TB_HideSelect').trigger("unload").unbind().remove();
	});
	$("#TB_ajaxContent").remove();
	$("#TB_load").remove();
	if (typeof document.body.style.maxHeight == "undefined") {//if IE 6
		$("body","html").css({height: "auto", width: "auto"});
		$("html").css("overflow","");
	}
	document.onkeydown = "";
	document.onkeyup = "";

	// modify H.takahashi >>>>>>>>>>>>>
	ajax_scrolling(true);
	// <<<<<<<<<<<<< modify H.takahashi

	return false;
}

function tb_position() {
$("#TB_window").css({marginLeft: '-' + parseInt((TB_WIDTH / 2),10) + 'px', width: TB_WIDTH + 'px'});
	if ( !(jQuery.browser.msie && jQuery.browser.version < 7)) { // take away IE6
		// TODO: window top margin
		var mt = parseInt((TB_HEIGHT / 2),10);// + (TB_HEIGHT/10);
		$("#TB_window").css({marginTop: '-' + mt + 'px'});
	}
}

function tb_parseQuery ( query ) {
   var Params = {};
   if ( ! query ) {return Params;}// return empty object
   var Pairs = query.split(/[;&]/);
   for ( var i = 0; i < Pairs.length; i++ ) {
      var KeyVal = Pairs[i].split('=');
      if ( ! KeyVal || KeyVal.length != 2 ) {continue;}
      var key = unescape( KeyVal[0] );
      var val = unescape( KeyVal[1] );
      val = val.replace(/\+/g, ' ');
      Params[key] = val;
   }
   return Params;
}

function tb_getPageSize(){
	var de = document.documentElement;
	var w = window.innerWidth || self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
	var h = window.innerHeight || self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight;
	arrayPageSize = [w,h];
	return arrayPageSize;
}

function tb_detectMacXFF() {
  var userAgent = navigator.userAgent.toLowerCase();
  if (userAgent.indexOf('mac') != -1 && userAgent.indexOf('firefox')!=-1) {
    return true;
  }
}

// ハッシュ化した QueryStringを取得する
function get_hashed_param(url) {
	var params = tb_parseQuery(url.replace(/^[^\?]+\??/,''));
	if (!params["item"]) {
		params["item"] = 0;
	}
	var hash = Array();
	hash["type"] = params["type"];
	hash["no"] = params["no"]
	hash["item"] = params["item"];
//	var hash = {type:params["type"],no:params["no"],item:params["item"]};
//alert("type = " + params["type"] + "\n" + "no = " + params["no"] + "\n" + "item = " + params["item"] );

	return hash;
}

function process_per_ua() {
	var ua = navigator.userAgent.toLowerCase();
	var version = parseInt(jQuery.browser.version);

	// all adjust
	$("#sub_window_main #sub_main h2").css("padding-top","8px");

	if (jQuery.browser.msie) {
		var titlePadding = 0;
		if (version < 7) {
			titlePadding = 0;
		} else {
			titlePadding = 5;
			$("#sub_window_main #sub_main h2").css("padding-top","10px");
		}

		var hght = $("#TB_title840").css("height");
		var ptop = $("#TB_title840").css("padding-top");
		if (hght) {
			hght = parseInt(hght.replace(/px/,''),10);
			hght -= titlePadding;
		}
		if (ptop) {
			ptop = parseInt(ptop.replace(/px/,''),10);
			ptop += titlePadding;
		}

	    //strUA = navigator.userAgent.toLowerCase();
		//if(!hght && strUA.indexOf("msie 7.0") != -1){
		//alert("hoge");

		if(hght){
			$("#TB_title840").css("height", hght + "px");
		}else{
			if(strUA.indexOf("msie 7.0") != -1){
				$("#TB_closeAjaxWindow a img").css("padding-top", "5px");
				//alert("ie7");
				//alert("error before");
				//$("#TB_title840").css("height", hght + "px");
				//alert("error after");
				//$("#TB_title840").css("height", "44px");
				//$("#TB_title840").css("line-height", "44px");
				//$("#TB_title840").css("background", "yellow");
				//$("#TB_title840").css("padding", "30");
				//$("#TB_title840").css("height", "px");
				//$("#TB_title840").css("height:px");
				//$("#TB_title840").css("height", "px");
				//$("#TB_title840").css("padding-top", "10px");
			}
		}


	} else if (ua.indexOf('firefox')!=-1) {
	} else if (ua.indexOf('chrome')!=-1) {
		// chromeの uaには、safariが含まれるので注意！
	} else if (ua.indexOf('safari')!=-1) {
	} else {
	}
}

// imgタグの階層をパラメータをもとに調整
function adjust_image_path(tag, level) {
	if (!level || isNaN(level)) {
		level = 0;
	}

	var path = get_adjust_path(level);
	var src = $(tag).attr("src");

	if (src && 0 == src.indexOf('/img')) {
		$(tag).attr("src", path + $(tag).attr("src"));
	}

	var target = ["onMouseOver", "onMouseOut", "onClick"];
	for (var idx = 0; idx < target.length; idx++) {
		var tmp = $(tag).attr( target[ idx ] );
		if (tmp) {
			if(String(tmp).match(RegExp(".src='(.*)'.*", "i"))){
				var url = RegExp.$1;
				if (0 == url.indexOf('/img')) {
					var urlAfter = path + url;
					$(tag).attr(target[ idx ], String(tmp).replace(url, urlAfter));
					$("#_preloadimagedivarea").append($("<img>").attr("src", urlAfter));
				}
			}
		}
	}
}

function get_adjust_path(level) {
	if (!level || isNaN(level)) {
		level = 0;
	}

	var path= "";

	if (0 == level) {
		path = "";
		path += ".";
	} else {
		path = "";
		for (var idx = 0; idx < level; idx++) {
			if (0 < idx) {
				path += "/";
			}
			path += "..";
		}
	}

	return path;
}

function test_test(e) {
//	alert("Event : " + e);
}

function ajax_scrolling(isEnable) {
	strUA = navigator.userAgent.toLowerCase();
	if(strUA.indexOf("msie 7.0") != -1){
		if (!isEnable) {
			$("html").css("overflow", "hidden");
		} else {
			$("html").css("overflow", "auto");
		}
	}else{
		if (!isEnable) {
			$("body").css("overflow", "hidden");
		} else {
			$("body").css("overflow", "auto");
		}
	}
}

/**********************************/
function init_select_flower(level,PARM_ITEM_ID_SYS ,processflg) {
	var adjustPath = get_adjust_path(level);

	//$("#button1").click(tb_remove);

	$("#style1").click(function() {
		document.getElementById('price_sample_image').src = adjustPath + "/img/assist/flower/hanataba.jpg";
	});
	$("#style2").click(function() {
		document.getElementById('price_sample_image').src = adjustPath + "/img/assist/flower/arrengement.jpg";
	});

	document.getElementById('thisHREF').value = document.location.href;

	document.getElementById('processflg').value = processflg;
	get_flower_info(level,PARM_ITEM_ID_SYS,processflg);
}

function init_guide_flower(level) {
	var adjustPath = get_adjust_path(level);

	new Image().src = adjustPath+"/img/assist/guide/tab_happyevent_act.gif";
	new Image().src = adjustPath+"/img/assist/guide/tab_happyevent_off.gif";
	new Image().src = adjustPath+"/img/assist/guide/tab_happyevent_on.gif";
	new Image().src = adjustPath+"/img/assist/guide/tab_condolence_act.gif";
	new Image().src = adjustPath+"/img/assist/guide/tab_condolence_off.gif";
	new Image().src = adjustPath+"/img/assist/guide/tab_condolence_on.gif";
	new Image().src = adjustPath+"/img/assist/guide/tab_general_act.gif";
	new Image().src = adjustPath+"/img/assist/guide/tab_general_off.gif";
	new Image().src = adjustPath+"/img/assist/guide/tab_general_on.gif";

	var list = [1, 2, 3];
	jQuery.each(list, function (tmp, index) {
		$("#guide_tab"+index).click(function() {
			// IE6
			if (jQuery.browser.msie && parseInt(jQuery.browser.version) < 7) {
				// IE6だと、attr("onMouseOver", ".....") が効かなかった
				document.getElementById("tab_img1").src = adjustPath+"/img/assist/guide/tab_happyevent_off.gif";
				document.getElementById("tab_img2").src = adjustPath+"/img/assist/guide/tab_condolence_off.gif";
				document.getElementById("tab_img3").src = adjustPath+"/img/assist/guide/tab_general_off.gif";

				$("#tab_img1").hover( function () { $(this).attr("src", adjustPath+"/img/assist/guide/tab_happyevent_on.gif");
									},function () { $(this).attr("src", adjustPath+"/img/assist/guide/tab_happyevent_off.gif");});
				$("#tab_img2").hover( function () { $(this).attr("src", adjustPath+"/img/assist/guide/tab_condolence_on.gif");
									},function () { $(this).attr("src", adjustPath+"/img/assist/guide/tab_condolence_off.gif");});
				$("#tab_img3").hover( function () { $(this).attr("src", adjustPath+"/img/assist/guide/tab_general_on.gif");
									},function () { $(this).attr("src", adjustPath+"/img/assist/guide/tab_general_off.gif");});

				if (1 == index) {
					document.getElementById("tab_img1").src = adjustPath+"/img/assist/guide/tab_happyevent_act.gif";
					$("#tab_img1").unbind("mouseenter").unbind("mouseleave");
				}
				if (2 == index) {
					document.getElementById("tab_img2").src = adjustPath+"/img/assist/guide/tab_condolence_act.gif";
					$("#tab_img2").unbind("mouseenter").unbind("mouseleave");
				}
				if (3 == index) {
					document.getElementById("tab_img3").src = adjustPath+"/img/assist/guide/tab_general_act.gif";
					$("#tab_img3").unbind("mouseenter").unbind("mouseleave");
				}
			} else {
				$("#tab_img1").attr({"src":adjustPath+"/img/assist/guide/tab_happyevent_off.gif","alt":"慶事", "onMouseOver":"document.getElementById('tab_img1').src='"+adjustPath+"/img/assist/guide/tab_happyevent_on.gif';", "onMouseOut":"document.getElementById('tab_img1').src='"+adjustPath+"/img/assist/guide/tab_happyevent_off.gif';","border":0});
				$("#tab_img2").attr({"src":adjustPath+"/img/assist/guide/tab_condolence_off.gif","alt":"弔辞", "onMouseOver":"document.getElementById('tab_img2').src='"+adjustPath+"/img/assist/guide/tab_condolence_on.gif';", "onMouseOut":"document.getElementById('tab_img2').src='"+adjustPath+"/img/assist/guide/tab_condolence_off.gif';","border":0});
				$("#tab_img3").attr({"src":adjustPath+"/img/assist/guide/tab_general_off.gif","alt":"一般", "onMouseOver":"document.getElementById('tab_img3').src='"+adjustPath+"/img/assist/guide/tab_general_on.gif';", "onMouseOut":"document.getElementById('tab_img3').src='"+adjustPath+"/img/assist/guide/tab_general_off.gif';","border":0});

				if (1 == index) {	$("#tab_img1").attr({"src":adjustPath+"/img/assist/guide/tab_happyevent_act.gif", "onMouseOver":"", "onMouseOut":"","border":0});}
				if (2 == index) {	$("#tab_img2").attr({"src":adjustPath+"/img/assist/guide/tab_condolence_act.gif", "onMouseOver":"", "onMouseOut":"","border":0});}
				if (3 == index) {	$("#tab_img3").attr({"src":adjustPath+"/img/assist/guide/tab_general_act.gif", "onMouseOver":"", "onMouseOut":"","border":0});}
			}

			if (1 == index) {
				$("#guide_f1").css("display","block");
				$("#guide_f2").css("display","none");
				$("#guide_f3").css("display","none");
			}
			if (2 == index) {
				$("#guide_f1").css("display","none");
				$("#guide_f2").css("display","block");
				$("#guide_f3").css("display","none");
			}
			if (3 == index) {
				$("#guide_f1").css("display","none");
				$("#guide_f2").css("display","none");
				$("#guide_f3").css("display","block");
			}
		});
	});

	$("#guide_tab1").click();
}

function init_help01(level) {
	var adjustPath = get_adjust_path(level);

	// IE6の相対パス対応のため、再度 load関数で取得＆設定
	$("#btn_youtobetuguid").click(function () {
		var url = adjustPath + "/assist/guide_flower.html?lvl="+level+"&np=yes";

		$("#TB_ajaxContent").load(url + "&random=" + (new Date().getTime()), function (html) {
			$("#TB_ajaxWindowTitle").text($("#TB_WindowTitle").text());
			init_guide_flower(level);
		});
	});
}

function init_corp_logout(level) {
	var target_url = get_adjust_path(level)+"/api/test/corporate_logout.php";

	$.get(target_url, function (xml) {
		var logout_auth    = $(xml).find("logout_auth").text();
		var logout_message = $(xml).find("logout_message").text();
		//logout_auth = 1; success
		//logout_auth = 0; error
		$("#corp_logout #message").empty();
		$("#corp_logout #message").text(logout_message);
	});
}

