// UTILITY FUNCTIONS

var bpt_uri_prefix = "";

function BPT_URIForImage( what ) {

	return bpt_uri_prefix + what;

}

function IsRunningInsideCallbackHost() {
	return ( (undefined != window["s3e"]) || 
		(undefined != window["js_to_sputm_callback"]) );
}

var stateThatTriggeredOptions = undefined;

function NimbusGames_send_host_message( msg ) {
	
	// console.log( "NO s3e.exec : " + msg );
	// simulate host...
	
	switch ( msg ) {
	
	case 'action=op_restart':
		ChangeState("_restart");
		break;

	case 'action=op_restart':
		ChangeState("_restart");
		break;

		
	case 'action=mm_options':
	case 'action=pm_options':
	case 'action=pc_menu_options':
		ChangeState("_options");
		stateThatTriggeredOptions = lastActiveState;
		break;
		
	case 'action=op_restore_purchase':
	case 'action=mm_unlock_now':
		ChangeState("_paywall");
		break;
		
	case 'action=restart_no':
		ChangeState("_options");
		break;

	case 'action=help_home':
		if ( '_toc_elements' == activeState ) {
			ChangeState("_options");
		} else {
			ChangeState("_toc_elements");
		}
		break;
		
	case 'action=toc_game_start_section':
		ChangeState("_1_1_elements");
		break;

	case 'action=toc_kenya_section':
		ChangeState("_2_1_elements");
		break;
		
	case 'action=toc_jambo_section':
		ChangeState("_3_1_elements");
		break;
		
	case 'action=toc_skeeter_section':
		ChangeState("_4_1_elements");
		break;
		
	case 'action=toc_zanzibar_section':
		ChangeState("_5_1_elements");
		break;
		
	case 'action=toc_masai_section':
		ChangeState("_6_1_elements");
		break;
		
	case 'action=toc_sammy_section':
		ChangeState("_7_1_elements");
		break;
		
	case 'action=op_help': // this only happens when validated by the game
		if ( !IsRunningInsideCallbackHost() ) {
			ChangeState("_toc_elements");
		}
		break;
	
	case 'action=op_home':
		if ( undefined != stateThatTriggeredOptions ) {
			ChangeState(stateThatTriggeredOptions);
		} else {
			ChangeState("_menu");
		}
		break;
		
	case 'action=ri_home':
	case 'action=pw_home':
	case 'action=restart_yes':
	case 'action=pm_main_menu':
		ChangeState("_menu");
		break;

	case 'action=zoom_back':
	case 'action=zoom_center':
	case 'action=zoom_right':
	case 'action=zoom_left':
		if ( "_rate_it" == lastActiveState ) {
			ChangeState("_rate_it");
		} else {
			ChangeState("_paywall");
		}
		break;
		
	case 'action=thumbnail_left':
		ChangeState("_zoom_left");
		break;
		
	case 'action=thumbnail_center':
		ChangeState("_zoom_center");
		break;
		
	case 'action=thumbnail_right':
		ChangeState("_zoom_right");
		break;
		
	case 'action=mm_like':
		ChangeState("_rate_it");
		break;
		
	case 'action=help_prev':
		ChangeState( PrevHelpPageFrom(activeState) );
		break;
		
	case 'action=help_next':
		ChangeState( NextHelpPageFrom(activeState) );
		break;
		
	}
	
	var sent_msg = false;
		
	if ( undefined != window["s3e"] ) {
	
		s3e.exec( msg );
		sent_msg = true;
		
	}
	
	if ( undefined != window["js_to_sputm_callback"] ) {
	
		js_to_sputm_callback( msg );
		sent_msg = true;
	
	}
	
	if ( !sent_msg) { ChangeMsg( msg ); }
	
} 

function noop_event_handler(event) {
	event.preventDefault();
}

// STATE INDICATOR

var lastMsg = "";

function ClearMsg() {
    lastMsg = ""
    var e = document.getElementById('msg');
	if ( null != e ) e.innerHTML = "";
}

function ChangeMsg(msg) {
	if ( lastMsg != msg ) {
	    var e = document.getElementById('msg');
	    if (null != e) {
	        e.innerHTML =
			    "<div id='msg_holder'><div id='msg_indicator'>" + msg + "</div></div>";
	    }
		lastMsg = msg;
	}
}

