o3djs.require('ft.config');
o3djs.require('ft.house');
o3djs.require('ft.viewinfo');
o3djs.require('ft.event');
o3djs.require('ft.orthoimage');
o3djs.require('ft.canvas');
o3djs.require('ft.action');
o3djs.require('ft.shapes');
o3djs.require('ft.load');
o3djs.require('ft.animation');

o3djs.require('o3djs.math');
o3djs.require('o3djs.picking');

var g_o3d;             
var g_client;         
var g_clock = 0;
var g_timeMult = 1;
var g_finished = false; 
var g_jump=false;

var g_pack;
var g_math;
var g_transform;
var g_transform_city;
var g_transform_list=[];
var g_transform_bottom=[];
var g_shape_housetop;
var g_transform_persp;
var g_transform_ortho;
var g_viewInfo_persp;
var g_viewInfo_ortho;


function renderCallback(renderEvent) 
{
    g_clock += renderEvent.elapsedTime * g_timeMult; 
	val=0.2 * g_clock;
	g_transform_city.identity(); 
	g_transform_city.translate([-7,-1,7]);
	g_transform_city.rotateY(val);
}

function onpostrender(render_event) {  
  var elapsedTime = render_event.elapsedTime;  
  
  // elapsedTime is the time elasped since the last callback.  
  // You can use this value to make your application frame rate independent.  
  // For example:  
  //position = position + velocity_in_units_per_second * elapsedTime;  
  if(g_jump)
	{
		var t1=-0.01;
		ft.viewinfo.persp_target=[ft.viewinfo.persp_target[0],ft.viewinfo.persp_target[1]+t1,ft.viewinfo.persp_target[2]];
		ft.viewinfo.persp_eye=[ft.viewinfo.persp_eye[0],ft.viewinfo.persp_eye[2]+t1,ft.viewinfo.persp_eye[2]];
        ft.viewinfo.persp_viewInfo.drawContext.view = g_math.matrix4.lookAt(ft.viewinfo.persp_eye,ft.viewinfo.persp_target,ft.viewinfo.persp_up);
	}
	gf_showTest(g_jump);
}

function init() 
{
	o3djs.util.makeClients(initStep2);
}

function initStep2(clientElements) 
{
    var o3dElement = clientElements[0];  
	g_client = o3dElement.client;  
    g_o3d = o3dElement.o3d;  
	g_math = o3djs.math;
    o3djs.base.init(o3dElement);
    g_pack = g_client.createPack();
    createViewInfo();
	createMap();
	createHoure();
	createFullScreenImage();
	ft.event.registerEvent(o3dElement);
	g_client.setRenderCallback(renderCallback);
	g_finished = true;
}

function createViewInfo()
{
	g_transform_persp=g_pack.createObject('Transform');
    g_viewInfo_persp=ft.viewinfo.createPerspViewinfo(g_transform_persp);
	
	g_transform_ortho=g_pack.createObject('Transform');
	g_viewinfo_ortho=ft.viewinfo.createOrthoViewinfo(g_transform_ortho);
	g_viewinfo_ortho.root.priority = g_viewInfo_persp.root.priority + 1;
}

function createFullScreenImage()
{
	var tf_ortho=ft.orthoimage.createFullScreen('fullscreen',100,50,'images/fullscreen.jpg');
	tf_ortho.identity();
	tf_ortho.translate(50,550,0);
	tf_ortho.rotateX(4.7123);
	tf_ortho.parent=g_transform_ortho;
	g_client.setFullscreenClickRegion(10,
        g_client.height - 50 - 10, 100,
        50, getFullscreenModeId(g_client));
}

getFullscreenModeId=function(client) {
  var displayModes = client.getDisplayModes();
  var bestMode;
  for (var index in displayModes) {
    var mode = displayModes[index];
    if (!bestMode ||
        (mode.width > bestMode.width && mode.height > bestMode.height)) {
      bestMode = mode;
    }
  }
  if (bestMode) {
    return bestMode.id;
  } else {  
    return g_o3d.Renderer.DISPLAY_MODE_DEFAULT;
  }
}

function uninit() 
{
	if (g_client) 
	{
		g_client.cleanup();  
	}
}

