var past = 0;
var counter = 0;
function ManageHeight()
{
	var block_bg = 233;
	var top_bg = 353;
	var height = $('#myElement').height() ;
	var remain = height - top_bg;
	var rate =remain / block_bg;
	var target = (Math.ceil(rate) * block_bg + top_bg);
	if(counter == 0)
	past = target;
	
	counter++;
	$('#myElement').height(target);
	
	
	
}



function init(){
	var list_height = $('#list').height() + 30;
	$('#div_table').height(list_height);
	
   ManageHeight();	
}



function init_press(){
	var list_height = $('#list').height() + 10;
	$('#div_table').height(list_height);
	
   ManageHeight();	
}










function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}


function _open(){
   $(gallery).attr("style", "display:inline");

	init();
//	$("#gallery").show("normal", function() { 
	var target = $("#step1");
	var targetOffset = target.offset().top;
	$('html,body').animate({scrollTop: targetOffset}, 750); 
	//});

	$(step1).attr("style", "display:none");
	$(step2).attr("style", "display:inline");
	$(step3).attr("style", "display:inline");
	
	$(facility1).attr("style", "display:none");
	$(facility2).attr("style", "display:inline");
	
	init();
}

function _close(){
	$(gallery).attr("style", "display:none");
	/*$("#gallery").slideUp("fast", function() { var target = $("#step1");
	var targetOffset = target.offset().top;
	$('html,body').animate({scrollTop: targetOffset}, 500); 
	});*/

	$(step1).attr("style", "display:inline");
	$(step2).attr("style", "display:none");
	$(step3).attr("style", "display:none");

	$(facility1).attr("style", "display:inline");
	$(facility2).attr("style", "display:none");
	
	backme();
	
}



function backme()
{
	$('#myElement').height(past);
}




function  swap_source(){ $("#close_img").attr("src", "lib/links/xover.jpg"); }
function  back_source(){ $("#close_img").attr("src", "lib/links/xnormal.jpg"); }


function  swap_source_cd(id){ $("#close_cd"+id).attr("src", "lib/links/xo.png"); }
function  back_source_cd(id){ $("#close_cd"+id).attr("src", "lib/links/xn.png"); }

function  swap_source_video(id){ $("#close_video"+id).attr("src", "lib/links/xo.png"); }
function  back_source_video(id){ $("#close_video"+id).attr("src", "lib/links/xn.png"); }








var past_section = 0;
var past_class = 0;

function _go(id,c){
	
	
	if(id == past_section) return;
$("#para"+past_section).hide();
	
	$("#para"+id).attr("style", "display:inline;");
	$("#header").attr("style", "display:inline");
	$("#footer").attr("style", "display:inline");
	
	var hx = $("#mytable"+id).height() ;
	$("#mycontents"+id).height(hx);
	

	var nc = "link_"+c+"_selected";
	var pc = "link_"+past_class;
	
	$("#sub"+id).attr("class", nc);
	$("#sub"+past_section).attr("class", pc);
	
	
	

	
											 
	var target = $("#sub"+id);
	var targetOffset = target.offset().top;
	$('html,body').animate({scrollTop: targetOffset}, 750); 

	//$("#para"+past_section).attr("style", "display:none;");
	
	
	past_section = id;
	past_class = c;
	
}



function appear(id){ $("#video_overlay"+id).show(); }
function disappear(id){ $("#video_overlay"+id).hide(); }






var cd_popup="";

var _current_contact = 0;
function _show_contact(id){
	_current_contact = id;
	_locate_contact();
	$("#black").css({
			"opacity": "0.75"
		});
		$("#black").fadeIn("normal",function(){
		$("#cd_"+id).show();
		
		});
	
	_locate_contact();
}


function _locate_contact(){
	
	$("body").css("overflow", "hidden");
	
	//request data for centering
	var windowWidth = $("body").width();
	var windowHeight = $("body").height();
	
	var popupHeight = $("#cd_"+_current_contact).height();
	var popupWidth =  $("#cd_"+_current_contact).width();
	
	var blackHeight = $("#black").height();
	var blackWidth =  $("#black").width();
	
	var scrollTop = $(window).scrollTop();

  

	//centering
	$("#cd_"+_current_contact).css({
		"position": "absolute",
		"top": windowHeight/2-popupHeight/2 + scrollTop ,
		"left": windowWidth/2-popupWidth/2 
	});

	
	$("#black").css({
		"position": "absolute",
		"top": windowHeight/2-blackHeight/2  + scrollTop,
		"left": windowWidth/2-blackWidth/2
	});
	
}





function _close_contact() {
$("#black").fadeOut("normal");
$("#cd_"+_current_contact).hide();
$("body").css("overflow", "auto");
}







function Show_Popup(tag) {
	cd_popup = tag;

		locate();
		$("#black").css({
			"opacity": "0.75"
		});
		$("#black").fadeIn("normal",function(){
		$(cd_popup).show();});
locate();
}





function locate(){
	
	$("body").css("overflow", "hidden");
	
	//request data for centering
	var windowWidth = $("body").width();
	var windowHeight = $("body").height();
	
	var popupHeight = $(cd_popup).height();
	var popupWidth = $(cd_popup).width();
	
	var blackHeight = $("#black").height();
	var blackWidth = $("#black").width();
	
	
	
	var scrollTop = $(window).scrollTop();


	//centering
	$(cd_popup).css({
		"position": "absolute",
		"top": windowHeight/2-popupHeight/2 + scrollTop,
		"left": windowWidth/2-popupWidth/2 
	});

	
	$("#black").css({
		"position": "absolute",
		"top": windowHeight/2-blackHeight/2  + scrollTop,
		"left": windowWidth/2-blackWidth/2
	});
	
}






function Close_Popup() {
$("#black").fadeOut("normal");
$(cd_popup).hide();
$("body").css("overflow", "auto");
}


$(document).ready(function(){
	

$("#black").click(function(){
								
_close_contact();
Close_Popup();
});




$(document).keypress(function(e){
								
		if(e.keyCode==27 ){
			//Close_Popup();
			//_close_contact();
		}
	});




});