function AppendMsg(msg) {
	ChangeMsg( lastMsg + msg );
}

// ANIM MANAGEMENT

function CycleThroughArray( frames ) {

	var f = -1;

	return function() {
	
		return frames[ f = ((++f >= frames.length) ? 0 : f) ];
	
	}
	
}

function AnimateVisibility( frames ) {

	var i;
	
	for ( i in frames ) {
	
		SetItemVisibility( frames[i], 'hidden' );
	
	}

	var nextFN = CycleThroughArray( frames );
	
	var prev = undefined;
	
	return function() {
	
		if ( undefined != prev ) SetItemVisibility( prev, 'hidden' );
		
		prev = nextFN(); 
		
		SetItemVisibility( prev, 'visible' );
	
	}

}

// animation controller

var active_animations = {}
var inactive_animations = {}

function ActivateAnimation( frames, bStart ) {

	if ( inactive_animations.hasOwnProperty( frames ) ) {
	
		if ( bStart ) {
		
			active_animations[ frames ] = inactive_animations[ frames ];
		
			delete inactive_animations[ frames ];
			
			return;
		
		}
		
	}

	if ( active_animations.hasOwnProperty( frames ) ) {
	
		if ( bStart ) return;

		inactive_animations[ frames ] = active_animations[ frames ];
	
		delete active_animations[ frames ];
		
	}
	
	if ( bStart ) {

		active_animations[ frames ] = AnimateVisibility( frames );
		
	}

}

function PerformAnimations() {

	for ( var i in active_animations ) {
	
		active_animations[i]();
	
	}

}

// STATE MANAGEMENT

var lastActiveState = "";
var activeState = "";

var y_head_animation_frames = [ "y_frame_01", "y_frame_02", "y_frame_03", "y_frame_04" ];
var n_head_animation_frames = [ "n_frame_01", "n_frame_02", "n_frame_03", "n_frame_04" ];
var m1_butterfly_frames = [];
var m2_butterfly_frames = [];
var pm_butterfly_frames = [];
var ri_butterfly_frames = [];
var pw_butterfly_frames = [];
var pc_butterfly_frames = [];

var stateInfo = {

	"_menu":{ divs:[ "menu_elements" ], items:[ "menu_elements" ], anims:[ m1_butterfly_frames, m2_butterfly_frames ] }
,	"_pause":{ divs:[ "pause_elements" ], items:[ "pause_elements" ], anims:[ pm_butterfly_frames ] }
,	"_options":{ divs:[ "options_elements" ], items:[ "options_elements" ] }
,	"_paywall":{ divs:[ "paywall_elements", "shared_thumbnail_elements" ], items:[ "paywall_elements", "shared_thumbnail_elements" ], anims:[ pw_butterfly_frames ] }
,	"_rate_it":{ divs:[ "rate_it_elements", "shared_thumbnail_elements" ], items:[ "rate_it_elements", "shared_thumbnail_elements" ], anims:[ ri_butterfly_frames ] }
,	"_zoom_left":{ divs:[ "zoom_elements" ], items:[ "zoom_elements" ], visible:{ "zoom_left":true, "zoom_center":false, "zoom_right":false} }
,	"_zoom_center":{ divs:[ "zoom_elements" ], items:[ "zoom_elements" ], visible:{ "zoom_left":false, "zoom_center":true, "zoom_right":false} }
,	"_zoom_right":{ divs:[ "zoom_elements" ], items:[ "zoom_elements" ], visible:{ "zoom_left":false, "zoom_center":false, "zoom_right":true} }
,	"_restart":{ divs:[ "restart_elements" ], items:[ "restart_elements" ], anims:[ y_head_animation_frames, n_head_animation_frames ] }
,	"_help":{ divs:[ "help_elements" ], items:[ "help_elements" ] }
,	"_ios_menu":{ divs:[ "menu_elements" ], items:[ "menu_elements" ], anims:[ m1_butterfly_frames, m2_butterfly_frames ] }
,	"_pc_menu":{ divs:[ "pc_menu_elements" ], items:[ "pc_menu_elements" ], anims:[ pc_butterfly_frames ] }
,	"_splash":{ divs:[ "splash_elements" ], items:[ "splash_elements" ]}

,	"_toc_elements": {
		"divs": [
			"help_elements",
			"help_bg_elements",
			"help_navigation_buttons",
			"_toc_elements",
		],
		"items": [
			"help_bg_elements",
			"help_navigation_buttons",
			"_toc_elements",
			"toc_sammy_section",
			"toc_masai_section",
			"toc_zanzibar_section",
			"toc_skeeter_section",
			"toc_jambo_section",
			"toc_kenya_section",
			"toc_game_start_section"
		]
	}


};

