function ObjectInfo(parent){
    this.parent_ = parent;
    Wiking.getMap().addOverlay(this);
}
ObjectInfo.prototype = new GOverlay();
ObjectInfo.prototype.initialize = function(map){
    var
      width = 210,
      height = 200;
    var div = document.createElement('div');
    div.style.position="absolute";
    var x = document.createElement("div");
    x.style.cssText='cursor:pointer;position:absolute;left:'+(width-17)+'px;top:-9px;width:27px;height:27px;background: url(\'wimg/x.png\')'
    
    this.visible_ = true;
    
    var self = this; //clousure
    GEvent.addDomListener(x,'click',function(){
        Wiking.getMap().removeOverlay(self);
    });
    div.appendChild(x);
    var innerdiv = document.createElement('div');
    div.appendChild(innerdiv);    

    innerdiv.innerHTML = 
                 '<div style="float:left;width:8px;height:39px;background:url(\'wimg/eclefttop.png\')"></div>'+
                 '<div style="float:left;background:url(\'wimg/ectop.png\');height:39px;width:'+(width-16)+'px">'+
                    '<p style="font-size:14px;font-weight:bold;margin-top:12px;margin-left:5px;">'+this.parent_.getName()+'</p>'+
                 '</div>'+
                 '<div style="float:left;width:8px;height:39px;background:url(\'wimg/ecrighttop.png\')"></div>'+
                 
                 '<div style="clear:left;float:left;width:8px;background:url(\'wimg/cleft.png\');height:'+(height-47)+'px"></div>'+
                 '<div style="float:left;background:#fff;height:'+(height-47)+'px;width:'+(width-16)+'px">'+
                    //TREŚĆ
                   '<div style="padding:10px;" id="infoboxtresc">'+
 
                   '</div>'+
                 '</div>'+
                 '<div style="float:left;width:8px;background:url(\'wimg/cright.png\');height:'+(height-47)+'px"></div>'+
               
                 '<div style="float:left;width:8px;height:8px;background:url(\'wimg/cleftbottom.png\')"></div>'+
                 '<div style="float:left;background:url(\'wimg/cbottom.png\');height:8px;width:'+(width-16)+'px"></div>'+
                 '<div style="float:left;width:8px;height:8px;background:url(\'wimg/crightbottom.png\')"></div>';
    
    map.getPane(G_MAP_FLOAT_PANE).appendChild(div);
    this.div_ = div;
    this.tresc_ = document.getElementById('infoboxtresc');
    this.tresc_.removeAttribute('id') //= ""; // TODO remove? 
    this.generateContent();
    return div;
}
ObjectInfo.prototype.redraw = function(force) {
    if (!force) return;
    
    var pos = this.parent_.getPosition();
    var point = Wiking.getMap().fromLatLngToDivPixel(pos);
    var viewpoint = Wiking.getMap().fromLatLngToContainerPixel(pos);
    var size = Wiking.getMap().getSize();
    
    var x = point.x+30;
    var y = point.y-50;
        
    //TODO przeliczenia by sie przesuwało ładnie
    
    // Now position our DIV based on the DIV coordinates of our bounds
    this.div_.style.width = 210 + "px";
    this.div_.style.height = 200 + "px";
    this.div_.style.left = x + "px";
    this.div_.style.top = y + "px";
    
    if (size.height < viewpoint.y + 200){
        Wiking.getMap().panBy(new GSize(0,-(viewpoint.y+200-size.height)));
    }else if(viewpoint.y < 70){
        Wiking.getMap().panBy(new GSize(0,50));
    }
    
}
ObjectInfo.prototype.remove = function(){
    this.visible_ = false;
    this.div_.parentNode.removeChild(this.div_);
}
ObjectInfo.prototype.generateContent = function(){
    var params = Wiking.DataLoader.getPeriod()
    var object = this.parent_;
    var content = '<a href="#" onClick="ObjectInfo.zoomin('+object.getImei()+');">Przybliż</a><br />'+
                      '<div style="margin:5px 0;height:1px; background:#eee"></div>'+
                      "Imei: "+object.getImei()+
                      "<br />Długość trasy: ~"+Math.round(routelen(object.getImei())*100)/100+" km"+
                      //'<br />Największa prędkość: '+object.getData()[2].max()+' km/h<br />'+
                      '<div style="margin:5px 0;height:1px; background:#eee"></div>'+
                      '<a id="infochangelook" href="#">Zmień wygląd</a>'+
                      '<div style="margin:5px 0;height:1px; background:#eee"></div>'+
                      '<a href="/thingy/geo2.kml?dev='+object.getImei()+"&from="+params[0]+"&to="+params[1]+'">Pobierz KML z trasą</a>';
    this.tresc_.innerHTML = content;   
    var a = document.getElementById('infochangelook');
    GEvent.addDomListener(a,'click',function(){
        Wiking.widgets.LookConfig.runConfig(object);
    });
    a.removeAttribute('id');   
}
ObjectInfo.prototype.isVisible = function(){
    return this.visible_;
}
ObjectInfo.zoomin = function(imei){
    var object = Wiking.getObjectByImei(imei);
    var z = Wiking.getMap().getZoom()+3;
    if (z > 18){
        z = 18;
    }
    Wiking.getMap().setCenter(object.getPosition(),z);
}

