var dirs_5255;
var dirs_v_step = 20; // metres
var dirs_v_tick = 40; // milliseconds
var dirs_v_poly;
var dirs_v_poly2;
var lastVertex = 0;
var dirs_v_eol;
var dirs_v_marker;
var dirs_v_k=0;
var dirs_v_stepnum=0;
var dirs_v_speed = "";
var dirs_v_ditml = "";

function fancy_send_print() {
	CallPrint(dirs_v_ditml);
}

function fancy_send_mail() {
	document.getElementById("senddir_mail_body").value = dirs_v_ditml;
	$("#senddir_form").bind("submit", function() {

		if ($("#senddir_mail_to").val().length < 1 || $("#senddir_mail_subject").val().length < 1) {
			$("#senddir_error").show();
			$.fancybox.resize();
			return false;
		}

		$.fancybox.showActivity();

		$.ajax({
			type		: "POST",
			cache	: false,
			url		: "/renk/ax_send_mail",
			data		: $(this).serializeArray(),
			success: function(data) {
				$.fancybox('Mail gönderildi.');
			}
		});

		return false;
	});

	$.fancybox({
		'scrolling'		: 'no',
		'titleShow'		: false,
		'href'			: '#senddir_form',
		'onClosed'		: function() {
			$("#senddir_error").hide();
		}
	});
}

function dirs_updatePoly(d) {
	if (dirs_v_poly2.getVertexCount() > 20) {
		dirs_v_poly2=new GPolyline([dirs_v_poly.getVertex(lastVertex-1)]);
		map_5255.addOverlay(dirs_v_poly2);
	}

	if (dirs_v_poly.GetIndexAtDistance(d) < lastVertex+2) {
		if (dirs_v_poly2.getVertexCount()>1) {
			dirs_v_poly2.deleteVertex(dirs_v_poly2.getVertexCount()-1);
		}
		dirs_v_poly2.insertVertex(dirs_v_poly2.getVertexCount(),dirs_v_poly.GetPointAtDistance(d));
	} else {
		dirs_v_poly2.insertVertex(dirs_v_poly2.getVertexCount(),dirs_v_poly.getVertex(lastVertex++));
	}
}

function dirs_animate(d) {
	if (d>dirs_v_eol) {
		dirs_show_tooltip('Yol Sonu');
		dirs_v_ditml = dirs_v_ditml + '<b>Toplam Yol: </b>' + d.toFixed(2) + ' m<br>';

		GEvent.addListener(dirs_v_marker, "click", function() {
			fancy_send_mail();
		});
		fancy_send_mail();

		return;
	}
	var p = dirs_v_poly.GetPointAtDistance(d);
	if (dirs_v_k++>=180/dirs_v_step) {
		map_5255.panTo(p);
		dirs_v_k=0;
	}
	dirs_v_marker.setPoint(p);
	if (dirs_v_stepnum+1 < dirs_5255.getRoute(0).getNumSteps()) {
		if (dirs_5255.getRoute(0).getStep(dirs_v_stepnum).getPolylineIndex() < dirs_v_poly.GetIndexAtDistance(d)) {
			dirs_v_stepnum++;
			var steptext = dirs_5255.getRoute(0).getStep(dirs_v_stepnum).getDescriptionHtml();
			dirs_v_ditml = dirs_v_ditml + (dirs_v_stepnum+1) + '. ' + steptext + '<br>';
			dirs_show_tooltip(steptext);
			var stepdist = dirs_5255.getRoute(0).getStep(dirs_v_stepnum-1).getDistance().meters;
			var steptime = dirs_5255.getRoute(0).getStep(dirs_v_stepnum-1).getDuration().seconds;
			var stepspeed = ((stepdist/steptime) * 2.24).toFixed(0);
			dirs_v_step = stepspeed/2.5;
		}
	} else {
		if (dirs_5255.getRoute(0).getStep(dirs_v_stepnum).getPolylineIndex() < dirs_v_poly.GetIndexAtDistance(d)) {
			var steptext = 'Hedefe Varış';
			//dirs_v_ditml = dirs_v_ditml + (dirs_v_stepnum+2) + '. ' + steptext + '<br>';
			dirs_show_tooltip('Hedefe Varış');
		}
	}
	dirs_updatePoly(d);
	setTimeout("dirs_animate("+(d+dirs_v_step)+")", dirs_v_tick);
}

