// Global objects
var browserId;
var rootUrl;
var siteId; 

var i3dCtrl         = null;
var intBrowserId    = null;

var siteReady	      = false;

var implementation  = 2;
var maximunSteps    = 0;
var selectedNode    = 0;

var procedureXml    = null;

var hsId            = 'siteobj';
var subhsId         = 'siteobj';

/**************************/
// Events                 //
/**************************/

function createBrowser( pageUrl, x1, y1, x2, y2 ) 
{
  if (i3dCtrl==null)
    return;
  var intBrwId = i3dCtrl.CreateBrowser( x1, y1, x2, y2 );;
  if( intBrwId != null )
  {
 		i3dCtrl.Browse( intBrwId, pageUrl );
 	}	
	return intBrwId;	
}

function onBeforeRender()
{
  if (i3dCtrl==null)
    return;
  
  if( siteId == "dnd" )
  { 
    LoadProcedureXML();
		SetObjectVisible("cockpit", false);
		SetObjectVisible("ch53", false);
		SetObjectVisible("landzone_copter", false);
		
	  i3dCtrl.ForceRotationMode( 2 );
    SelectObject("plane");		
  }
	if( siteId == "copter" )
    i3dCtrl.ForceRotationMode( 2 );
    
  var obj = GetObjById("map");
  if( obj != null )
  {
    obj.style.display = "inline";
  }
  
  var obj = GetObjById("hotspots");
  if( (obj != null) )
  {
    obj.style.display = "inline";
  }
  
  obj = GetObjById("hasWnd");  
  if( (obj != null) && obj.value == "true" )
  {
    var pageUrl = rootUrl + "sources/internal.aspx?site=" + siteId + "&lang=" + GetObjById("lanId").value;
		intBrowserId = createBrowser( pageUrl, 470, 30, 205, 272 );
		i3dCtrl.SetBrowserVisible( intBrowserId, true );
  }
}

function onMoveCamera(x, y, z, view_angle, speed)
{
  var obj = GetObjById("frame_map");
  if( obj != null )
    top.frames["frame_map"].window.onMoveCamera(x, y, z, view_angle, speed);
}

function onObjectSelected( objectId )
{
  var obj = GetObjById("frame_map");
  if( obj != null )
    top.frames["frame_map"].window.onObjectSelected(objectId);
}

/**************************/
// Control                //
/**************************/

function InitClientVars()
{
  rootUrl = window.location.href.substring(0, (window.location.href.lastIndexOf("/")) + 1 );
  
	obj = GetObjById("siteId");
	if( obj != null ) 
	  siteId = obj.value;
	
	obj = GetObjById("browser");	
	if( obj != null ) 
		browserId = obj.value;
	
	if( IsInstalled() )
	  setTimeout("LoadSite()", 250)
}

function RunAnimation( objectId, animationId )
{
  if (i3dCtrl==null)
    return;

	i3dCtrl.ForceRotationMode( 2 );
	i3dCtrl.RunAnimation( objectId, animationId );
}

function SetObjectVisible( objectId, value )
{
  if (i3dCtrl==null)
    return;

  if(value == "false")
  {
    i3dCtrl.SetObjectVisible(objectId, false);
  } 
  else if(value == "true")
  {
    i3dCtrl.SetObjectVisible(objectId, false);
  } 
  else i3dCtrl.SetObjectVisible(objectId, value);
}

function SelectObject( objectId )
{
  if (i3dCtrl==null)
    return;

	i3dCtrl.SelectObject(objectId)
}

function ShowInternalWndContent( _hsId, _subhsId )
{
  if (i3dCtrl==null)
    return;

  hsId = _hsId;
  subhsId = _subhsId;
  
  var pageUrl = rootUrl + "sources/internal.aspx?site=" + siteId + "&hsId=" + hsId + "&subhsId=" + subhsId;
  
  i3dCtrl.Browse( intBrowserId, pageUrl );
  SelectObject(hsId);
}

