

var XTR = "094FvLzvu7-_u7_kuejs6em_6-TtvOu55bu57eu55O3k7rwD090";

// **************************************************************
// ENCODER
// **************************************************************

var trix = trix || {};

(function () {

    function Encoder() {
        this.inizialize();
    }
    var p = Encoder.prototype;

    // static methods

    Encoder.END_OF_INPUT = -1;

    Encoder.base64Chars = new Array(
		    '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', '0', '1', '2', '3',
		    '4', '5', '6', '7', '8', '9', '+', '/'
		);

    if (!Encoder.reverseBase64Chars) {
        Encoder.reverseBase64Chars = new Array();
        for (var i = 0; i < Encoder.base64Chars.length; i++) {
            Encoder.reverseBase64Chars[Encoder.base64Chars[i]] = i;
        }
    }

    // instance methods

    p.base64Str = "";

    p.base64Count = 0;

    p.inizialize = function () {
        this.base64Str = "";
        this.base64Count = 0;
    }

    p.setBase64Str = function (str) {
        this.base64Str = str;
        this.base64Count = 0;
    }

    p.readBase64 = function () {
        if (!this.base64Str) return Encoder.END_OF_INPUT;
        if (this.base64Count >= this.base64Str.length) return Encoder.END_OF_INPUT;
        var c = this.base64Str.charCodeAt(this.base64Count) & 0xff;
        this.base64Count++;
        return c;
    }

    p.encodeBase64 = function (str) {
        this.setBase64Str(str);
        var result = '';
        var inBuffer = new Array(3);
        var lineCount = 0;
        var done = false;
        while (!done && (inBuffer[0] = this.readBase64()) != Encoder.END_OF_INPUT) {
            inBuffer[1] = this.readBase64();
            inBuffer[2] = this.readBase64();
            result += (Encoder.base64Chars[inBuffer[0] >> 2]);
            if (inBuffer[1] != Encoder.END_OF_INPUT) {
                result += (Encoder.base64Chars[((inBuffer[0] << 4) & 0x30) | (inBuffer[1] >> 4)]);
                if (inBuffer[2] != Encoder.END_OF_INPUT) {
                    result += (Encoder.base64Chars[((inBuffer[1] << 2) & 0x3c) | (inBuffer[2] >> 6)]);
                    result += (Encoder.base64Chars[inBuffer[2] & 0x3F]);
                } else {
                    result += (Encoder.base64Chars[((inBuffer[1] << 2) & 0x3c)]);
                    result += ('=');
                    done = true;
                }
            } else {
                result += (Encoder.base64Chars[((inBuffer[0] << 4) & 0x30)]);
                result += ('=');
                result += ('=');
                done = true;
            }
            lineCount += 4;
            if (lineCount >= 76) {
                //result += ('\n');
                lineCount = 0;
            }
        }
        return result;
    }

    p.readReverseBase64 = function () {
        if (!this.base64Str) return Encoder.END_OF_INPUT;
        while (true) {
            if (this.base64Count >= this.base64Str.length) return Encoder.END_OF_INPUT;
            var nextCharacter = this.base64Str.charAt(this.base64Count);
            this.base64Count++;
            if (Encoder.reverseBase64Chars[nextCharacter]) {
                return Encoder.reverseBase64Chars[nextCharacter];
            }
            if (nextCharacter == 'A') return 0;
        }
        return this.END_OF_INPUT;
    }

    p.ntos = function (n) {
        n = n.toString(16);
        if (n.length == 1) n = "0" + n;
        n = "%" + n;
        return unescape(n);
    }

    p.decodeBase64 = function (str) {
        this.setBase64Str(str);
        var result = "";
        var inBuffer = new Array(4);
        var done = false;
        while (!done && (inBuffer[0] = this.readReverseBase64()) != Encoder.END_OF_INPUT
	        && (inBuffer[1] = this.readReverseBase64()) != Encoder.END_OF_INPUT) {
            inBuffer[2] = this.readReverseBase64();
            inBuffer[3] = this.readReverseBase64();
            result += this.ntos((((inBuffer[0] << 2) & 0xff) | inBuffer[1] >> 4));
            if (inBuffer[2] != Encoder.END_OF_INPUT) {
                result += this.ntos((((inBuffer[1] << 4) & 0xff) | inBuffer[2] >> 2));
                if (inBuffer[3] != Encoder.END_OF_INPUT) {
                    result += this.ntos((((inBuffer[2] << 6) & 0xff) | inBuffer[3]));
                } else {
                    done = true;
                }
            } else {
                done = true;
            }
        }
        return result;
    }

    p.base64UrlEncode = function (str) {
        console.log(str);
        str = this.encodeBase64(str);
        console.log(str);
        console.log(this.decodeBase64(str));
        str = str.replace(new RegExp('=', 'g'), '');
        str = str.replace(new RegExp('\\+', 'g'), '-');
        str = str.replace(new RegExp('/', 'g'), '_');
        return str;
    }

    p.base64UrlDecode = function (encoded) {
        try {
            var r = encoded.length % 4;
            if (r == 1)
                encoded += "=";
            else if (r == 2)
                encoded += "==";
            else if (r == 3)
                encoded += "=";
            //
            encoded = encoded.replace(new RegExp('-', 'g'), '+');
            encoded = encoded.replace(new RegExp('_', 'g'), '/');
            //
            return this.decodeBase64(encoded);
        }
        catch (e) {
        }
        return "";
    }

    trix.Encoder = Encoder;
}());

