﻿var _showCounter = 0;
var _slideCounter = 0;

$(document).ready(
	function()
	{
		if ( ( window.location.href.indexOf( "index" ) == -1 ) && ( window.location.href.indexOf( ".html" ) != -1 || window.location.href.indexOf( ".aspx" ) != -1 ) )
		{
			// MakeDivInvisible( "big_mask" );
			
			return;
		}
		
		$( "#arrow_a" ).fadeTo( 0, 0 );
		MakeDivInvisible( "text_a" );
		$( "#graphic_a" ).fadeTo( 0, 0 );
		
		$( "#arrow_b" ).fadeTo( 0, 0 );
		MakeDivInvisible( "text_b" );
		$( "#graphic_b" ).fadeTo( 0, 0 );
		
		$( "#arrow_c" ).fadeTo( 0, 0 );
		MakeDivInvisible( "text_c" );
		$( "#graphic_c" ).fadeTo( 0, 0 );
		
		MakeDivInvisible( "text_across_1" );
		MakeDivInvisible( "text_across_2" );
		$( "#graphic_d" ).fadeTo( 0, 0 );
		
		MakeDivInvisible( "main_page_links" );
		MakeDivInvisible( "replay_introduction" );
		
		$( "#text_a_mask" ).fadeTo( 0, 0 );
		$( "#text_b_mask" ).fadeTo( 0, 0 );
		$( "#text_c_mask" ).fadeTo( 0, 0 );
		$("#text_across_mask").fadeTo(0, 0);
		MakeDivVisible("main_page_links_mask");
		$("#main_page_links_mask").fadeTo(0, 0);
		MakeDivVisible("replay_introduction_mask");
		$("#replay_introduction_mask").fadeTo(0, 0);
		
		MakeDivInvisible( "big_mask" );
		
		setTimeout ( "AdvanceFade()", 2000 );
	}
);

function AdvanceFade()
{
	if ( _showCounter >= 7 )
	{
		return;
	}
	
	switch ( _showCounter )
	{
		case 0:
		{
			$( "#arrow_a" ).fadeTo( 1000, 1 );
			// avoid fading text because it looks bad in IE
			MakeDivVisible( "text_a" );
			$( "#text_a_mask" ).fadeTo( 0, 1 );
			$( "#text_a_mask" ).fadeTo( 1000, 0 );
			$( "#graphic_a" ).fadeTo( 1000, 1 );
			break;
		}
		case 1:
		{
			$( "#arrow_b" ).fadeTo( 1000, 1 );
			// avoid fading text because it looks bad in IE
			MakeDivVisible( "text_b" );
			$( "#text_b_mask" ).fadeTo( 0, 1 );
			$( "#text_b_mask" ).fadeTo( 1000, 0 );
			$( "#graphic_b" ).fadeTo( 1000, 1 );
			break;
		}
		case 2:
		{
			$( "#arrow_c" ).fadeTo( 1000, 1 );
			// avoid fading text because it looks bad in IE
			MakeDivVisible( "text_c" );
			$( "#text_c_mask" ).fadeTo( 0, 1 );
			$( "#text_c_mask" ).fadeTo( 1000, 0 );
			$( "#graphic_c" ).fadeTo( 1000, 1 );
			break;
		}
		case 3:
		{
			MakeDivInvisible( "text_a" );
			MakeDivInvisible( "text_b" );
			MakeDivInvisible( "text_c" );
			MakeDivVisible( "text_across_1" );
			$( "#text_across_mask" ).fadeTo( 0, 1 );
			$( "#text_across_mask" ).fadeTo( 1000, 0 );
			setTimeout( "SlideArrows()", 60 );
			break;
		}
		case 4:
		{
			MakeDivInvisible( "text_across_1" );
			MakeDivVisible( "text_across_2" );
			$( "#text_across_mask" ).fadeTo( 0, 1 );
			$( "#text_across_mask" ).fadeTo( 1000, 0 );
			$( "#graphic_a" ).fadeTo( 1000, 0 );
			$( "#graphic_b" ).fadeTo( 1000, 0 );
			$( "#graphic_c" ).fadeTo( 1000, 0 );
			break;
		}
		case 5:
		{
			MakeDivVisible( "main_page_links" );
			MakeDivVisible( "replay_introduction" );
			$( "#main_page_links_mask" ).fadeTo( 0, 1 );
			$( "#main_page_links_mask" ).fadeTo( 1000, 0 );
			$( "#replay_introduction_mask" ).fadeTo( 0, 1 );
			$( "#replay_introduction_mask" ).fadeTo( 1000, 0 );
			$( "#graphic_d" ).fadeTo(1000, 1);
			break;
		}
		case 6:
		{
			MakeDivInvisible( "main_page_links_mask" );
			MakeDivInvisible( "replay_introduction_mask" );
			break;
		}
	}
	
	_showCounter++;
	setTimeout( "AdvanceFade()", ( _showCounter >= 5 ) ? 1000 : 2000 );
}

function SlideArrows()
{
	ChangeX( "arrow_a", 3 );
	ChangeX( "arrow_c", -3 );
	_slideCounter++;
	if ( _slideCounter < 25 )
	{
		setTimeout( "SlideArrows()", 60 );
	}
}

function PreloadHighlights()
{
	var a = PreloadHighlights.arguments;
	
	document._savedImages = new Array();
	
	var d = document;
	if ( !d.images )
	{
		return;
	}
	
	var i;
	var j = document._savedImages.length;
	for ( i = 0 ; i < a.length ; i++ )
	{
		document._savedImages[ j ] = new Image();
		document._savedImages[ j ].src = "images/topNav_" + a[ i ] + "_highlight.gif";
		j++;
	}
}

