$( document ).ready(function() {
	
	fitty('h1.main-title',{maxSize:60});
	
	var lightboxesBound = false;
	
	const options = {
		size: 'xl'
	};
	
	// Bind default lightboxes
	document.querySelectorAll('.lightbox').forEach((el) => el.addEventListener('click', (e) => {
		e.preventDefault();
		const lightbox = new Lightbox(el, options);
		lightbox.show();
	}));

	// Submitting the first step of an offer form
	$( '#offer_step_one' ).click(function(e) {
					
		if ($('form#offer_form_first')[0].checkValidity()) {
			
			$('section#make_offer_full_screen').removeClass('d-none');
			var offer_first = $('input#offer_first').val();
			$('input#offer').val(offer_first);
			return false;
			
		} 
		
	});
	
	var source_lang = 'en';
	
	// Close the make offer dialog and reset it's content				
	$( 'a.translate').click(function( e ) {
			

			e.preventDefault();
			$('div#preloader').show();
			
			var destination_lang = $(this).data('lang');
			var translations = {};
			var languages = {};
			
			languages['en'] = 'English';
			languages['nl'] = 'Dutch';
			languages['de'] = 'German';
			languages['es'] = 'Spanish';
			languages['fr'] = 'French';
			languages['jp'] = 'Japanese';
			languages['et'] = 'Estonian';
			languages['fi'] = 'Finnish';
			languages['hu'] = 'Hungarian';
			languages['id'] = 'Indonesian';
			languages['it'] = 'Italian';
			languages['lt'] = 'Lithuanian';
			languages['lv'] = 'Latvian';
			languages['pl'] = 'Polish';
			languages['pt'] = 'Portuguese';
			languages['ro'] = 'Romanian';
			languages['ru'] = 'Russian';
			languages['sk'] = 'Slovak';
			languages['sl'] = 'Slovenian';
			languages['sv'] = 'Swedish';
			languages['tr'] = 'Turkish';
			languages['uk'] = 'Ukrainian';
			languages['zh'] = 'Chinese (simplified)';
			languages['bg'] = 'Bulgarian';
			languages['cs'] = 'Czech';
			languages['da'] = 'Danish';
			languages['el'] = 'Greek';
			
			$('.translatable').each(function(i, obj) {
				
				var current_txt = $(obj).html();
				var label = $(obj).data('element');
				translations[label] = current_txt;
				
			});
			
			$.post('ajax/smartlanders/translate/',
			{
				lang: destination_lang,
				text: translations
			},
			function(data, status){
				translations = JSON.parse(data);
				
				$.each(translations, function(index, value) {
					$('*[data-element="'+index+'"]').html(value);
				}); 
				
				source_lang = destination_lang;
				$('.curlang').html(languages[destination_lang]);
			})  
			.done(function() {
				$('div#preloader').hide();
			});
			//Odf.ajax.get('ajax/smartlanders/translate/', $(obj), { function () { console.log('test'); } });    
			//console.log(translations);
			
	});

	// Close the make offer dialog and reset it's content				
	$( '#close_offer_box').click(function( e ) {
		
			e.preventDefault();
			resetAndHideOfferDialog();
		
	});
			
	// Close the make offer dialog on hitting Escape
	$(document).keyup(function(e) {
		if (e.key === "Escape") {
			resetAndHideOfferDialog();
		}
	});
	
	
	function resetAndHideOfferDialog() {
		
		// Reset box with AJAX call feedback
		$('#form_feedback').empty();
		
		// Reset form input fields
		$("#offer_form_second")[0].reset();
		
		// Reset form
		//$('#offer_box_second_form').removeClass('d-none');
		//$('#offer_box_second_success').addClass('d-none');
		
		// Reset form
		//$('#offer_box_first_form').removeClass('d-none');
		//$('#offer_box_first_success').addClass('d-none');	
	
		$('section#make_offer_full_screen').addClass('d-none');
		return false;
		
	}


	$( "#show_offer_box" ).click(function() {
		
		$('#buy_now').addClass('d-none');
		$('#offer_form').removeClass('d-none');
		return false;
		
	});


	$( "#show_bin_box" ).click(function() {
		
		$('#buy_now').removeClass('d-none');
		$('#offer_form').addClass('d-none');
		return false;
		
	});
	
	// Hitting the lease to own button
	$( '#bin_dan_installments' ).click(function(e) {
		$('section#installments_full_screen').removeClass('d-none');
		$('body').addClass('prevent-double-scroll');
		return false;
		
	});


	// Close theinstallment dialog and reset it's content				
	$( '#close_installment_box').click(function() {
		
		$('section#installments_full_screen').addClass('d-none');
		$('body').removeClass('prevent-double-scroll');
		return false;
		
		
	});

	$( "#edit-btn" ).click(function() {
		
		var edit_mode = $('.edit-on').hasClass('d-none');
		var edit_icon_on = $('.edit-on');
		var edit_icon_off = $('.edit-off');
		
		if (edit_mode) {
			
			$(this).removeClass('btn-dark');
			$(this).addClass('btn-success');
			$(edit_icon_on).removeClass('d-none');
			$(edit_icon_off).addClass('d-none');
			$('body').addClass('editmode');
			$('.instantedit').removeClass('d-none');
			$('.userhidden').removeClass('d-none');
			
			// Add BS tooltips
			var tooltipTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="tooltip"]'))
			var tooltipList = tooltipTriggerList.map(function (tooltipTriggerEl) {
				return new bootstrap.Tooltip(tooltipTriggerEl, {delay: 400})
			})
			
			const options = {
				size: 'xl'
			};
			
			if (lightboxesBound == false ) {
				
				// Bind lightbox
				document.querySelectorAll('.editable').forEach((el) => el.addEventListener('click', (e) => {
					e.preventDefault();
					const lightbox = new Lightbox(el, options);
					lightbox.show();
				}));
			}
			
			lightboxesBound = true;
			
		} else {
			
			$(this).addClass('btn-dark');
			$(this).removeClass('btn-success');
			$(edit_icon_on).addClass('d-none');
			$(edit_icon_off).removeClass('d-none');
			$('body').removeClass('editmode');
			$('.editable').attr('data-bs-toggle', '');
			$('.userhidden').addClass('d-none');
		}
	});
	
});