// ************************


var App = {  

    Env: {
  "DomainFull": "maestrosacademy.samsung.it",
  "Domain": "samsung.it",
  "BaseUrl": "https://maestrosacademy.samsung.it/",
  "HomeUrl": "https://maestrosacademy.samsung.it/",
  "BaseUrlSocial": "https://maestrosacademy.samsung.it/social/",
  "IsSecureConnection": true,
  "LocaleList": [
    "it_IT",
    "en_EN"
  ],
  "Locale": "it_IT",
  "ApplicationName": "MaestrosAcademy",
  "CurrentApplication": "ww",
  "IsCanvas": false
},

    SessionCookie: null,

    UserSession: null,
    
    UrlRewrite: function(url, additionalArgs, ignoreCookieless) {
        var idx = url.indexOf("?");
        var pre = idx == -1 ? url : url.substr(0,idx);
        var qs  = idx == -1 ? "" : url.substr(idx + 1); 
        var bag = {};
        var tokens = qs.split("&");
        for (var i=0;i<tokens.length;i++)
        {
            var tt = tokens[i].split("=");
            if (tt.length>1)
                bag[tt[0]] = decodeURIComponent(tt[1]);
        }
        if (additionalArgs)
        {
            for (var k in additionalArgs)
                bag[k] = additionalArgs[k];
        }
        if (App.SessionCookie && !ignoreCookieless)
            bag[App.SessionCookie.Name] = App.SessionCookie.Value;
        else
            delete bag["wlfsid"];
        //
        qs = "";
        for (var k in bag)
        {
            qs += (qs.length>0 ? "&" : "");
            qs += k+"="+encodeURIComponent(bag[k]);
        }
        var ret = pre+(qs.length>0 ? "?" : "")+qs;
        return ret;
    },

    UrlRewrite1: function(url, additionalArgs, ignoreCookieless) {
        var idx = url.indexOf("?");
        var pre = idx == -1 ? url : url.substr(0,idx);
        var qs  = idx == -1 ? "" : url.substr(idx + 1); 
        var bag = {};
        var tokens = qs.split("&");
        for (var i=0;i<tokens.length;i++)
        {
            var tt = tokens[i].split("=");
            if (tt.length>1)
                bag[tt[0]] = decodeURIComponent(tt[1]);
        }
        if (additionalArgs)
        {
            for (var k in additionalArgs)
                bag[k] = additionalArgs[k];
        }
        if (App.SessionCookie && !ignoreCookieless)
            bag[App.SessionCookie.Name] = App.SessionCookie.Value;
        else
            delete bag["wlfsid"];
        //
        qs = "";
        for (var k in bag)
        {
            qs += (qs.length>0 ? "&" : "");
            qs += k+"="+encodeURIComponent(bag[k]);
        }
        //
        if (qs.length==0)
            return pre;
        //
        var ret = pre+(qs.length>0 ? "?" : "")+qs;
        return ret;
    },

    DoLogout: function(args,callBack) {
        var proxiedCallBackName  = null;
        var isCallBack           = (callBack!=null);
        //
        if (isCallBack)
        {
            proxiedCallBackName = "DoLogout_"+(new Date()).getTime();
            args["redirect_url"] = "javascript:parent."+proxiedCallBackName;
            //
            window[proxiedCallBackName] = function(data) {
                $("#"+proxiedCallBackName).remove();
                if (typeof(callBack)=="string")
                {
                    $("#"+externalIdSWF).externalInterface({
                        method: 'SwfExternalCallBack',
                        args: [callBack, data]
                    });
                }
                else
                {
                    callBack(data);
                }
            }
            // apro iframe
            var iframe = $("<iframe width='1' height='1' style='visibility:hidden'></iframe>");
            iframe.attr("id",proxiedCallBackName);
            $("body").append(iframe);
            iframe.attr("src",App.UrlRewrite(App.Env.BaseUrlSocial+"dialogs/logout/",args));
        }
        else
        {
            document.location.href = App.UrlRewrite(App.Env.BaseUrlSocial+"dialogs/logout/",args);
        }
    },

    DoLogin: function(args, callBack) {
        var loginWin             = null;
        var proxiedCallBackName  = null;
        var windowThread         = 0;
        var isCallBack           = (callBack!=null);
        //
        if (isCallBack)
        {
            proxiedCallBackName = "DoLogin_"+(new Date()).getTime();
            args["redirect_url"] = "javascript:opener."+proxiedCallBackName;
            //
            window[proxiedCallBackName] = function(data) {
                clearInterval(windowThread);
                if (loginWin)
                    loginWin.close();
                loginWin = null;
                if (typeof(callBack)=="string")
                {
                    $("#"+externalIdSWF).externalInterface({
                        method: 'SwfExternalCallBack',
                        args: [callBack, data]
                    });
                }
                else
                {
                    callBack(data);
                }
            }
            // apro la win
            loginWin = window.open(App.UrlRewrite1(App.Env.BaseUrlSocial+"dialogs/login/",args),
                                    "loginWin",'menubar=0, status=0, location=0, resizable=1, width=800,height=600',true);
            windowThread = setInterval(function(){
                    if (loginWin!=null)
                        if (loginWin.closed)
                            window[proxiedCallBackName](null);
                },100);
        }
        else
        {
            document.location.href = App.UrlRewrite1(App.Env.BaseUrlSocial+"dialogs/login/",args);
        }
    },


    DoFbShare: function(args, callBack) {
        //alert(args);
        args.method = 'feed';
        if (args.actions)
            if (typeof(args.actions)!="string")
                args.actions = JSON.stringify(args.actions);
        //
        FB.ui(args, function(response) {
                    if (callBack) {
                        if (typeof(callBack)=="string") {
                            $("#"+externalIdSWF).externalInterface({
                                method: 'SwfExternalCallBack',
                                args: [callBack, response]
                            });
                        }
                        else {
                            callBack(response);
                        }
                   }
              });
    },

    // DA FINIRE
    DoFbPermission: function(args,callBack) {
        var perms = "";
        if (args.perms)
        {
            for (var i=0;i<args.perms.length;i++)
                perms += (perms.length>0 ? "," : "")+args.perms[i];
        }
        alert(perms);
        /*
        FB.login(function(response){
        },{scope: perms});
        */
        FB.ui({method: "permissions.request", "perms":perms},function(response){
        });
    }



    /*
    // Crea una callback per flash
    CreateCallBackForSWF:function( callBackId ) {
        window[callBackId] = function(data) {
               $("#"+externalIdSWF).externalInterface({
                    method: 'SwfExternalCallBack',
                    args: [callBackId, data]
                });
            }
    }
    */
}