// --------------------------------------------------------

function SetItemVisibility( id, vis ) {

	var e = $( document.getElementById( id ) );
	
	var bVisible = ('visible' == vis);
	
	if ( $.ui ) {
	
		e.switchClass(
			bVisible ? 'bpt_hidden_item' : 'bpt_visible_item',
			bVisible ? 'bpt_visible_item' : 'bpt_hidden_item', 0,
			function() {
				ChangeLoadedStateForThese( e, bVisible );
			});
			
; // load/unload assets

	} else {
	
		e	.removeClass( bVisible ? 'bpt_hidden_item' : 'bpt_visible_item')
			.addClass( bVisible ? 'bpt_visible_item' : 'bpt_hidden_item' );
		
		ChangeLoadedStateForThese( e, bVisible );

	}
	
}

function SetElementsVisibility( id, vis ) {

	var e = $( document.getElementById( id ) );
	
	var bVisible = ('visible' == vis);
	
	if ( $.ui ) {
	
		e.switchClass(
			bVisible ? 'bpt_hidden_elements' : 'bpt_visible_elements',
			bVisible ? 'bpt_visible_elements' : 'bpt_hidden_elements', 0 );
			
; // load/unload assets

	} else {
	
		e	.removeClass( bVisible ? 'bpt_hidden_elements' : 'bpt_visible_elements')
			.addClass( bVisible ? 'bpt_visible_elements' : 'bpt_hidden_elements' );
			
	}

}

// --------------------------------------------------------

function ChangeLoadedStateForThese( sel, bLoad, classes ) {

	if ( undefined == classes ) classes = '';

	$('img._src_' + classes,$(sel)).each( function( i, img ) {
	
		img = $(img);
	
		if ( bLoad ) {
		
			img.attr( 'src', BPT_URIForImage(img.attr('_src_')) );
			
			if ( img.hasClass( '_keep_' ) || img.hasClass( '_preload_' )) {
			
				img.removeClass( '_src_' ); // once this is loaded it stays loaded...
			
			}
			
		} else {

			img.attr( 'src', BPT_URIForImage('_.gif') );
			
		}
		
	});
	
}

function ChangeLoadedStateForImgsWithin( who, bLoad ) {

	ChangeLoadedStateForThese( $(who), bLoad );
	
}

// --------------------------------------------------------

function SetStateVisibility( state, vis ) {

	var info = stateInfo[ state ];
	
	if ( undefined == info ) return;
	
	var bHide = ('hidden' == vis);
		
	var items = info.items;
	
	for ( var i = items.length; 0 <= --i; ) {
	
		SetItemVisibility( items[ i ], vis );
		
	}
	
	// manage the display state of the controlling divs
	
	var divs = info.divs;
	
	for ( var i = divs.length; 0 <= --i; ) {
	
		SetElementsVisibility( divs[ i ], vis );
		
	}
	
	// check for specific visibility exclusive
	
	for ( var v in info.visible ) {
	
		SetItemVisibility( v, info.visible[v] ? vis : 'hidden' );
	
	}
	
	for ( var a in info.anims ) {
	
		ActivateAnimation( info.anims[ a ], !bHide );
	
	}

}

// ---------------------------------------------------------------

function RedirectStateToExistingState( target, source ) {
	if ( stateInfo.hasOwnProperty( source ) ) {
		stateInfo[target] = stateInfo[source];
	}
}

