// Cookies Library GPS-Tracks.com
// Copyright GPS-Tracks.com
// ACHTUNG: Darf kein ASPx enthalten!

// COOKIE HANDLING
function CookieData(){
 
  this.put = function(key, value){
    this[key] = value;
  };
  
  this.get = function(name){
    if(this[name])
      return this[name];
    else
      return null;
  };
  
  this.contains = function( key ){
    return (typeof this[key] != undefined)?true:false;
  };
  
  this.join = function(){
    var tmp = "";
    var counter = 0;
    for(var i in this){
      if (typeof this[i] == "function" || typeof this[i] == "object") 
        continue;
      if(counter > 0) tmp += "&";
      tmp += escape(i) + ":" + escape(this[i]);
      counter++;
    }
    return tmp;
  }
  
  this.set = function(str){
    if(str.length == 0)
      return false;
    var arr = unescape(str).split("&");
    var datas = new Array();
    for(var i = 0; i < arr.length; i++){
      datas[i] = arr[i].split(":");
    }
    for(var i = 0; i < datas.length; i++){
      this.put(datas[i][0], datas[i][1]);
    }
    return true;
  }

  this.toString = function(){
    var ret = "{";
    for(p in this ){
      if (typeof this[p] == "function" || typeof this[p] == "object") continue;
      if(ret.length > 1)
        ret += ",";
      ret += p + ":" + this[p];
    }
    return ret + "}";
  }
  
  this.setData = function(cookies, cookieName){
    var a = cookies.split("; ");
    var val = "";
    for(var i = 0; i < a.length; i++){
      // name steht am Anfang des cookie-Strings
      if( a[i].search(cookieName) == 0 ){
        var paramString = a[i].substring(cookieName.length+1);
        this.set( paramString );
        return paramString;
      }
    }
    return false;
  }
}

// format der Speicherung:
// name=value; name2=value2; ...
// wobei value = "k1=v1&k2=v2..."
function Cookie(name, cookieTest){
  this.name = name;
  this.expires = new Date().getTime() + 365 * 24 * 60 * 60 * 1000;
  this.data = new CookieData();
  this.domain;
  this.path;
  this.paramString = this.data.setData(document.cookie, this.name);
  this.fromUser = this.paramString.length > 0 ? true : false;
  this.enabled = false;
  
  if(typeof cookieTest != "undefined" && cookieTest == true)
    this.test();
}

Cookie.prototype.getName = function(){
  return this.name;
}
Cookie.prototype.setExpires = function(timestamp){
  this.expires = timestamp;
}
Cookie.prototype.setPath = function(path){
  this.path = path;
}
Cookie.prototype.setDomain = function(domain){
  this.domain = domain;
}
Cookie.prototype.setValue = function(key, value){
  this.data.put(key, value);
}
Cookie.prototype.getValue = function(name){
  return this.data.get(name);
}
Cookie.prototype.getParamString = function(){
  return this.paramString;
}
Cookie.prototype.send = function(){
  document.cookie = this.name +"=" + this.data.join() +
                    "; expires=" + new Date(this.expires).toGMTString() +
                    (this.path ? "; path=" + this.path : "") +
                    (this.domain ? "; domain=" + this.domain : "");
}
Cookie.prototype.test = function(){
  if(this.fromUser == true) return true;
  var now = new Date().getTime();
  document.cookie = "cookieEnabled="+now + 
                    "; expires=" + new Date(now + 5000).toGMTString() +
                    "; path=/";
  var re = new RegExp("cookieEnabled="+now, "g");
  this.enabled = re.test(document.cookie);
}

Cookie.prototype.remove = function(){
  this.expires = new Date().getTime();
  this.data = new CookieData();
  this.send();
}

// SaveMapStatusCookie
function SaveMapStatusCookie(MapStatusCookie, map){
MapStatusCookie.setPath = ("/");
MapStatusCookie.setValue ("MapMiddleX", map.getCenter().lng());
MapStatusCookie.setValue ("MapMiddleY", map.getCenter().lat());
MapStatusCookie.setValue ("MapMst", map.getZoom());
var szCurrentMap = map.getCurrentMapType().getName(true);
  if(szCurrentMap.indexOf('Swiss-Map') != -1 || szCurrentMap.indexOf('Deutschland-Map Utm') != -1 || szCurrentMap.indexOf('KOMPASS Austria-Map') != -1){
  MapStatusCookie.setValue ("MapStartType", szCurrentMap);
  } else {
  MapStatusCookie.setValue ("MapStartType", 'G_SATELLITE_MAP');
  }
MapStatusCookie.send();
}


