Wiking.widgets.Spider = new function()
{
    var handlediv;
    var dragHandlerBinding;
    var dragEndBinding;
    var dragPrePosition;
    /*
     * fabryka buttonów
     */
    var createButton = function(position,offset,width,title,action)
    {
        function Control(){
            Wiking.getMap().addControl(this);
        }
        Control.prototype = new GControl();
        Control.prototype.initialize = function(map){
            var div = document.createElement('div');
			div.style.cssText = "cursor: pointer; background: url('/wimg/wcontrols.png') "+offset+"px 0px; width: "+width+"px; height: 23px;";
            div.title = title;
            map.getContainer().appendChild(div);
            GEvent.addDomListener(div,'click',action);
            return div;       
        }
        Control.prototype.selectable = function(){return false;}
        Control.prototype.printable = function(){return true;}
        Control.prototype.getDefaultPosition = function(){
            return new GControlPosition(G_ANCHOR_TOP_LEFT,position);
        }; 
        new Control();     
    }
    var createSlider = function()
    {
        function Control(){
            Wiking.getMap().addControl(this);
        }
        Control.prototype = new GControl();
        Control.prototype.initialize = function(map){
            var div = document.createElement('div');
            div.style.cssText="background: url('/wimg/slider.png'); width:14px; height:181px;";
            map.getContainer().appendChild(div);
            GEvent.addDomListener(div,'click',function(e){
                sliderclick(e);
            });
            return div;       
        }
        Control.prototype.selectable = function(){return false;}
        Control.prototype.printable = function(){return true;}
        Control.prototype.getDefaultPosition = function(){
            return new GControlPosition(G_ANCHOR_TOP_LEFT,new GSize(65,187));
        };  
        new Control();
    }
    var sliderclick = function(e) // TODO: to wywołuje się też na dragEndzie ;/
    {
        var y = e.pointerY()-329;
        var c = -Math.round(y/10)+6;
        
        if (c<20 && c>2){
            Wiking.getMap().setCenter(Wiking.getMap().getCenter(),(c));
        }
    }
    var createSliderHandle = function()
    {
        function Control(){
            Wiking.getMap().addControl(this);
        }
        Control.prototype = new GControl();
        Control.prototype.initialize = function(map){
            handlediv = Element.extend(document.createElement('div'));  //extend IE FIX
            handlediv.style.cssText="cursor:hand;background: url(\'/wimg/sliderhandle.png\'); width:24px; margin-top: 104px ;height:14px;";
            
            handlediv.observe('click',sliderclick.bind(this)); 
            handlediv.observe('mousedown',dragStart.bind(this));
            handlediv.observe('mousup',dragEnd.bind(this));// czy to potrzebne.. hmm
            
            dragHandlerBinding = handleDrag.bind(this);
            dragEndBinding = dragEnd.bind(this);
        
            map.getContainer().appendChild(handlediv);
            return handlediv;       
        }
        Control.prototype.selectable = function(){return false;}
        Control.prototype.printable = function(){return true;}
        Control.prototype.getDefaultPosition = function(){
            return new GControlPosition(G_ANCHOR_TOP_LEFT,new GSize(60,187));
        };
        new Control();
/*
        handlediv = Element.extend(document.createElement('div'));
        handlediv.style.cssText="";
        var s = new TMapStaticLayer(options,handlediv,Wiking.getMap())
        s.draw();
        s.element.observe('click',sliderclick.bind(this)); 
        handlediv.observe('mousedown',dragStart.bind(this));
        handlediv.observe('mousup',dragEnd.bind(this));// czy to potrzebne.. hmm
        dragHandlerBinding = handleDrag.bind(this);
        dragEndBinding = dragEnd.bind(this);
        
        */
    }
    var dragStart = function(e)
    { 
        Event.stop(e);
        dragPrePosition=e.pointerY();
        document.body.observe('mousemove',dragHandlerBinding);
        document.body.observe('mouseup',dragEndBinding);
    }
    var dragEnd = function(e){
        document.body.stopObserving('mousemove',dragHandlerBinding);
        document.body.stopObserving('mouseup',dragEndBinding);
        //var dest = parseInt(handlediv.style.marginTop)+10;
        var y = parseInt(handlediv.style.marginTop)-134;
        var c = -Math.round(y/10)+6;
        if (c<20 && c>2){
            Wiking.getMap().setCenter(Wiking.getMap().getCenter(),(c));
        }   
    }
    var handleDrag = function(e)
    { // maksy: 296 - 317, przeskok: 10
        var vecY = dragPrePosition - e.pointerY();
        dragPrePosition = e.pointerY();
        var dest = parseInt(handlediv.style.marginTop)-vecY;
        if (dest < 3)
        {
            pos = 4
        }
        else if (dest > 164)
        {
            pos = 164
        }
        else
        {
            pos = dest
        }
        handlediv.style.marginTop=pos+"px"
    }
    var zoomChange = function()
    {
        if (zoom<3) return;//bo tak
        var zoom = Wiking.getMap().getZoom()-6;
        handlediv.style.marginTop=(134-10*zoom)+"px";
    }
    /*
     * funkcja inicjująca
     */
    this.init = function()
    {
        createButton(new GSize(35,100),0,22,'W lewo',function(){Wiking.getMap().panDirection(1,0)}); //left
        createButton(new GSize(86,100),-45,22,'W prawo',function(){Wiking.getMap().panDirection(-1,0)}); //right
        createButton(new GSize(60,74),-22,23,'W górę',function(){Wiking.getMap().panDirection(0,1)}); //top
        createButton(new GSize(60,126),-67,23,'W dół',function(){Wiking.getMap().panDirection(0,-1)}); //bottom
        createButton(new GSize(60,100),-90,23,'Wycentruj na obiektach',function(){Wiking.tools.zoomToObjects()});//center
        createButton(new GSize(60,160),-113,23,'Przybliż',function(){Wiking.getMap().zoomIn()});//plus
        createButton(new GSize(60,372),-136,23,'Oddal',function(){Wiking.getMap().zoomOut()});//minus
         delete createButton;
        createSlider();
         delete createSlider;
        createSliderHandle();
         delete createSliderHandle;
        GEvent.addListener(Wiking.getMap(), "zoomend", zoomChange);
    };
    this.toString = function()
    {
        return "Wiking Spider";
    };
}
