$( function() {

		$('#iphone-tutorial-link').click( function() {

				this.blur();

				// toggle off
				if ( ! $('#content').is(':visible') ) {
					tutorialClose();
					return false;
				}

				// toggle on

				if ( $('#iphone-tutorial').length == 0 ) {
					$( '#structure').prepend ( '<div id="iphone-tutorial">Help!!</div>' );				
				}

				$('#footer-wrap').hide();
				$('#content').hide();
				$('#background').addClass('background-iphone-tutorial');
				$('body').css ( { background: '#222' } );
				$('#iphone-tutorial').show();

				var s = '';
				s += '<a href="/" id="iphone-tutorial-close"><span>Close</span></a>';
				s += '<p id="iphone-tutorial-text">Get d-cyphaTrade on your iPhone!</p>';
				s += '<div id="iphone-tutorial-screen" rel="0"></div>';
  				$('#iphone-tutorial').html ( s );

				$('#iphone-tutorial-close').click( function() {
						tutorialClose();
						return false;
					});

				// slide display duration in milliseconds
				setInterval ( 'tutorialRotate();', 5000 );

				return false;

			});


	});


function tutorialClose() {
	$('#background').removeClass('background-iphone-tutorial');
	$('body').css ( { background: '#CFE287' } );	
	$('#iphone-tutorial').remove();
	$('#content').show();
	$('#footer-wrap').show();
	return false;
}

function tutorialRotate() {

	var steps = { 
		0: 'Get d-cyphaTrade on your iPhone!',
		1: 'Tap on Safari', 
		2: 'Type in <strong>dcypha.com.au</strong> and hit Go',
		3: 'Tap on the blue bars to see data',
		4: 'Tap on buttons to select states',
		5: 'Tap <strong>+</strong> to add to your Home Screen',
		6: 'Tap Add to Home Screen',
		7: 'Tap Add',
		8: 'Go direct from your Home Screen'
	};

	var step = $('#iphone-tutorial-screen').attr( 'rel' );
	step ++;

	// number of slides
	if ( step > 8 ) {
		step = 0;
	}
	var position = '-' + ( step * 320 ) + 'px 0';

	$('#iphone-tutorial-text').html( steps[step] );

	$('#iphone-tutorial-screen').stop().animate(
												{backgroundPosition: position }, 
												{duration:250});

	var step = $('#iphone-tutorial-screen').attr( 'rel', step );

}