function ChangeState( state ) {

	if ( activeState == state ) return;
	
	$('#loading_indicator').show();
	
	//ChangeMsg( state );
	
	if ( "" != activeState ) {
	
		SetStateVisibility( activeState, 'hidden' );
		
	}
	
	lastActiveState = activeState;
	
	activeState = state;
	
	SetStateVisibility( activeState, 'visible' );
	
	$('#loading_indicator').hide();
}

function HideAllStates() {

	for ( var state in stateInfo ) {
	
		SetStateVisibility( state, 'hidden' );
	
	}

}

// ------------------------------------------------------------------------

function DetermineTruth( truthyOrFalse ) {

	if ( 'boolean' == typeof truthyOrFalse ) return (false != truthyOrFalse);
	
	return !('false' == truthyOrFalse);
}

function SetContextInformation( context, truthyOrFalse ) {

	if ( undefined == context ) return;

	var contextHolders = $( '.' + context );
	
	if ( (undefined == contextHolders) && (0 == contextHolders.length) ) return;
	
	var context_truth_class = context + '_true';
	
	if ( DetermineTruth(truthyOrFalse) ) {
	
		contextHolders.addClass( context_truth_class );
	
	} else {

		contextHolders.removeClass( context_truth_class );
	
	}

}

function EnableContext( context, bEnable ) {
	SetContextAsDisabled( context, !DetermineTruth(bEnable) );
}

function SetContextAsDisabled( context, bDisabled ) {

	if ( undefined == context ) return;

	var contextHolders = $( '.' + context );
	
	if ( (undefined == contextHolders) && (0 == contextHolders.length) ) return;
	
	var context_disabled_class = '_disabled';
	
	if ( DetermineTruth(bDisabled) ) {
	
		contextHolders.addClass( context_disabled_class );
	
	} else {

		contextHolders.removeClass( context_disabled_class );
	
	}

}
// -------------------------------------------------------

var old_id_to_context = {

	"op_skip_check" : "_skip"
,	"op_game_center_check" : "_game_center"
,	"op_text_check" : "_text"
,	"op_music_check" : "_music"
,	"op_messages_check": "_messages"
,	"op_disable_iap_check" : "_disable_iap"
,	"op_stretch_check" : "_stretch"
,	"op_fullscreen_check" : "_fullscreen"

};

function TranslateIDToContext(id) {

	if ( old_id_to_context.hasOwnProperty( id ) ) return old_id_to_context[id];
	
	return undefined;
}

function PMCheck( id, bChecked ) {

	bChecked = DetermineTruth( bChecked );

	var context = TranslateIDToContext(id);

	SetContextInformation( context, bChecked );
	
}

function PMShow( id, bShow ) {

	bShow = DetermineTruth( bShow );
	
	if ( 'mm_unlock_now' == id ) {
	
		SetContextInformation( '_locked', bShow );
		SetContextInformation( '_unlocked', !bShow );
	
	}
}

// -------------------------------------------------------

/* these need to be styles!!! */

var checkbox_ids = [
	"op_skip"
,	"op_game_center"
,	"op_text"
,	"op_music"
,	"op_disable_iap"
,	"op_fullscreen"
,	"op_stretch"
];

var button_ids = [
	"restart_yes"
,	"restart_no"
,	"pm_home"
,	"pm_options"
,	"pm_main_menu"
,	"pm_play_now"
,	"mm_options"
,	"mm_more_games"
,	"mm_newsletter"
,	"mm_tellafriend"
,	"mm_like"
,	"mm_unlock_now"
,	"mm_play_now"
,	"mm_ads"
,	"op_restart"
,	"op_help"
,	"op_support"
,	"op_home"
,	"op_version"
,	"op_terms"
,	"op_privacy"
,	"op_restore_purchase"
,	"op_messages"
,	"pw_home"
,	"pw_unlock_now"
,	"pw_already_purchased"
,	"thumbnail_left"
,	"thumbnail_center"
,	"thumbnail_right"
,	"ri_home"
,	"ri_rate_it_now"
,	"zoom_back"
,	"zoom_left"
,	"zoom_center"
,	"zoom_right"
,	"help_home"
,	"help_prev"
,	"help_next"
,	"toc_sammy_section"
,	"toc_masai_section"
,	"toc_zanzibar_section"
,	"toc_skeeter_section"
,	"toc_jambo_section"
,	"toc_kenya_section"
,	"toc_game_start_section"
,	"pc_play_now"
,	"pc_menu_options"
,	"pc_menu_load"
,	"pc_menu_save"
,	"pc_quit_game"
];

