﻿// JScript File
glPlazaRedirect = "";
var BLLGeoLocalizacion = Class.create({
    initialize: function(sInstanceName, sPlaza) {
        this.instanceName = sInstanceName;
        this.timerID = null;
        this.delay = 100;        //cada 100 milisegundos verifica que la variable ya tenga un valor.
        this.maxdelay = 4000;    //(2 seg) tiempo máximo para abortar el ciclo en caso de que la variable no regrese valor.
        this.countdelay = 0;
        this.urlredirect = "";
        this.bllCookie = new BLLCookie();
        this.idPlaza = null;
        this.pageDefault = "FotoTienda.aspx";
        //this.host = "http://" + window.location.host + "/FotoTiendaWeb/";
        this.host = "";
        this.host = this.host.toLowerCase();
        this.parameterNamePlaza = 'plaza';
        this.plazasValidas = ["101", "102", "103"];
        if (sPlaza != null && sPlaza != '') {
            var sIdPlaza = this.getIdPlazaByPlaza(sPlaza);
            if (this.validaPlazaByIdPlaza(sIdPlaza)) {
                this.idPlaza = sIdPlaza;
                this.bllCookie.setCookiePlaza(this.idPlaza, 30);
            }
        }

        this.idPlaza = this.bllCookie.getCookiePlaza();
        if (this.idPlaza == null || typeof (this.idPlaza) == "undefined" || this.idPlaza == "") {
            getPlazaRedirect("fototienda");
            this.startTheTimer();
        }
        else {
            this.redireccionar(this.idPlaza.toString());
        }

    }
    , startTheTimer: function() {
        if (glPlazaRedirect != "" || this.countdelay >= this.maxdelay) {
            clearTimeout(this.timerID);
            // Ya esta disponible el resultado desde aqui se puede mandar llamar a otra función.
            if (glPlazaRedirect != "" && glPlazaRedirect != "NotFound") {
                this.idPlaza = glPlazaRedirect;
                if (navigator.cookieEnabled) {
                    this.bllCookie.setCookiePlaza(this.idPlaza, 30);
                    this.redireccionar(this.idPlaza.toString());
                }
                else {
                    this.redireccionar("102");
                }
            }
        }
        else {
            this.timerID = self.setTimeout(this.instanceName + "." + "startTheTimer()", this.delay);
            this.countdelay += this.delay;
        }
    }

    , redireccionar: function(sIdPlazaParm) {
        window.location.href = this.host + this.getRedirectPageByIdPlaza(sIdPlazaParm);
    }

    , getIdPlazaByPlaza: function(sPlazaParm) {
        var sResult = "";
        sPlazaParm = sPlazaParm.toLowerCase();
        switch (sPlazaParm) {
            case "elnorte":
                sResult = "101";
                break;
            case "reforma":
                sResult = "102";
                break;
            case "mural":
                sResult = "103";
                break;
        }
        return sResult;
    }

     , getPlazaById: function(sIdPlazaParm) {
         var sResult = "";
         switch (sIdPlazaParm) {
             case "101":
                 sResult = "elnorte";
                 break;
             case "102":
                 sResult = "reforma";
                 break;
             case "103":
                 sResult = "mural";
                 break;
         }
         return sResult;
     }

     , getRedirectPageByIdPlaza: function(sIdPlazaParm) {
         var sResult = "";
         switch (sIdPlazaParm) {
             case "101":
                 sResult = this.pageDefault + "?" + this.getParameter(this.parameterNamePlaza, this.getPlazaById(sIdPlazaParm));
                 break;
             case "102":
                 sResult = this.pageDefault + "?" + this.getParameter(this.parameterNamePlaza, this.getPlazaById(sIdPlazaParm));
                 break;
             case "103":
                 sResult = this.pageDefault + "?" + this.getParameter(this.parameterNamePlaza, this.getPlazaById(sIdPlazaParm));
                 break;
         }
         return sResult;
     }

     , getParameter: function(sFieldNameParm, sFieldValueParm) {
         var sResult = "";
         sResult = sFieldNameParm + "=" + sFieldValueParm;
         return sResult;
     }
     , validaPlazaByIdPlaza: function(sIdPlazaParm) {
         if (this.plazasValidas.indexOf(sIdPlazaParm) != -1)
             return true;
         else
             return false;
     }

     , getPlazaFromQueryString: function(qs) {
         var qsParam = new Array();
         if (window.location.search) {
             var querys = window.location.search.substring(1);
             var param = querys.split('&');
             for (var i = 0; i < param.length; i++) {
                 var pos = param[i].indexOf('=');
                 if (pos > 0) {
                     var key = param[i].substring(0, pos);
                     var val = param[i].substring(pos + 1);
                     qsParam[key] = val;
                 }
             }
             querystring = qsParam[qs];
             return querystring;
         }
         return null;
     }

});


