function getBaseURL(e) {
	if ($("base:first", "head").length === 1) {
		return $("base:first", "head").attr("href");
	}
}
function isInArray(value, array) {
	return array.indexOf(value) > -1;
}
$('form.projects-filter').submit(function () {
	var city = $('select.city-select').val();
	var donor = $('select.donor-select').val();
	var sector = $('select.sector-select').val();
	if (city === '' && donor === '' && sector === '') {
		$('div.city-select').css({"border": "1px solid #f99d22"});
		$('div.sector-select').css({"border": "1px solid #f99d22"});
		$('div.donor-select').css({"border": "1px solid #f99d22"});
		return false;
	} else {
		$('div.city-select').css({"border": "1px solid #e8e8e8"});
		$('div.sector-select').css({"border": "1px solid #e8e8e8"});
		$('div.donor-select').css({"border": "1px solid #e8e8e8"});
	}
});

$('form.careers-filter').submit(function () {
	var city = $('select.city-select').val();
	var country = $('select.country-select').val();
	var position = $('select.position-select').val();
	if (city === '' && country === '' && position === '') {
		$('div.city-select').css({"border": "1px solid #f99d22"});
		$('div.country-select').css({"border": "1px solid #f99d22"});
		$('div.position-select').css({"border": "1px solid #f99d22"});
		return false;
	} else {
		$('div.city-select').css({"border": "1px solid #e8e8e8"});
		$('div.country-select').css({"border": "1px solid #e8e8e8"});
		$('div.position-select').css({"border": "1px solid #e8e8e8"});
	}
});

$('form.subscribe-form').submit(function (e) {
	e.preventDefault();
	var email = $('input#email', $(this));
	if (email.val() !== '') {
		if (isValidEmailAddress(email.val())) {
			$.ajax({
				url: getBaseURL() + 'home/add_subscriber',
				type: 'POST',
				dataType: 'JSON',
				data: {email: email.val()},
				beforeSend: function () {
					$('input, textarea').attr('disabled', 'disabled');
					$('.ld-sf-spinner').css({"visibility": "visible", "opacity": "1"});
				},
				success: function (response) {
					$('input, textarea').removeAttr('disabled');
					$('.ld-sf-spinner').css({"visibility": "hidden", "opacity": "0"});
					if (response.subscriberAdded) {
						$('input:not(#add_subscriber)').val('').text('');
						subscriberMsg(response.message, 'success');
						$('#ld_sf_response').css({"padding": "10px", "border-radius": "5px"});
					} else {
						subscriberMsg(response.error, 'error');
					}
				},
				error: defaultAjaxJSONErrorsHandler
			});
		} else {
			subscriberMsg('Invalid email address', 'error');
		}
	} else {
		subscriberMsg('All fields are required', 'error');
	}
	setTimeout(function () {
		$('#ld_sf_response').fadeOut('slow');
	}, 5000);
});
function subscriberMsg(content, className) {
	$('#ld_sf_response')
			.html(content)
			.addClass(className)
			.slideDown();
}

$('form#message-form').submit(function (e) {
	e.preventDefault();
	var that = this,
			firstname = $('input#firstname', $(that)),
			lastname = $('input#lastname', $(that)),
			email = $('input#email', $(that)),
			phone = $('input#phone', $(that)),
			message = $('textarea#message', $(that)),
			sendMessageBtn = $('#send_message', $(that)),
			sendMessageBtnTxt = sendMessageBtn.html();
	if (firstname.val() !== '' || lastname.val() !== '' || email.val() !== '' || message.val() !== '') {
		if (isValidEmailAddress(email.val())) {
			$.ajax({
				url: getBaseURL() + 'home/contact',
				type: 'POST',
				dataType: 'JSON',
				data: $(that).serialize(),
				beforeSend: function () {
					$('input, textarea').attr('disabled', 'disabled');
					$('#message-form #send_message').after(' <img src="compressor/index/img/loader.gif" class="loader pull-right" />')
				},
				success: function (response) {
					$('input, textarea').removeAttr('disabled');
					if (response.emailSent) {
						$('input:not(#send_message), textarea').val('').text('');
						contactMsg(response.message, 'success');
					} else {
						contactMsg(response.error, 'error');
					}
				},
				error: defaultAjaxJSONErrorsHandler
			});
		} else {
			contactMsg('Invalid email address', 'error');
		}
	} else {
		contactMsg('All fields are required', 'error');
	}
	$('#message-form img.loader').fadeOut('slow', function () {
		$(this).remove()
	});
	setTimeout(function () {
		$('#contact-message').fadeOut('slow');
	}, 5000);
});

