/**
 * @author Administrator
 */

TGZoom = Class.create();

TGZoom.prototype = 
{   
    initialize : function(mapObject,options)
    {
         this.mapObject = mapObject;       
         this.rectangle = null;
         
         this.dragging = false;
         this.dragStartEventHandle = null;
         this.dragMoveEventHandle = null;
         this.dragEndEventHandle = null;
         
         this.mapCover = null;
         this.x = 0;
         this.y = 0;
         
         this.xOffset = 0;
         this.yOffset = 0;
         
         if(options)
         {
             if(options.xOffset)
             {
                 this.xOffset = options.xOffset; 
             }
             
             if(options.yOffset)
             {
                 this.yOffset = options.yOffset; 
             }
         }
    },
    
    activate : function()
    {  
        this.mapCover = document.createElement("div");
        this.mapCover.id = "mapcover";
        this.mapCover.style.position = "absolute";      
        this.mapCover.style.left = "0px";
        this.mapCover.style.top = "0px";
        
        this.updateSize();
        
        this.mapCover.style.cursor = 'crosshair';
        
        
        // Workaround fuer IE
        
        if(document.all)
        {
            if(debug) GLog.write("Zoom Workaround ...");
            this.mapCover.style.background = "url(../images/blank128.gif) repeat";
        }
        
        this.mapObject.getContainer().appendChild(this.mapCover);
        
        
        this.rectangle = document.createElement("div");
        this.rectangle.style.display = "none";
        this.rectangle.style.border = "1px solid #FF0000";
        this.rectangle.style.width = "1px";
        this.rectangle.style.height ="1px";
        this.rectangle.style.position = "absolute";
        
        
        this.mapCover.appendChild(this.rectangle);
        
        this.dragStartEventHandle = this.dragStart.bindAsEventListener(this);
        this.dragMoveEventHandle = this.drag.bindAsEventListener(this);
        this.dragEndEventHandle = this.dragEnd.bindAsEventListener(this);
        
        Event.observe(this.mapCover,"mousedown", this.dragStartEventHandle);
        Event.observe(this.mapCover,"mousemove",this.dragMoveEventHandle);
        Event.observe(this.mapCover,"mouseup",this.dragEndEventHandle);
    },
    
    deactivate : function()
    {
        Event.stopObserving(this.mapCover,"mousedown",this.dragStartEventHandle);
        Event.stopObserving(this.mapCover,"mousemove",this.dragMoveEventHandle);
        Event.stopObserving(this.mapCover,"mouseup",this.dragEndEventHandle);
        
        this.dragStartEventHandle = null;
        this.dragMoveEventHandle = null;
        this.dragEndEventHandle = null;
        
        if(this.rectangle)
        {
            Element.remove(this.rectangle);
            this.rectangle = null;
            Element.remove(this.mapCover);
            this.mapCover = null;
        }
        
    },
    
    updateSize : function()
    {
        if(this.mapCover)
        {
            var dimensions = Element.getDimensions(this.mapObject.getContainer());
            this.mapCover.style.width = dimensions.width + "px";
            this.mapCover.style.height = dimensions.height + "px";
        }
    },
    
    dragStart : function(event)
    {
        this.dragging = true;
        
        this.x = Event.pointerX(event) - this.xOffset;
        this.y = Event.pointerY(event) - this.yOffset;
        
        this.rectangle.style.top = this.y + 1 + "px";
        this.rectangle.style.left = this.x + "px";
        this.rectangle.style.width = "1px";
        this.rectangle.style.height = "1px";
        
        if(document.all)
        {
            this.rectangle.style.fontSize = "1px";
        }
        
        this.rectangle.style.display = "";
        
    },
    
    drag : function(event)
    {
        if(this.dragging)
        {
           var x = Event.pointerX(event) - this.xOffset;
           var y = Event.pointerY(event) - this.yOffset;
           
           if(x > this.x)
           {
               this.rectangle.style.width = x - this.x + "px"; 
           }
           else
           {
               this.rectangle.style.left = x + "px";
               this.rectangle.style.width = this.x - x + "px";
           }
           
           if(y > this.y)
           {
               this.rectangle.style.height = y - this.y + "px"; 
           }
           else
           {
               this.rectangle.style.top = y + "px";
               this.rectangle.style.height = this.y - y + "px";
           }
           
        }
    },
    
    dragEnd : function(event)
    {
        this.drag(event);
        this.dragging = false;
        
        var x = parseInt(this.rectangle.style.left);
        var y = parseInt(this.rectangle.style.top);   
        
        this.rectangle.style.display = "none";
        
        var width = parseInt(this.rectangle.style.width);
        var height = parseInt(this.rectangle.style.height);
        
        var sw = this.mapObject.fromContainerPixelToLatLng(new GPoint(x,y+height));
        var ne = this.mapObject.fromContainerPixelToLatLng(new GPoint(x + width,y));
        
        var bounds = new GLatLngBounds(sw,ne);
        
        var zoomLevel = this.mapObject.getBoundsZoomLevel(bounds);
        
        this.mapObject.setCenter(bounds.getCenter(),zoomLevel);
    }
}
