var Szcch = {
    currentRoom: null,
    clientPath: "szcch.swf",
    clientWrapPath: "szcch_game.swf",
    gamePath: "http://resource.17play8.com/szcch/hall/",
    createFlash: function (/* URL */url) {
        var r = Szcch.computeFlashRect();
        var isIE8 = !!window.XDomainRequest && !!document.documentMode;
        var movieParams = {
            src: url,
            base: Szcch.gamePath,
            AllowScriptAccess: "always",
            allowFullScreen: "true",
            wmode: isIE8 ? "window" : "opaque", //wmode : "opaque",
            menu: "false",
            // scale : "noscale",
            allownetworking: "true",
            quality: "high"
        };

        var embedAttributes = {
            type: "application/x-shockwave-flash"

        };
        var objectParams = {
            movie: url
        };
        var objectAttributes = {
            classid: "clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"
        };
        var movie = {
            tag: "EMBED",
            styles: {
                position: "relative",
                left: "1px",
                top: "1px",
                width: r.width + "px",
                height: r.height + "px"
            },
            attributes: {
                name: "szcch_game",
                id: "szcch_game",
                width: r.width,
                height: r.height
            },
            children: []
        };
        Szcch.clone(movieParams, movie.attributes);
        Szcch.clone(embedAttributes, movie.attributes);
        var layer3 = {
            tag: "DIV",
            styles: {
                backgroundColor: "#ffffff",
                position: "relative",
                top: "1px",
                //left : "1px",
                left: "0",
                margin: "0 auto",
                width: (r.width + 1) + "px",
                height: (r.height + 2) + "px"
            },

            children: [movie]
        };
        var layer2 = {
            tag: "DIV",
            styles: {
                backgroundColor: "#ffffff",
                position: "relative",
                top: "1px",
                left: "0px",
                margin: "0 auto",
                width: (r.width + 4) + "px",
                height: (r.height + 4) + "px"
            },
            children: [layer3]
        };
        var layer1 = {
            tag: "DIV",
            styles: {
                backgroundColor: "#ffffff",
                position: "relative",
                top: r.top + "px",
                margin: "0 auto",
                left: "0px",
                width: (r.width + 6) + "px",
                height: (r.height + 6) + "px"

            },
            children: [layer2],
            attributes: {
                id: 'flashcontent'
            }
        };
        return Szcch.toDOM(layer1);
    },
    randomString: function (/* int */size) {
        var result = [];
        for (var i = 0; i < size; i++)
            result.push(String.fromCharCode("a".charCodeAt(0)
					+ Math.floor(Math.random() * 26)));
        return result.join("");
    },
    activeDialog: null,
    defaultLayerStyle: {
        //position : "absolute",
        margin: "0 auto",
        backgroundColor: "#104a4b",
        width: "982px",
        height: "590px"
    },
    flash: {
        width: 982,
        height: 590
    },
    computeFlashRect: function () {
        var rect = {
            top: 0,
            left: 0,
            width: Szcch.flash.width,
            height: Szcch.flash.height
        };
        rect.left = (994 - rect.width) / 2;
        return rect;
    },

    clone: function (/* object */src, /* object */dst) {
        for (var name in src) {
            dst[name] = src[name];
        }
    },

    toDOM: function (/* JSON */js_model) {
        if (typeof (js_model) == "string") {
            return document.createTextNode(js_model);
        }
        var dom = document.createElement(js_model.tag);
        if (js_model.attributes) {
            for (var name in js_model.attributes) {
                dom.setAttribute(name, js_model.attributes[name]);
            }
        }
        if (js_model.styles) {
            for (var name in js_model.styles) {
                dom.style[name] = js_model.styles[name];
            }
        }
        if (js_model.children) {
            for (var i in js_model.children) {
                dom.appendChild(Szcch.toDOM(js_model.children[i]));
            }
        }
        js_model.view = dom;
        return dom;
    },

    /* username, siteid, game_id, room_id, host, port, referrer */
    newFrameEnterRoom: function (
		username, /* param@String 接入玩家登陆账号 */
		siteid,   /* param@String 接入玩家域名 */
		game_id,  /* param@String 游戏ID，通过接口获取得到*/
		room_id,  /* param@String 要进入游戏房间ID,通过接口获取得到 */
		host,     /* param@String 要进入的游戏连接IP或域名 */
		port,     /* param@int 要进入的游戏连接的端口号 */
		referrer,  /* param@String 游戏返回按钮跳转回到的页面地址，如'http://www.360song.com' */
		recharge  /* param@String 游戏充值跳转充值地址，如'http://www.360song.com' */) {

        
        var gameScreen = document.getElementById("gameScreen");
        var room = document.createElement("DIV");
        Szcch.clone(Szcch.defaultLayerStyle, room.style);
        var args = [];
        args.push("game=" + encodeURIComponent(game_id));
        args.push("desk=" + encodeURIComponent(room_id));
        args.push("player=" + encodeURIComponent(username));
        args.push("hall=" + encodeURIComponent(siteid));
        args.push("referrer=" + encodeURIComponent(referrer));
        args.push("port=" + port);
        args.push("base=" + Szcch.gamePath);
        args.push("debug=off");

        if (recharge == null) recharge = "http://www.17play8.com/hall/recharge/alipay";

        args.push("recharge=" + encodeURIComponent(recharge));

        args.push("host=" + host);

        /*
        * playURL:Szcch.gamePath + Szcch.clientWrapPath + "?" + args.join("&").
        * 将表单提交 POST 方式
        *
        * 这里修改 下面三句 包括  return; 
        * 
        */ 
        $j("#playgameform input[name='urlxx']").val(Szcch.gamePath + Szcch.clientWrapPath + "?" + args.join("&"));
        $j("#playgameform").submit();
        return;

        room.appendChild(Szcch.createFlash(Szcch.gamePath + Szcch.clientWrapPath + "?" + args.join("&")));
        if (Szcch.currentRoom != null) {
            Szcch.currentRoom.parentNode.removeChild(Szcch.currentRoom);
        }
        Szcch.currentRoom = room;

        //gameScreen.appendChild(room);
        while (document.body.lastChild != null) {
            document.body.removeChild(document.body.lastChild);
        }
        document.body.style.backgroundColor = "#104a4b";
        document.body.appendChild(room);
        room.focus();
    },

    /* username, siteid, game_id, room_id, host, port, referrer */
    oldFrameEnterRoom: function (
		username, /* param@String 接入玩家登陆账号 */
		siteid,   /* param@String 接入玩家域名 */
		bootfile,  /* param@String 游戏ID，通过接口获取得到*/
		room_id,  /* param@String 要进入游戏房间ID,通过接口获取得到 */
		host,     /* param@String 要进入的游戏连接IP或域名 */
		port,     /* param@int 要进入的游戏连接的端口号 */
		referrer  /* param@String 游戏返回按钮跳转回到的页面地址，如'http://www.360song.com' */) {
        var gameScreen = document.getElementById("gameScreen");
        var room = document.createElement("DIV");
        Szcch.clone(Szcch.defaultLayerStyle, room.style);
        var args = [];
        args.push("game=" + encodeURIComponent(bootfile));
        args.push("desk=" + encodeURIComponent(room_id));
        args.push("player=" + encodeURIComponent(username));
        args.push("hall=" + encodeURIComponent(siteId));
        args.push("referrer=" + encodeURIComponent(referrer));
        args.push("base=" + Szcch.gamePath);
        args.push("host=" + host);
        args.push("port=" + port);
        args.push("debug=off");
        args.push("password=test");

        room.appendChild(Szcch.createFlash(Szcch.gamePath + Szcch.clientPath + "?" + args.join("&")));
        if (Szcch.currentRoom != null) {
            Szcch.currentRoom.parentNode.removeChild(Szcch.currentRoom);
        }
        Szcch.currentRoom = room;

        //gameScreen.appendChild(room);
        while (document.body.lastChild != null) {
            document.body.removeChild(document.body.lastChild);
        }
        document.body.style.backgroundColor = "#104a4b";
        document.body.appendChild(room);

        room.focus();
    },

    inArray: function (array, obj) {
        for (var i in array) {
            if (array[i] === obj) {
                return true;
            }
        }
        return false;
    },

    /* game_id, room_id, username, siteid, host, port */
    enterRoom: function (/* String */bootfile, /* String */roomid, /* String */username, host, port, siteId, gameid, referrer, recharge) {

        var newFrameTag = ['lord', 'ZhaJH', 'lord2', 'cchBullFight', 'mahjong'];

        if (Szcch.inArray(newFrameTag, gameid)) {
            Szcch.newFrameEnterRoom(username, siteId, gameid, roomid, host, port, referrer, recharge);
        } else {
            Szcch.oldFrameEnterRoom(username, siteId, bootfile, roomid, host, port, referrer);
        }
    }
};
