var html = "<div class=\"routeMain\" style=\"border-top: 0px;\"><div class=\"routeSign {imageRoute} fix\"><span></span></div>" +
    "{operation} <br><i><b>{street}</b></i><br>"+
    "{operation2} <b>{length}</b></div>";

var htmlStartStop = "<div class=\"routeMain\" style=\"border-top: 0px;\"><div class=\"routeSign fix\" style=\"background-image: "+
    "url('{imageRoute}'); background-repeat: no-repeat; width: {width}px; height: {height}px; "+
    "margin-top: 5px; margin-left: 4px; margin-right: 8px;\"><span></span></div>" +
    "{operation} <br><i><b>{street}</b></i><br>"+
    "{operation2} <b>{length}</b></div>";

var operationTranslate = {'start'    : {'pl': 'Zacznij jechać z ',
					'en': 'Start of '},

			  'opposite' : {'pl': 'Wjedź w ',
					'en': 'Merge onto '},
			  
			  'right'    : {'pl': 'Skręć w prawo ',
					'en': 'Turn right at '},

			  'left'     : {'pl': 'Skręć w lewo ',
					'en': 'Turn left at '},

			  'stop'     : {'pl': 'Jestes u celu ',
					'en': 'You are at your destination '},

			  'ride'     : {'pl': 'przejedź ',
					'en': 'ride '}};

var operationTranslate_empty_street = {'start'    : {'pl': 'Na początku ',
						     'en': 'Start and '},
				       
				       'opposite' : {'pl': 'Dalej',
						     'en': 'Next '},
				       
				       'right'    : {'pl': 'Skręć w prawo i ',
						     'en': 'Turn right at '},
				       
				       'left'     : {'pl': 'Skręć w lewo i ',
						     'en': 'Turn left at '},
				       
				       'stop'     : {'pl': 'Jestes u celu ',
						     'en': 'You are at your destination '},
				       
				       'ride'     : {'pl': 'przejedź ',
						     'en': 'ride '}};

var imageRoute =  {'opposite' : 'routeGo',
		   'left'     : 'routeLeft',
		   'right'    : 'routeRight',
		   'back'     : 'routeBack',
		   'start'    : 'routeStart',
		   'stop'     : 'routeStop'};

function angleToOperation(angle) {
    if (angle >= 30) {
	operation="right";
    }
    else if(angle <= -30) {
	operation="left";
    }
    else {
	operation="opposite";
    }
    return operation;
};

function calDist(dist) {
    var distance;
    if (dist) {
	if (dist >= 1000) {
	    distance=dist/1000;
	    return distance.toFixed(1)+"km";
	}
	else {
	    return dist.toFixed(0)+"m";
	}
    }
};


var create_route = function(objRoutes, container, langue) {
    var result = "";

    for (var i=0; i<objRoutes.length; i++) {
	var obj = objRoutes[i];
	var insertVar = {};
	var insertStop = {};
	var operation;

	if (obj.operation == "ride" || obj.operation == "stop") {
	    var angle = objRoutes[i-1].angle;
	    operation = angleToOperation(angle);
	}
	else operation = obj.operation;

	insertVar.imageRoute = imageRoute[operation];
	if(obj.streetName == "Bez nazwy") {
	    insertVar.operation  = operationTranslate_empty_street[operation][langue];
	    insertVar.street = operationTranslate['ride'][langue];
	}
	else {
	    insertVar.street     = obj.streetName;
	    insertVar.operation  = operationTranslate[operation][langue];
	    insertVar.operation2 = operationTranslate['ride'][langue];
	}
	insertVar.length     = calDist(obj.distance);
	if (obj.operation == "stop") {
	    insertStop.imageRoute = "http://v2.dojazd.pl/designs/v3/css/marker-red.png";
	    insertStop.width = "21";
	    insertStop.height = "25";
	    insertStop.operation = operationTranslate['stop'][langue];
	    result += html.substitute(insertVar);
	    result += htmlStartStop.substitute(insertStop);

	    log(insertStop);
	    log(htmlStartStop.substitute(insertStop));
	}
	else if(obj.operation == "start") {
	    insertVar.imageRoute = "http://v2.dojazd.pl/designs/v3/css/marker-green.png";
	    insertVar.width = "21";
	    insertVar.height = "25";
	    result += htmlStartStop.substitute(insertVar);
	}
	else result += html.substitute(insertVar);

    }
    $(container).set('html', result);

}




