HMTour = {
	
	container:false,
	
	Show:function( media, title ){
		
		// Need to recreate teh containing DIV every time otherwise WebKit gets confused
		this.container = $('<div></div>');
		$( document.body ).append( this.container );
		// Stop IE getting fussy about DIV size
		this.container.css( 'overflow', 'hidden' );

		this.container.dialog
		(
			{
				bgiframe:true,
				draggable:false,
				modal:true,
				resizable:false,
				width:360,
				height:400,
				close:function(){ HMTour.Hide(); },
				buttons:{
					Close:function(){
						HMTour.container.dialog( 'close' );
					}
				}
			}
		);
		// Set the title		
		this.container.dialog( 'option', 'title', title );

		// Create the iframe
		var iframe = new IFrame( this.container[ 0 ] );
		iframe_body = $( iframe.doc.body );
		iframe = $( iframe );
		iframe.css
		(
			{
				width:'320px',
				height:'240px',
				'overflow':'hidden'
			}
		);
		
		iframe_body.css
		(
			{
				'margin':'0px',
				'padding':'0px',
				'background':'url(/sites/miramar-bournemouth.com/themes/hotelmiramar/img/loading.gif) white center no-repeat fixed',
				'overflow':'hidden'
			}
		);
		
		// Set the applet code
		iframe_body.html
		(
			'<APPLET name="studioviewer" codebase="." archive="/sites/miramar-bournemouth.com/themes/hotelmiramar/tour/studioviewer-csa.jar" code="StudioViewer.class" width="320" height="240" MAYSCRIPT> ' +
			'<PARAM NAME="***** Global Parameters ********" VALUE=""> ' +
			'<PARAM NAME="MarkCode"                         VALUE="740000"> ' +
			'<PARAM NAME="Skin File URL"                 VALUE="/sites/miramar-bournemouth.com/themes/hotelmiramar/tour/Empty.skz"> ' +
			'<PARAM NAME="Help File URL"                 VALUE="/help_Reception/help.htm"> ' +
			'<PARAM NAME="Help Window Size"              VALUE="(320,240)"> ' +
			'<PARAM NAME="POPUP Help"                    VALUE="YES"> ' +
			'<PARAM NAME="Media URL"                      VALUE="/sites/miramar-bournemouth.com/themes/hotelmiramar/tour/images"> ' +
			'<PARAM NAME="Media File URL"                   VALUE="/sites/miramar-bournemouth.com/themes/hotelmiramar/tour/' + media + '.cfg"> ' +
			'<PARAM NAME="Audio Streaming Buffer Size"      VALUE="5"> ' +
			'<PARAM NAME="Mute"                             VALUE="NO"> ' +
			'<PARAM NAME="***** Panorama Parameters ******" VALUE=""> ' +
			'<PARAM NAME="Initial Panorama"                 VALUE="1"> ' +
			'<PARAM NAME="Panorama Memory Manager"          VALUE="1"> ' +
			'<PARAM NAME="-------- Flat Parameters -------" VALUE=""> ' +
			'<PARAM NAME="Flat Frame Rate"                  VALUE="30"> ' +
			'<PARAM NAME="Flat Play X Shift"                VALUE="1"> ' +
			'<PARAM NAME="Flat Play Y Shift"                VALUE="1"> ' +
			'<PARAM NAME="Flat Drag X Max Shift"            VALUE="10"> ' +
			'<PARAM NAME="Flat Drag Y Max Shift"            VALUE="10"> ' +
			'<PARAM NAME="-------- Immersive Parameters -------" VALUE=""> ' +
			'<PARAM NAME="Imm High Quality"                 VALUE="YES"> ' +
			'<PARAM NAME="Imm Frame Rate"                   VALUE="99999"> ' +
			'<PARAM NAME="Imm Play X Shift"                 VALUE="0.2"> ' +
			'<PARAM NAME="Imm Play Y Shift"                 VALUE="0.2"> ' +
			'<PARAM NAME="Imm Drag X Max Shift"             VALUE="7.0"> ' +
			'<PARAM NAME="Imm Drag Y Max Shift"             VALUE="7.0"> ' +
			'<PARAM NAME="Imm Zoom Step"                        VALUE="1.0"> ' +
			'<TABLE width="320" height="240"><TR><TD bgcolor="#FFFFFF" align="center" valign="middle">' +
			'If the viewer does not load, click <a href="http://www.3dvista.com/java.htm">here</a> ' +
			'</TD></TR></TABLE>' +
			'</APPLET>'
		);
		// Instructions
		this.container.append( '<p style="font-size:x-small">Click on the image and move the mouse to look around</p>' );

	},
	
	
	Hide:function(){
		// Delete the container element
		this.container.remove();
		this.container = false;
	}
}

// Adapted from http://bindzus.wordpress.com/2007/12/24/adding-dynamic-contents-to-iframes/#
function IFrame(parentElement)
{
   // Create the iframe which will be returned
   var iframe = $( '<iframe frameBorder="0"></iframe>' )[0];

   // If no parent element is specified then use body as the parent element
   if(parentElement == null)
      parentElement = document.body;

   // This is necessary in order to initialize the document inside the iframe
   parentElement.appendChild(iframe);

   // Initiate the iframe's document to null
   iframe.doc = null;

   // Depending on browser platform get the iframe's document, this is only
   // available if the iframe has already been appended to an element which
   // has been added to the document
   if(iframe.contentDocument)
      // Firefox, Opera
      iframe.doc = iframe.contentDocument;
   else if(iframe.contentWindow)
      // Internet Explorer
      iframe.doc = iframe.contentWindow.document;
   else if(iframe.document)
      // Others?
      iframe.doc = iframe.document;

   // If we did not succeed in finding the document then throw an exception
   if(iframe.doc == null)
      throw "Document not found, append the parent element to the DOM before creating the IFrame";

   // Create the script inside the iframe's document which will call the
   iframe.doc.open();
   iframe.doc.close();

   // Return the iframe, now with an extra property iframe.doc containing the
   // iframe's document
   return iframe;
}

