FC.vars = {
	selectors : {
		CAROUSEL : ".highlights-carousel",
		NEXT : ".next",
		PREVIOUS : ".previous",
		SCROLLER : ".carousel",
		SCROLLER_INNER : ".carousel-content",
		CAROUSEL_ITEM : ".carousel-content li"
	}
}

if(typeof(window.FC_data) == 'undefined') {
	window.FC_data = {};
}

FC.pathToCssImagesFolder = function() {
	var stylesheetUrl = $('link').get(0).href;
	var stylesheetDirectory = stylesheetUrl.slice(0, stylesheetUrl.search(/[^/]*$/));
	return stylesheetDirectory + 'css-images/';
}

FC.urlify = function(s) {
	var s = String(s);
	
	return s.toLowerCase().replace(' ', '-').replace('&', 'and');
}

FC.urlDropdown = function() {
	if($('.urlDropdown').length > 0) {
	
		var dropdowns = window.FC_data['dropdowns'];
		
		function UrlDropdown(link) {
			// Takes a link (an HTML <a> tag) with an id attribute. The value of the link's id attribute is used to look up an object containing text/url pairs.
			
			var dropdown_id = link.id;
			var dropdown_urls = dropdowns[dropdown_id]['countries'];
			var DEFAULT_TEXT = dropdowns[dropdown_id]['Default Text'];
			var BUTTON_TEXT = dropdowns[dropdown_id]['Button Text'];
			
			this.dropdown = document.createElement('select');
			$(this.dropdown).append($('<option value="">' + DEFAULT_TEXT + '</option>'));
			
			this.dropdown.id = dropdown_id;
			
			for (text in dropdown_urls) {
				var option = $('<option>');
				
				option.append(text);
				option.attr('value', dropdown_urls[text]);
				
				$(this.dropdown).append(option);
			}
			
			var label = document.createElement('label');
			label.htmlFor = dropdown_id;
			$(label).text(DEFAULT_TEXT);
			
			var button = $('<button class="button button-regular button-right"><span>' + BUTTON_TEXT + '</span></button>');
			var that = this;
			button.bind('click', function(){that.okgo();});
	
			$(link).replaceWith(label);
			$(this.dropdown).insertAfter($(label));
			button.insertAfter($(this.dropdown));
		}
		
		UrlDropdown.prototype.okgo = function() {
			var url = this.dropdown.options[this.dropdown.selectedIndex].value;
			if (url !== '') {
				document.location = url;
			}
		}
	
		$('.urlDropdown').each(function(){
			new UrlDropdown(this);
		});
	}
}

FC.audienceQuicklinks = function() {
	var audienceQuicklinks = window.FC_data['audienceQuicklinks'];
	
	if(audienceQuicklinks) {
		function AudienceQuicklinks(input) {

			this.controls = [];
			this.html = this.buildHtml(input);
			
			this.controls[0].makeSelected();
			$('.hero-overlay-home .box-f-m').append(this.html);
		}
		
		AudienceQuicklinks.prototype.buildHtml = function(input) {
			var html = $('<div class="audience-quicklinks"></div>')
			
			html.append($('<h3>' + input['Heading Text'] + '</h3>'))
			
			var itemsHtml = $('<div class="ul"></div>');
			
			var items = input['items'];
			for(var i in items) {
				var itemHtml = $('<div class="li"></div>');
				itemHtml.attr('id', "aq-0" + (Number(i) + 1));
				
				if (i == 0 || i == 1 || i == 2) {
					itemHtml.addClass('top');
				}
				else {
					itemHtml.addClass('bottom');
				}
				
				var audience = items[i];
				
				for(var j in audience) {
					var control = new AudienceQuickLinkControl(j, this, itemHtml);
					itemHtml.append(control.html);
					this.controls.push(control);
					
					var itemLinkList = $('<ul></ul>');
					
					var links = audience[j];
					
					for(var k in links) {
						itemLinkList.append($('<li><a href="' + links[k] + '">' + k + '</a></li>'))
					}
					
					itemHtml.append(itemLinkList);
				}
				
				itemsHtml.append(itemHtml);
			}
			
			html.append(itemsHtml);
			return html;
		}
		
		function AudienceQuickLinkControl(name, group, parentHtml) {
			this.group = group;
			this.parentHtml = parentHtml;
			this.urlifiedName = FC.urlify(name);
			
			this.html = $('<h4><a href="#' + this.urlifiedName + '"><span>' + name + '</span></a></h4>');
			
			var that = this;
			this.html.bind('click', function(){
				that.makeSelected();
				return false;
			});
		}
		
		AudienceQuickLinkControl.prototype.makeSelected = function() {
			for(var i in this.group.controls) {
				this.group.controls[i].makeUnselected();
			}
			this.parentHtml.addClass('selected');
			this.html.find('a').removeAttr('href');
		}
		
		AudienceQuickLinkControl.prototype.makeUnselected = function() {
			if(this.parentHtml.hasClass('selected')) {
				this.parentHtml.removeClass('selected');
				this.html.find('a').attr('href', '#' + this.urlifiedName);
			}
		}
		
		new AudienceQuicklinks(audienceQuicklinks);
	}
}