function contactMsg(content, className) {
	jQuery('#contact-message')
			.html(content)
			.addClass(className)
			.slideDown();
}

function isValidEmailAddress(emailAddress) {
	var pattern = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
	return pattern.test(String(emailAddress).toLowerCase());
}
function defaultAjaxJSONErrorsHandler(e, t, n) {
	alert({
		m: e.responseText,
		ty: "warning",
		ti: e.textStatus
	});
}

function countWords(txt) {
	return txt.replace(/(^\s*)|(\s*$)/gi, "").replace(/[ ]{2,}/gi, " ").replace(/\n /, "\n").split(' ').length;
}

$(document).ready(function () {
	$('#other').hide();
	$('#iraq').hide();
	$('#turkey').hide();
	$('#syria').hide();
	$('#other_language').hide();
	$("input[name='country'").click(function () {
		var radioValue = $("input[name='country']:checked").attr("data-country");
		switch (radioValue) {
			case'Syria':
				$('#iraq').hide();
				$('#turkey').hide();
				$('#syria').show();
				$('#other').hide();
				break;
			case'Turkey':
				$('#iraq').hide();
				$('#turkey').show();
				$('#syria').hide();
				$('#other').hide();
				break;
			case'Iraq':
				$('#iraq').show();
				$('#turkey').hide();
				$('#syria').hide();
				$('#other').hide();
				break;
		}
	});

	$("input[name='legal_docs']").click(function () {
		if ($("input.other_legal_docs").is(':checked')) {
			$('#other').show();
		}
		if (!$("input.other_legal_docs").is(':checked')) {
			$('#other').hide();
		}
	});
	$("input[name='other_language']").click(function () {
		if ($("input[name='other_language']").is(':checked')) {
			$('#other_language').show();
		}
	});

	$('.title-style').each(function () {
		var _txt = $(this).text();
		var _1st_txt = _txt.split(" ", Math.ceil((countWords(_txt) / 2))).join(" ").replace(',', ' ');
		var _2nd_txt = _txt.substr(_1st_txt.length, _txt.length);
		$(this).html('<span>' + _1st_txt + '</span><span>' + _2nd_txt + '</span>').addClass("visible");
	});

	$('.slider-style').each(function () {
		var _txt = $(this).text();
		var _1st_txt = _txt.split(" ", Math.ceil((countWords(_txt) / 2) - 1)).join(" ").replace(',', ' ');
		var _2nd_txt = _txt.substr(_1st_txt.length, _txt.length);
		$(this).html('<span>' + _1st_txt + '</span><br><span>' + _2nd_txt + '</span>').addClass("visible");
	});

	var url = $(location).attr('href');
	var parts = url.split("/");
	var last_part = parts[3];
	var navlist = ["home", "about", "projects", "blogs", "news", "contact"];


	$('li', '.main-menu').removeClass('main-menu__item--active')
	$('li', '.footer-menu').removeClass('footer-menu__item--active');

	last_part = isInArray(last_part, navlist) ? last_part : 'home';

	$('li#' + last_part, '.main-menu').addClass('main-menu__item--active')
	$('li#' + last_part, '.footer-menu').addClass('footer-menu__item--active');
});

$(document).ready(function () {
	$('.languages').on('click', function () {
		var lang = jQuery(this).data("langval");
		$.ajax({
			type: 'POST',
			url: getBaseURL() + 'home/switch_language/' + lang,
			success: function (response) {
				if (response.status) {
					window.location.reload(true);
				}
			}
		});
	});
	if ($('.preloader').length) {
		$('.preloader').delay(200).fadeOut(500);
	}
});