// JavaScript Document



function imgclick( obj, id , src )
{

	//var pop =  new PopBg( document.body );
	//	pop.setDisplay( true );
	
	
	var loading = new PopFrame( document.body, function()
	{
		return true;
	}  ,  "roBox"  );

	
	loading.midcell.style.backgroundColor = "#FFF";
	loading.midcell.style.color = "#333";		
	loading.setSize(60	, 60);
	loading.popbg.setDisplay( false );
	loading.popbg.setDisplay(true);
	loading.frame.closeButton.thebody.style.display = "none";
	
	var loadingimg = document.createElement("img");
		loading.appendChild( loadingimg );
		loadingimg.src = IMG_DIR+"syc_or_gr.gif";
	
	loading.setDisplay( true );

	
	

	var pop = new PopFrame( document.body, function()
	{
		return true;
	}  ,  "roBox"  );
	
	pop.midcell.style.backgroundColor = "#FFF";
	pop.midcell.style.color = "#333";		
	pop.setSize(800,600);
	pop.popbg.setDisplay(true);
	//pop.setDisplay( true );
	pop.frame.closeButton.thebody.style.display = "none";
	//alert( pop.frame.closeButton.thebody.tagName );
	
	var linkDiv = document.createElement("div");
	pop.appendChild( linkDiv );
		linkDiv.style.width = "100%";
		linkDiv.align = "right";
		linkDiv.style.paddingBottom = "10px";
	
	
	
	this.closeButton = new imgButton( linkDiv, linkDiv, IMG_DIR+"close.gif" , "Close", function( event, obj )
	{
		pop.hide();
	} );	
	

	
	var img = document.createElement("img");
		pop.appendChild( img );
		img.style.visibility = "hidden";
		
		img.onload = function()
		{
			pop.setSize( this.offsetWidth,this.offsetHeight );
			
			loading.hide();
			
			if( document.all )
			{
				img.style.visibility = "visible";
				pop.setDisplay( true );
			}
			else
			{
				setOpacity( pop.thebody , 0 );
				img.style.visibility = "visible";
							
				pop.setDisplay( true );
				
				var runner = new transform(
					function( intv ){
						setOpacity( pop.thebody , intv );								
					} , 
					function(){
						setOpacity( pop.thebody , 100 );
					} , 20,  100 );
				runner.run();
			}
		}
				
		img.src = "../img/photos/"+src;




}




function fnSetRotation(oObj, deg)
{   
	if( document.all )
	{
		oObj.style.filter = "progid:DXImageTransform.Microsoft.Matrix(sizingMethod='auto expand')";
		
		
		var deg2radians = Math.PI * 2 / 360;
		rad = deg * deg2radians ;
		costheta = Math.cos(rad);
		sintheta = Math.sin(rad);
		
		oObj.filters.item(0).M11 = costheta;
		oObj.filters.item(0).M12 = -sintheta;
		oObj.filters.item(0).M21 = sintheta;
		oObj.filters.item(0).M22 = costheta;
	}
	else
	{
		oObj.style.MozTransform = "rotate("+ deg +"deg)";
		oObj.style.WebkitTransform = "rotate("+ deg +"deg)";
	}
}






