function Polyline(opt){
    this.isDeviation = false;
    this.isNew = true; 
    this.isFocus = true;
    
    this.points = [];
    this.polyline = null;
    
    this.markers = null;
    this.phaseId = '';
    this.isDirectionsSenseShow = false;
    this.isOldGeometry = false;
    
    this.directionSign = null;

    this.customColor = '';
    this._speedValue = -1;
    this.stepsPoints =  null;

    this.allowCheck = true;
    
    this.markersPoints = null;
    this.initialize(opt || {});
    
    if(checkInterface(this, IGeometry)){
        return;
    }
};

Polyline.prototype.initialize = function(opt){
    this.map = opt.map;

    this.stepsPoints =  null;
    this.isNew = true; 
    this.isOldGeometry = false;
    this.isFocus = true;
    this.points = new Array();
    this.polyline = new google.maps.Polyline();
    this.markers = new Array();
    this.directionSign = new DirectionSign();

    this.allowCheck = true;
    
    this.markersPoints = '';
    this.customColor = '';
    this._speedValue = -1;
};

Polyline.prototype.setMarkersPoints = function(value){
    this.markersPoints = value;
}

Polyline.prototype.setPolylineColor = function(color){
    this.customColor = color;
}

Polyline.prototype.length = function(){
	return this.geometryPath().length;
};

Polyline.prototype.addPoint = function(point){
    var m =  new google.maps.Marker({
            map: this.map,
            position: point
    });
    this.markers.push(m);   
    this.points.push(point);

    //redraw
    this.setOptions({
        map: this.map,
        path: this.points
    });
    var opt = this.generateOptions();
    this.setOptions(opt);     
};

Polyline.prototype.setPath = function(value){
	this.points = value;
	
    this.setOptions({
	        map: this.map,
	        path: this.points
	    });
	 var opt = this.generateOptions();
	 this.setOptions(opt);     
	    
	for(var i=0, item; item = value[i]; i++){
		var m =  new google.maps.Marker({
            position: item
		});
	    this.markers.push(m);
	}
	
};

Polyline.prototype.generateOptions = function(){
    var opt = helpersGenerateStyle(this.isFocus, this.isDeviation, this.isNew, this.isOldGeometry);


    if(this.customColor != ''){
       opt.strokeColor = this.customColor;
    }
  
    return opt;
};

Polyline.prototype.setOptions = function(opt){
	if(opt != null)
		this.map = opt.map;
    this.polyline.setOptions(opt);
};
Polyline.prototype.getMap = function(){
    return this.map;
}

Polyline.prototype.geometryPath = function(){
    return this.points;
};

Polyline.prototype.getGeometryJSON = function(){
    var path = this.geometryPath();
    var geometry = new Array();
    for(var i=0, item; item = path[i]; i++){
        geometry.push({
            lat: item.lat(),
            lng: item.lng()
        });
    }
    //remove deviation when calculate street
    var stepsPoints = null;
    if(this.isDeviation == false){
        stepsPoints= this.stepsPoints;
    }
    var values = {
        isDeviation: this.isDeviation == true ? 1:0,
        geometry: geometry,
        deviationArrows: this.directionSign.GetPoints(),
        stepsPoints: stepsPoints,
        markersPoints: this.markersPoints
    };
    return values;
};

Polyline.prototype.focus = function(){
    this.isFocus = true;
    var opt = this.generateOptions();
    this.setOptions(opt);
};

Polyline.prototype.loseFocus = function(){
    this.isFocus = false;
    var opt = this.generateOptions();
    this.setOptions(opt);
};

Polyline.prototype.showMarkers = function(value){
    for(var i=0, item; item = this.markers[i]; i++){
        item.setOptions({
            map: (value == true ? this.map : null)
        });
    } 
};

Polyline.prototype.showDirectionsSense = function(value){
	this.isDirectionsSenseShow = value;
};

Polyline.prototype.setDirectionsSense = function(value){
	this.directionSign.DrawMarkersEditable(value, this.map);
};

Polyline.prototype.setSpeed = function(value){
    this._speedValue = value;
}
Polyline.prototype.getSpeed = function(){
    return this._speedValue;
    //return 51;
};

Polyline.prototype.clear = function(){
	this.setOptions({
		map: null
	});
	this.markers = new Array();  
	this.points = new Array();
	this.directionSign.ClearMarkers();
};
Polyline.prototype.getBounds = function(){
	return this.polyline.getBounds();
};

Polyline.prototype.CurrentPhase = function(value){
    if(value == true){
        this.setOptions({
            strokeOpacity: 1
        });
    }else{
        this.setOptions({
            strokeOpacity: 0.5
        });
    }
}
