/**
 * 
 * @application Virtual Business Builder
 * @version 0.0.0.1
 * @author Chris Keegan
 * @copyright Evans & Finch, 2009
 * 
 */

/////////////////////////////////////////////////////////////////////////////
//
// Tracking
//
/////////////////////////////////////////////////////////////////////////////

function getSessionId()
{
	if ( typeof( window.m_tracker ) == "undefined" )
		return -1;
	
	return window.m_tracker.m_session_id;
}

function onTrackingInitialised( tracking )
{
}

/////////////////////////////////////////////////////////////////////////////
//
// Flash Event Handlers
//
/////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////
//
// Screen Transitions
//
/////////////////////////////////////////////////////////////////////////////

function onScreenTransition( context )
{
	var sector_name, size_name;
	switch ( context.phase )
	{
		case "begin":
		{
			switch ( context.to )
			{
				case "state_business":
				{
				} break;
				
				case "state_size":
				{
					sector_name = window.page.getSectorName( window.page.sector );
					size_name = window.page.getSizeName( window.page.size );
					$(".nav_tab_link#nav_tab_link_sector").html("<span></span>"+sector_name);
					$(".nav_tab_link#nav_tab_link_size").html("<span></span>"+size_name);
					$( ".container#widget_container_flash" ).animate({borderColor: "#CCD0D3"},{duration:500});

					$( ".nav_tab#nav_tab_size" ).fadeIn( 500 );
					$( ".nav_tab#nav_tab_sector" ).fadeIn( 500 );
					$( ".nav_tab#nav_tab_explorer" ).fadeIn( 500 );
					
				} break;
				case "state_sector":
				{
					sector_name = window.page.getSectorName( window.page.sector );
					$(".nav_tab_link#nav_tab_link_sector").html("<span></span>"+sector_name);

					$( ".nav_tab#nav_tab_size" ).fadeOut( 500 );
					$( ".nav_tab#nav_tab_sector" ).fadeIn( 500 );
					$( ".nav_tab#nav_tab_explorer" ).fadeIn( 500 );
					$( ".container#widget_container_flash" ).animate({borderColor: "#CCD0D3"},{duration:500});
					
				} break;
				case "state_explorer":
				{
					$(".nav_tab#nav_tab_sector").fadeOut( 500 );
					$(".nav_tab#nav_tab_size").fadeOut( 500 );
					
					$( ".nav_tab#nav_tab_explorer" ).fadeIn( 500 );
					$( ".container#widget_container_flash" ).animate({borderColor: "#CCD0D3"},{duration:500});
				} break;
				case "state_home":
				{
					$(".nav_tab#nav_tab_sector").fadeOut( 500 );
					$(".nav_tab#nav_tab_size").fadeOut( 500 );
					$( ".container#widget_container_flash" ).animate({borderColor: "#ffffff"},{duration:500});
					$( ".nav_tab#nav_tab_explorer" ).fadeOut( 500 )
					
				} break;
			}
		} break;
		case "end":
		{
			switch ( context.to )
			{
				case "state_explorer":
				{
					window.page.getPageCopy( -1, -1 );
				} break;
				case "state_home":
				{
					if ( context.from != "state_preloader" )
						window.page.getPageCopy( -1, -1 );
					
					$(".primary_container#go_container").children().css("cursor","pointer");
				} break;
			}
		} break;
	}
}

/////////////////////////////////////////////////////////////////////////////
//
// Game Events
//
/////////////////////////////////////////////////////////////////////////////

function onGameEvent( context )
{
	switch ( context.type )
	{
		case "sector_selected":
		{
			window.page.sector = context.sector;
			window.page.selection_changed = true;
			onSectorSelected( context.sector );
		} break;	
		case "size_selected":
		{
			window.page.size = context.size;
			window.page.selection_changed = true;
			onSizeSelected( window.page.sector, context.size );
		} break;	
	}
}

function onSectorSelected( sector )
{
	window.page.getPageCopy( sector, -1 );
	window.page.rollingPageCopy();
}

function onSizeSelected( sector, size )
{
	window.page.getPageCopy( sector, size );
	window.page.rollingPageCopy();
}

/////////////////////////////////////////////////////////////////////////////
//
// Widget Event Handlers
//
/////////////////////////////////////////////////////////////////////////////