// -------------------------------------------------------

function Action( action, actionVisibilityOwner ) {

	if ( undefined != actionVisibilityOwner ) {
	
		var e = document.getElementById( actionVisibilityOwner );
		
		if ( undefined != e ) {
	
			if ( 'hidden' == e.style.visibility ) {
			
				NimbusGames_send_host_message( "skipping=" + action );
				return;
				
			}
			
		}
	
	}
	
	NimbusGames_send_host_message( "action=" + action );
}

// I'm sure there is an existing idiom for this...

function TransferClassToSelector( selector, classname ) {

	$('.'+classname).removeClass(classname);
	$(selector).addClass(classname);

}

function Dispatch( who ) {

	var sel = "#" + who.id;
	
	if ( $(sel).hasClass( "_disabled" ) ) {
		return;
	}
	
	TransferClassToSelector( sel, '_last_dispatch' );

	if ( $(sel).hasClass( "_state_changer" ) ) {
	
		var where = $(sel).attr('_to_');
		
		if ( where ) {
		
			ChangeState( where );
	
			return;
			
		}
	
	}

	Action( who.id, who.parentNode.id );
}

// -------------------------------------------------------

function AttachInputEventHandlers( element, fn ) {
	
	if ( 'IMG' == element.tagName ) {
		
		$(element).addClass('_interactive');
		
	}
	
	$('img',$(element)).addClass('_interactive');

	element.addEventListener(('ontouchend' in window) ? 'touchend': 'click', fn, false );

}

function SetupButton( id ) {

	if ( undefined != document.getElementById( id ) ) {
	
		AttachInputEventHandlers(
			document.getElementById( id ),
			function() {
				Dispatch( document.getElementById( id ) );
			} );
		
	}

}

function SetupCheckbox( id ) {

	SetupButton( id );
	
	// other actions specific to checkboxes

}

function SetupInteractiveElements() {

	for ( var b in button_ids ) {
		SetupButton( button_ids[b] );
	}

	for ( var c in checkbox_ids ) {
		SetupCheckbox( checkbox_ids[c] );
	}
	
	// ---------------------------------------------------
	
	$("._state_changer").each( function( i, changer ) {
	
		SetupButton( $(changer).attr('id') );
	
	});
	
	// ---------------------------------------------------

}

// -------------------------------------------------------

var help_navigation_info = { page_to_name:[], name_to_page:{} };

function PrevHelpPageFrom( current ) {

	// TODO
	// on invalid reference it really should take them to the TOC (once that exists)
	
	if ( !help_navigation_info.name_to_page.hasOwnProperty( current ) ) {
	
		return help_navigation_info.page_to_name[ 0 ];
	
	}
	
	var index = help_navigation_info.name_to_page[ current ] - 1;
	
	if ( 0 > index ) {
	
		index = help_navigation_info.page_to_name.length - 1;
		
		return "_toc_elements";
		
	}
	
	return help_navigation_info.page_to_name[ index ];

}

function NextHelpPageFrom( current ) {

	if ( !help_navigation_info.name_to_page.hasOwnProperty( current ) ) {
	
		return help_navigation_info.page_to_name[ 0 ];
	
	}
	
	var index = help_navigation_info.name_to_page[ current ] + 1;
	
	if ( help_navigation_info.page_to_name.length <= index ) {
	
		index = 0;
		
		return "_toc_elements";
		
	}
	
	return help_navigation_info.page_to_name[ index ];
}

// -------------------------------------------------------