function dirs_show_tooltip(tip) {
	tooltip_5255.innerHTML = '<div class="tooltip_5255">'+tip+'</div>';
	var point=map_5255.getCurrentMapType().getProjection().fromLatLngToPixel(map_5255.getBounds().getSouthWest(),map_5255.getZoom());
	var offset=map_5255.getCurrentMapType().getProjection().fromLatLngToPixel(dirs_v_marker.getPoint(),map_5255.getZoom());
	var anchor=dirs_v_marker.getIcon().iconAnchor;
	var width=dirs_v_marker.getIcon().iconSize.width;
	var pos = new GControlPosition(G_ANCHOR_BOTTOM_LEFT, new GSize(offset.x - point.x - anchor.x + width,- offset.y + point.y +anchor.y)); 
	pos.apply(tooltip_5255);
	tooltip_5255.style.visibility="visible";
}


function dirs_init_vars(marker_to) {
	dirs_5255 = new GDirections();
	dirs_v_step = 40; // metres
	dirs_v_tick = 40; // milliseconds
	lastVertex = 0;
	dirs_v_eol = 0;
	dirs_v_k=0;
	dirs_v_stepnum=0;
	dirs_v_speed = "";
	dirs_v_ditml = "";
	document.getElementById("senddir_mail_body").value = "";

	if (dirs_v_marker) {
		map_5255.removeOverlay(dirs_v_marker);
	}
	if (dirs_v_poly) {
		map_5255.removeOverlay(dirs_v_poly);
	}
	if (dirs_v_poly2) {
		map_5255.removeOverlay(dirs_v_poly2);
	}

	GEvent.addListener(dirs_5255,"load", function() {
		dirs_v_poly=dirs_5255.getPolyline();
		dirs_v_eol=dirs_v_poly.Distance();
		map_5255.setCenter(dirs_v_poly.getVertex(0),17);
		dirs_v_marker = new GMarker(dirs_v_poly.getVertex(0),{icon:car_icon});
		map_5255.addOverlay(dirs_v_marker);
		var steptext = dirs_5255.getRoute(0).getStep(dirs_v_stepnum).getDescriptionHtml();
		dirs_v_ditml = dirs_v_ditml + '1. ' + steptext + '<br>';
		dirs_show_tooltip(steptext);
		dirs_v_poly2 = new GPolyline([dirs_v_poly.getVertex(0)]);
		map_5255.addOverlay(dirs_v_poly2);
		dirs_animate(0);
	});

	GEvent.addListener(dirs_5255,"error", function() {
		alert("Hata oluştu. Hata Kodu: "+dirs_5255.getStatus().code);
	});

	dirs_v_ditml = 'http://www.acabanerede.net/renk adresinden alınan adres tarifi<br><br>';
	dirs_v_ditml = dirs_v_ditml + '<b>Bayi Adı: </b>'+marker_to.poi_item.name+'<br>';
	dirs_v_ditml = dirs_v_ditml + '<b>Bayi Adresi: </b>'+marker_to.poi_item.adres+'<br>';
	dirs_v_ditml = dirs_v_ditml + '<b>Bayi Telefonu: </b>'+marker_to.poi_item.phone1+'<br>';
	dirs_v_ditml = dirs_v_ditml + '<br><br><b>Adres Tarifi:</b><br><br>';

	dirs_5255.loadFromWaypoints([""+my_place.getLatLng().lat()+","+my_place.getLatLng().lng()+"",""+marker_to.getLatLng().lat()+","+marker_to.getLatLng().lng()+""], {getPolyline:true,getSteps:true,locale:'tr'});

}