function onVBBClicked( data )
{
	var flash = $( "#main_flash_window" );
	if ( $(flash).length > 0 )
	{
		$( "#main_flash_window" ).get(0).controllerEvent( "goto_explorer" );
		window.location = "#header";
	}
	else
		window.location = "/explorer";
}

function onVBBMouseEnter( data )
{
}

function onVBBMouseLeave( data )
{
}

/////////////////////////////////////////////////////////////////////////////
//
// VBB Page
//
/////////////////////////////////////////////////////////////////////////////

( 
	function()
	{
		var vbbPage = window.vbbPage = function()
		{
			return vbbPage.fn.__init();
		};
	
		vbbPage.fn = (Object) (vbbPage.prototype =
		{
			version: "0.0.1",
			sector: -1,
			size: -1,
			business: -1,
			out_of_hours: false,
			graphs: null,
			call: false,
			go: false,
			animating: false,
			go_copy: "",
			selection_changed: false,
				
			__init : function()
			{
				window.page = this;
				
				this.business_name = $("meta[name=business_name]").attr("content");
				this.path = $("meta[name=path]").attr("content").split(".");
				this.out_of_hours = $("meta[name=out_of_hours]").attr("content") == "yes";
				this.go_copy = $("meta[name=go_copy]").attr("content"); 
				this.analysePath();
				
				if ( typeof( this.init ) != "undefined" )
					this.init();
				
				this.loadHoverImages();
				
				this.primaryEventHandlers();
				this.headerClickHandlers();
				this.navTabClickHandlers();
				//this.seoClickHandlers();
				this.footerClickHandlers();
				
				this.rollingPageCopy();
				
				return this;
			}, // init

			/////////////////////////////////////////////////////////////////////////////
			//
			// Rolling Copy
			//
			/////////////////////////////////////////////////////////////////////////////
			
			rollingPageCopy: function()
			{
				var page = this;
				$(this).stopTime( "rolling_copy" );
				$(this).everyTime(12000, "rolling_copy", function()
				{
					page.getPageCopy( page.sector, page.size );
					return true;
				});
				
				return this;
			}, // rollingPageCopy
			
			/////////////////////////////////////////////////////////////////////////////
			//
			// Path
			//
			/////////////////////////////////////////////////////////////////////////////
			
			analysePath: function()
			{
				if ( this.path[ 0 ] == "explorer" )
				{
					if ( this.path.length > 1 )
					{
						this.sector = this.getSectorIdxFromPath( this.path[ 1 ] );
						if ( this.path.length > 2 )
						{
							this.size = this.getSizeIdxFromPath( this.path[ 2 ] );
						}
					}
				}
				
				
				return this;
			}, //analysePath
			
			loadHoverImages: function()
			{
				$().loadImg({src:"assets/images/panels/call_on_left.jpg"});
				$().loadImg({src:"assets/images/panels/call_on_right.jpg"});
				$().loadImg({src:"assets/images/panels/go_on_left.jpg"});
				$().loadImg({src:"assets/images/panels/go_on_right.jpg"});
				
				return this;
			}, //loadHoverImages
			
			/////////////////////////////////////////////////////////////////////////////
			//
			// Event Handlers
			//
			/////////////////////////////////////////////////////////////////////////////

			/////////////////////////////////////////////////////////////////////////////
			//
			// Primary Event Handlers
			//
			/////////////////////////////////////////////////////////////////////////////
			
			primaryEventHandlers: function()
			{
				/*var page = this;
				$(".primary_container#call_container").children().mouseenter( function( ev ) 
				{
					$( ".primary_container_left#call_container_left" ).removeClass("call_off_left").addClass( "call_on_left" );
					$( ".primary_container_right#call_container_right" ).removeClass("call_off_right").addClass( "call_on_right" );
					page.showCallContainer();
					return false;
				});

				$(".primary_container#call_container").children().mouseleave( function( ev ) 
				{
					$( ".primary_container_left#call_container_left" ).addClass("call_off_left").removeClass( "call_on_left" );
					$( ".primary_container_right#call_container_right" ).addClass("call_off_right").removeClass( "call_on_right" );
					return true;
				});

				$(".primary_container#go_container").children().mouseenter( function( ev ) 
				{
					$( ".primary_container_left#go_container_left" ).removeClass("go_off_left").addClass( "go_on_left" );
					$( ".primary_container_right#go_container_right" ).removeClass("go_off_right").addClass( "go_on_right" );
					page.showGoContainer();
					return true;
				});

				$(".primary_container#go_container").children().mouseleave( function( ev ) 
				{
					$( ".primary_container_left#go_container_left" ).addClass("go_off_left").removeClass( "go_on_left" );
					$( ".primary_container_right#go_container_right" ).addClass("go_off_right").removeClass( "go_on_right" );
					return true;
				});
				
				$(".primary_container#call_container").click( function( ev )
				{
					if ( page.out_of_hours )
					{
						window.location = "/callback";
					}
					else
					{
						window.location = "/callback";
					}
					return false;
				});

				$(".primary_container#go_container").click( function( ev )
				{
					var flash = $( "#main_flash_window" );
					if ( $(flash).length > 0 )
						$( "#main_flash_window" ).get(0).controllerEvent( "goto_video" );
					else
						window.location = "/find_out_more";
					
					return false;
				});*/
				
			
			/////SEO DROPDOWN JUMP MENU (LINK = VALUE)
				$(".dropdown_seo").change(function()
				{
						window.location.href = this.options[this.selectedIndex].value;
											
				});			
				
				return this;
			}, // primaryEventHandlers
			
			/////////////////////////////////////////////////////////////////////////////
			//
			// Header Event Handlers
			//
			/////////////////////////////////////////////////////////////////////////////
			
			
			headerClickHandlers: function()
			{
				var page = this;
				$(".header_link#header_nav_link_home").click( function( ev ) 
				{
					page.selection_changed = true;
					page.sector = -1;
					page.size = -1;
					page.getPageCopy( -1, -1 );
					page.rollingPageCopy();
					
					if ( $( "#main_flash_window" ).length > 0 )
					{
						if ( page.path[ 0 ] == "index" || page.path[ 0 ] == "explorer" || page.path[ 0 ] == "business" )
						{
							$( "#main_flash_window" ).get(0).controllerEvent( "goto_home" );
							return false;
						}
					}
					
					return true;
				});
				
				return this;
			}, // headerClickHandlers
			
			navTabClickHandlers: function()
			{
				$(".nav_tab_link#nav_tab_link_explorer").click( function( ev ) 
				{
					var flash = $( "#main_flash_window" );
					if ( $(flash).length > 0 )
					{
						$( "#main_flash_window" ).get(0).controllerEvent( "goto_explorer" );
						return false;
					}
				
					return true;
				});
				
				$(".nav_tab_link#nav_tab_link_sector").click( function( ev ) 
				{
					var flash = $( "#main_flash_window" );
					if ( $(flash).length > 0 )
					{
						$( "#main_flash_window" ).get(0).controllerEvent( "goto_sector" );
						return false;
					}
					
					return true;
				});
				
				$(".nav_tab#widget_nav_tab_size").click( function( ev ) 
				{
					var flash = $( "#main_flash_window" );
					if ( $(flash).length > 0 )
					{
						$( "#main_flash_window" ).get(0).controllerEvent( "goto_size" );
						return false;
					}
					return true;
				});
				
				return this;
			}, // navTabClickHandlers			
			
			/////////////////////////////////////////////////////////////////////////////
			//
			// SEO Event Handlers
			//
			/////////////////////////////////////////////////////////////////////////////
			
			seoClickHandlers: function()
			{
				$(".footer_seo_link").click( function( ev ) 
				{
					var info_id = $(this).attr("info_id");
					$.get( "/api/get-info-html.php", 
					{ 
						info_id: info_id
					}, function(data, textStatus)
					{
							page.loadInfoHtml( data );
							return this;
					}, "html" );
					
					return false;
				});
				
				return this;
			}, // seoClickHandlers
			
			/////////////////////////////////////////////////////////////////////////////
			//
			// Footer Event Handlers
			//
			/////////////////////////////////////////////////////////////////////////////
			
			footerClickHandlers: function()
			{
				$(".footer_link#footer_nav_link_home").click( function( ev ) 
				{
					var flash = $( "#main_flash_window" );
					if ( $(flash).length > 0 )
					{
						$( "#main_flash_window" ).get(0).controllerEvent( "goto_home" );
						//$(".wrapper#content_wrapper_middle").fadeOut(500);
						return false;
					}
					
					return true;
				});

				$(".footer_link#footer_nav_link_explore").click( function( ev ) 
				{
					var flash = $( "#main_flash_window" );
					if ( $(flash).length > 0 )
					{
						$( "#main_flash_window" ).get(0).controllerEvent( "goto_explorer" );
						//$(".wrapper#content_wrapper_middle").fadeOut(500);
						return false;
					}
					
					return true;
				});
				
				$(".link#fact_link").click( function(ev)
				{
					
					//$( "#main_flash_window" ).get(0).controllerEvent( "goto_explorer" );
					//$(".wrapper#content_wrapper_middle").fadeOut(500);
					return true;
				});
				
				return this;
			}, // footerClickHandlers
			
			/////////////////////////////////////////////////////////////////////////////
			//
			// Helpers
			//
			/////////////////////////////////////////////////////////////////////////////
			
			showCallContainer: function()
			{
				if ( this.call == true || this.animating == true )
					return this;
				
				if ( this.go == true )
					this.hideGoContainer();

				this.animating = true;
				
				var page = this;
				$(".primary_container#call_container").animate({ height:"209px"});
				$(".primary_container_right#call_container_right").animate({ height:"196px"});
				
				$( ".primary_body#call_body" ).fadeOut( 100, function()
				{
					if ( page.out_of_hours == true )
					{
						//$( ".primary_body#call_body" ).html("The free health for work adviceline for small business helps you to quickly and effectively address the issue of employee ill health, minimise the impact of staff illness, and provide essential support to staff with physical or mental health issues.<br>The adviceline is open from 9am until 5pm on weekdays excluding national bank holidays.");
						$( ".primary_body#call_body" ).html("The free 'Health for Work Adviceline for Small Business' is open from 9am until 5pm on weekdays excluding national bank holidays.<br><br>Fill in our simple <a id='primary_link_callback' class='primary_link' href='/callback'>callback form</a> and a local expert will call you back within two hours of the next working day.");
					}
					else
					{
						$( ".primary_body#call_body" ).text("The free 'Health for Work Adviceline for Small Business' helps you to quickly and effectively address the issue of employee ill health, minimise the impact of staff illness, and provide essential support to staff with physical or mental health issues.");
					}
					$( ".primary_body#call_body" ).fadeIn( 500, function()
					{
						page.animating = false;
						return this;
					});
					return this;
				});
				
				this.call = true;
				return this;
			}, //showCallContainer
			
			hideCallContainer: function()
			{
				if ( this.call == false || this.animating == true )
					return this;
				this.animating = true;
		
				var page = this;
				$(".primary_container#call_container").animate({ height:"65px"});
				$(".primary_container_right#call_container_right").animate({ height:"52px"});
				$( ".primary_body#call_body" ).fadeOut( 100, function()
				{
					if ( page.out_of_hours == true )
					{
						$( ".primary_body#call_body" ).html("Fill in our simple <a id='primary_link_callback' class='primary_link' href='/callback'>callback form</a>");
					}
					else
					{
						$( ".primary_body#call_body" ).text("Your first step to a healthy business");
					}
					$( ".primary_body#call_body" ).fadeIn( 500, function()
					{
						page.animating = false;
						return this;
					});
					return this;
				});
				
				this.call = false;
				return this;
			}, //showCallContainer
			

			showGoContainer: function()
			{
				if ( this.go == true || this.animating == true )
					return this;
				
				if ( this.call == true )
					this.hideCallContainer();

				this.animating = true;
				
				var page = this;
				$(".primary_container#go_container").animate({ height:"202px"});
				$(".primary_container_right#go_container_right").animate({ height:"196px"});
				
				$( ".primary_body#go_body" ).fadeOut( 100, function()
				{
					$( ".primary_body#go_body" ).text(page.go_copy);
					$( ".primary_body#go_body" ).fadeIn( 500, function()
					{
						page.animating = false;
						return this;
					});
					return this;
				});
				
				this.go = true;
				return this;
			}, //showGoContainer
			
			hideGoContainer: function()
			{
				if ( this.go == false || this.animating == true )
					return this;
				this.animating = true;
				
				var page = this;

				$(".primary_container#go_container").animate({ height:"65px"});
				$(".primary_container_right#go_container_right").animate({ height:"52px"});

				$( ".primary_body#go_body" ).fadeOut( 100, function()
				{
					page.go_copy = $( ".primary_body#go_body" ).text(); 
					$( ".primary_body#go_body" ).text("Helping you build a healthier business");
					$( ".primary_body#go_body" ).fadeIn( 500, function()
					{
						this.animating = false;
						return this;
					});
					return this;
				});
				
				this.go = false;
				return this;
			}, //hideGoContainer
			
			getPageCopy: function ( sector, size )
			{
				var page = this;
				
				$.get( "/api/get-text.php", 
				{ 
					sector: sector,
					size: size
				}, function(data, textStatus)
				{
					if ( data.result == true )
					{
						page.updatePageCopy( data );
					}
					return true;
				}, "json" );
				
				return this;
			}, //getPageCopy

			updatePageCopy: function( data )
			{
				var decoded = "";
				var page = this;
				
				$( ".secondary_body#fact_body_copy" ).fadeOut( 500 , function( ev )
				{ 
					decoded = $('<textarea/>').html(data.fact).val();
					$( this ).text( decoded ).fadeIn( 500 );
					return true;
				});
				$( ".secondary_body#scenario_body_copy" ).fadeOut( 500 , function( ev )
				{ 
					decoded = $('<textarea/>').html(data.scenario.scenario).val();
					
					/*
					decodedlength = decoded.length;
					
					end_char = decoded.charAt(decodedlength-1);
										
					if (end_char == ".")
					{
						decoded = decoded.slice(0,decodedlength-1);
					}
					
					decoded = decoded + " - ";
									
					decoded_name = $('<textarea/>').html(data.scenario.business_name).val();
					$( this ).html( decoded + "<span class='secondary_body italic_on' id='scenario_business_name_body_copy'>" + decoded_name + "</span>").fadeIn( 500 );
					*/
					
					$( this ).html( decoded ).fadeIn( 500 );
					return true;
				});
				/*
				$( ".secondary_body#scenario_business_name_body_copy" ).fadeOut( 500 , function( ev )
				{ 
					decoded = $('<textarea/>').html(data.scenario.business_name).val();
					$( this ).text( decoded ).fadeIn( 500 );
					return true;
				});*/
				
				var go_decoded = $('<textarea/>').html(data.primary.one + " " + data.primary.two ).val();
				if ( page.selection_changed == true )
				{
					page.selection_changed = false;
					if ( page.go && page.animating == false )
					{
						page.animating = true;
						$( ".primary_body#go_body" ).fadeOut( 250, function( ev )
						{ 
							$( this ).text( go_decoded ).fadeIn( 250, function()
							{
								page.animating = false;
								return this;
							});
							return this;
						});
					}
					else
					{
						page.go_copy = go_decoded;
					}
				}
				
				return this;
			}, //updatePageCopy

			loadInfoHtml: function( html )
			{
				$(".wrapper#content_wrapper_middle").fadeOut(500, function()
				{
					$(this).hide().empty().append(html).fadeIn(500)
					return true;
				});
				
				return this;
			}, //loadInfoHtml

			/////////////////////////////////////////////////////////////////////////////
			//
			// Admin Helpers
			//
			/////////////////////////////////////////////////////////////////////////////

			getSectorIdxFromPath: function( path )
			{
				var urls = 	{ 
				            	agriculture													: 0, 
				              arts_entertainment_leisure 					: 1,
				              construction												: 2,
				              education														: 3,
				              finance															: 4,
				              health															: 5,
				              hotels_catering											: 6,
				              manufacturing												: 7,
				              post_telecommunications							: 8,
				              professional_scientific_technical		: 9,
				              property_business_services					: 10,
				              publicadmin													: 11,
				              retail_wholesale_motor_trade				: 12,
				              transport														: 13 
				             };
				
				
				return urls[ path ];
			}, //getSectorIdxFromPath
			
			getSizeIdxFromPath: function( path )
			{
				var urls = 	{ 	
				             	micro		: 0, 
				              small 	: 1,
				              medium	: 2,
				              large		: 3
										};
				return urls[ path ];
			}, //getSectorIdxFromPath
			
			getSectorName: function( sector_idx )
			{
				var names = [ "Agriculture", 
				              "Arts, Entertainment and Leisure",
				              "Construction",
				              "Education",
				              "Finance",
				              "Health",
				              "Hotels and Catering",
				              "Manufacturing",
				              "Post and Telecommunications",
				              "Professional, Scientific and Technical",
				              "Property and Business Services",
				              "Public Admin",
				              "Retail, Wholesale and MotorTrade",
				              "Transport" ];
				
				return names[ sector_idx ];
			}, // getSectorName
			
			getSizeName: function( size_idx )
			{
				var names = [ "Micro", 
				              "Small",
				              "Medium",
				              "Large" ];
				
				return names[ size_idx ];
			}, // getSizeName			
			
			getPhaseEnum: function( phase_idx )
			{
				var phase = "";
				switch ( phase_idx )
				{
					case 0:
					{
						phase = "eBirth";
					} break;
					case 1:
					{
						phase = "eSurvival";
					} break;
					case 2:
					{
						phase = "eSuccess";
					} break;
					case 3:
					{
						phase = "eDecline";
					} break;
				}
				
				return phase;
			}, // getPhaseEnum
			
			getDeptIdx: function( dept )
			{
				var dept_idx = -1;
				switch ( dept )
				{
					case "hr":
					case "eHumanResources":
					{ 
						dept_idx  = 0;
					} break;
					case "research":
					case "eResearch":
					{
						dept_idx  = 1;
					} break;
					case "marketing":
					case "eMarketing":
					{
						dept_idx  = 2;
					} break;
					case "operations":
					case "eProduction":
					{
						dept_idx  = 3;
					} break;
				}
				
				return dept_idx;
			}, //getDeptIdx

			getDeptEnum: function( dept_idx )
			{
				var dept = "";
				switch ( dept_idx )
				{
					case 0:
					{
						dept = "eHumanResources";
					} break;
					case 1:
					{
						dept = "eResearch";
					} break;
					case 2:
					{
						dept = "eMarketing";
					} break;
					case 3:
					{
						dept = "eProduction";
					} break;
				}
				
				return dept;
			}, //getDeptEnum
			
			getDeptName: function( dept_idx )
			{
				var dept = "";
				switch ( dept_idx )
				{
					case 0:
					{
						dept = "hr";
					} break;
					case 1:
					{
						dept = "research";
					} break;
					case 2:
					{
						dept = "marketing";
					} break;
					case 3:
					{
						dept = "operations";
					} break;
				}
				
				return dept;
			}, //getDeptName

			getPhaseIdx: function( phase )
			{
				var phase_idx = -1;
				switch ( phase )
				{
					case "birth":
					case "eBirth":
					{
						phase_idx = 0;
					} break;
					case "survival":
					case "eSurvival":
					{
						phase_idx = 1;
					} break;
					case "success":
					case "eSuccess":
					{
						phase_idx = 2;
					} break;
					case "decline":
					case "eDecline":
					{
						phase_idx = 3;
					} break;
				}
				
				return phase_idx;
			}, //getPhaseIdx

			getPhaseName: function( phase_idx )
			{
				var phase = "";
				switch ( phase_idx )
				{
					case 0:
					{
						phase = "birth";
					} break;
					case 1:
					{
						phase = "survival";
					} break;
					case 2:
					{
						phase = "success";
					} break;
					case 3:
					{
						phase = "decline";
					} break;
				}
				
				return phase;
			}, //getPhaseName

			getPhaseEnum: function( phase_idx )
			{
				var phase = "";
				switch ( phase_idx )
				{
					case 0:
					{
						phase = "eBirth";
					} break;
					case 1:
					{
						phase = "eSurvival";
					} break;
					case 2:
					{
						phase = "eSuccess";
					} break;
					case 3:
					{
						phase = "eDecline";
					} break;
				}
				
				return phase;
			} //getPhaseEnum
		});
	}
)();

vbbPage.fn.__init.prototype = vbbPage.fn;

$().ready( function()
{
	vbbPage();
	
    //$('#info_page_title_dropdown').selectbox();
    
});

