/**
*   @desc   State machine class for geostart. First draft version. 
*           Saves state of current GeoStart.
*            
*            Version history:
*               0.1:    Primary skeleton
*
*   @author Bjorn Brala - Swis BV
*   
**/
function cStateMachineMapOnly() {
    this.aCookie = Array();
    this.bDisabled = false;
    
    /**
    *   @desc Initiliaze
    **/
    this.init = function(){
        if ( oGeoStart.oMap.isLoaded() ) {
            var days = days || 365;
            var date = new Date();
            date.setTime(date.getTime()+(days*24*60*60*1000));
            this.expires = " expires="+date.toGMTString()+"; path=/";
        
            this.loadCurrentState();
            if ( this.oState && this.bDisabled == false ) {
                this.loadMapState();        
            }        
        } else {
            setTimeout('oState.init()', 100);
        }
    }
    
    
    /**
    *   @desc Update cookie information and write it ( mapmove etc )
    **/
    this.update = function(){
        this.createCookie();
        this.saveCookie();
    }
    
    /**
    *   @desc Save current state
    **/
    this.saveCurrentState = function(){
        this.saveCookie();
    }

    
    /**
    *   @desc Load current state
    **/
    this.loadCurrentState = function(){
        // Load cookie information into state variable.
        this.oState = this.readCookie();               
    }
    

    /**
    *   @desc Get and save current map view
    **/
    this.getMapState = function(){
        var aCookie = Array();
        
        aCookie.push("GeoStart_lng=" + oGeoStart.oMap.getCenter().lng() + ";" + this.expires);
        aCookie.push("GeoStart_lat=" + oGeoStart.oMap.getCenter().lat() + ";" + this.expires);
        aCookie.push("GeoStart_zoom=" + oGeoStart.oMap.getZoom() + ";" + this.expires);
        
        return aCookie;
    
        // Get Center
        // Get Zoom
        // Get maptype
    }
    
    /**
    *   @desc Load Map State
    **/
    this.loadMapState = function(){
        setTimeout('oGeoStart.oMap.setZoom(parseInt(' + this.oState.GeoStart_zoom + '))', 1);
        setTimeout('oGeoStart.oMap.setCenter(new GLatLng(parseFloat(' + this.oState.GeoStart_lat + '), parseFloat(' + this.oState.GeoStart_lng + ')))', 100);
    }
    
    /**
    *   @desc Write information to a cookie
    **/
    this.saveCookie = function(days){
        this.aCookie = Array();
    
        this.createCookie();
        
        var i = this.aCookie.length;
        
        while ( i-- ) {
            document.cookie = this.aCookie[i];
        }
    }
  
    /**
    *   @desc Create cookie string from saved variabled.
    **/  
    this.createCookie =function(){
        var aMapState = this.getMapState();
        var i = aMapState.length;
        while ( i-- ) {
            this.aCookie.push(aMapState[i]);
        }        

        return;
    }
    
    /**
    *   @desc Read information from cookie
    **/
    this.readCookie = function(){
        var ca = document.cookie.split(';');
        var oReturn = false;
        
        for(var i=0;i < ca.length;i++) {
            var c = ca[i];
            while (c.charAt(0)==' ') c = c.substring(1,c.length);
                if ( c.indexOf("GeoStart_") != -1 ) {
                    if ( !oReturn ) {
                        oReturn = new Object();
                    }
                    oReturn[c.substring(0, c.indexOf("="))] = c.substring(c.indexOf("=")+1, c.length);
                }
        }
        return oReturn;
    }
    
    
    /**
    *   @desc Delete current cookie
    **/
    this.deleteCookieVars = function(){
        this.saveCookie(-1);
    }
    
    
}
