var _PTVIEWERAPPLETPARAMS = { archive: 'fileadmin/java/ptviewer.jar', code: 'ptviewer.class' };
var _PTVIEWERPARAMS       = [ { name: 'auto', value: '0.1' } ];

var _TARGETID             = 'contentMiddle';
var _LISTCONTAINERID      = 'list360';
var _360CONTAINERID       = 'container360';

var _NOJAVATEXT           = 'Der Inhalt der Seite kann nicht dargestellt werden, da Sie das notwendige Java-Plugin nicht installiert haben.';
var _NOJAVALINK           = 'http://www.java.com';
var _FREEMOVETEXT         = 'Klicken Sie in das Bild um die 360-Grad - Anzeige selbst zu steuern!';

var sCurrentFile          = '';

/*
 * Start the loading of the 360° picture after loading the page
 */
Event.observe(window, 'load', function()
  {
    var oNoJavaLink = noJava();
    if ( navigator.javaEnabled() ) {
      init360(oNoJavaLink);
    } else {
      $(_TARGETID).insert(oNoJavaLink);
    }
  }
);

/*
 * Print out the java downloadlink
 *
 * @return void
 */
function noJava()
{
  return new Element('a', { href: _NOJAVALINK } ).update(_NOJAVATEXT);
}

/*
 * Print out the page-content
 *
 * @return void
 */
function init360()
{
  var oFreeMoveTextContainer = new Element('p').update(_FREEMOVETEXT);
  $(_TARGETID).insert( { bottom: oFreeMoveTextContainer } )

  var oListContainer = new Element('ul', { id: _LISTCONTAINERID });

  ajPictures.each(function(jPicture)
  {
    oListElement = addPictureLink(jPicture);
    oListContainer.insert( { bottom: oListElement } );
  });
  $(_TARGETID).insert( { bottom: oListContainer } );
  
  var jFirstPicture = ajPictures.first();
  load360(jFirstPicture.file);
}

/*
 * Generates a link to a picture
 *
 * @return html element
 */
function addPictureLink(jPicture)
{
  if ( typeof(jPicture) != 'undefined' && jPicture.file != '' )
  {
    var sFile = jPicture.file;
  }
  else
  {
    return;
  }
  
  if ( jPicture.title && jPicture.title != '' )
  {
    var sTitle = jPicture.title;
  }
  else
  {
    return;
  }
  
  var oCurElement = new Element ('li').update(sTitle);
  Event.observe(oCurElement, 'click', function()
    {
      load360(sFile);
    });
  return oCurElement;
}

/*
 * Generates a panorama applet and inserts it
 *
 * @return void
 */
function load360(sFile)
{
  cleanUp(sFile);

  var o360Container = new Element('div', { id: _360CONTAINERID } );
  var o360Applet = new Element('applet', _PTVIEWERAPPLETPARAMS);
  var oFileParam = new Element('param', { name: 'file', value: sFile } );
  o360Applet.insert(oFileParam);
  o360Applet.insert(_NOJAVATEXT);
  
  _PTVIEWERPARAMS.each(function(jParams)
  {
    var oCurrentParam = new Element('param',jParams);

    o360Applet.insert( { bottom: oCurrentParam } );
  });
  
  o360Container.insert(o360Applet);
  $(_TARGETID).insert( { bottom: o360Container } );
}

/*
 * Checks and sets sCurrentFile and calls 360-element remover
 *
 * @return void
 */
function cleanUp(sFile)
{
  if ( sCurrentFile == '' )
  {
    sCurrentFile = sFile;
    return;
  }
  else
  {
    remove360();
    sCurrentFile = sFile;
    return;
  }
}

/*
 * Removes an applet-tag
 *
 * @return void
 */
function remove360()
{
  $(_360CONTAINERID).remove();
}