(function($){var a={},c="doTimeout",d=Array.prototype.slice;$[c]=function(){return b.apply(window,[0].concat(d.call(arguments)))};$.fn[c]=function(){var f=d.call(arguments),e=b.apply(this,[c+f[0]].concat(f));return typeof f[0]==="number"||typeof f[1]==="number"?this:e};function b(k){var l=this,g,i={},m=arguments,h=4,f=m[1],j=m[2],o=m[3];if(typeof f!=="string"){h--;f=k=0;j=m[1];o=m[2]}if(k){g=l.eq(0);g.data(k,i=g.data(k)||{})}else{if(f){i=a[f]||(a[f]={})}}i.id&&clearTimeout(i.id);delete i.id;function e(){if(k){g.removeData(k)}else{if(f){delete a[f]}}}function n(){i.id=setTimeout(function(){i.fn()},j)}if(o){i.fn=function(p){o.apply(l,d.call(m,h))&&!p?n():e()};n()}else{if(i.fn){j===undefined?e():i.fn(j===false);return true}else{e()}}}})(jQuery);

var geocoder;
var map;
var markersArray = [];


 $.fn.initiateMap = function(){
	var latlng = new google.maps.LatLng(52.09396621587106, 5.092463141784669);    
	var myOptions = {
		zoom: 7,
		center: latlng,
		mapTypeId: google.maps.MapTypeId.ROADMAP
	}
	map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
}

$.fn.getAllLocations = function(wbUrl){
	$.getJSON(wbUrl+"/modules/maps/getAllLocations.php", function(vestigingen){ 
		if (vestigingen){
			for (var i = 0; i < vestigingen.length; i++) {
				$.fn.setMarker(vestigingen[i], wbUrl);
			}
		}
	});
}

$.fn.getLocaties = function(address, wbUrl){
	$('.maps-popup.dealeroverzicht').show();
	$('#found-dealers .content').append('<ul id="loadstatus" style="display:none"><li style="font-size:10px;">Vestigingen laden...</li></ul>');
	$('#loadstatus').fadeIn(1000);
	geocoder = new google.maps.Geocoder();
	if (geocoder) {
		geocoder.geocode( { 'address': address+' nederland'}, function(results, status) {
			if (status == google.maps.GeocoderStatus.OK) {
				qlat = results[0].geometry.location.lat();
				qlng = results[0].geometry.location.lng();
				map.setCenter(results[0].geometry.location);
				$.getJSON(wbUrl+"/modules/maps/searchdealer.php", { lat : qlat, lng : qlng, radius : "50"}, function(locaties){ 
				
				if (locaties.length > 0){
					$.fn.setResults(locaties, wbUrl)
				}else{
					$.doTimeout(500, function(){
						$('#loadstatus li').html('Geen resultaten gevonden.');
					})
				}
				   
				});
			} else {
				$.doTimeout(500, function(){
					$('#loadstatus li').html('Geen resultaten gevonden.');
				})
				//alert("Het volgende is fout gegaan: " + status);
			}
		});
	}
}

$.fn.setResults = function(locaties, wbUrl){
	for(var i=0; i<locaties.length; i++) {
		$('#found_dealers').append('<li><a class="dealer_link" rel="'+locaties[i].dealer_id+'" href="#dealer">' + locaties[i].plaats + ' ('+locaties[i].distance+' km)</a></li>');
		$('a.dealer_link[rel='+locaties[i].dealer_id+']').data('position',{latitude:locaties[i].lat, longitude:locaties[i].lng});
	};
	$('#loadstatus').remove();
	$.fn.ActivateBtns(wbUrl);
}


$.fn.ActivateBtns = function(wbUrl){
	$('a.dealer_link').live('click', function(){
		$.fn.ViewLocation($(this).attr('rel'), wbUrl);
		return false;
	})
}

$.fn.ViewLocation = function(id, wbUrl){
	lat = markersArray[id].position.lat();
	lng = markersArray[id].position.lng();
	map.setCenter(new google.maps.LatLng(lat, lng));
	map.setZoom(10);
	$.fn.showLocationOuput(id, wbUrl);
	if (markersArray) {
		for (i in markersArray) {
			if (markersArray[i].id === id){
				markersArray[i].setIcon(wbUrl+"/modules/maps/images/markerHover.png");
			}else{
				markersArray[i].setIcon(wbUrl+"/modules/maps/images/marker.png");
			}
		}
	}
}