function HideInternalWndContent( obj1, obj2 )
{
  if (i3dCtrl==null)
    return;

  var obj = GetObjById( obj2 );
  if( siteReady && (obj1 != null) && (obj != null) )
  {
    obj1.style.display  = "none";
    obj.style.display   = "inline";
    
    if( obj1.id == "swh_wndOpen" )
    {
      ShowInternalWndContent( hsId, subhsId )    
      i3dCtrl.SetBrowserVisible( intBrowserId, true );
    }
    else i3dCtrl.SetBrowserVisible( intBrowserId, false );
  }
}

function LoadSite()
{
	i3dCtrl = GetObjById( "ctrl" + browserId );
	if( i3dCtrl != null )
	{
		var resourcesUrl = rootUrl + "sites/" + siteId;
		var xml3dDataUrl = resourcesUrl + "/" + siteId + ".xml";
		
		try
		{
 		  i3dCtrl.Finalize3D();
		  //i3dCtrl.SetImplementation(implementation);
		  i3dCtrl.Init3D();
		
		  switch( browserId )
		  {
		    case "1" : i3dCtrl.ResourcesURL = resourcesUrl; break
		    case "2" : 
		    case "3" : i3dCtrl.PutResourcesURL( resourcesUrl ); break;
		  }
		  i3dCtrl.LoadSiteFromURL(xml3dDataUrl);
		}
		catch(e)
		{
		  //throw e;
		}
	}
}

function IsInstalled()
{
   return true; 
  var installed = true;
	var obj = GetObjById("browser");	
	if( obj != null ) 
		browserId = obj.value;
		
  if (browserId == "1") // IE
  { 
    try 
    {
      var comp = new ActiveXObject("iSite.3DRenderer");
      if (comp) 
        installed = true;
      else
        installed = false;
      comp = null; 
    }
    catch (e) 
    {
      if (e.message == "Automation server can't create object")
        installed = false;
      else throw e;
    }
  }
  else if ( (browserId == "2") || ((browserId == "3")) )
  {
    if( navigator.mimeTypes && navigator.mimeTypes["application/isitecorp-3d-render"] && navigator.mimeTypes["application/isitecorp-3d-render"].enabledPlugin && navigator.plugins ) 
      installed = true;
    else
      installed = false;
  }
  return installed;
}

function LoadProcedureXML() 
{
	selectedNode = 0;
  
  var taskXml = rootUrl + "sites/" + siteId + "/dnd-tasks.xml";
  
  if( browserId == "1" )
  {
	  procedureXml = new ActiveXObject("MSXML.DOMDocument");
    procedureXml.async = false;
	  if( procedureXml.load(taskXml) )
	  {
      var nodes = procedureXml.selectNodes("tasks/task");
      maximunSteps = nodes.length;
    }
  }
  else if ( (browserId == "2") || (browserId == "3") ) // netscape
  {
    procedureXml = document.implementation.createDocument('','',null);  
    procedureXml.async = false;
    
    if( procedureXml.load(taskXml) )
    {
      var nodes = procedureXml.evaluate("//task", procedureXml, null, XPathResult.ANY_TYPE, null );
      while( resultNode = nodes.iterateNext() )
        maximunSteps += 1;
    }
  }
  
	var xPathAction = "tasks/task[@id='" + selectedNode + "']/step[@motion='forward']/action";
	ExecuteStep( xPathAction );
}