WObjInfo22 = Class.create(TMapMovableLayer,
{
    initialize:function($super,object)
    {
        var 
          width = 210,
          height = 200;
        
        this.object = object;

        var options=
        {
            'point':object.getPosition(),
            'displacement':new TPoint(10,height+30),
            'size':new TSize(width,height),
            'clickable':true
        }
        var div = document.createElement('div');
       
        
        
        Event.observe(x,'click',this.clear.bind(this));
        div.appendChild(x);
        var innerdiv = document.createElement('div');
        div.appendChild(innerdiv);
        innerdiv.innerHTML = 
               '<div>'+
                 '<div style="float:left;width:8px;height:39px;background:url(\'wimg/eclefttop.png\')"></div>'+
                 '<div style="float:left;background:url(\'wimg/ectop.png\');height:39px;width:'+(width-16)+'px">'+
                    '<p style="font-size:14px;font-weight:bold;margin-top:12px;margin-left:5px;">'+object.getName()+'</p>'+
                 '</div>'+
                 '<div style="float:left;width:8px;height:39px;background:url(\'wimg/ecrighttop.png\')"></div>'+
                 
                 '<div style="clear:left;float:left;width:8px;background:url(\'wimg/cleft.png\');height:'+(height-47)+'px"></div>'+
                 '<div style="float:left;background:#fff;height:'+(height-47)+'px;width:'+(width-16)+'px">'+
                    //TREŚĆ
                   '<div style="padding:10px;" id="infoboxtresc">'+
 
                   '</div>'+
                 '</div>'+
                 '<div style="float:left;width:8px;background:url(\'wimg/cright.png\');height:'+(height-47)+'px"></div>'+
               
                 '<div style="float:left;clear:left;width:8px;height:8px;background:url(\'wimg/cleftbottom.png\')"></div>'+
                 '<div style="float:left;background:url(\'wimg/cbottom.png\');height:8px;width:'+(width-16)+'px"></div>'+
                 '<div style="float:left;width:8px;height:8px;background:url(\'wimg/crightbottom.png\')"></div>'+
               '</div>';
        $super(options,div,Wiking.getMap());
        this.draw();

        //this.generateContent();
        //TODO bind for refresh
    },
    clear:function($super)
    {
        this.visible = false;   
        $super();
    },
    generateContent:function()
    {
        var params = Wiking.DataLoader.getPeriod()
        var object = this.object;
        var content = '<a href="#" onClick="WObj.Info.zoomin('+object.getImei()+');">Przybliż</a><br />'+
                      '<div style="margin:5px 0;height:1px; background:#eee"></div>'+
                      "Imei: "+object.getImei()+
                      "<br />Długość trasy: ~"+Math.round(routelen(object.getImei())*100)/100+" km"+
                      //'<br />Największa prędkość: '+object.getData()[2].max()+' km/h<br />'+
                      '<div style="margin:5px 0;height:1px; background:#eee"></div>'+
                      '<a id="infochangelook" href="#">Zmień wygląd</a>'+
                      '<div style="margin:5px 0;height:1px; background:#eee"></div>'+
                      '<a href="/thingy/geo2.kml?dev='+object.getImei()+"&from="+params[0]+"&to="+params[1]+'">Pobierz KML z trasą</a>';
        this.tresc.innerHTML = content;   
        a = $('infochangelook');
        a.observe('click',Wiking.widgets.LookConfig.runConfig.bind({'object':object,'self':Wiking.widgets.LookConfig}));
        a.id = "";                 
    },
    draw:function($super)
    {
        var width = 210; //temp TODO
        var height = 200;
        var pos = this.object.getPosition();
		var tech = Wiking.getMap().getTechState();
        this.options.set('point',pos);
        this.visible = true;
         
        var mpoint = Wiking.getMap().getViewPortBB().getNorthEast().getMapDivPixel(tech);
        var tpoint = pos.getMapDivPixel(tech);
        var diff = ((mpoint.x-335)-(tpoint.x+width));
        var diff2 = (mpoint.y-(tpoint.y-height-30));
		var disp = diff < 0 ? 30 + diff*(-1) : 30;
        if (diff2 > 0)
        {
            Wiking.getMap().move(0,diff2+30);
        }    
        this.options.set('displacement',new TPoint(disp,height+30));
        
        $super();
        if (!this.tresc)
        {
            this.tresc = $('infoboxtresc');
            this.tresc.removeAttribute('id'); //= ""; // TODO remove? 
        }
        this.generateContent();     
    },
    isVisible:function()
    {
        return this.visible;
    }
});
WObjInfozoomin = function(imei)
{
    var object = Wiking.getObjectByImei(imei);
    var z = Math.min(Wiking.getMap().getZoom()+3, 16);
    Wiking.getMap().setCenter(object.getPosition(),z);
}