$.fn.ViewDirectLocation = function(id, lat, lng, wbUrl){	
	map.setCenter(new google.maps.LatLng(lat, lng));
	map.setZoom(10);
	$.fn.showLocationOuput(id, wbUrl);
	markersArray[id].setIcon(wbUrl+"/modules/maps/images/markerHover.png");
}


$.fn.setMarker = function(vestiging, wbUrl){
	
	var image = wbUrl+"/modules/maps/images/marker.png";

	var myLatLng = new google.maps.LatLng(vestiging.lat, vestiging.lng);
	var marker = new google.maps.Marker({
		icon: image,
		id: vestiging.dealer_id,
		position: myLatLng,
		map: map,
		title: vestiging.name
	});
	
	markersArray[vestiging.dealer_id] = marker;		
	google.maps.event.addListener(marker, 'click', function() {
		
		$.fn.ViewLocation(this.id, wbUrl);
	});
}


$.fn.searchLocation = function(searchQ, wbUrl){
	$.fn.getLocaties(searchQ, wbUrl);
}

$.fn.toggleDealerInfo = function(){
	if(dealerInfoState === 'open'){
		$('#dealer-info .content').hide();
		$('#dealer-info').stop().animate({
			width: '190px',			
			left: (668 - 190) + 'px'
		})
		dealerInfoState = 'closed';
		$('#close-dealer-info').addClass('closed');
	}else{
		
		$('#dealer-info').stop().animate({
			width: '300px',			
			left: (668 - 300) + 'px'
		}, function(){
			$('#dealer-info .content').show();		
		
		})
		dealerInfoState = 'open';
		$('#close-dealer-info').removeClass('closed');
	}
};

$.fn.toggleDealers = function(){
	if(foundDealerState === 'open'){
		$('#found-dealers .content').hide();
		foundDealerState = 'closed';
		if($('#found-dealers').css('top') == '10px'){
			$('#dealer-info').stop().animate({
				top: $('#found-dealers').outerHeight(true) + 20,
				left: (668 - $('#dealer-info').outerWidth(true))
			});
		}
		$('#close-found-dealers').addClass('closed');
	}else{
		$('#found-dealers .content').show();
		foundDealerState = 'open';
		
		if($('#found-dealers').css('top') == '10px'){
			$('#dealer-info').stop().animate({
				top: $('#found-dealers').outerHeight(true) + 20,
				left: (668 - $('#dealer-info').outerWidth(true))
			});
		}
		$('#close-found-dealers').removeClass('closed');
	}
};


var mapoutputexst = 'no';
$('#map_output').fadeOut();
$.fn.showLocationOuput = function(locationId, wbUrl){
	
	
	$('#found-dealers').stop().animate({
		top:'10px',
		left: (668 - $('#found-dealers').outerWidth(true))
	});
	$('#dealer-info').stop().animate({
		top: $('#found-dealers').outerHeight(true) + 20,
		left: (668 - $('#dealer-info').outerWidth(true))
	});
	if(mapoutputexst === 'yes'){
		$('#map_output').fadeOut(300, function(){
			$('#map_output').load(wbUrl+'/modules/maps/get_dealer_info.php', {'dealer_id': locationId}, function(){	
				$('#dealer-info').css({top: $('#found-dealers').outerHeight(true) + 20});
				$('#map_output').fadeIn(300);
				
				dealerInfoState = 'open';
				
			});
		});	
	}else{
		$('#map_output').load(wbUrl+'/modules/maps/get_dealer_info.php', {'dealer_id': locationId}, function(){	
			$('#dealer-info').css({top: $('#found-dealers').outerHeight(true) + 20})
			mapoutputexst = 'yes';
			
			$('#map_output').fadeIn(300);
		});
	}
	$('#vest-tabs a').live('click', function(){
		var clicked = '#' + $(this).attr('rel');
		$('#dealer-info .tabcont').not(clicked).hide()
		$(clicked).show();
		$(this).addClass('active');
		$('#vest-tabs a').not(this).removeClass('active')
		return false;
	});
}


var dealerInfoState = 'open';
$('#close-dealer-info').live('click',function(){
		$.fn.toggleDealerInfo();
		return false;
})

var foundDealerState = 'open';
$('#close-found-dealers').live('click',function(){
		$.fn.toggleDealers();
		return false;
})
