var Site = new Class(
{
	// properties
	aElements: [],

	// methods
	initialize: function()
	{
		// submarine flash
		this.sub_flash();
		// trailer flash
		this.trailer_flash();
		// get all content blocks
		this.get_elements();
		// make links in menu clickable
		this.create_links();
		// set block events
		this.set_block_events();
		// check content division
		this.check_content();
		// set events for content division links
		this.division_links();
		// open up the content block
		this.open_hash();
	},

	sub_flash: function()
	{
		var flashversion = 8;
		var so = new SWFObject("http://www.submarinechannel.com/flash/back.swf", "Submarinechannel","120", "144", flashversion);

		so.addParam("bgcolor", "#00ff00");
		so.addParam("menu", "false");
		so.addParam("salign", "LT");
		so.addParam("wmode", "transparent");

		so.addVariable("backurl", "http://www.submarinechannel.com");
		so.addVariable("logocolor", "ffffff");
		so.addVariable("bgcolor", "919390");
		so.addVariable("fontcolor1", "ffffff");
		so.addVariable("fontcolor2", "ffffff");

		so.write("submarine_flash");
	},
	
	trailer_flash: function()
	{
		var flashversion = 8;				
		var so = new SWFObject("./_flash/VideoPlayer.swf", "TrailerPlayer", "500", "435", flashversion);
					
		so.addParam("bgcolor", "#ffffff");
		so.addParam("menu", "false");
		so.addParam("salign", "LT");
		so.addParam("wmode", "opaque");
		so.addParam("allowScriptAccess", "always");
					
		so.write("trailer_content");
	},

	open_hash: function()
	{
		// remove preceding hash
		var sBlock = location.hash.replace( /^#/, '' );
		
		//is is there a block?
		if( sBlock.length > 0 )
			// show the block
			$( 'content_' + sBlock ).setStyle( 'visibility', 'visible' );
	},

	// get all the content blocks for later use
	get_elements: function()
	{
		this.aElements = $$( 'div#contentblocks div.content' );
	},

	// make sure the menu items are clickable
	create_links: function()
	{
		$$( 'ul#navigation li' ).each( function( oLink )
		{
			// attach event to show the content block
			oLink.addEvent( 'click', this.show_content.create({ 'arguments': oLink, 'bind': this }) );
		}, this );
	},

	// set some events for every content block
	set_block_events: function()
	{
		this.aElements.each( function( oBlock )
		{
			// get block name
			var sBlock = oBlock.getProperty( 'id' ).replace( /content_/i, '' ).trim();
			// make block draggable
			oBlock.makeDraggable({ handle: oBlock.getElement( 'span.handle' ) });
			// make sure block is shown in front of the others when clicked
			oBlock.addEvent( 'click', this.set_layering.create({ 'arguments': sBlock, 'bind': this }) );
			// close button
			oBlock.getElement( 'a.close' ).addEvent( 'click', this.hide_content.create({ 'arguments': sBlock, 'bind': this }) );
		}, this );
	},

	set_layering: function( sBlock )
	{
		this.aElements.each( function( oBlock )
		{
			oBlock.setStyle( 'z-index', '500' );
		}, this );
		$( 'content_' + sBlock ).setStyle( 'z-index', '501' );
	},

	// show a content block
	show_content: function( oLink )
	{
		var sBlock		= oLink.getProperty( 'class' ).replace( /sIFR\-replaced/i, '' ).trim();
		var sVisible	= $( 'content_' + sBlock ).getStyle( 'visibility' );

		if( sVisible == 'hidden' )
			this.display_content( sBlock );
		else
			this.hide_content( sBlock );

		this.set_layering( sBlock );
	},
	
	display_content: function( sBlock )
	{
		$( 'content_' + sBlock ).setStyle( 'visibility', 'visible' );
		// If the content block was the trailer, we need to stop the flash trailer
		if ( sBlock == 'trailer' )
		{
			start_flash_trailer();
		}
	},

	hide_content: function( sBlock )
	{
		$( 'content_' + sBlock ).setStyle( 'visibility', 'hidden' );
		// If the content block was the trailer, we want to start the flash trailer
		if ( sBlock == 'trailer' )
		{
			stop_flash_trailer();
		}
	},

	check_content: function()
	{
		this.aElements.each( function( oBlock )
		{
			if( oContent = oBlock.getElement( '.content_pt' ) )
				oContent.setStyle( 'display', 'block' );
		}, this );
	},

	switch_content: function( oParent, oLink )
	{
		// get which content block is supposed to be shown
		var sPart = oLink.getProperty( 'class' ).replace( /link_/, '' );

		// hide all content blocks
		oParent.getElements( '.content_pt' ).setStyle( 'display', 'none' );

		// show the correct content block
		oParent.getElement( '.' + sPart ).setStyle( 'display', 'block' );
	},

	division_links: function()
	{
		this.aElements.each( function( oBlock )
		{
			// content_division
			if( oLists = oBlock.getElements( '.content_division' ) )
			{
				oLists.each( function( oList )
				{
					oList.getElements( 'li' ).each( function( oLink )
					{
						oLink.addEvent( 'click', this.switch_content.create({ 'arguments': [ oBlock, oLink ], 'bind': this }) );
					}, this );
				}, this );
			}
		}, this );
	}

});

init = function()
{
	new Site();
}

window.addEvent( 'domready', init );

/** Added by Jorrit for controlling the Flash Trailer player when the content block holding the player is opened or closed. */
var gTrailerPlayer = null;
start_flash_trailer = function()
{
	if ( gTrailerPlayer )
	{
		gTrailerPlayer.playTrailer();
	}
}
	
stop_flash_trailer = function()
{
	if ( !gTrailerPlayer)
	{
		var isIE = navigator.appName.indexOf("Microsoft") != -1;
		gTrailerPlayer = (isIE) ? window['TrailerPlayer'] : document.getElementById( 'TrailerPlayer' );
	}
	
	if ( gTrailerPlayer )
	{
		gTrailerPlayer.stopTrailer();
	}
}