function AddHelpPagesToAvailableStates() {

	var sections = $("._section_container");
	
	sections.each( function( i, section ) {
	
		section = $(section);
	
		var pages = $("._section_page",section);
		
		var section_div_id = "" + section.attr('id');
		
		var section_items = [ "help_navigation_buttons", section_div_id ]
		
		$("._section_header",section).each( function( i, item ) {
		
			item = $(item);
		
			section_items.push( "" + item.attr('id') );
			
		});
		
		pages.each( function( i, page ) {
		
			page = $(page);
		
			var page_id = "" + page.attr('id');
			
			var info = stateInfo[ page_id ] = {};
			
			info.divs = [
				"help_elements"
			,	"help_navigation_buttons"
			,	"help_page_banner"
			,	"help_bg_elements"
			,	page_id
			];
			
			info.items = [
				page_id
			,	"help_navigation_buttons"
			,	"help_page_banner"
			,	"help_bg_elements"
			];
			
			$.each( section_items, function() { info.items.push( this ); });
			
		});
		
	});
	
}

// -------------------------------------------------------

function BuildHelpNavigationTables() {

	$("._section_page").each( function( i, item ) {
		help_navigation_info.page_to_name.push( "" + $(item).attr('id') );
	});
	
	help_navigation_info.page_to_name.sort();
	
	for ( var i in help_navigation_info.page_to_name ) {
	
		help_navigation_info.name_to_page[ help_navigation_info.page_to_name[ i ] ] = parseInt(i);
	
	}
}

// -------------------------------------------------------

function SetStateChangeInfoForSelector( sel, state ) {

	$(sel).addClass( '_state_changer' );
	$(sel).attr('_to_', state );

}

function MakeSeperatePagesForPanels( holder, combined_page ) {

	var page_id = $(combined_page).attr('id');
	
	$("._section_panels", combined_page ).each( function( i, panel ) {
	
		$( $(panel).children() ).each( function( i, content ) {
	
			var content_id = $(content).attr('id');
		
			var sub_page = $("<div>")
				.attr('id', '_zoom' + content_id + '_elements')
				.attr('class', $(combined_page).attr('class') )
				;
				
			sub_page.appendTo( $(holder) );
			
			var sub_panel_holder = $("<div>")
				.attr('id', content_id + '_panels')
				.attr('class', $(panel).attr('class') )
				;

			SetStateChangeInfoForSelector( $(sub_panel_holder), page_id );
			
			SetStateChangeInfoForSelector( $(content), $(sub_page).attr('id') );
				
			$(sub_panel_holder).appendTo( $(sub_page) );
			
			var clo = $(content).clone(); // what about data???
			
			$(clo).attr('id', $(clo).attr('id') + '_');
			
			$(clo).removeClass( content_id + '_position' );
			$(clo).removeClass( content_id + '_p' );
			
			$(clo).addClass( 'zoomed_and_centered_panel' );
			
			// reposition it
			
			$(clo).appendTo( $(sub_panel_holder) );
				
		});
	
	});

}

function AddAlternativeHelpLayouts()
{
	var all_sections = $("._section_container", $("#help_elements") );
	
	$(all_sections).each( function( idx, section ) {
	
		var section_id = $(section).attr('id');
	
		var section_pages = $("._section_page", $(section) );
		
		var holder = $("<div>").attr('id',section_id + '_panels_as_seperate_pages');
		
		holder.appendTo( $(section) );
		
		$(section_pages).each( function( i, combined_page ) {
		
			MakeSeperatePagesForPanels( holder, combined_page );
			
		});
	
	});

}

// -------------------------------------------------------

function SetupHelpPages() {

	// now make the alternative layout
	
	AddAlternativeHelpLayouts();
	
	// link them up
	
	BuildHelpNavigationTables();
	
	// now put them so they can be accessed :)
	
	AddHelpPagesToAvailableStates();
	
}

// -------------------------------------------------------

function SetupButterflyAnim( holder, placeholder, prefix, framesArray ) {

	holder = $('#' + holder);
	
	placeholder = $('#' + placeholder);
	
	for ( var i = 1; i <= 22; ++i ) {
	
		var clone = placeholder.clone();
		
		var tens = Math.floor( i / 10 ) % 10;
		var ones = i % 10;
		
		clone.attr('_src_', 'bf/bf_' + tens + '' + ones + '.png' );
		
		var id = prefix + tens + '' + ones;
		
		clone.attr('id', id );
		
		clone.appendTo( holder );
		
		framesArray[ i - 1 ] = id;
	
	}

	placeholder.remove();
}

