/**
 * Initialization script for all application components
 */

$(document).ready(function() {
	
	if (typeof MainMenu == "function") { new MainMenu(".main-navigation"); }
		
	$(".tabbed-module").each(function (index, module) {
		if (typeof TabbedModule == "function") { new TabbedModule(module); }
	});
	
	$(".flash-player > a.flash-link, .flash-player > object").each(function (index, element) {
		if (typeof FlashPlayer == "function") { new FlashPlayer(element); }
	});

	if($(document.body).hasClass("home-page")) {
		if (typeof CompanyViewer == "function") { new CompanyViewer(".company-viewer .companies"); }
		if (typeof Poll == "function") { new Poll(".poll-results .poll-options"); }
	}

	if($(document.body).hasClass("content-page")) {
		if (typeof ExpandableModule == "function") { new ExpandableModule(".expandable-sidebar-module"); }
		
		if (typeof SemanticForm == "function") { new SemanticForm("form.form-section"); }
	}

	if($(document.body).hasClass("results-page")) {
		if (typeof ExpandableModule == "function") { new ExpandableModule(".expandable-sidebar-module"); }
		
		if (typeof RefineSearch == "function") { new RefineSearch(".expandable-refine-filter"); }
	}
	
	$('.branch-results-page .resultlisting').each(function (index, element) {
		if (typeof Map == "function") {
			//Map.addLocation(0, 52.1, 4.4);
			//Map.addLocation(1, 52.2, 4.5);
			//Map.addLocation(2, 52.3, 4.6);
			new Map(element, '.branch-result .branch-postcode-city', Map.USETEXT);
			
			// Map is disabled, enable map in search results by uncommenting this line.
			//new Map(element, '.search-result', Map.USEINDEX);
		}
	});
	
	/**********************************
	 * Code below is for dev purposes only, should be removed before production
	 **********************************/
	if (document.location.href.indexOf('-debug.html') >= 0) {
		$("a[href]").each(function(index, element) {
			var href = $(element).attr('href');
			if (href.indexOf('-debug.html') == -1) {
				$(element).attr('href', href.replace('.html', '-debug.html'));
			}
		});
		
		$("form[action]").each(function(index, element) {
			var action = $(element).attr('action');
			if (action.indexOf('-debug.html') == -1) {
				$(element).attr('action', action.replace('.html', '-debug.html'));
			}
		});
	}
	
	$(".brand-header .service-concept").each(function (index, element) {
		if ($('a', element).attr('href') != '#') {
			return;
		}
		
		$(element).click(function(e) {
			$(".brand-header .service-concept").each(function (index, element) {
				$(document.body).removeClass($(element).attr("id"));
			});
			$(document.body).addClass($(this).attr("id"));
		});
	});
	
	
	$('.sidebar-map a, .job-search .tabs li:nth-child(2) a').click(function() {
		$('.search-results-page .resultlisting').each(function (index, element) {
			if (typeof Map == "function") {
				Map.addLocation(0, 52.1, 4.4);
				Map.addLocation(1, 52.2, 4.5);
				Map.addLocation(2, 52.3, 4.6);
				
				new Map(element, '.search-result', Map.USEINDEX);
			}
		});
	})
	
	//new Styleguide();
});