/**************************/
// DHTML utils            //
/**************************/
function MM_preloadImages() 
{ //v3.0
	var d = document; 
	if (d.images) 
	{ 
		if (!d.MM_p) 
			d.MM_p = new Array();
		var i;
		var j = d.MM_p.length;
		var 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_swapImgRestore() 
{ //v3.0
	var i;
	var x
	var a = document.MM_sr; 
	for (i=0; a && i < a.length && (x=a[i]) && x.oSrc;i++) 
		x.src=x.oSrc;
}

function MM_findObj(n, d) 
{ //v4.01
	var p;
	var i;
	var 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;
	var j=0;
	var x;
	var 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 SwapPNG( elem, imgPNG, brwId )
{
  var obj = GetObjById(elem);
  if( obj != null )
  {
    if( brwId == "IE" )
      obj.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='images/" + imgPNG + ".png', sizingMethod='')";
    else
      obj.src= "images/" + imgPNG + ".png";
  }
}

function GetObjById( objId )
{
	return document.getElementById(objId);
}

function ChangeImage(id, src)
{
  var obj = GetObjById(id);
	if( obj != null )
	  obj.src = src;  
}

function ShowElement( divId )
{
	var obj = GetObjById( divId );
	if( obj != null )
	{
	  var state = obj.style.display;
		switch( state )
		{
			case "inline"	: obj.style.display = "none"; break;
			case "none"	  : obj.style.display = "inline"; break;
		}
	}
}

function Switcher( obj1, obj2 )
{
  var obj = GetObjById( obj2 );
  if( siteReady && (obj1 != null) && (obj != null) )
  {
    obj1.style.display = "none";
    obj.style.display = "inline";
    
    ShowElement("howto");
    
    var iframe = GetObjById( "frame_map" )
    if( iframe != null )
    {
      var map = GetObjById( "map" );
      if( map != null )
      {
        if( obj1.name == "swh_open" )
        {
          map.style.display = "none";
        }
        else map.style.display = "inline";
      }
    }
    
    var hotspots = GetObjById( "hotspots" );
    if( hotspots != null )
    {
      if( obj1.name == "swh_open" )
      {
          hotspots.style.display = "none";
      }
      else hotspots.style.display = "inline";
    }
    
    var steps = GetObjById( "steps" );
    if( (steps != null) && (steps.style.display == "inline") )
    {
      steps.style.display = "none";  
    } 
    else if( (steps != null) && (steps.style.display == "none") )
    {
      steps.style.display = "inline";  
    }
    var textbox = GetObjById( "textbox" );
    if( (textbox != null) && (textbox.style.display == "inline") )
    {
      textbox.style.display = "none";  
    }
    else if( (textbox != null) && (textbox.style.display == "none") )
    {
      textbox.style.display = "inline";  
    }
  }
}

function OnSiteClick( url )
{
  if( browserId == "1" )
  {
    if( IsInstalled() )
      window.location = url;
    else 
      window.location = url + "&inst=0";
  }
  else
  {
    if( IsInstalled() )
      window.location = url;
    else
    {
      window.location = url + "&inst=0";
    }
  }
}

function ExecuteStep( xPathAction )
{
	var xPathText = "tasks/task[@id='" + selectedNode + "']/text";
	
	var obj = GetObjById("textbox");	
  if( browserId == "1" ) // IE
  {
    var nodeList = procedureXml.selectNodes(xPathAction);
    var nodeText = procedureXml.selectSingleNode(xPathText);
    for( var i = 0; i < nodeList.length; i++ )
		  ExecuteTasks( nodeList(i) );
		
		if( obj != null && (nodeText != null) )  
	    obj.innerText = nodeText.text;
	}
	else if( (browserId == "2") || (browserId == "3") ) // ns
	{
    nodeActions = procedureXml.evaluate(xPathAction, procedureXml, null, XPathResult.ANY_TYPE, null );
    nodeText    = procedureXml.evaluate(xPathText,   procedureXml, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null );
    var resultNode;
    while( resultNode = nodeActions.iterateNext() ) 
		  ExecuteTasks(resultNode);
		  
		if( obj != null && (nodeText != null) )  
      obj.innerHTML = nodeText.singleNodeValue.firstChild.nodeValue;
  }
}

function BtnRollOver( element, mouseEvent )
{
  var btnPrev = GetObjById( "ImgPrev" );
  var btnNext = GetObjById( "ImgNext" );
  
  if( (btnPrev != null) && (btnNext != null) )
  {
    if( element == "ImgPrev" )
    {
      if( selectedNode > 0 )
      {
        if( selectedNode < maximunSteps )
        {
          btnNext.src = "images/en/btnNextNormal.jpg";
          btnNext.style.cursor = "hand";
        }
        else btnNext.src = "images/en/btnNextOver.jpg";
          
        switch( mouseEvent )
        {
          case "over" : btnPrev.src = "images/en/btnPrevOver.jpg"; break;
          case "out"  : btnPrev.src = "images/en/btnPrevNormal.jpg"; break;
        }  
      }
      else
      {
        btnNext.src = "images/en/btnNextNormal.jpg";
        btnPrev.src = "images/en/btnPrevOver.jpg";
        btnPrev.style.cursor = "default";
      } 
    }
    else
    {
      if( selectedNode < maximunSteps - 1 )
      {
        if( selectedNode > 0 )
        {
          btnPrev.src = "images/en/btnPrevNormal.jpg";
          btnPrev.style.cursor = "hand";
        }
        else btnPrev.src = "images/en/btnPrevOver.jpg";
        
        switch( mouseEvent )
        {
          case "over" : btnNext.src = "images/en/btnNextOver.jpg"; break;
          case "out"  : btnNext.src = "images/en/btnNextNormal.jpg"; break;
        }  
      }
      else
      {
        btnPrev.src = "images/en/btnPrevNormal.jpg";
        btnNext.src = "images/en/btnNextOver.jpg";
        btnNext.style.cursor = "default";
      } 
    }
  }
}

function ExecutePrevStep()
{
  if( siteReady )
  {
	  if( selectedNode > 0 )
	    selectedNode--;
  	  
	  var xPathAction = "tasks/task[@id='" + selectedNode + "']/step[@motion='backward']/action";
	  ExecuteStep(xPathAction);
  	  
    var obj = GetObjById( "ImgInfoStep" );
    if(obj != null )
      obj.src = "images/en/info_step_0" + selectedNode + ".jpg";
  	  
    obj = GetObjById( "ImgNext" );
    if(obj != null )
    {
      obj.src = "images/en/btnNextNormal.jpg";
      obj.style.cursor = "hand";
    }
  }
}

function ExecuteNextStep()
{
  if( siteReady )
  {
	  if( selectedNode < maximunSteps - 1)
	    selectedNode++;
  	
	  var xPathAction = "tasks/task[@id='" + selectedNode + "']/step[@motion='forward']/action";
	  ExecuteStep(xPathAction);

    var obj = GetObjById( "ImgInfoStep" );
    if(obj != null )
      obj.src = "images/en/info_step_0" + selectedNode + ".jpg";

    obj = GetObjById( "ImgPrev" );
    if(obj != null )
    {
      obj.src = "images/en/btnPrevNormal.jpg";
      obj.style.cursor = "hand";
    }
  }
}

function ExecuteTasks( step )
{
	var type = step.attributes.getNamedItem("type").value;
	switch (type)
	{
		case "selectObject":
		{
			SelectObject(step.attributes.getNamedItem("objId").value)
			break;
		}
		case "setObjectVisible":
		{
			var object = step.attributes.getNamedItem("objId").value;
			var value  = step.attributes.getNamedItem("value").value
			SetObjectVisible(object, value)
			break;
		}
		case "runAnimation":
		{
			var object = step.attributes.getNamedItem("objId").value;
			var anim   = step.attributes.getNamedItem("animId").value
			RunAnimation(object, anim)
			break;
		}
	}
}

function openWindow(url, name, w, h, scroll, menubar) 
{
  var sWidth  = screen.width; 
  var midX    = ((sWidth/2)-w/2); 
  var sHight  = screen.height; 
  var midY    = ((sHight/2) - h/2); 
  
  window.open(url, name, 'width='+w+', height='+h+', directories=no, location=no, menubar='+menubar+', status=no, scrollbars='+scroll+', toolbar=no, resizable=no, left='+midX+', top='+midY+'' );
}