function imgOnload( img , size , rot )
{
	var _this = this;
	
	var imgHeight = img.offsetHeight;
	var imgWidth = img.offsetWidth;

	var width = size;
	var height = parseInt( width*imgHeight/imgWidth );
	

	if( imgWidth < imgHeight )
	{
		height = size;
		width = parseInt( height*imgWidth / imgHeight );
	}
	
	
	if( document.all )  
	{
		
	}
	else
	{
		setOpacity( img , 0 );
	}
	
	img.style.visibility = "visible";
	
	
	if( imgWidth > width )
	{
		img.style.width = width + "px";
		img.style.height =  parseInt( width * imgHeight / imgWidth ) + "px";
	}
	

	imgHeight = img.offsetHeight;
	imgWidth = img.offsetWidth;
	
	
	if( imgHeight > height )
	{
		 img.style.height = height + "px";
		 img.style.width = parseInt( height * imgWidth / imgHeight ) + "px";
	}
		

	//var topM = parseInt( ( img.parentNode.offsetHeight - img.offsetHeight  ) /2 );
	
	//if( topM < 0 ) topM = 0;
	
	//img.style.marginTop  =  topM +"px";
	//img.parentNode.style.paddingTop = "10px";
	
	//alert(  parseInt( ( img.parentNode.offsetHeight - img.offsetHeight  ) /2 ) +"px" );
	
	img.parentNode.parentNode.style.backgroundImage = "none";

	
	var deg = getRandomNum( 1, 3 );
	var nag = getRandomNum( 0, 1 );
	
	if( nag == 0 ) deg = 360 - deg;
	
	if( rot )  fnSetRotation( img, deg );
	
	if( document.all )
	{
		//none
	}
	else
	{
		runner = new transform( 
		function(intv){
		
			setOpacity( img, intv );
		
		}, function(){
			
			setOpacity( img, 100);
			
			delete runner;
						
		}, 20, 30);
		runner.run();
	}
	
	
	
}




function popdes( idname )
{
	
	var parent = document.getElementById( idname );
	var divs = parent.getElementsByTagName("div");
	
	var title = "";
	var des = "";
	var email = "";
	var location = "";
	
	var imgs = "";
	
	for(var i=0; i< divs.length ;i++ )
	{
		if( divs[i].id == "title" ) title = divs[i].innerHTML;
		if( divs[i].id == "des" ) des = divs[i].innerHTML;
		if( divs[i].id == "email" ) email = divs[i].innerHTML;
		if( divs[i].id == "location" ) location = divs[i].innerHTML;
		if( divs[i].id == "imgs" ) 	imgs = divs[i];
	}
	
	
	imgs = imgs.getElementsByTagName("img");
	
	var theHeight = window.innerHeight;
	if( document.all ) theHeight = document.documentElement.clientHeight;

	var pop = new PopFrame( document.body, function()
	{
		return true;
	}  ,  "roBox" );
	
	pop.midcell.style.backgroundColor = "#FFF";
	pop.midcell.style.color = "#333";
	//pop.midcell.style.padding = "15px";
	
	
	pop.setSize( 600,  theHeight -  50 );
	pop.popbg.setDisplay(true);
	//pop.setDisplay( true );
	//pop.frame.closeButton.thebody.style.display = "none";
	//pop.frame.closeButton.thebody.style.display = "none";
	
	pop.frame.closeButton.thebody.parentNode.removeChild( pop.frame.closeButton.thebody );
	//alert( pop.frame.closeButton.thebody.tagName );
	
	var linkDiv = document.createElement("div");
	pop.appendChild( linkDiv );
		linkDiv.style.width = "100%";
		linkDiv.align = "left";
		//linkDiv.style.paddingBottom = "10px";
	
	
	this.closeButton = new imgButton( linkDiv, linkDiv, IMG_DIR+"close.gif" , "Close", function( event, obj )
	{
		pop.hide();
	} );	
	
	/*
	var img = document.createElement( "img" );
		titleImgDiv.appendChild( img );	
		img.src = "../img/logo_l.png";
	*/
	
	var desCell = document.createElement( "div" );
	
		pop.midcell.appendChild( desCell );
		desCell.style.padding = "20px";
		
	
	var topTable = new tableLayout( this , desCell  );
	
	//topTable.table.style.borderLeft = "dotted 1px #eee";
	
	var titleImgDiv = document.createElement("div");
		//pop.midcell.appendChild( titleImgDiv );
		//titleImgDiv.style.borderRight = "dotted 1px #ccc";

		titleImgDiv.align = "left";
		
		titleImgDiv.style.width = "260px";
		titleImgDiv.style.height = "260px";
		
	
	var imgCell = topTable.addCell( titleImgDiv );
		

	if( imgs.length > 0 )
	{
			
		var img = document.createElement( "img" );
			titleImgDiv.appendChild( img );	
			img.border = 0;
			//img.height = "200";
			img.style.visibility = "hidden";
		
			img.onload = function()
			{
				imgOnload( this , 260 , false );
			}
		
			img.src = imgs[0].src;
			
	}
	
	var emailTitle = "email:";
	if( lang =="cn" )
	{
		emailTitle = "邮箱: ";	
	}
	else if( lang == "jp" )
	{
		emailTitle = "邮箱: ";
	}
	
	var locationTitle = "Office: ";
	if( lang =="cn" )
	{
		locationTitle = "办公地点: ";	
	}
	else if( lang == "jp" )
	{
		locationTitle = "办公地点: ";
	}
	
	
	
	var titleH3 = document.createElement("div");
		titleH3.innerHTML = '<h1 style=" padding-left:10px;">'+title+"</h1>" + 
							'<div style=" padding:10px;" >'+emailTitle+" <a href='mailto:"+ email +"'>"+ email +"</a></div>"+
							'<div style=" padding:10px;" >'+locationTitle+" " +location +"</div>";
		titleH3.style.padding = "20px";	
		
		titleH3.style.paddingLeft = "0px";	

	var titleCell = topTable.addCell(titleH3);
	
	//if( document.all ) theHeight = document.documentElement.clientHeight;
	
	var desHTML = document.createElement("div");
		//desHTML.style.width = "750px";
		desHTML.innerHTML = des;
		desHTML.style.paddingTop = "20px";
		
		//desHTML.style.height = theHeight - 200 + "px"; 
		//desHTML.style.overflow = "auto";
		
		//desHTML.className = "newsdiv";

	//pop.midcell.appendChild( titleH3 );
	
	desCell.appendChild( desHTML );
	
	//pop.midcell.appendChild( desHTML );
	pop.setSize( 600, desHTML.offsetHeight + topTable.table.offsetHeight + linkDiv.offsetHeight );
	
	pop.setDisplay( true );
	
	
}







