Complex_Graphics.prototype= new Extra_Graphics();
function Complex_Graphics()
{
    this._list_of_graphics=new Array();
}
Complex_Graphics.prototype.add_graphics=function(graphics)
{
    this._list_of_graphics.push(graphics);
};
Complex_Graphics.prototype.add_list_of_graphics=function(list)
{
    this._list_of_graphics=this._list_of_graphics.concat(list);
};
Complex_Graphics.prototype.draw=function(jg)
{
    var lookup_x1 = new Array();
    var lookup_y1 = new Array();
    var lookup_x2 = new Array();
    var lookup_y2 = new Array();
    var draw = true;
    try{
        for(var i=0;i< this._list_of_graphics.length; i++)
        {
            if( this._list_of_graphics[i].constructor == Line ){
                var pxl1 = _MAP_WRAPPER.convert_to_pixel(this._list_of_graphics[i]._p1._x,
                    this._list_of_graphics[i]._p1._y,true);
                var pxl2 = _MAP_WRAPPER.convert_to_pixel(this._list_of_graphics[i]._p2._x,
                    this._list_of_graphics[i]._p2._y,true);
                draw = true;
                for(var ii=0; ii<lookup_x1.length; ii++ ){
                    if( lookup_x1[ii] ==pxl1[0] && lookup_y1[ii] == pxl1[1] && lookup_x2[ii] ==pxl2[0] &&
                        lookup_y2[ii] == pxl2[1]){
                        draw = false;
                        break;
                    }
                }
                if(!draw){
                    continue;
                }
                lookup_x1.push(pxl1[0]);
                lookup_y1.push(pxl1[1]);
                lookup_x2.push(pxl2[0]);
                lookup_y2.push(pxl2[1]);
            }

            this._list_of_graphics[i]._color = this._color;
            this._list_of_graphics[i]._stroke = this._stroke;
            this._list_of_graphics[i]._printable = this._printable;
            this._list_of_graphics[i].draw(jg);
        }
    }catch(e)
    {
        alert("Complex_Graphics.draw(): "+e);
        return false;
    }
};
Complex_Graphics.prototype.clear=function(jg)
{
    try{
        for(var i=0;i <this. _list_of_graphics.length;i++)
            this._list_of_graphics[i].clear(jg);
			
        this._list_of_graphics.splice(0,this._list_of_graphics.length);
    }catch(e)
    {
        alert("Complex_Graphics.clear(): "+e);
        return false;
    }
};
Complex_Graphics.prototype.snap_point=function(x,y) 
{
    var res=false;
    for(var i=0;i < this._list_of_graphics.length;i++)
    {
        res = this._list_of_graphics[i].snap_point(x,y);
        if(res != false)
            return res;
    }
    return res;
};
Complex_Graphics.prototype.to_post_request=function()
{
    try
    {
        var x="";
        var y="";
        var first=true;
        var separator = "";
        for(var i=0; i < this._list_of_graphics.length; i++)
        {
            if(this._list_of_graphics[i].constructor == Line )
            {
                if(first)
                    first=false;
                else
                    separator="_";
                var tpr = this._list_of_graphics[i].to_post_request();
                x+=separator+tpr[0];
                y+=separator+tpr[1];
            }
        }
        return new Array(x,y);
    }
    catch(e)
    {
        alert("Complex_Graphics.to_post_request() "+e);
        return false;
    }
};
Complex_Graphics.prototype.to_polygon_post_request=function()
{
    try
    {
        //Check if the gathering place is positioned
        if(this._list_of_graphics.length == 0)
        {
            alert(_MSG_DEFINE_GEOMETRY);
            return false;
        }
	
        //close the polygon
        this._list_of_graphics[this._list_of_graphics.length-1]._p2=this._list_of_graphics[0]._p1.clone();
	
        //encode the polygon points
        var x="";
        var y="";
        var first = true;
        for( var i=0; i < this._list_of_graphics.length; i++ )
        {
			
            var obj = this._list_of_graphics[i];
			
            if(obj.constructor != Line)
            {
                throw "Sono supportate unicamente linee nella definizione del poligono";
            }
			
            if(first)
            {
                x+=obj._p1._x;
                y+=obj._p1._y;
	
                x+="_";
                y+="_";
				
                x+=obj._p2._x;
                y+=obj._p2._y;
                first = false;
            }
            else
            {
                x+=obj._p2._x;
                y+=obj._p2._y;
			
            }
            if( i+1 < this._list_of_graphics.length )
            {
                x+="_";
                y+="_";
            }
        }
		
        return new Array(x,y);
	
    }catch(e)
    {
        alert("Complex_Graphics.to_polygon_post_request() "+e);
        return false;
    }
};

Complex_Graphics.prototype.move=function(x,y)
{
    try{
        for(var i=0;i <this. _list_of_graphics.length;i++){
            this._list_of_graphics[i].move(x,y);
        }
    }catch(e)
    {
        alert("Complex_Graphics.move(): "+e);
        return false;
    }
};
