/*
This is a class to watch the current window for changes in viewport size,
due to a resize of the frame.
*/
var ResizeWatcher = new Class({
    Implements: [Options, Events],
    
    options: {
        onResizeWidth: $empty,
        onResizeHeight: $empty,
        onResize: $empty
    },
    
    isWatching:false,
    prevSize:null,
    
    initialize: function(options)
    {
        if(options != undefined)
            this.setOptions(options);
    },
    
    start: function(interval)
    {
        if(!$chk(Window.getViewport)){ return; }
        
        this.isWatching = true;
        this._watch_.call(this, interval);
    },
    
    stop: function(interval)
    {
        this.isWatching = false;
    },
    
    _watch_: function(interval) {
        if(!this.isWatching) { return; }
        
        if(this.prevSize == undefined || this.prevSize == null)
        {
            this.prevSize = Window.getViewport();
        }
        
        var currentSize = Window.getViewport();
        var resized = false; 
        
        if(currentSize.height != this.prevSize.height)
        {
            this.fireEvent("resizeHeight", [this.prevSize.height, currentSize.height]);
            resized = true;
        }
        
        if(currentSize.width != this.prevSize.width)
        {
            this.fireEvent("resizeWidth", [this.prevSize.width, currentSize.width]);
            resized = true;
        }
        
        if(resized)
        {
            this.fireEvent("resize", [this.prevSize, currentSize]);
            this.prevSize = currentSize;
        }
        
        var self = this;
        setTimeout(function(){ self._watch_.call(self, interval); }, interval);
    }
});