function imgMclick( id  , index )
{
	var imgs = document.getElementById( "img"+id );
		imgs = imgs.getElementsByTagName( "span" );
		
		
	var imgList = new Array();
	var len = imgs.length;
	
	var index = index || 0;
	
	
	var h = document.documentElement.clientHeight;
	var w = document.documentElement.clientWidth;
	
	var loadingDiv = document.createElement( "div" );
		document.body.appendChild( loadingDiv );
		loadingDiv.style.position = "absolute";
		loadingDiv.style.width = "200px";
		loadingDiv.style.height = "15px";

		loadingDiv.align = "center";
		loadingDiv.innerHTML = "0 %";
		loadingDiv.style.color = "#333";

		loadingDiv.style.left = parseInt(( w/2) -  (loadingDiv.offsetWidth /2 )) + "px";
		loadingDiv.style.top = parseInt(( h/2) -  (loadingDiv.offsetHeight /2 ))  + "px";
			
	var infoBox = document.createElement( "div" );
		document.body.appendChild( infoBox );
		infoBox.style.position = "absolute";
		infoBox.style.height = "20px";
		infoBox.style.width = "400px";
		infoBox.style.color = "#333";
		infoBox.style.paddingTop = "5px";
	
	var page_span = document.createElement("span");
	
		infoBox.appendChild( page_span );
		
	var resize = function()
	{
		var sTop =  document.body.scrollTop();

		
		var h = document.documentElement.clientHeight;
		var w = document.documentElement.clientWidth;
		
		for( var i=0; i< imgList.length; i++  )
		{
			var img = imgList[ i ];
			
			
			var oH = img.getAttribute( "oh" );
			var oW =  img.getAttribute( "ow"  );
			
			img.style.height = oH +"px";
			img.style.width = oW +"px";
			
			
			if( img.offsetHeight > h-(infoBox.offsetHeight *2) )
			{
				img.style.height = ( h-(infoBox.offsetHeight *2) )+"px";
				img.style.width = parseInt( ( h- (infoBox.offsetHeight *2) ) * oW / oH ) + "px";	
			}
			
			
			if( img.offsetWidth > w )
			{
				img.style.width = w+"px";
				img.style.height = parseInt( w * oH / oW )+"px";
			}
			
			//==
			imgList[ i ].style.left = parseInt(( w/2) -  (imgList[ i ].offsetWidth /2 )) + "px";
			imgList[ i ].style.top = parseInt(( h/2) -  (imgList[ i ].offsetHeight /2 )) + sTop + "px";
			
		}
		
		
		if( loadingDiv.style.display != "none" )
		{
			loadingDiv.style.left = parseInt(( w/2) -  ( loadingDiv.offsetWidth /2 )) + "px";
			loadingDiv.style.top = parseInt(( h/2) -  ( loadingDiv.offsetHeight /2 ))  + "px";
		}
		
		infoBox.style.left = imgList[ index ].style.left;
		infoBox.style.top = imgList[ index ].offsetTop + imgList[ index ].offsetHeight + "px"
		
		
	}	
		
		
	var popbg = new PopBg( document.body  , "#FFFFFF" , null , 
	function( obj ){
	
		resize();
		
	} , 
	function()
	{
		
		document.body.removeChild( loadingDiv );
		document.body.removeChild( infoBox );
		
		for( var i=0; i< imgList.length; i++  )
		{
			document.body.removeChild( imgList[ i ] );
			delete imgList[ i ];
		}
		
		//_this.frame.remove();
	
	});
	
		var s_span = document.createElement( "span" );
			infoBox.appendChild( s_span );
			s_span.innerHTML = " | ";
			
				
		var close_button = document.createElement( "span" );
			infoBox.appendChild( close_button );
			
		close_button.innerHTML =" <a href='#'>Close</a>";
		close_button.onclick = function()
		{
			popbg.dispatch();
		}
				
		
	//z Index
	loadingDiv.style.zIndex = popbg.zIndex + 1;
	infoBox.style.zIndex = popbg.zIndex + ( imgs.length + 1  );
	//z Index
	
	popbg.setDisplay(true);
	
	var imgLoadCount = 0;		
			
	var imgLoaded = function( imgListItem )
	{
		imgLoadCount++;
		
		if( imgLoadCount >= len )
		{
			loadingDiv.innerHTML =  "100 %";
			
			loadingDiv.style.display = "none";
			
			//ini img
			if( index < 0 || index >= imgList.length )
			{
				index == 0;
			}

			imgList[ index ].className = "item_show";

			page_span.innerHTML = ( index +1 ) + " / " + (imgList.length );
			
			//infoBox.style.left = imgList[ index ].style.left;
			//infoBox.style.top = imgList[ index ].offsetTop + imgList[ index ].offsetHeight + "px"
			
			resize();
			
		}
		else
		{
			loadingDiv.innerHTML =  parseInt(( imgLoadCount*100 ) / imgs.length ) + " %"
		}
		
		
	}
			
	
	
	for( var i=0; i< imgs.length; i++  )
	{
		
		imgList[ i ] = new Image();
			
		document.body.appendChild( imgList[ i ] );
		
		//imgList[ i ].style.visibility = "hidden";
		imgList[ i ].className = "item_hidden";
		
		imgList[ i ].style.position = "absolute";
		
		imgList[ i ].style.zIndex = popbg.zIndex + ( imgs.length - i  );
		imgList[ i ].style.left ="0px";
		imgList[ i ].style.top ="0px";
		
		imgList[ i ].name = "none";
		
		imgList[ i ].id = i;
		imgList[ i ].style.cursor = "pointer";
		
		imgList[ i ].title = "Next Image";
		
		/*
			var h = document.documentElement.clientHeight;
			var w = document.documentElement.clientWidth;
			imgList[ i ].style.left = parseInt(( w/2) -  (this.offsetWidth /2 )) + "px";
			imgList[ i ].style.top = parseInt(( h/2) -  (this.offsetHeight /2 ))  + "px";
		*/
		
		
		imgList[ i ].onload = function()
		{
			var h = document.documentElement.clientHeight;
			var w = document.documentElement.clientWidth;
			
			this.style.left = parseInt(( w/2) -  (this.offsetWidth /2 )) + "px";
			this.style.top = parseInt(( h/2) -  (this.offsetHeight /2 ))  + "px";
			
			this.setAttribute( "oh" , this.offsetHeight );
			this.setAttribute( "ow" , this.offsetWidth );
			
			
			imgLoaded( this );
			
		}
		
		imgList[ i ].onclick = function()
		{
			
			//imgList[ index ].style.visibility = "hidden";
			imgList[ index ].className = "item_hidden";
			
			index++;
			if( index >=  len ) index = 0;
			
			//imgList[ index ].style.visibility = "visible";
			imgList[ index ].className = "item_show";
			
			//infoBox.innerHTML = ( index +1 ) + " / " + (imgList.length ) ;
			page_span.innerHTML = ( index +1 ) + " / " + (imgList.length );
						
			infoBox.style.left = imgList[ index ].style.left;
			infoBox.style.top = imgList[ index ].offsetTop + imgList[ index ].offsetHeight + "px"
			
		}

		if( imgs[ i ].innerText ) imgList[ i ].src = IMG_DIR + "photos/" + imgs[ i ].innerText;
		else imgList[ i ].src = IMG_DIR + "photos/" + imgs[ i ].innerHTML;
		
		
	}
	
}