function AddAnimationFramesToPlaceholders() {

	SetupButterflyAnim( 'ri_anim_holder', 'menu_ri_anim', 'ri_bf_', ri_butterfly_frames );
	SetupButterflyAnim( 'pw_anim_holder', 'menu_pw_anim', 'pw_bf_', pw_butterfly_frames );
	SetupButterflyAnim( 'pm_anim_holder', 'menu_pm_anim', 'pm_bf_', pm_butterfly_frames );
	SetupButterflyAnim( 'mm_play_anim', 'menu_mm_play_anim', 'm1_bf_', m1_butterfly_frames );
	SetupButterflyAnim( 'mm_unlock_anim', 'mm_unlock_anim_2', 'm2_bf_', m2_butterfly_frames );
	SetupButterflyAnim( 'pc_play_anim_holder', 'menu_pc_play_anim', 'pc_bf_', pc_butterfly_frames );
	
}

// -------------------------------------------------------

$(document).ready(function() {

	document.body.addEventListener('touchmove', noop_event_handler, false );
	document.body.addEventListener('gesturestart', noop_event_handler, false );
	document.body.addEventListener('gesturechange', noop_event_handler, false );
	document.body.addEventListener('gestureend', noop_event_handler, false );
	
	document.body.addEventListener('ondragstart', noop_event_handler, true );
	document.body.addEventListener('ondrag', noop_event_handler, true );
	document.body.addEventListener('ondragend', noop_event_handler, true );

	SetupHelpPages();
	
	AddAnimationFramesToPlaceholders();
	
	HideAllStates();
	
	ChangeLoadedStateForThese( $('#menu_elements') , true, '._preload_' );

});


// ----------------------------------------------------------------------------

function TESTING_PC(blocked) {
	
	RedirectStateToExistingState('_menu','_pc_menu'); // Aric the PC needs call this :)
	
	SetContextInformation('_has_terms',false);
	SetContextInformation('_has_privacy',false);
	SetContextInformation('_has_support',false);
	SetContextInformation('_show_version',false);
	
	SetContextInformation('_has_stretch',false);
	EnableContext('_has_stretch', false);
	
	SetContextInformation('_has_fullscreen',true);
	EnableContext('_has_fullscreen', false);
	
	SetContextInformation('_has_quit_button',true);
	SetContextInformation('_user_has_not_tapped_rating',true);

	SetContextInformation('_has_game_center',false);
	SetContextInformation('_push',false);
	SetContextInformation('_disable_iap',!blocked);
	SetContextInformation('_iap_enabled',blocked);
	SetContextInformation('_unlocked',(!blocked));
	SetContextInformation('_locked',blocked);
	
	SetContextInformation('_has_save', true);
	EnableContext('_has_save', false);
	
	SetContextInformation('_has_load', true);
	EnableContext('_has_load', false);
	
	ChangeState("_splash");
	
	//code
}

function TESTING_iOS( blocked ) {
	
	SetContextInformation('_has_terms',true);
	SetContextInformation('_has_privacy',true);
	SetContextInformation('_has_support',true);
	SetContextInformation('_show_version',true);
	
	SetContextInformation('_has_stretch',false);
	SetContextInformation('_has_fullscreen',false);
	SetContextInformation('_has_quit_button',false);
	SetContextInformation('_user_has_not_tapped_rating',true);

	SetContextInformation('_has_game_center',true);
	SetContextInformation('_push',false);
	SetContextInformation('_disable_iap',blocked);
	SetContextInformation('_iap_enabled',!blocked);
	SetContextInformation('_unlocked',(!blocked));
	SetContextInformation('_locked',blocked);
	ChangeState("_menu");

}

// ----------------------------------------------------------------------------

$(window).load(function() {

	NimbusGames_send_host_message("status=menu_is_finally_ready");

	SetupInteractiveElements();
	
	setInterval( PerformAnimations, 100 );
	
	ChangeLoadedStateForThese( $('#container') , true, '._preload_' );
	
	if ( !IsRunningInsideCallbackHost() ) {
	
		TESTING_PC(false);
		//TESTING_iOS(false);
	
	}
	
});