FC.timeout = null;


FC.navigation = function() {
	function Navigation() {
		this.html = $('.navigation');
		
		this.navigationItems = [];
		
		var navigation = this;
		$('.navigation>ul>li').each(function(){
			if($(this).find('.submenu').length > 0) {
				$(this).addClass('hasSubmenu');
				navigation.navigationItems.push(new NavigationItem(this, navigation));
			}
		});
		
		$('body').bind('click', function(){
			navigation.hideSubMenu();
		});
	}
	
	Navigation.prototype.hideSubMenu = function() {
		for(var i in this.navigationItems){
			var navigationItem = this.navigationItems[i];
			
			if(navigationItem.shown) {
				navigationItem.hide();
			}
		}
	}
	
	function NavigationItem(html, navigation) {
		this.html = $(html);
		this.text = this.html.find('a span').text();
		this.navigation = navigation;
		this.submenu = new NavigationSubMenu(this);
		
		this.shown = false;
		
		this.addShowHideControl();
	}
	
	NavigationItem.prototype.addShowHideControl = function() {
		this.showHideControl = new NavigationItemShowHideControl(this);
		this.html.find('.submenu').before(this.showHideControl.html);
	}
	
	NavigationItem.prototype.show = function() {
		this.navigation.hideSubMenu();
		
		this.submenu.setWidth();
		this.submenu.setPosition();

		this.html.addClass('shown');
		this.shown = true;
	}
	
	NavigationItem.prototype.hide = function() {
		this.html.removeClass('shown');
		this.shown = false;
	}
	
	NavigationItem.prototype.setEdgePositions = function() {
		this.leftEdge = this.html.position().left;
		this.rightEdge = 980 - this.html.position().left - this.html.width();
	}
	
	function NavigationSubMenu(navigationItem) {
		this.navigationItem = navigationItem;
		this.html = this.navigationItem.html.find('.submenu');
		
		this.html.bind('click', function(event){
			event.stopPropagation();// Prevent clicks within the submenu from triggering the body's onclick event handler, which hides the shown menu
		});
	}
	
	NavigationSubMenu.prototype.setWidth = function() {
		this.width = (this.html.find('ul').length * 125) + 38;
		
		if(this.width < this.navigationItem.html.width()) {
			this.width = this.navigationItem.html.width() + 8;
		}
		
		this.html.width(this.width);
	}
	
	NavigationSubMenu.prototype.setPosition = function() {
		this.navigationItem.setEdgePositions();
		
		if(this.navigationItem.html.parent().hasClass('main')) {
			if(this.navigationItem.leftEdge + this.width > 980) {
				if(this.navigationItem.rightEdge + this.width > 980) {
					var leftPosition = ((980 - this.width)/2) - this.navigationItem.leftEdge;
					this.html.css({'left': leftPosition + 'px'});
				}
				else {
					this.html.addClass('submenu-right');
				}
			}
		}
	}
	
	function NavigationItemShowHideControl(navigationItem) {

		this.html = $('<a class="nav-expand" href="#' + FC.urlify(navigationItem.text) + '">' + window.FC_data['Navigation More link alt text'] + '</a>');
		this.navigationItem = navigationItem;
			
		
		var that = this;
		this.html.bind('click', function(){
			if(that.navigationItem.shown) {
				that.navigationItem.hide();
			}
			else {
				that.navigationItem.show();
			}
			return false;
		});
		this.html.bind('mouseover focus', function(){
			that.navigationItem.html.addClass('hover');
		});
		this.html.bind('mouseout blur', function(){
			that.navigationItem.html.removeClass('hover');
		});
	}
	
	new Navigation();
}


FC.collapsables= function()
{
	var collapsable = function(element)
	{
		$(element).find(".toggler span").wrap(document.createElement("a"));
		var toggler = $(element).find(".toggler a");
		var content = $(element).find(".sub-content");

		collapsable.prototype.prepareElements = function()
		{
			toggler.attr({href:"#"});
			content.hide();
		}

		collapsable.prototype.bindEvent = function()
		{
			toggler.bind
			(
		 		"click",
				function(e)
				{
					$(this).toggleClass("collapse");
					content.slideToggle('fast');
				}
			);
		}

		collapsable.prototype.init = function()
		{
			this.prepareElements();
			this.bindEvent()
		}
		this.init();
	}

	$('.collapsable').each
	(
	 	function()
		{
			new collapsable(this);
		}
	);
}

$(function(){
	FC.setJS();
	FC.navigation();
});