function popVideo( obj,  id )
{
	
	var videoSRC=  document.getElementById(  "videoSRC" +id  );
	var preView=  document.getElementById(  "preView" +id  );
	
	
	var pop = new PopFrame( document.body, function()
	{
		return true;
	}  ,  box_class , "#fff" );
	
	pop.midcell.style.backgroundColor = "#FFF";
	pop.midcell.style.color = "#333";		
	pop.setSize( 800 ,507);
	pop.popbg.setDisplay(true);
	//pop.setDisplay( true );
	pop.frame.closeButton.thebody.parentNode.removeChild(  pop.frame.closeButton.thebody );
	//alert( pop.frame.closeButton.thebody.tagName );
	
	var linkDiv = document.createElement("div");
	pop.appendChild( linkDiv );
		linkDiv.style.width = "100%";
		linkDiv.align = "left";
		linkDiv.style.paddingBottom = "10px";
	
	
	this.closeButton = new imgButton( linkDiv, linkDiv, IMG_DIR+"close.gif" , "Close", function( event, obj )
	{
		pop.hide();
	} );	
	
	var div = document.createElement( "div" );
		pop.appendChild( div );
	
			div.style.width = "640px";
			div.style.height = "507px";
			
			div.innerHTML  = '<div id="flvplayer" style="width:640px; height:507px;">'+
							'<embed type="application/x-shockwave-flash" src="mpw_player.swf" width="640" height="507" style="undefined" id="swfplayer" '+
							'name="swfplayer" bgcolor="#000000" quality="high" allowfullscreen="true" '+
							'flashvars="flv=../img/photos/'+ videoSRC.innerHTML +'&amp;jpg=../img/photos/' + preView.innerHTML + '" ></div>';
	
	pop.setSize( 640 ,507);
	pop.setDisplay( true );

	
}



function videoPreviewLoad( img , id )
{
	if( img )
	{
		var width = img.offsetWidth;
		
		if( width  <= 0 ) width = 330;
		
		var theVideoDiv = document.getElementById( "theVideoDiv" +id  );	
		
		if( theVideoDiv )
		{
			theVideoDiv.style.width = width+ "px";	
		}
		
		var imgDiv = document.getElementById( "imgDiv" +id  );		
		
		if( imgDiv  )
		{
			imgDiv.style.width = width + "px";	
		}
	
		
		var palyimgDiv = document.getElementById( "palyimgDiv" +id  );		
	
		if( palyimgDiv  )
		{
			palyimgDiv.style.left =  parseInt( ( width /2 ) -  ( 118 /2) ) + "px";	
		}
	
		var titleBg = document.getElementById( "titleBg" +id  );
		
		if( titleBg  )
		{
			titleBg.style.width =  width + "px";	
		}
	
	
		var titleDiv = document.getElementById( "titleDiv" +id  );		
		if( titleDiv   )
		{
			titleDiv.style.width =  width + "px";	
		}
	
	}
}


