
/******************************************************************************
 *
 *	PROJECT: Flynax Classifieds Software
 *	VERSION: 4.X
 *	LICENSE: FL0M2SG681CQ - http://www.flynax.com/license-agreement.html
 *	PRODUCT: Pets Classifieds
 *	DOMAIN: pupsnshop.com
 *	FILE: LIB.JS
 *
 *	The software is a commercial product delivered under single, non-exclusive, 
 *	non-transferable license for one domain or IP address. Therefore distribution, 
 *	sale or transfer of the file in whole or in part without permission of Flynax 
 *	respective owners is considered to be illegal and breach of Flynax License End 
 *	User Agreement. 
 *
 *	You are not allowed to remove this information from the file without permission
 *	of Flynax respective owners.
 *
 *	Flynax Classifieds Software 2014 |  All copyrights reserved. 
 *
 *	http://www.flynax.com/
 *
 ******************************************************************************/

var lfHandler = function(base) {
	var self = this;

	if ( lfConfig['use_location'] ) {
		if ( navigator.geolocation ) {
			navigator.geolocation.getCurrentPosition(function(position) {
				self.update(position.coords.latitude, position.coords.longitude);
			}, function() {
				// nothing to do
			});
		}
	}

	if ( base.markers[0] ) {
		/* set marker draggable */
		base.markers[0].setDraggable(true);
		
		/* attache hint */
		if ( !lfConfig['postZoom'] ) {
			base.infoWindows[0].setContent(lfConfig['phrase_drag_hint']);
			base.infoWindows[0].open(base.map, base.markers[0]);
		}
		else {
			lfUse(base);
		}
		
		/* set listeners */
		google.maps.event.addListener(base.markers[0], 'dragstart', function(){
			base.infoWindows[0].close();
		});
		
		google.maps.event.addListener(base.markers[0], 'dragend', function(){
			lfUpdate(base);
			$('#lf_use').attr('checked', true);
		});
		
		google.maps.event.addListener(base.map, 'zoom_changed', function(){
			lfUpdate(base);
		});
		
		lfUpdate(base);
		
		$('#lf_use').change(function(){
			base.infoWindows[0].close();
			lfUse(base);
		});
		
		/* search */
		$('#lf_search').click(function(){
			lfSearch(base);
		});
		$('#lf_query').keydown(function(event){
			if ( event.keyCode == 13 ) {
				lfSearch(base);
				
				$(this).closest('form').attr('onsubmit', 'return false;');
			}
		});
		$('#lf_query').blur(function(){
			$(this).closest('form').attr('onsubmit', 'return true;');
		});
	}

	this.update = function(lat, lng) {
		var pos = new google.maps.LatLng(lat, lng);
		base.markers[0].setPosition(pos);
		base.map.setCenter(pos);

		base.infoWindows[0].close();
		base.infoWindows[0].setContent(lfConfig['phrase_your_location']);
		base.infoWindows[0].open(base.map, base.markers[0]);
		lfUpdate(base);
	};
};

var lfSearch = function(base) {
	var request = $('#lf_query').val();
			
	if ( request ) {
		lfConfig['geoCoder'].geocode({'address': request}, function(results, status) {
			if ( status == 'OK' ) {
				var lat = results[0].geometry.location.lat();
				var lng = results[0].geometry.location.lng();
				var latLng = new google.maps.LatLng(lat, lng)
				
				base.markers[0].setPosition(latLng);
				base.map.setCenter(latLng);
				
				lfUpdate(base);
			}
			else {
				printMessage('warning', lfConfig['phrase_not_found'].replace('{location}', request));
			}
		});
	}
};

var lfUpdate = function(base){
	var zoom = base.map.getZoom();
	var lat = base.markers[0].getPosition().lat();
	var lng = base.markers[0].getPosition().lng();
	
	$('#lf_zoom').val(zoom);
	$('#lf_lat').val(lat);
	$('#lf_lng').val(lng);
};

var lfUse = function(base){
	base.markers[0].setVisible($('#lf_use').is(':checked'))
};

$(document).ready(function(){
	/* assign map container */
	if ( lfConfig['containerPosition'] == 'bottom' ) {
		$('div#controller_area form table.submit:last').before($('#lf-container'));
	}
	else if ( lfConfig['containerPosition'] != 'top' ) {
		if ( lfConfig['containerPositionType'] == 'prepend' ) {
			$('div#fs_'+lfConfig['containerPosition']+' > div.body').prepend($('#lf-container'));
		}
		else {
			$('div#fs_'+lfConfig['containerPosition']+' > div.body').append($('#lf-container'));
		}
	}
	
	$('#lf-container').show();

	/* generate map */
	$('#lf_map').flMap({
		addresses: [
			[lfConfig['postLat'] && lfConfig['postLng'] ? lfConfig['postLat']+','+lfConfig['postLng'] : lfConfig['location'], lfConfig['location'], lfConfig['postLat'] && lfConfig['postLng'] ? 'direct' : 'geocoder']
		],
		phrases: {
			notFound: lfConfig['phrase_not_found']
		},
		scrollWheelZoom: false,
		zoom: lfConfig['postZoom'] ? lfConfig['postZoom'] : lfConfig['zoom'],
		ready: lfHandler
	});
});