/**
 * @author Koli
 */

function LinkHereDB(){
  var link_token ="";
  var s92_x = 0.0;
  var s92_y = 0.0;
  var scale = 0; 
  var type  = "Szukaj w pobliżu";
  var after_hash = "";
  var fromLink = false;
  var linkStep=0;
  this.last_nearest_token ='';
  this.last_route_token='';
  
  function isFromLink(){
    if (window.location.hash.length > 0 && linkStep==0) {
      fromLink = true;
      linkStep = 1;
      return true;
    }
  }
  this.isFromLink= isFromLink;
  function getScale(){
   return scale;
  }
  this.getScale = getScale;
  function switchToken(way){ //true => searchNearest   false=>route
    if(way){
      link_token = this.last_nearest_token;
    }else{
      link_token = this.last_route_token;
    }
  }
  this.switchToken = switchToken;
  
  function performStep(){
    var fields = window.location.hash.split("-");
    if(linkStep==1){
      if (fields.length > 1) {
        readLocationToken(fields[1]);
        linkStep = 2;
        if(fields.length >2){
          if (fields[2] != "") {
             link_token = fields[2];
             makeQueryFind(fields[2], fields[0]);
          }
        }else{
          mvfa.set_coords(s92_x, s92_y, scale);
        }
      }
    }else if(linkStep==2){
        readLocationToken(fields[1]);
        mvfa.set_coords(s92_x, s92_y, scale);
        linkStep = 0;
    }else if(linkStep==3){
      if (fields.length > 3) {
             additionalSteps(fields[3]);
          }
      linkStep=0;
    }
  }
  this.performStep = performStep;
  function makeQueryFind(q,type){
    var _action = loadJSONDoc(JSON_REQUESTS_PATH,
                              { method: "LinkHereDB",
                                link_token : q
                              });
    if (type == "#S") {
        setMessage("Proszę czekać.");
        _action.addCallbacks(onNearestResponse, onNearestResponseFail);
    }else if(type=="#R" || type=="#r"){
        setMessage("Proszę czekać.",0,'route_status_messages');
        _action.addCallbacks(onTraceRouteResponse, onTraceRouteResponseFail);
    }else if(type=="#W"){
        //_action.addCallbacks();
    }

  }
  this.makeQueryFind=makeQueryFind;

  function onNearestResponse(response){
    search_nearest.onNearestResponseFromLink(response);
  }
  this.onNearestResponse = onNearestResponse;
  function onNearestResponseFail(response){
    search_nearest.onNearestResponseFail(response);
  }
  this.onNearestResponseFail = onNearestResponseFail;
  function onTraceRouteResponse(response){
    trace_route.onTraceRouteResponseFromFile(response);
  }
  this.onTraceRouteResponse = onTraceRouteResponse;
  function onTraceRouteResponseFail(response){
    trace_route.onTraceRouteResponseFail(response);
  }
  this.onTraceRouteResponseFail = onTraceRouteResponseFail;
  function additionalSteps(steps){
    
    //TODO:podstrony
    var pageno = stripos("s");
    if(pageno!=false){

    }
    //TODO:stan paneli
    //TODO:spelen ekran
    
  }
  this.additionalSteps = additionalSteps;
  function getLinkStep(){
    return linkStep;
  }
  this.getLinkStep = getLinkStep;

  function setLocation(sx, sy, sc){
      s92_x = sx;
      s92_y = sy;
      scale = sc;
  }
  this.setLocation = setLocation;
  function updatelink(l_token){
    if(l_token!=undefined)
      link_token = l_token;
    //window.location.hash = 
    createAfterHash();
  }
  this.updatelink = updatelink;
  
  function createAfterHash(){
    after_hash = "";
    switch(linkHereDB.type){
       case "Szukaj w pobliżu":
           after_hash = "S-";
         break;  
       case "Zaplanuj swoją trasę":
           after_hash = "R-";
         break;
       case "Wizytówka":
           after_hash = "W-";
         break;
       default:
        after_hash = "-";
    }
    after_hash += createLocationToken();
    if (link_token != "") {
        after_hash += "-" + link_token;
     }
    return after_hash;
  }
  this.createAfterHash = createAfterHash;
  function createLocationToken(){
    loc = scale+"a"+Math.round(s92_x)+"a"+Math.round(s92_y);
    loc = to10(loc, 11);
    loc = convertToFrom10(loc,61);
    return loc;
  }
  this.createLocationToken = createLocationToken;

function readLocationToken(loctoken){
  
if(loctoken=='hjtdjxwkq'){
    loctoken="hJTeEB3kX";
 }
 if(loctoken=='flblbo6ik'){
	loctoken="hJTeEB3kX";
}

  var read = to10(loctoken, 61);
  read = convertToFrom10(read, 11);
  para = read.toString().split("a");
  scale = para[0];
  s92_x = para[1];
  s92_y = para[2];
}
this.readLocationToken =readLocationToken;

  var chars62        = ["0","1","2","3","4","5","6","7","8","9",
                        "a","b","c","d","e","f","g","h","i","j",
                        "k","l","m","n","o","p","q","r","s","t",
                        "u","v","w","x","y","z","A","B","C","D",
                        "E","F","G","H","I","J","K","L","M","N",
                        "O","P","Q","R","S","T","U","V","W","X",
                        "Y","Z"];
    
    function convertToFrom10(number, base) {
        var temp = 0;
        var result = "";
        var pot = 0;
        nolength = number.toString().length;
        for(var i = nolength; i>=0; i--){
            pot = Math.pow(base, i);
            temp = Math.floor(number / pot);
            number -= temp * pot;
            result += chars62[temp];
        }
        var zeroi = result.charAt(0);
        while(zeroi=='0'){
            result = result.substring(1);
            zeroi = result.charAt(0);
        }
        return result;
    }
  
    function to10(no, from){
      var nout = 0;
      nolen = no.toString().length;
      for(i=0; i<nolen;i++){
        c = no.charCodeAt(i);
        if(96<c && c<123){
          c = Number(c)-97+10;
        }else if (64<c&&c<91){
          c = Number(c) - 65+10+26;
        }else if(47<c&&c<58){
          c= Number(c)-48;
        }
        nout = nout*from +c;
      }
      return nout;
    }
}








