﻿/*
* Class for handling resizing of the iframe and map inside the iframe. It depends on the Resizer class above.
*/
Utils.Resize = function(){
    Utils.Resize.initializeBase(this);
    this.iframe;
    this.offsetWidth;           //widht of content in addition to the map
    this.offsetHeight;          //height of content in addition to the map
    this.minHeight = 200;       //min size of iframe
    this.minWidth = 200;        //      ''
    this.maxHeight = 1000;      //max size of iframe
    this.maxWidth = 1000;       //
    
    this.iWidth = -1;             //current width of window
    this.iHeight = -1;            //current height of window
    this.bStarted = true;
    
    this.windowHeight=-1;
    this.windowWidth=-1;
    this.aHandlers = new Array();   
    
    //map api specific
    this.mapApi;
    this.prevWidth=-1;
    this.prevHeight=-1;
}

Utils.Resize.prototype.init = function(iframe, mapApi, offsetWidth, offsetHeight, minWidth,  minHeight, maxWidth, maxHeight){
    this.iframe = iframe;
    this.offsetWidth = offsetWidth;  
    this.offsetHeight = offsetHeight; 
    this.mapApi = mapApi; //Global.getMapApi(); //is it necessary? fire event instead - handled by user
    this.minHeight = minHeight;
    this.minWidth = minWidth;
    this.maxHeight = maxHeight;
    this.maxWidth = maxWidth
    
    //hook up to resizing event.
    window.onresize = Function.createDelegate(this, this.onStartResize);
    this.onStartResize();    
    this.addHandler("resize", Function.createDelegate(this, this.onResize));
    this.addHandler("resizeend", Function.createDelegate(this, this.onResizeEnd));

}

//resizestart, resizeend, resize
Utils.Resize.prototype.addHandler=function(eventName, delegate){
    this.aHandlers[this.aHandlers.length]=new Array(eventName, delegate);
}

Utils.Resize.prototype.onStartResize = function(){
    this.onResize(true);
}

//On resizing
Utils.Resize.prototype.onResize = function(bStart){
    var size = Utils.getWindowSize();
    this.resize(size) //resize map frame
    
    //if this is the start of resize action
    if(bStart && this.iWidth==-1 && this.iHeight==-1){
        setTimeout(Function.createDelegate(this,this.onResize), 200);
        this.iWidth =size.width;
        this.iHeight=size.height;
    }

    //resize fired after timeout
    if(!bStart){ //if bStart is undefined - not fired from code, but fired from event.
        //resize completed
         if (this.iWidth==size.width && this.iHeight==size.height){
            this.iWidth=-1;
            this.iHeight=-1;
            this.onResizeEnd(size);
            return false;
         }
         // reset timeout.
         else {
            this.iWidth =size.width;
            this.iHeight=size.height;
            setTimeout(Function.createDelegate(this,this.onResize), 200);
            return false; 
         }   
    }
    
    var isNS4=document.layers?true:false;
    var isIE=document.all?true:false;
    var isNS6=!isIE&&document.getElementById?true:false;
        
    if(isNS6 || isNS4){ //onresize is a single (not continous event)
        this.onResizeEnd(size);
    }
}

Utils.Resize.prototype.onResizeEnd= function(size)
{
    //mapapi specific
    // TODO: fire event to be caught by mapframe instead.. that way this can be used with 
    //all sorts of 'frames'
    if(!this.mapApi) this.mapApi = Global.getMapApi();
    if(this.mapApi){
        var height = this.iframe.height;
        var width = this.iframe.width;
        
        if (height != this.prevHeight || width != this.prevWidth){
           this.mapApi.loadMap();
        }
        
        this.prevHeight=height;
        this.prevWidth=width;
    }
}

//resize frame
Utils.Resize.prototype.resize = function(size){
    var newHeight = size.height-this.offsetHeight;
    var newWidth  = size.width-this.offsetWidth;
    newHeight = newHeight<this.minHeight?this.minHeight:newHeight;
    newWidth = newWidth<this.minWidth?this.minWidth:newWidth;
    newHeight = newHeight>this.maxHeight?this.maxHeight:newHeight;
    newWidth = newWidth>this.maxWidth?this.maxWidth:newWidth;
    
    //resize iframe, but do not load map
    this.iframe.height=newHeight;
    this.iframe.width = newWidth;
}


// Fire event 
Utils.Resize.prototype.fire=function(eventName){
    var size = Utils.getWindowSize();
    for(var i=0;i<this.aHandlers.length; i++){
        if(this.aHandlers[i][0]==eventName){
            this.aHandlers[i][1](size);
        }
    }
}

Utils.Resize.registerClass('Utils.Resize',Sys.Component);
