/**
 *
 * Techni-Cal
 *
 * front-end scripts
 *
 */

if (window.DEVELOPMENT === undefined) var DEVELOPMENT = false;

$.RELATIVE_PATH_NO_I18N = '/';
$.RELATIVE_PATH = '/en/'; // hier iets fixen voor i18n, vooralsnog niet nodig

$(function() {
	if (DEVELOPMENT && window.console) console.info('===[ IN DEVELOPMENT ]===');

	// open external links in new tab/window
	$('a[rel=external]').attr('target', '_blank');

	// distributors per country, click on flag or countryname to see distributors for that country
	$('a.show-distributors').click(function() {
		var countryCode = $(this).parent().attr('id').replace('flag-', '');
		if (DEVELOPMENT && window.console) console.info(countryCode);

		$('div.per-country-distributors').removeClass('active');
		$('div#country-' + countryCode).addClass('active');

		return(false);
	});

	// on faq load check for # and open that question
	if ($('input#faq_ac').length > 0) {
		if (DEVELOPMENT && window.console) console.info('faq page loaded');
		var docLoc = (document.location.href).split('#');
		if (DEVELOPMENT && window.console) console.info(docLoc.length);
		if (docLoc.length == 2) {
			var answerDiv = $('div#faq-' + docLoc[1] + ' .answer:first');
			if ($(answerDiv).is('.closed')) {
				$(answerDiv).removeClass('closed').addClass('opening');
				$(answerDiv).slideDown('slow', function() {
					$(answerDiv).removeClass('opening').addClass('open');
				});
			}
		}
	}

	// faq search
	$('input#faq_ac').focus(function() {
		if ($(this).val() == 'Search') {
			$(this).val('');
		}
	}).blur(function() {
		if ($(this).val() == '') {
			$(this).val('Search');
		}
	}).autocomplete('en/searchfaq', {
		selectFirst: true
	}).result(function(event, item) {
		if (DEVELOPMENT && window.console) console.info(item);
		$(this).blur();
		setTimeout(function() { // without this, stuff below has no effect apparently
			var docLoc = (document.location.href).split('#');
			var newLoc = item[2];
			if (newLoc == 'uncategorized') {
				newLoc = '';
			} else {
				newLoc = '/' + newLoc;
			}
			if (DEVELOPMENT && window.console) console.info(docLoc[0] + '   -   ' + newLoc);
			if (newLoc != docLoc) {
				document.location.href = $.RELATIVE_PATH + 'faq' + newLoc + '#' + item[1];
			}
			//document.location.hash = item[1];
			var answerDiv = $('div#faq-' + item[1] + ' .answer:first');
			if ($(answerDiv).is('.closed')) {
				$(answerDiv).removeClass('closed').addClass('opening');
				$(answerDiv).slideDown('slow', function() {
					$(answerDiv).removeClass('opening').addClass('open');
				});
			}
		}, 100);
	});


	// open close faq-answers
	$('div.question span').click(function() {
		if (DEVELOPMENT && window.console) console.info($(this));
		var answerDiv = $(this).parent().siblings('div.answer:first');
		if (DEVELOPMENT && window.console) console.info($(answerDiv));
		if ($(answerDiv).is('.closed')) {
			$(answerDiv).removeClass('closed').addClass('opening');
			$(answerDiv).slideDown('slow', function() {
				$(answerDiv).removeClass('opening').addClass('open');
			});
		} else if ($(answerDiv).is('.open')) {
			$(answerDiv).removeClass('open').addClass('closing');
			$(answerDiv).slideUp('slow', function() {
				$(answerDiv).removeClass('closing').addClass('closed');
			});
		}
	});


	// voedselaanbeveling modal
	$('div.recommendation-container').jqm();

	// blur links on click
	$('a').click(function() {
		$(this).blur();
	});

});
