function getLocation(fromModal = false) {
	if('geolocation' in navigator) {
		jQuery('#locationModal button').html('<i class="fa fa-spin fa-spinner"></i>');
		navigator.geolocation.getCurrentPosition(function(p) { geoSuccess(p, fromModal); }, function(e) { geoError(e, fromModal); });
	} else {
		alert('Your browser doesn\'t support GeoLocation');
	}
}

function geoSuccess(position, modal, near=false) {
   // console.log(position);
	jQuery.getJSON('mwSearch.php?dir=r&lat='+position.coords.latitude+'&lon='+position.coords.longitude, function(data) {
	  // jQuery.getJSON('mwSearch.php?dir=r&lat=52.48636000&lon=-0.80330000', function(data) {
	    if(data){
		jQuery('#searchMod input[name="location"]').val(data.name);
		jQuery('#searchMod input[name="lat"]').val(position.coords.latitude);
		jQuery('#searchMod input[name="lon"]').val(position.coords.longitude);

		/*document.cookie = "town="+data.name;
		document.cookie = "lat="+position.coords.latitude;
		document.cookie = "lon="+position.coords.longitude;*/
		
		if(modal) {
			jQuery('#locationModal').modal('hide');
		}
		
		//if(near) {
			jQuery('#searchMod form').submit();
		//}
}
	});	
	
}

function geoError(error, fromModal = false) {
	var userDeclined = false;
		
	if(error.code == 1) {
		userDeclined = true;
	}
	
	if(!fromModal) {
		jQuery('#locationModal').modal();
	}		
	
	var ModalContent = jQuery('#locationModal .modal-body');
	ModalContent.html('<p>Sorry, we weren\'t able to get your location from your browser.</p>');
	
	if(userDeclined) {
		ModalContent.append('<p>Please allow Location Access for GoingOut.co.uk in your browser.</p>');
	}
	
	ModalContent.append('<p>You can still search using your town/postcodes.</p> <p style="font-size: 11px;"><strong><em>'+error.message+'</em></strong></p><p><button onclick="declineLocation()">Close</button></p>');
}

function setLatLon(lat, lon, name) {
	document.cookie = "town="+name;
	/*document.cookie = "lat="+lat;
	document.cookie = "lon="+lon;*/
	jQuery('#searchMod input[name="location"]').val(unescape(name));
	jQuery('#searchMod input[name="lat"]').val(lat);
	jQuery('#searchMod input[name="lon"]').val(lon);
	
	jQuery('.tagListDrop').hide();	
}

function setTag(tag) {
	jQuery('#searchMod input[name="tags"]').val(unescape(tag));
	jQuery('.tagListDrop').hide();
}

function declineLocation() {
	Cookies.set('declineLocation', true);
	jQuery('#locationModal').modal('hide');
}

function haveLocation() {
	var Town = Cookies.get('town');
	var Declined = Cookies.get('declineLocation');
	
	if(typeof Town == 'undefined' && typeof Declined == 'undefined') {
		jQuery('#locationModal').modal();
	}
}
	

jQuery(document).ready(function() {
	jQuery('.claimBusiness a').on('click', function(e) {
		e.preventDefault();
		var url = jQuery(this).attr('href')+'&tmpl=component';
		
		jQuery('#claimBusiness .modal-body').load(url);
		jQuery('#claimBusiness').modal();
	});
	
	jQuery('#claimBusiness').on('hidden.bs.modal', function() {
		jQuery('#claimBusiness .modal-body').html('<i class="fa fa-spin fa-spinner"></i>');
	});
	
	jQuery('.selectLatLon').on('click', function() {
		alert('aaaa');
	});
	
	jQuery('.apiPage h3').on('click', function() {
		jQuery(this).parent().toggleClass('open');
	});
	
	jQuery('.filterBlock .filterMore').on('click', function(e) {
		e.preventDefault();
		
		jQuery(this).parent().addClass('open');
	});
	
	jQuery('.activeFilter').on('click', function(e) {
		var catid = jQuery(this).data('category');
		var el =  jQuery('.searchFilter input[value="'+catid+'"]');
		
		el.attr('checked', false);
		jQuery(this).remove();
	});
	
	jQuery('.clearTags').on('click', function() {
		jQuery('#searchMod input[name="tags"]').val('');
	});
	
	jQuery('.whatsnear').on('click', function(e) {
		e.preventDefault();
		
		navigator.geolocation.getCurrentPosition(function(p) { geoSuccess(p, false, true); }, function(e) { geoError(e, false); });

	});
	
	/*jQuery('.menuLinks a').on('click', function(e) {
		e.preventDefault();
		var href = jQuery(this).attr('href');
		//alert(href);
		
		jQuery('#claimBusiness .modal-content').html('<img src="'+href+'" />');
		jQuery('#claimBusiness').modal();
		
	});*/

});


function saveVenue(venue, cat) {
	jQuery.ajax({
		type: 'POST',
		url: 'index.php?option=com_members&task=venue.save&nosef=1&id='+venue,
		success: function(response) {
			response = JSON.parse(response);

			if(response.status == 'OK') {
				if(!cat) {
					jQuery('.profileMembership a i').addClass('fa-heart').removeClass('fa-heart-o');
				} else {
					jQuery(cat).addClass('savedVenue');
				}
			}

		}
	});
}

function loginModal() {
	jQuery('#loginModal').modal();
}

function serve(lat, lon) {
	jQuery('.partnersBox').load('/index.php?nosef=1&option=com_members&task=members.serve&lat='+lat+'&lon='+lon);
}
		
		
function closeAC() {
	jQuery('.tagListDrop').hide();
}

function toggleFilter() {
	jQuery('.left_col').toggleClass('active');
}

function shareProfile(title) {
	navigator.share({
		title: title,
		url: window.location
	});
}
