/**
 *
 */

var g_start_time = (new Date()).getTime();
var g_info_width  = 800;
var g_info_height = 550;

var g_lm = 2011111901;

// alert( "g_start_time:" + g_start_time );

function bg_switch( flag ) {
	$( ".box_left" ).css( 'visibility', 'visible' ); 

	if( $('.btn_news').length ) {
	    last_modify_info( g_lm );
	}
}

$( function () {
	$('.box_main_conent_margin_top').hide();
	$('.box_main_conent_margin_bottom').hide();

	/**
	 * IE6で背景の点滅を防ぐ
	 */
	try {
		document.execCommand("BackgroundImageCache", false, true);
	} catch(e) {
		// nothing;
	}

	img_fit( false );

	$( "div[_href]").each( function () {
		var o = $(this);
		o.click( function () {
			var o = $(this);
//			alert( o.attr( '_href' ) );
			location.href = o.attr( '_href' );
		});
	}).css( 'cursor', 'pointer' );
	$(".qa_a").hide();
	$(".qa").click( function() {
		var o = $(this);
		if( $(".qa_a", o).css( 'display' ) == "none" ) {
			$(".qa_a").hide();
//			$(".qa_a", o).slideDown( 1000 );
			$(".qa_a", o).show();
		} else {
			$(".qa_a").hide();
		}
	} ).css( 'cursor', 'pointer' );

	$(".report_all").hide();
	$(".report table.frame").click( function() {
		var o = $(this);
		var readmore = $(".readmore", o);
		o = o.parent().parent();
		$("[_save_html]").each( function () {
			var o = $(this);
//			alert( o.attr('_save_html') );
			o.html( o.attr('_save_html') );
		});
		if( $(".report_all", o).css( 'display' ) == "none" ) {
			$(".report_all").hide();
			$(".report_all", o).show();
			$(".report_all", o).slideDown( 500 );
			var html;
			html = readmore.html();
			readmore.attr( "_save_html", html );
//			alert( html );
			html = html.replace( /[^>]*<\/p>/i, "閉じる</p>" );
//			alert( html );
			readmore.html( html );
		} else {
			$(".report_all").hide();
		}
	} ).css( 'cursor', 'pointer' );

	$(".hs_overlay").click( function () {
		hs.skin = {
			contentWrapper:
				'<div class="highslide-header"><ul>'+
				'<li class="highslide-previous">'+
				'<a href="#" title="{hs.lang.previousTitle}" onclick="return hs.previous(this)">'+
				'<span>{hs.lang.previousText}</span></a>'+
				'</li>'+
				'<li class="highslide-next">'+
				'<a href="#" title="{hs.lang.nextTitle}" onclick="return hs.next(this)">'+
				'<span>{hs.lang.nextText}</span></a>'+
				'</li>'+
				'<li class="highslide-move">'+
				'<a href="#" title="{hs.lang.moveTitle}" onclick="return false">'+
				'<span>{hs.lang.moveText}</span></a>'+
				'</li>'+
				'<li class="highslide-close">'+
				'<a href="#" title="{hs.lang.closeTitle}" onclick="return hs.close(this)">'+
				'<span>{hs.lang.closeText}</span></a>'+
				'</li>'+
				'</ul></div>'+
				'<div class="highslide-body"></div>'
		};

		hs.allowSizeReduction = true;
		hs.marginLeft = 16;
		hs.marginRight = 16;
		hs.marginTop = 16;
		hs.marginBottom = 16;
		hs.numberPosition = '';

		hs.Expander.prototype.onAfterExpand = function ( sender, e ) {
			if( typeof sender.iframe.contentWindow.set_height != "undefined" ) {
				sender.iframe.contentWindow.set_height();
			}
		};
		

		return hs.htmlExpand( this, { 
			objectType: 'iframe', 
//			outlineType: 'rounded-white', 
			outlineType: 'rounded-blue', 
			wrapperClassName: 'highslide-wrapper drag-header', 
			outlineWhileAnimating: true, 
			preserveContent: true, 
			width: 800,
			height: 600,

			dimmingOpacity: 0
		} );
	} );


	/*
	 * メニューのカレントマーク
	 */
	var path;

	path = document.location.href;
	path = path.replace( /^.*\//, "" );
	path = path.replace( /\?.*$/, "" );
	path = path.replace( /#.*$/, "" );
// alert( path );
	if( path == "" ) {
	    path = "index.html";
	}

	var active_no = -1;
	var no = 0;
	if( 0 ) {
	    $('#accordion .item_sub').hide();
	}
	$('#accordion h3').each( function () {
		var o = $(this);
		var p = $('a', o);
		$( 'a', o.add( o.next() ) ).each( function () {
			var o = $(this);
			var href = o.attr('href');
			if( !href ) {
				href = "";
			}
			href = href.replace( /^.*\//, "" );
			href = href.replace( /\?.*$/, "" );
			href = href.replace( /#.*$/, "" );
			if( href == path ) {
			    //				o.prepend( '<img src="images/triangle_right.png">&nbsp;' );
				o.attr('_mark',1);
				if( o.get(0) != p.get(0) && 0 ) {
					if( !p.attr('_mark') ) {
						p.prepend( '<img src="images/triangle_right.png">&nbsp;' );
					}
				}
				if (1) {
					var q = o;
					for (;;) {
						if (q.attr('id') == "accordion") {
							break;
						}
						q.show();
						q = q.parent();
					}
					q = o.next();
					if (q.is('.item_sub')) {
						q.show();
					}
				}
				active_no = no;
			} else {
				if( o.get(0) != p.get(0) ) {
					o.prepend( '<img src="images/triangle_right_null.png">&nbsp;' );
				}
			}
			return true;
		} );
		//		if( active_no >= 0 ) {
		//			return false;
		//		}
		no++;
		return true;
	} );

	$('#accordion a').each( function () {
		var f = false;
		var o = $(this);
		if( o.attr('_mark') ) {
		    $('a', o.next() ).each( function () {
			    if( $(this).attr('_mark') == 1 ) {
				f = true;
			    }
			} );
		    if( !f && o.parent().get(0).tagName != "H3" ) {
			o.prepend( '<img src="images/triangle_right.png">&nbsp;' );
		    } else {
			o.prepend( '<img src="images/triangle_right_null.png">&nbsp;' );
		    }
		}
	    } );

	$('#accordion h3').each( function () {
		this.onfocus = function() {this.blur(); };
	} );
//	$('#accordion h3').dropShadow();

	if( active_no < 0 ) {
		active_no = -1;
	}

	$('.accordion .sub').show();

	$('#accordion').accordion( {
		active: active_no,
		//event: "mouseover",
		//event: "click",
		event: "mouseDwon",
		autoHeight: false,
		animated:false,
		header: 'h3'
	} );

	/**
	 * 領域外へいったら初期状態に戻す
	 */
if( path != "index.html" ) {
		if( 1 ) {
			$('.box_main_content,.box_whole_inner').hover( function () {
				$('#accordion').accordion( "activate", active_no );
			}, function () {
			} );
		}
		if( 0 ) {
			$('#accordion').hover( function () {
				// nothing
			}, function () {
				$('#accordion').accordion( "activate", active_no );
			} );
		}
	}

	/**
	 * Tipsイメージ
	 */
	$('a.clue').cluetip();
	hsInit();

	$(".btn_news").click( function () {
		return OverlayShow( this );
	} ).css( 'cursor', 'pointer' );

	/**
	 * Slideショー
	 */
	if (0) {
		var o;
		o = $('#main_photo');
		//	alert( o.length );
		if (o.length) {
			var img = new Image();
			img.onload = function(){
				var wx;
				//				wx = $('.box_main_content_left').width(); // コラム幅に合わせる
				wx = 278;
				var z = wx / img.width;
				o.width(wx);
				o.height(Math.floor(img.height * z));
				window.onresize();
			};
			img.src = o.attr('src');
		} else {
			window.onresize();
		}
	} else {
		window.onresize();
	}
	
	/**
	 * 
	 */

	g_ext_info = false;
	
	if( $('.btn_news').length ) {
		g_ext_info = true;
		
		wx = g_info_width;
		wy = g_info_height;

		var ox;
		var oy;
		ox = ($('.box_all').width() - g_info_width) / 2;
		oy = ($('.box_all').height()- g_info_height) / 2;
		$('#box_overlay_inner').css( 'margin-left', ox);
		$('#box_overlay_inner').css( 'margin-top',  oy);
		$('#box_overlay').unbind( 'click' );
		$('#box_overlay').click( function () {
			return OverlayHide();
		} );
	}

	function box_paint_init() {
	    if( typeof document.box_paint_swf.setplayback == "undefined" ||
		typeof document.box_paint_swf.set_already == "undefined" ) {
		setTimeout( box_paint_init, 100 );
		return false;
	    }

	    $('#box_proxy').mousemove( function(e) {
		    if( event_check( e, false ) ) {
			var x;
			var y;
			x = e.pageX;
			y = e.pageY;
			if( typeof document.box_paint_swf.moveMouse != "undefined" ) {
			    document.box_paint_swf.moveMouse( x,y );
			}
		    }
		    return false;
		} ).mousedown( function(e) {
			if( event_check( e, false ) ) {
			    document.box_paint_swf.setDrawFlag(true);
			}
			return false;
		    } ).mouseup( function(e) {
			    if( event_check( e, true ) ) {
			    }
			    document.box_paint_swf.setDrawFlag(false);
			    return false;
			} ).hide();

	    var wx;

	    wx = $('.box_all').width();
	    wx -= $('.box_left').width();
	    $('#box_proxy').width( wx );
	    $('#box_proxy').css( 'left', $('.box_left').width() );
	    $('#box_proxy').height( $('.box_all').height() - 48 );

	    document.box_paint_swf.setMenuWidth( $('.box_left').width() + 12 ); // index.tpl
	    document.box_paint_swf.setplayback( true );

	    document.box_paint_swf.set_already( true );

	    return true;
	}

	if( typeof document.box_paint_swf != "undefined" ) {
	    box_paint_init();
	} else {
	    //	    alert( 'no box_paint_swf' );
	}
} );


/**
 * 
 * @param {Object} show
 */
function img_fit(show) {
	$('img').each( function() {
		var o = $(this);
		var maxsize = o.attr('_maxsize');
		var w,h;
		if( !maxsize ) {
			return;
		}

		if( !show ) {
			o.hide();
			return;
		}

		o.hide();
		//			alert( o.attr( 'width' ) );
		if( !o.attr('_org_width') ) {
			o.attr('_org_width',  o.attr( 'width' ) );
			o.attr('_org_height', o.attr( 'height') );
		}
		var s;
		if( maxsize == 'parent' ) {
			s = o.parent();
		} else {
			s = $(maxsize);
		}
		var z;
		//			alert( s.width() );
		w =	o.attr('_org_width')  - 0;
		h =	o.attr('_org_height') - 0;
		z = s.width() / w;

		w = Math.floor( w * z - 1 );
		h = Math.floor( h * z - 1 );

		o.width( w );
		o.height( h );
		o.show();
	} );
}

/**
 * JST同期用
 * @param {Object} json
 */
function jsont( json ) {
	g_start_time =  json.st * 1000;
//	alert( (g_start_time - (new Date()).getTime()) / 1000 );
}

/**
 * 新着の有無
 * @param {Object} lm
 */
function last_modify_info( lm )
{
	var la = $.cookie('la');
	$.cookie('la', null );
	//	$.cookie('la', g_start_time + '', { path: '/', expires: 31 });
	$.cookie('la', lm + '', { path: '/', expires: 31 });
	// alert( ((la - 0) - lm) / 1000 / 60 + ',' + la + ',' + $.cookie('la') + ',' + g_start_time );
	// alert( la + ', lm = ' + lm );
	if( (la) && ((la - 0) >= lm ) ) {
		// 新着なし
	} else {
		// 新着あり
		OverlayShow();
	}
}

/**
 * 新着非表示
 */

var cancel_event = false;
	

function OverlayHide() {
    alert( OverlayHide );
	document.box_paint_swf.set_already( true );
	if( cancel_event ) {
		cancel_event = false;
		return false;
	}
	$('#box_overlay').hide( 'slide' );
	$('#box_overlay_base').fadeTo( 1000, 0, function() {
		$('#box_overlay_base').hide();
		document.box_paint_swf.set_already( true );
	} );
	document.box_paint_swf.set_already( true );
	return false;
}

/**
 * 新着表示
 */
function OverlayShow() {
    var html;
    html = (
	    "<div class=box_info>" +
	    "<iframe src='news.html' frameborder=0 width=" + wx + " height=" + wy + "></iframe>" +
	    "</div>"
	    );
    $('#box_overlay_inner').html( html );

    var o;

    o = $(".btn_news");
    //    alert( hs );

    hs.skin = {
	contentWrapper:
	'<div class="highslide-header">' +
	'<div style="height:10px;">' + 
	'<div style="position: relative; top: 10px;">' +
	'<img src="img-news/b_news_information.jpg" alt="" width="489" height="25" style="margin-left:10px; margin-top:10px; padding:0px;"/><span style="color:#7772AB;margin-left: 5em;position: relative; top: -4px;">最終更新日 ' +
	Math.floor( (g_lm / 1000000) ) + '.' + 
	Math.floor( (g_lm / 10000  ) ) % 100 + '.' + 
	Math.floor( (g_lm / 100    ) ) % 100 +
	'</span>' +
	'</div>' + 
	'</div>' + 
	'<ul>'+
	'<li class="highslide-previous">'+
	'<a href="#" title="{hs.lang.previousTitle}" onclick="return hs.previous(this)">'+
	'<span>{hs.lang.previousText}</span></a>'+
	'</li>'+
	'<li class="highslide-next">'+
	'<a href="#" title="{hs.lang.nextTitle}" onclick="return hs.next(this)">'+
	'<span>{hs.lang.nextText}</span></a>'+
	'</li>'+
	'<li class="highslide-close">'+
	'&nbsp;&nbsp;&nbsp;' + 
	'<a href="#" title="{hs.lang.closeTitle}" onclick="return hs.close(this)">'+
	'<span><img src="img-news/b_close.gif" alt="" width="26" height="25" style="position:relative; top:10px;" />{hs.lang.closeText}&nbsp;</span></a>'+
	'</li>'+
	'</ul><br style="font-size:8px; clear:both;" /></div>'+
	'<div class="highslide-body"></div>'
    };

    hs.allowSizeReduction = true;
    hs.marginLeft = 8;
    hs.marginRight = 8;
    hs.marginTop = 16;
    hs.marginBottom = 16;
    hs.numberPosition = '';

    hs.Expander.prototype.onAfterExpand = function ( sender, e ) {
	if( typeof sender.iframe.contentWindow.set_height != "undefined" ) {
	    sender.iframe.contentWindow.set_height();
	}
    };

    hs.Expander.prototype.onAfterClose = function ( sender, e ) {
	if( typeof document.box_paint_swf != 'undefined' ) {
		document.box_paint_swf.set_already( true );
	}
    };
		
    $( 'html,body' ).css( 'overflow', 'hidden' );
    
    return hs.htmlExpand( o.get(0), { 
	    objectType: 'iframe', 
		outlineType: 'rounded-blue', 
		wrapperClassName: 'highslide-wrapper drag-header', 
		outlineWhileAnimating: true, 
		preserveContent: true, 
		width: 975,
		height: 600,

		dimmingOpacity: 0.5
		} );
}


function obj_anime( o, sx,sy, ex,ey, duration )
{
	var start = (new Date()).getTime();
	o.css( 'top',  sy );
	o.css( 'left', sx );
	o.show();

// alert( 'obj_anime' );

	function proc_timeout() {
		var d;
		var y;
		var x;
		d = (new Date()).getTime() - start;
		d = d / duration;
		if( d > 1 ) {
			d = 1;
		}
		x = sx + (ex - sx) * d;
		y = sy + (ey - sy) * d;
//		console.debug( 'objMove:y=' + y );
		o.css( 'top', y );
		o.css( 'left', x );
		if( d < 1 ) {
			window.setTimeout( proc_timeout, 0 );
		}
	} 
	window.setTimeout( proc_timeout, 0 );
}

function hsOpen( obj, opt )
{
	opt.width  = $('.box_all').width() - 100;
	opt.height = $('.box_all').height() - 50;

	if( opt.width > 800 ) {
		opt.width = 800;
	}

	return hs.htmlExpand( obj, opt );
}

function hsInit() {
	hs.graphicsDir = 'js/highslide/highslide/graphics/';
	hs.showCredits = false;
	hs.align = 'center';
	hs.transitions = ['expand', 'crossfade'];
	hs.outlineType = 'rounded-white';
	hs.fadeInOut = true;
	hs.numberPosition = 'caption';
	hs.dimmingOpacity = 0.70;
	hs.allowSizeReduction = true;

	hs.lang = {
			   cssDirection: 'ltr',
			   loadingText : '読み込み中...',
			   loadingTitle : 'クリックで中断',
			   focusTitle : 'Click to bring to front',
			   fullExpandTitle : '拡大表示 (f)',
			   creditsText : 'Powered by <i>Highslide JS</i>',
			   creditsTitle : 'Go to the Highslide JS homepage',
			   previousText : '前',
			   nextText : '次', 
			   moveText : '',
			   closeText : '<span style="white-space: nowrap;cursor:pointer;">閉じる</span>', 
			   closeTitle : '閉じる (esc)', 
			   resizeTitle : 'リサイズ',
			   playText : 'スライドショー',
			   playTitle : 'スライドショー (spacebar)',
			   pauseText : '一時停止',
			   pauseTitle : '一時停止 (spacebar)',
			   previousTitle : '前 (arrow left)',
			   nextTitle : '次 (arrow right)',
			   moveTitle : '',
			   fullExpandText : '1:1',
			   number: '%2枚中 %1枚目',
			   restoreTitle : 'Click to close image, click and drag to move. Use arrow keys for next and previous.'
	};

//	hs.transitions = [];
	hs.transitions = ['expand', 'crossfade'];
	hs.transitionDuration = 1000; // default 250
	
//	hs.marginBottom = hs.getPageSize().pageHeight / 10 * 1.2;
//	hs.marginTop    = hs.getPageSize().pageHeight / 10 * 1.2;

//	hs.marginBottom = 48;
//	hs.marginTop    = 48;

	hs.expandDuration = 250;  // 最初の表示
	hs.restoreDuration = 250;
}

window.onresize = function() {
	var wx;
	var wy;
	var h;
	var w;
	var window_width;
	
	var min_width;
	var max_width;
	var h_min;

	img_fit( false );

	var save_display;
	save_display = $('#box_overlay').css( 'display' );
	$('#box_overlay').hide();

	$('#box_proxy').width( 0 );

//	$('.box_main_content_left').css( 'margin-right', 0 );
	$('.box_vpad').height( 0 );
	$('.box_vpad').hide();
//	$('.box_head').hide(); 
//	$('.box_tail').hide();
	
	$('.box_hpad').hide();
	$('.box_cpad').hide();
	$('.box_lpad').hide();
	$('.box_all').width('auto');
	
	window_width = $('.box_all').width();

	var l = $('#main_photo').width() + 4;
	var lp = $('.box_main_content_left').width();
	var c = $('.box_main_content_center').width();
	var r = $('.box_main_content_right').width();

	if( c == null ) {
		lp = r = 0;
		c = $('.box_main_content').width();
	}
	min_width =  lp + c + r + $('.box_left').width() + $('.box_right').width();
	if( min_width < 960 ) {
		min_width = 960;
	}
	max_width =  l  + c + l + $('.box_left').width() + $('.box_right').width();
// alert(window_width + ',min=' + min_width + ',max=' + max_width );
//	$('#box_debug').text( l + ',' + c + ',' + r );
//	$('.box_main_content_left').css( 'margin-right', lp - l - 1 );
	if( window_width < min_width ) {
		$('.box_all').width( min_width );
	} else {
		h = Math.floor( (window_width - min_width) / 6 );
		if( window_width > max_width ) {
			h_max = Math.floor( (window_width - max_width) / 6 );
			if( h_max > h ) {
				h = h_max;
			}
		}
		if( h > 1 ) {
			$('.box_hpad').html( '&nbsp;' );
			$('.box_hpad').width(h);
			$('.box_hpad').show();

			h = h * 2 - (lp - l);
			if( h > 1 ) {
				$('.box_cpad').html( '&nbsp;' ).show();
				$('.box_cpad').width( h );
			}
		}
	}

	$('.box_main_conent_margin_top').hide();
	$('.box_main_conent_margin_bottom').hide();
	
	wx = $('.box_all').width();
	wy = $('.box_all').height();

	wy = wy - $('.box_whole').height();

	h = ($('.box_head').height() + $('.box_tail').height());
	// $('.box_whole').height( h );
	h = (wy - h) / 2;
	if( h < 0 ) {
		h = 0;
	}

	h = 0;
	if( h > 0 ) {
		$('.box_main_conent_margin_top').height( h );
		$('.box_main_conent_margin_bottom').height( h );

		$('.box_main_conent_margin_top').show();
		$('.box_main_conent_margin_bottom').show();

		$('.box_menu .box_vpad').height( h ).show();
		$('.box_menu .box_vpad').height( 0 ).show();
	}

	h = $('.box_whole').height();
//	$('.box_menu .box_vpad').height( (h - $('.box_left' ).height()) / 2 ).show();
	$('.box_ad .box_vpad').height( (h - $('.box_right' ).height())).show();

	$('#box_overlay').css( 'display', save_display );
	img_fit( true );

	if( 0 ) {
		$('.dropshadow').dropShadow({
									opacity:0.2,
									top:4,
									left:4
									});
		$('.dropshadow2').dropShadow({
									opacity:0.2,
									top:2,
									left:2
									});
	}

};

function event_check( e, click ) {
	var result;

	result = _event_check( e, click );
	if( result ) {
		$('#box_proxy').hide();
		result = _event_check( e, click );
		$('#box_proxy').show();
	}

	if( !result ) {
		$('#box_proxy').css( 'cursor', 'pointer' );
	} else {
		$('#box_proxy').css( 'cursor', 'auto' );
	}
	return result;
}

function _event_check( e, click ) {
	var o;
	o = document.elementFromPoint(e.pageX, e.pageY);
	if( !o ) {
		return true;
	}
	var tag;
	$('#box_proxy').show();
	o = $(o);
	while( o.length && (tag = o.get(0).tagName) != 'BODY' ) {
//		alert( tag );
		var c = o.attr( 'onClick' );
		if( tag == 'A' || typeof c != 'undefined' ) {
			if( click ) {
				o.click();
			}
			return false;
		}
		o = o.parent();
	}
	return true;
}

function click_theraphy( o ) {
	return hsOpen( o , {objectType: 'iframe'} );
}

/**
 *
 */