function SetButtonImage( id, highlighted )
{
	x = document.getElementById( id );
	if ( x == null )
	{
		return;
	}
	var s;
	if ( highlighted )
	{
		s = "images/topNav_" + id + "_highlight.gif";
	}
	else
	{
		s = "images/topNav_" + id + ".gif";
	}
	x.src = s;
}

var _subTabs;

function SetSubTabs()
{
	_subTabs = SetSubTabs.arguments;
	SelectSubTab( _subTabs[ 0 ] );
}

function SelectSubTab( id )
{
	if ( id == "prev" )
	{
		for ( i = 0 ; i < _subTabs.length ; i++ )
		{
			if ( _subTabs[ i ] == _selectedSubTab )
			{
				if ( i == 0 )
				{
					return false;
				}
				id = _subTabs[ i - 1 ];
			}
		}
	}
	
	if ( id == "next" )
	{
		for ( i = 0 ; i < _subTabs.length ; i++ )
		{
			if ( _subTabs[ i ] == _selectedSubTab )
			{
				if ( i == ( _subTabs.length - 1 ) )
				{
					return false;
				}
				id = _subTabs[ i + 1 ];
			}
		}
	}
	
	for ( i = 0 ; i < _subTabs.length ; i++ )
	{
		if ( _subTabs[ i ] == id )
		{
			ShowDiv( _subTabs[ i ] );
			document.getElementById( "subMenu_" + _subTabs[ i ] ).style.backgroundColor = "#FFD550";
		}
		else
		{
			HideDiv( _subTabs[ i ] );
			document.getElementById( "subMenu_" + _subTabs[ i ] ).style.backgroundColor = "#FFFFFF";
		}
	}
	
	_selectedSubTab = id;
	
	return false;
}

function ShowDiv( id )
{
	document.getElementById( id ).style.display = "block";
}

function HideDiv( id )
{
	document.getElementById( id ).style.display = "none";
}

function MakeDivVisible( id )
{
	document.getElementById( id ).style.visibility = "visible";
}

function MakeDivInvisible( id )
{
	document.getElementById( id ).style.visibility = "hidden";
}

function ChangeX( id, dX )
{
	var x = 0;
	if ( _slideCounter > 0 )
	{
		x = parseInt( document.getElementById( id ).style.left );
	}
	document.getElementById( id ).style.left = ( x + dX ) + "px";
}

////////////////////////////////////////////////////////////////////////////////////////////////////

var myListener = new Object();

var currentIndex = -1;
var fileList = [ "cover_page", "spread_1", "spread_2", "spread_3", "spread_4", "spread_5", "spread_6", "spread_7", "summary_page" ];
var exposureIndex = 0;
var exposureList = [ [ "p1", "r1", "j1", "c1", "d1", "m1", "c2" ],
                     [ "c2", "r1", "d1", "c1", "m1", "p1", "j1" ],
                     [ "m1", "d1", "p1", "c1", "r1", "j1", "c2" ],
                     [ "d1", "c2", "r1", "j1", "c1", "p1", "m1" ] ];
var wasPlaying = false;
var startNext = false;

function CheckStartNext()
{
    if ( startNext )
    {
        currentIndex++;
        if ( currentIndex < fileList.length )
        {
            wasPlaying = false;
            play();
        }
        startNext = false;
    }
}

myListener.onInit = function()
{
    this.position = 0;
    if ( window.location.href.indexOf( "s=1" ) != -1 )
    {
        curentIndex = -1;
        startNext = true;
    }
}

myListener.onUpdate = function()
{
    if ( this.isPlaying )
    {
        wasPlaying = true;
    }
    
    // document.getElementById("info_position").innerHTML = this.position;
    // document.getElementById("info_duration").innerHTML = this.duration;
    // document.getElementById("info_playing").innerHTML = this.isPlaying;
    // document.getElementById("info_was_playing").innerHTML = wasPlaying;
    
    if ( wasPlaying && this.isPlaying == "false" )
    {
        wasPlaying = false;
        startNext = true;
    }
}

function getFlashObject()
{
    return document.getElementById( "myFlash" );
}

function play()
{
    $( "#image_id" ).attr( "src", "images/lotronex/" + fileList[ currentIndex ] + ".png"  );
    getFlashObject().SetVariable( "method:setUrl", "images/lotronex/" + fileList[ currentIndex ] + ".mp3" );
    getFlashObject().SetVariable("method:play", "");
    getFlashObject().SetVariable("enabled", "true");
}

function pause()
{
    getFlashObject().SetVariable("method:pause", "");
}

function stop()
{
    getFlashObject().SetVariable("method:stop", "");
}

function setPosition()
{
    var position = document.getElementById("inputPosition").value;
    getFlashObject().SetVariable("method:setPosition", position);
}

function setVolume()
{
    var volume = document.getElementById("inputVolume").value;
    getFlashObject().SetVariable("method:setVolume", volume);
}

function CheckChangeExposureFrame()
{
    if ( window.location.href.indexOf( "e=" ) == -1 || exposureIndex >= 7 )
    {
        return;
    }
    
    exposureIndex++;
    
    var n = -1;
    if ( window.location.href.indexOf( "e=1" ) != -1 )
    {
        n = 0
    }
    if ( window.location.href.indexOf( "e=2" ) != -1 )
    {
        n = 1
    }
    if ( window.location.href.indexOf( "e=3" ) != -1 )
    {
        n = 2
    }
    if ( window.location.href.indexOf( "e=4" ) != -1 )
    {
        n = 3
    }
    if ( n != -1 )
    {
        $( "#image_id" ).attr( "src", "images/lotronex/" + exposureList[ n ][ exposureIndex ] + ".png"  );
    }
}

// setInterval( "CheckStartNext()", 1000 );
// setInterval( "CheckChangeExposureFrame()", 7000 );