function createHoure()
{
	var rangv_num=rangv.length;
	var doorx;
	var doory;
	var doorz;

	var shape_bottom=ft.shapes.createBox(1,0.01,1,city_texture[1]);
	var shape_door=ft.shapes.createZdoor();
	g_shape_housetop=ft.shapes.createHoureTop(g_mapimg+'/houretop.jpg');
	g_transform=g_pack.createObject('Transform');
	
	for(var ui=0;ui<houselist.length;ui++)
	{
		
		g_transform_list[ui]=ft.house.selectHouse(houselist[ui].houreid,
																	g_viewInfo_persp,
																	houselist[ui].url_texture,
																	houselist[ui].style);
		
		g_transform_list[ui].translate(houselist[ui].position);
		if(houselist[ui].action==1)
		{
			var trs=ft.animation.TRSToMatrix4(g_transform_list[ui],"rotateY",0,0,5,Math.PI * 3);
			trs.translateX=houselist[ui].position[0];
			trs.translateY=houselist[ui].position[1];
			trs.translateZ=houselist[ui].position[2];
		}
		if(houselist[ui].action==2)
		{
			var k2Output=g_math.pseudoRandom()*20;
			if(k2Output>5)k2Output=5;
			if(k2Output<1)k2Output=1;
			var trs=ft.animation.TRSToMatrix4(g_transform_list[ui],"scaleY",0,0,Math.round(g_math.pseudoRandom()*9)+1,k2Output);
			trs.translateX=houselist[ui].position[0];
			trs.translateY=houselist[ui].position[1];
			trs.translateZ=houselist[ui].position[2];
		}
		//g_transform_list[ui].translate(houselist[ui].position);
	    g_transform_list[ui].parent = g_transform;

		var transform_bottom=g_pack.createObject('Transform');
		transform_bottom.addShape(shape_bottom);
		transform_bottom.translate(houselist[ui].position);
		transform_bottom.parent=g_transform;

		var transform_door=g_pack.createObject('Transform');
	    transform_door.createParam('diffuse', 'ParamFloat4').value = [1,0.6,0.4,0.8];
		transform_door.addShape(shape_door);
		doorx=houselist[ui].position[0];
	    doory=houselist[ui].position[1]+(-0.25);
	    doorz=houselist[ui].position[2];
		if(houselist[ui].direction==0)
		{
			doorx=houselist[ui].position[0]-0.5;
			transform_door.translate([doorx,doory,doorz]);
		}
		if(houselist[ui].direction==1)
		{
			doorz=houselist[ui].position[2]+0.5;
			transform_door.translate([doorx,doory,doorz]);
			transform_door.rotateY(1.5707963);
		}
		if(houselist[ui].direction==2)
		{
			doorx=houselist[ui].position[0]+0.5;
			transform_door.translate([doorx,doory,doorz]);
		}
		if(houselist[ui].direction==3)
		{
			doorz=houselist[ui].position[2]-0.5;
			transform_door.translate([doorx,doory,doorz]);
			transform_door.rotateY(1.5707963);
		}
		rangv[rangv_num++]=[doorx-0.15,doorx+0.15,doorz-0.15,doorz+0.15,houselist[ui].position[1],-2];
		transform_door.parent=g_transform;
	}
	g_transform.parent=g_transform_persp;
}

setEye=function()
{
    gf_comecity(g_cityid);
}

goCity=function()
{
	var nx=ft.viewinfo.persp_eye[0];
	var nz=ft.viewinfo.persp_eye[2];
	var x1;
	var x2;
	var y1;
	var y2;
	for(ri=0;ri<go_citys.length;ri++)
	{
		x1=go_citys[ri].gocity[0];
		x2=go_citys[ri].gocity[1];
		y1=go_citys[ri].gocity[2];
		y2=go_citys[ri].gocity[3];
		if(ft.math.israngeXZ(x1,x2,y1,y2,nx,nz))
		{
			gf_gocity(go_citys[ri].cityid,g_cityid);
		}
	}
}

goHoure=function()
{
	var nx=ft.viewinfo.persp_eye[0];
	var nz=ft.viewinfo.persp_eye[2];
	for(var ui=0;ui<houselist.length;ui++)
	{
		doorx=houselist[ui].position[0];
	    doorz=houselist[ui].position[2];
		if(houselist[ui].direction==0)
		{
			if(ft.math.israngeXZ(doorx-0.45,doorx-0.35,doorz-0.5,doorz+0.5,nx,nz))
			{
				gf_gohoure(houselist[ui].position,0,g_cityid,houselist[ui].name,houselist[ui].houreid);
			}
		}
		if(houselist[ui].direction==1)
		{
			if(ft.math.israngeXZ(doorx-0.5,doorx+0.5,doorz+0.35,doorz+0.45,nx,nz))
			{
				gf_gohoure(houselist[ui].position,1,g_cityid,houselist[ui].name,houselist[ui].houreid);
			}
		}
		if(houselist[ui].direction==2)
		{
			if(ft.math.israngeXZ(doorx+0.35,doorx+0.45,doorz-0.5,doorz+0.5,nx,nz))
			{
				gf_gohoure(houselist[ui].position,2,g_cityid,houselist[ui].name,houselist[ui].houreid);
			}
		}
		if(houselist[ui].direction==3)
		{
			if(ft.math.israngeXZ(doorx-0.5,doorx+0.5,doorz-0.45,doorz-0.35,nx,nz))
			{
				gf_gohoure(houselist[ui].position,3,g_cityid,houselist[ui].name,houselist[ui].houreid);
			}
		}
	}
}

goStart=function()
{
	if (window.gameStarted == true) {
        return;
    }
    window.gameStarted = true;
    document.getElementById('o3d').style.visibility = 'visible';

    // Wait until the plug-in has initialized before starting the game.
    var clearId = window.setInterval(function() {
            if (g_client) {
                g_client.setRenderCallback(renderCallback);
            } else {
                return;
            }
        window.clearInterval(clearId);
        }, 1);  
}