/**
*   @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 cStateMachine() {
    this.aCookie = Array();
    
    /**
    *   @desc Initiliaze
    **/
    this.init = function(){
        this.loadCurrentState();
        if ( this.oState ) {
            this.changeToDelete();
            this.loadMapState();        
            this.loadMenuState();
        }
    }
    
    
    /**
    *   @desc Save current state
    **/
    this.saveCurrentState = function(){
        this.changeToDelete();
        this.saveCookie();
    }
    
    
    this.deleteCurrentState = function(){
        this.changeToSave();
        this.deleteCookieVars();
    }
    
    /**
    *   Change icon to delete state
    **/
    this.changeToDelete = function(){
        if ( !oGeoStart.oStateMachine.oImage ) {
            oGeoStart.oStateMachine.oImage = document.getElementById('save_settings_image');
        }
        if ( !this.oLink ) {
            oGeoStart.oStateMachine.oLink =  document.getElementById('save_settings_link');
        }
        oGeoStart.oStateMachine.oImage.src = "GeoStart/images/icon_save_loading.gif";
        oGeoStart.oStateMachine.oLink.innerHTML = "Positie wordt opgeslagen...";
                
        setTimeout('oGeoStart.oStateMachine.oImage.src = "GeoStart/images/icon_save_delete.gif";oGeoStart.oStateMachine.oLink.innerHTML = "reset bewaarde positie";oGeoStart.oStateMachine.oLink.onclick=function(){oGeoStart.oStateMachine.deleteCurrentState();return false;}', 1000);
        
    }
    
    
    this.changeToSave = function(){
        if ( !oGeoStart.oStateMachine.oImage ) {
            oGeoStart.oStateMachine.oImage = document.getElementById('save_settings_image');
        }
        if ( !oGeoStart.oStateMachine.oLink ) {
            oGeoStart.oStateMachine.oLink =  document.getElementById('save_settings_link');
        }
        
        oGeoStart.oStateMachine.oImage.src = "GeoStart/images/icon_save_loading.gif";
        oGeoStart.oStateMachine.oLink.innerHTML = "bewaarde positie wordt verwijderd";
        
        setTimeout('oGeoStart.oStateMachine.oImage.src = "GeoStart/images/icon_save_as.gif";oGeoStart.oStateMachine.oLink.innerHTML = "huidige positie bewaren";oGeoStart.oStateMachine.oLink.onclick=function(){oGeoStart.oStateMachine.saveCurrentState();return false;}', 1000);        
    }
    
    /**
    *   @desc Load current state
    **/
    this.loadCurrentState = function(){
        // Load cookie information into state variable.
        this.oState = this.readCookie();               
    }
    

    /**
    *   @desc Get and save menu state
    **/
    this.getMenuState = function(){    
        
        // Menu CheckBox State (oStateCB)
        var oStateCB = "";
        for ( var key in oMenu.oStateCB ) {
            oStateCB += key + ":" + oMenu.oStateCB[key] + "|";
        }    
        
        // Menu cat state ( oStateCategory
        var oStateCategory = "";
        for ( var key in oMenu.oStateCategory ) {
            oStateCategory += key + ":" + oMenu.oStateCategory[key] + "|";  
            
        }    
        
        
        // Menu category classname (oStateMenu)
        var oStateMenu = "";
        for ( var key in oMenu.oStateMenu ) {
            oStateMenu += key + ":" + oMenu.oStateMenu[key] + "|";
        }
        
        var aCookie = Array();        
        aCookie.push("GeoStart_oStateCB="       + oStateCB + ";"        + this.expires);
        aCookie.push("GeoStart_oStateMenu="     + oStateMenu + ";"      + this.expires);
        aCookie.push("GeoStart_oStateCategory=" + oStateCategory + ";"  + this.expires);
        
        return aCookie;
    }
    
    /**
    *   @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   Load menu state from the cookie
    *           Helaas heel custom, kreeg JSON niet in 2 min aan de praat.
    **/
    this.loadMenuState = function(){
        if ( this.oState.GeoStart_oStateCB.length > 0 ) {
            var aResult = this.oState.GeoStart_oStateCB.split("|");
            var i = aResult.length;
            while ( i-- ) {
                if ( aResult[i].length > 0 ) {
                    var aItem = aResult[i].split(":");
                    oMenu.oStateCB[aItem[0]] = aItem[1];
                }
            }
        }        

        
        if ( this.oState.GeoStart_oStateCategory.length > 0 ) {
            var aResult = this.oState.GeoStart_oStateCategory.split("|");
            var i = aResult.length;
            while ( i-- ) {
                if ( aResult[i].length > 0 ) {
                    var aItem = aResult[i].split(":");
                    oMenu.oStateCategory[aItem[0]] = aItem[1];
                }
            }
        }        
        
        
        if ( this.oState.GeoStart_oStateMenu.length > 0 ) {
            var aResult = this.oState.GeoStart_oStateMenu.split("|");
            var i = aResult.length;
            while ( i-- ) {
                if ( aResult[i].length > 0 ) {
                    var aItem = aResult[i].split(":");
                    oMenu.oStateMenu[aItem[0]] = aItem[1];
                }
            }
        }        

    }
    
    /**
    *   @desc Write information to a cookie
    **/
    this.saveCookie = function(days){
        this.aCookie = Array();
    
        var days = days || 365;
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        this.expires = " expires="+date.toGMTString();
    
        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]);
        }        

        var aMenuState = this.getMenuState();
        var i = aMenuState.length;
        while ( i-- ) {
            this.aCookie.push(aMenuState[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);
    }
    
    
}
