//chamap www
var search_query = "";
var GLanguage = "";


var cm_citem = new Array();
var cm_citem_cnt = 0;
var cm_mkmngcnt = 0;
var cm_mkmngid = 0;
var mc_Chara = {};
var cm_mkmng = null;
var cm_xr = null;
var cnlinf = {};

var CM = {
    c: 0,
    n: "",
    f: "",
    r: 0,
    mr: 0,
    lmr: 0,
    i: 0,
    u: 0,
    m: "",
    Mk:null,
    PMk:null,
    Timer: {
        Map: 0,
        User: 0,
        Main: 0,
        Scroll: 0
    },
    Cst: {
        PlayIntarval: 30000,
        UserIntarval: 1000 * 270
    }
}


var Stage = {
    Loaded: false,
    Unload: function() {
        if (cm_xr) cm_xr.unload();
        if (CM.u > 0) User.Sender("unload");
        GUnload();
    },
    Make: function() {
        IframeURL.init();
        FrontPage.init();
        l();
        function l() {
            if (
                    $('f_msg') &&
                    $('idx_main')
               ) {
                MsgBox.show("Loading GoogleMap...", -1, { title: "Loading ChaMap..." });
                setTimeout(Stage.GMap, 1);
            } else { setTimeout(l, 100); }
        }
    },
    GMap: function() {
        if (!GBrowserIsCompatible()) {
            location.href = "/pages/sorry.html";
            return;
        }
        if (!InitializeGmap()) MsgBox.show("It failed in reading the Google map.")

        GEvent.addListener(cm_map, 'load', function() {
            MsgBox.show("Loading systems...", -1, { title: "Loading ChaMap..." });
            Stage.System();
        });
        GEvent.addListener(cm_map, 'dragstart', Ctls.Map.TStop);
        GEvent.addListener(cm_map, 'dblclick', Ctls.Map.TStop);
        GEvent.addListener(cm_map, 'click', function(o, p) { if (p && !CM.Mk.isHidden()) CM.Mk.setLatLng(p); Ctls.Map.TStop(); Ctls.Msg.Focus(); });

        cm_map.setCenter(new GLatLng(36.738884, 137.109375), 5);

        cm_mkmng = new MarkerManager(cm_map);

        var d = new GIcon();
        d.image = "/images/icons/write.gif";
        d.iconSize = new GSize(25, 48);
        d.iconAnchor = new GPoint(12, 48);
        d.shadow = "/images/icons/write_shadow.png";
        d.shadowSize = new GSize(50, 48);
        d.infoWindowAnchor = new GPoint(12, 0);
        CM.Mk = new GMarker(cm_map.getCenter(), { icon: d, draggable: true, bouncy: true });

        var p = new GIcon();
        p.image = "/images/icons/paste.gif";
        p.iconSize = new GSize(25, 48);
        p.iconAnchor = new GPoint(12, 48);
        p.shadow = "/images/icons/write_shadow.png";
        p.shadowSize = new GSize(50, 48);
        p.infoWindowAnchor = new GPoint(12, 0);
        CM.PMk = new GMarker(cm_map.getCenter(), { icon: p });
        GEvent.addListener(CM.PMk, 'click', Paste.Click);

        MashMaps.Init();

        $('message').onkeypress = Ctls.Msg.onKeyPress;
        $('chat').onmouseover = Stage.E.ScCatch;
        $('chat').onmouseout = Stage.E.ScRelease;

        cm_xr = new XMLRequest();
    },
    System: function() {
        GMark.AClose();
        Player.MuteExe(true);
        jsonp("/req/?rx=i&m=s&r=" + encodeURIComponent(document.referrer), "systemid");
    },
    CB_System: function(v) {
        var c = v.icons;
        for (var i = 0; i < c.length; i++) {
            var d = new GIcon();
            d.image = "/images/icons/" + (isIE ? "ie/" : "") + c[i].f;
            d.iconSize = new GSize(c[i].w, c[i].h);
            d.iconAnchor = new GPoint(parseInt(c[i].w / 2), c[i].h);
            d.infoWindowAnchor = new GPoint(parseInt(c[i].w / 2), 0);
            mc_Chara[c[i].r] = { n: c[i].n, f: c[i].f, c: c[i].c, i: d };
        }

        if (location.href.match(/http:\/\/www\.chamap\.net\/channel\/([^\/$|.]+)/)) {
            $("cm_head").style.height = "70px"; View.P.top = false; Disp.Resize();
            Stage.Channel(encodeURIComponent(RegExp.$1));
        } else {
            Stage.ShowHTML();
        }
    },
    ShowHTML: function() {
        document.title = 'Chamap - Enjoy Geo Communication!';
        Ctls.Map.Zoom(false);
        Ctls.Map.Stop();
        if (!View.P.top) View.Top(f);
        else f();
        function f() {
            GMark.AClose();
            IframeURL.show();
            MsgBox.close();
            $('w_f').style.display = 'none';
        }
    },
    Channel: function(c) {
        MsgBox.show("Loading Channel...", -1, { title: "Loading ChaMap..." });
        IframeURL.close();
        jsonp("/req/?rx=i&c=" + c + ReqID.Channel(), "channelid");
    },
    CB_Channel: function(v) {
        cnlinf = v;
        Stage.Loaded = false;
        Ctls.Map.Zoom(false);
        Ctls.Map.Start(true);

        Funcs.ClearParams();
        document.title = 'Chamap - ' + cnlinf.n;
        $('cm_cnlname').innerHTML = cnlinf.n;
        $('cm_cnlurl').value = "http://www.chamap.net/channel/" + cnlinf.r + "/";

        if (GLanguage!="") Stage.LangMake();
        else Stage.LangSelect();
    },
    LangSelect: function() {
        var s = "";
        s += "<div style='text-align:center;font-size:30px;margin-top:50px;'>Select your Language<br />";
        s += "<select style='font-size:30px;color:Red;' id='dstlanguage'></select></div>";
        FrontPage.show("<span>Channel&nbsp;:&nbsp;</span>" + cnlinf.n, s, { 1: "Ok", 2: "Top Page" }, f);
        MsgBox.close();

        GLanguage = "";
        var l = $("dstlanguage");
        var d = Cookies.Read("GLanguage");
        l.options[0] = new Option("- Select Language! -", "");
        for (var i in google.language.Languages) {
            if (google.language.Languages[i] != "" && google.language.isTranslatable(google.language.Languages[i])) {
                var o = new Option(i.replace("_", " "), google.language.Languages[i]);
                if (google.language.Languages[i] == d) o.selected = true;
                l.options[l.options.length] = o;
            }
        }

        function f(v) {
            if (v == 1) {
                GLanguage = l.options[l.selectedIndex].value;
                Cookies.Write("GLanguage", GLanguage, 7);
                if (GLanguage == "") {
                    MsgBox.show("Please select your language.", null);
                    return false;
                }
                Stage.LangMake();
                return true;
            } else Stage.ShowHTML();
        }
        
    },
    LangMake: function() {

        Lang.Create.Main(GLanguage);

        if (View.P.top) View.Top(fl);
        else fl();

        function fl() {
            if (Lang.Process) setTimeout(fl, 100);
            else f();
        }

        function f() {
            //lang draw
            $("ln_cnl").innerHTML = Lang.T.Channel;
            $("ln_cnlurl").innerHTML = Lang.T.CnlURL;
            $("ln_chkfile").innerHTML = Lang.T.ChkFile;
            $("ln_sound").innerHTML = Lang.T.Sound;
            $("ln_automove").innerHTML = Lang.T.AutoMove;
            $("ln_logout").innerHTML = Lang.T.Logout;
            $("ln_autozoom").innerHTML = Lang.T.Zoom + ", " + Lang.T.MapType;

            $("ln_Picts").innerHTML = Lang.T.Picts;
            $("ln_OnMap").innerHTML = Lang.T.OnMap;
            $("ln_CurLoco").innerHTML = Lang.T.CurLoco;
            $("formimg").innerHTML = Lang.T.Show + " " + Lang.T.Form;

            $("chat_head").innerHTML = Lang.T.ChatHead;
            $("ln_imgfile").innerHTML = Lang.T.ImgFile;

            GMark.AClose();

            if (cnlinf.ps == 1) {
                MsgBox.close();
                FRONT.PassWord();
            } else {
                CM.m = getGMapTypes(GmapTypeDef[cnlinf.mt].o);
                if (CM.m == "DEFAULT") {
                    MashMaps.load(cm_map);
                    MashSearch.load(cm_map);
                    $('pebtn').style.display = "inline-block";
                    $("ln_CurLoco").style.display = "inline-block";
                }
                MTypeChanger(CM.m, GmapTypeDef[cnlinf.mt].o);
                MashMap_Set(cnlinf.a.split('|'));
                User.Cnt = cnlinf.u;

                MsgBox.show(Lang.T.Loading + "<br />" + cnlinf.n + "...", -1, { title: "ChaMap " + Lang.T.Loading });
                cm_map.setCenter(new GLatLng(cnlinf.lt, cnlinf.ln), cnlinf.z);
                CM.c = cnlinf.r;
                CM.Timer.Main = 0;
                Msg.Req();
                l();
            }
            function l() {
                if (CM.Timer.Main != 0) {
                    if (IframeURL.IsLoad) FRONT.CharaSelect();
                    else FRONT.Channel(false);
                } else setTimeout(l, 100);
            }
        }
    },
    EditChannel: function() {
        IframeURL.IsLoad = 1;
        $('idx_main').src = "/pages/create/?c=" + cnlinf.r;
        if (Stage.Loaded) Funcs.Logout();
        else Stage.ShowHTML();
    },
    E: {
        ScCatch: function() { $('chat').onscroll = Ctls.Scroll.Draw; },
        ScRelease: function() { $('chat').onscroll = null; }
    }
}


var FRONT = {
    Channel: function(v) {
    var b = v ? { 3: Lang.T.Back, 4: Lang.T.Logout} : { 1: Lang.T.Next, 2: Lang.T.Back };
    var d = new Date(cnlinf.d*1000).toLocaleString();
    
        var s = "";
        s += "<div class='small_right'>" + Lang.T.Stage + ":<b>" + GmapTypeDef[cnlinf.mt].n + "</b>&nbsp;&nbsp;&nbsp;" + Lang.T.Owner + ":<b>" + cnlinf.o + "</b>&nbsp;";

        s += "[<a href='javascript:void(0);' onclick='Stage.EditChannel();return false;'>" + Lang.T.Admin + "</a>]";

        s += "&nbsp;&nbsp;&nbsp;" + Lang.T.Date + "：<b>" + d + "</b></div>";
        s += Lang.T.CnlExp + ":<div class='desc'>" + cnlinf.m + "</div>";
        if (cnlinf.a) s += Lang.T.CnlAPI + ":<div class='desc2'>" + MashMaps_ApiList(cnlinf.a.split('|'), false) + "</div>";
        FrontPage.show("<span>" + Lang.T.Channel + "&nbsp;:&nbsp;</span>" + cnlinf.n, s, b, f);
        MsgBox.close();
        function f(v) {
            switch (v) {
                case 1: FRONT.CharaSelect(); return false; break;
                case 2: Stage.LangSelect(); return false; break;
                case 3: return true; break;
                case 4: Funcs.Logout(); return true; break;
                default: break;
            }
            return false;
        }
    },
    CharaSelect: function() {
        CM.i = 0;
        if ($("front_image").style.display == 'block') {
            if (McSlider.active) McSlider.complete();
            $("front_image").style.display = 'none';
        }
        var t = 0, s = "";
        s += Lang.T.Chara;
        s += "<table id='selchara'><tr>";
        for (var i in mc_Chara) {
            s += "<td><div onclick='FRONT.CharaName(" + i + ")'>";
            s += "<img id='ico" + i + "' src='/images/icons/" + mc_Chara[i].f + "' /><br />";
            s += mc_Chara[i].n;
            s += "</div></td>";
            t++;
            if (t > 9) { s += "</tr><tr>"; t = 0; }
        }
        s += "</tr></table>";
        s += "<div class='small' align='right'>Icon By <A href='http://1kb-sozai.com/' target='_blank'>1Kb Web Sozai.</A></div>";

        FrontPage.show("<span>" + Lang.T.Channel + "&nbsp;:&nbsp;</span>" + cnlinf.n, s, { 1: Lang.T.Back }, f);
        function f(v) {
            if (IframeURL.IsLoad) { Stage.ShowHTML(); return true; }
            else { FRONT.Channel(false); return false; }
        }
        MsgBox.close();
    },
    CharaName: function(i) {
        CM.i = i;
        var p = new Image();
        p.src = "/images/icons/" + mc_Chara[i].f;
        $("front_image").style.display = 'block';
        $("front_image").style.width = p.width + "px";
        $("front_image").style.height = p.height + "px";
        $("front_image").src = "/images/icons/" + mc_Chara[i].f;

        var pos = getPosition($("ico" + i));
        $("front_image").style.top = pos.y + "px";
        $("front_image").style.left = pos.x + "px";

        var s = "";
        s += Lang.T.EnterName + "<br />";
        s += "<div style='padding-top:20px;text-align:center;'><div id='icon_folder' style='width:150px;height:150px;border:solid #999 1px;display:inline-block;'></div></div>";
        s += "<div style='font-size:24px;padding-top:20px;text-align:center;'>" + Lang.T.Nickname + "&nbsp;:&nbsp;<input type='text' id='myname' style='font-size:24px;' size='16' maxlength='16' value='" + cnlinf.cn + "'/></div>";

        FrontPage.show("<span>" + Lang.T.Channel + "&nbsp;:&nbsp;</span>" + cnlinf.n, s, { 1: Lang.T.Login, 2: Lang.T.Back }, c);

        $('myname').onkeypress = function(e) { if (IsEnterKey(e)) FRONT.Login(); };

        pos = getPosition($("icon_folder"));
        var pw = $("front_image").width * 3;
        var ph = $("front_image").height * 3;
        pos.x = pos.x + 150 / 2 - pw / 2;
        pos.y = pos.y + 150 / 2 - ph / 2;
        McSlider.start("front_image", { x: pos.x, y: pos.y, w: pw, h: ph });
        $('myname').focus();

        function c(v) { if (v == 1) return FRONT.Login(); else FRONT.CharaSelect(); }
    },
    PassWord: function() {
        var s = '';
        s += Lang.T.NeedPass + "<br />";
        s += "<div style='padding-top:20px;text-align:center;'><img src='/images/msg/key.gif' /></div>";
        s += "<div style='font-size:24px;padding-top:20px;text-align:center;'>" + Lang.T.Password + ":<input type='text' id='ckeyword' style='font-size:24px;' size='16' maxlength='16' /></div>";

        FrontPage.show("<span>" + Lang.T.Channel + "&nbsp;:&nbsp;</span>" + cnlinf.n, s, { 1: Lang.T.Next, 2: Lang.T.Back }, c);
        $('ckeyword').focus();
        $('ckeyword').onkeypress = function(e) { if (IsEnterKey(e)) c(1); };

        function c(v) {
            $("ckeyword").blur();
            if (v == 1) {
                if (isNull($('ckeyword').value)) MsgBox.show(Lang.T.NoPass, null, { func: function() { $("ckeyword").focus(); } });
                else {
                    MsgBox.show(Lang.T.Inquire + "...", -1, { title: Lang.T.ChkPass });
                    jsonp("/req/?rx=i&c=" + cnlinf.r + "&p=" + encodeURIComponent($('ckeyword').value) + ReqID.Channel(), "channelid");
                }
            } else Stage.ShowHTML();
            return false;
        }
    },
    CBPass: function(r, o) {
        if (r) Stage.CB_Channel(o);
        else MsgBox.show(Lang.T.ReTryPass, null,{ title: Lang.T.WrgPass, icon: 'critical', func: function() { $('ckeyword').focus(); $("ckeyword").select(); } });
    },
    Login: function() {
        if (isNull($('myname').value)) {
            MsgBox.show(Lang.T.NoName, null, { func: function() { $("myname").focus(); } });
            return false;
        } else {
            Funcs.StartMap();
            return true;
        }
    }
}



var Funcs = {
    StartMap: function() {
        var s;
        CM.n = $('myname').value;
        CM.u = 0;

        User.Sender();
        CM.Timer.User = setInterval(User.Sender, CM.Cst.UserIntarval)

        if (McSlider.active) McSlider.complete();
        if ($("front_image").width > $("front_image").height) s = "width:80px;margin-top:" + parseInt((80 - 80 * $("front_image").height / $("front_image").width) / 2) + "px;";
        else s = "height:80px;";

        $("cm_mychar").innerHTML = "<img src='/images/icons/" + mc_Chara[CM.i].f + "' style='" + s + "'/><div>" + CM.n + "</div><div style='color:#0d8901;'>" + Lang.T[GLanguage] + "</div>";
        $("front_image").style.display = 'none';
        $('w_f').style.display = 'none';

        GMark.AOpen();
        Player.Stop();
        CM.mr = CM.r;
        if (!GMark.isHiddenInfoWindow()) cm_map.closeInfoWindow();
        Player.MuteExe(false);
        Ctls.Map.Zoom(false);
        Ctls.SendBtn.enabled();
        
        var c;
        var gcl = google.loader.ClientLocation;
        if (gcl && gcl.latitude && gcl.longitude) c = new GLatLng(gcl.latitude, gcl.longitude);
        else c = new GLatLng(cnlinf.lt, cnlinf.ln);

        cm_map.setCenter(c, cnlinf.z);
        CM.Mk.setLatLng(c);
        CM.Mk.show();
        $("chat_w").style.visibility = "visible";

        l(); function l() { if (CM.u > 0) x(); else setTimeout(l, 100); }
        function x() {
            Stage.Loaded = true;
            Ctls.Map.TStop();
            Ctls.Map.Id = 3;
            if (View.P.form) View.Form(function() { if (View.P.chat) View.Chat(a); else a(); })
            else if (View.P.chat) View.Chat(a);
            else a();
            //if (CM.m == "DEFAULT") Koozyt.Load(true);
        }
        function a() {
            $('cm_logout').style.visibility = "visible";
            if ($("chat").lastChild) $("chat").scrollTop = $("chat").lastChild.offsetTop;
            if (!Koozyt.Process) {
                if (Ctls.Map.Id == 3) {
                    Ctls.Map.Start();
                    cm_map.panTo(new GLatLng(cnlinf.lt, cnlinf.ln));
                } else Ctls.Map.TStop();
            }
        }
    },
    ClearParams: function() {
        CM.c = 0;
        CM.n = "";
        CM.r = 0;
        CM.i = 0;
        CM.u = 0;
        CM.mr = 0;
        CM.lmr = 0;
        CM.f = "";
        CM.m = "";

        clearTimeout(CM.Timer.User); CM.Timer.User = 0;
        clearTimeout(CM.Timer.Main); CM.Timer.Main = 0;
        cm_mkmng.clearMarkers();
        cm_mkmng.refresh();
        MashMaps.unload();
        MashSearch.unload();
        cm_map.clearOverlays();

        cm_citem = new Array();
        cm_citem_cnt = 0;
        cm_mkmngcnt = 0;
        cm_mkmngid = 0;

        $('chat').innerHTML = "";
        $('cm_logout').style.visibility = "hidden";
        $('pebtn').style.display = "none";
        $("ln_CurLoco").style.display = "none";

        Ctls.Map._Cursor = true
        cm_map.addOverlay(CM.Mk);
        CM.Mk.hide();
        cm_map.addOverlay(CM.PMk);
        CM.PMk.hide();
    },
    Logout: function() {
        MsgBox.show(Lang.T.AskLout, { 1: Lang.T.Yes, 2: Lang.T.Cancel }, { title: Lang.T.Logout, icon: "question", func: f });
        function f(v) { if (v == 1) Funcs.LogoutExe(); }
    },
    LogoutExe: function() {
        document.title = 'Chamap - Enjoy Geo Communication!';
        MsgBox.show(Lang.T.PrcLout, -1, { title: Lang.T.Logout });

        Ctls.SendBtn.disabled();
        Player.MuteExe(true);

        clearTimeout(CM.Timer.User); CM.Timer.User = 0;
        clearTimeout(CM.Timer.Main); CM.Timer.Main = 0;
        User.Sender("unload");

        User.Cnt--; +
        User.DrawCnt();

        Ctls.Map.Zoom(false);
        Ctls.Map.Stop();

        if (!View.P.form) View.Form(function() { if (!View.P.chat) View.Chat(f); else f(); })
        else if (!View.P.chat) View.Chat(f);
        else f();

        function f() {
            $("chatimg").src = "/images/spacer.gif";
            $("chat_w").style.visibility = "hidden";
            Stage.ShowHTML();
        }
    }

}


var Koozyt = {
    Process: false,
    Load: function(g) {
        var c = false, cl = false, b, p;
        b = "<div id='koozytcredit'>" + Lang.T.PEwifi + "<br />";
        b += "<div><a href='http://www.placeengine.com/en' target='_blank'><img src='/images/tool/placeengine.gif' /></a><br />";
        b += "<a href='javascript:void(0);' onclick='registerLocation(cm_map);return false;'><img src='/images/tool/placeengine_tell.gif' /></a></div></div>";

        if (!g) MsgBox.show(Lang.T.PEneed + b, null, { title: Lang.T.PEDo });
        p = new PEngine({
            onGetLocation: function(x, y, r, info) {
                if (r > 0 && cl == false) {
                    Ctls.Map.Stop();
                    Koozyt.Process = true;
                    CM.Mk.setLatLng(new GLatLng(y, x));
                    cm_map.setCenter(new GLatLng(y, x), 17);
                    var s = "<b>" + info.addr + "</b><br /><br />" + Lang.T.PEtellok;
                    MsgBox.show(s + b, { 1: Lang.T.Yes, 2: Lang.T.No }, { title: Lang.T.PEtell, icon: "question", func: f });
                } else {
                    if (g) MsgBox.close();
                    else MsgBox.show(Lang.T.PEsetting + b, null, { title: Lang.T.PEbad });
                }
                p = null;

                function f(v) {
                    if (v == 1) {
                        var t = getGmapType(cm_map.getCurrentMapType());
                        var n = encodeURIComponent(CM.n);
                        var m = encodeURIComponent(info.addr);
                        cm_xr.post("/req/?rx=m", 'm=' + m + '&c=' + CM.c + '&i=' + CM.i + '&lt=' + y + '&ln=' + x + '&z=17&t=' + t + '&n=' + n + '&r=' + CM.r + '&d=1' + '&x=' + ReqID.Msg(), Msg.CB, Msg.Timeout);
                    } else {
                        if (g) {
                            CM.Mk.setLatLng(new GLatLng(cnlinf.lt, cnlinf.ln));
                            cm_map.setCenter(new GLatLng(cnlinf.lt, cnlinf.ln), cnlinf.z);
                        }
                    }
                    Ctls.Map.Start(true);
                    Koozyt.Process = false;
                }

            },
            onFindClient: function(v) {
                p.getLocation();
                c = true;
                var s = "<p id='pemessage'>" + Lang.T.Waiting + "<br /><b class='pink'" + Lang.T.PENotify + "</b></p>"
                MsgBox.show(s + b, -1, { title: Lang.T.PEDoing, icon: "network", func: function(v) { cl = true; } });
            },
            onMessage: function(v) { if (c) $('pemessage').innerHTML = v; },
            appkey: mm_plugins.Koozyt.k
        });
    }
}



var Ctls = {
    Map: {
        Id: -1,
        _Play: true,
        _Stop: false,
        _Zoom: 0,
        _Cursor: true,
        Start: function(f) { if (Ctls.Map._Stop && !f) return; Ctls.Map._Stop = false; clearTimeout(CM.Timer.Map); Ctls.Map._Play = true; Ctls.Map.Draw(0); },
        TStop: function(f) { if (Ctls.Map._Stop && !f) return; Ctls.Map._Stop = false; Ctls.Map._Play = false; clearTimeout(CM.Timer.Map); CM.Timer.Map = setTimeout(Ctls.Map.Start, CM.Cst.PlayIntarval); Ctls.Map.Draw(1); },
        Stop: function() { Ctls.Map._Play = false; clearTimeout(CM.Timer.Map); CM.Timer.Map = 0; Ctls.Map.Draw(2); Ctls.Map._Stop = true; },
        Draw: function(v) {
            var s = '';
            if (Ctls.Map.Id == v) return;
            Ctls.Map.Id = v;
            if (v == 2) {
                s += "<img src='/images/play_off.gif' onclick='Ctls.Map.Start(true);' class='on'/><img src='/images/tstop_off.gif' onclick='Ctls.Map.TStop(true);' class='on'/><img src='/images/stop_on.gif'/>";
            } else if (v == 1) {
                s += "<img src='/images/play_off.gif' onclick='Ctls.Map.Start(true);' class='on'/><img src='/images/tstop_on.gif'/><img src='/images/stop_off.gif' onclick='Ctls.Map.Stop();' class='on'/>";
            } else {
                s += "<img src='/images/play_on.gif'/><img src='/images/tstop_off.gif' onclick='Ctls.Map.TStop(true);' class='on'/><img src='/images/stop_off.gif' onclick='Ctls.Map.Stop();' class='on'/>";
            }
            $('mapplayer').innerHTML = s;
        },
        Zoom: function(v) {
            Ctls.Map._Zoom = v;
            var s = '';
            if (Ctls.Map._Zoom) {
                s += "<img src='/images/play_on.gif'/><img src='/images/stop_off.gif' onclick='Ctls.Map.Zoom(false);' class='on'/>"
            } else {
                s += "<img src='/images/play_off.gif' onclick='Ctls.Map.Zoom(true);' class='on'/><img src='/images/stop_on.gif'/>"
            }
            $('zoomplayer').innerHTML = s;
        },
        Cursor: function() {
            if (Ctls.Map._Cursor) {
                CM.Mk.hide();
                Ctls.Map._Cursor = false;
                $('speechmapbtn').src = "/images/speechmap_off.gif"
                Paste.Cancel();
            } else {
                CM.Mk.setLatLng(cm_map.getCenter());
                CM.Mk.show();
                Ctls.Map._Cursor = true;
                $('speechmapbtn').src = "/images/speechmap_on.gif"
            }
        }
    },
    Scroll: {
        _Play: true,
        Start: function() { clearTimeout(CM.Timer.Scroll); Ctls.Scroll._Play = true; },
        Stop: function() { Ctls.Scroll._Play = false; clearTimeout(CM.Timer.Scroll); CM.Timer.Scroll = setTimeout(Ctls.Scroll.Start, CM.Cst.PlayIntarval); },
        Draw: function() {
            if (McScroll.active) return;
            if ($('chat').offsetHeight * 2 + $('chat').scrollTop > $('chat').scrollHeight) {
                Ctls.Scroll.Start();
            } else {
                Ctls.Scroll.Stop();
            }
        }
    },
    SendBtn: {
        enabled: function() {
            $("cm_btn_send").innerHTML = "<a href='javascript:void(0);' onclick='Msg.Send();return false;'>" + Lang.T.Say + "</a>";
        },
        disabled: function() {
            $("cm_btn_send").innerHTML = "<div>" + Lang.T.Say + "</div>";
        }
    },
    Msg: {
        _focus: false,
        onFocus: function() { Ctls.Msg._focus = true; },
        onBlur: function() { Ctls.Msg._focus = false; },
        Focus: function() {
            if (View.P.form) return;
            try { if (!Ctls.Msg._focus) $("message").focus(); } catch (e) { }
        },
        onKeyPress: function(e) {
            if (!CM.Mk.isHidden()) {
                if (Ctls.Map._Play) cm_map.panTo(CM.Mk.getLatLng());
                Ctls.Map.TStop();
            } else if (!CM.PMk.isHidden()) {
                if (Ctls.Map._Play) cm_map.panTo(CM.PMk.getLatLng());
                Ctls.Map.TStop();
            }
            if (IsEnterKey(e)) Msg.Send();
        }
    },
    Chat: {
        ondblclick: function(m) {
            Ctls.Map.TStop();
            $f('player').mc_stop();
            Player.Process = false;
            Player.Play(m);
            Ctls.Msg.Focus();
        },
        onclick: function(m) {
            if ($("cc" + m).offsetTop > $('chat').scrollHeight - $('chat').offsetHeight) Ctls.Map.Start();
            else Ctls.Map.TStop();
            GMark.Show(m, true);
            if (Player.Id != m) Player.StopAndPlay(m);
            Ctls.Msg.Focus();
        },
        onmouse: function(m, v) {
            if (Player.Id == m) return;
            var c = $("cc" + m);
            if (c) c.style.backgroundColor = v > 0 ? "#f0f0ff" : "transparent";
        },
        playing: function(m, v) {
            var c = $("cc" + m);
            if (c) c.style.backgroundColor = v > 0 ? "#ffefef" : "transparent";
        }
    }
}


var ReqID = {
    MsgID: "", UserID: "",
    Msg: function() { ReqID.MsgID = "r" + (new Date()).getTime(); return ReqID.MsgID; },
    User: function() { ReqID.UserID = "u" + (new Date()).getTime(); return ReqID.UserID; },
    Channel: function() { return "&x=" + (new Date()).getTime(); }
}



var User = {
    Process: 0,
    isLogin: 0,
    Arr: new Array(),
    Cnt: 0,
    Sender: function(o) {
        if (User.Process != 0) return;
        l(); function l() { if (Msg.Process == 0) User.SenderExe(o); else setTimeout(l, 100); }
    },
    SenderExe: function(o) {
        if (User.Process != 0) return;
        User.Process = setTimeout(User.Timeout, 10000);
        var n = encodeURIComponent(CM.n);
        if (o == "unload") {
            if (!User.isLogin) return;
            User.isLogin = false;
            if (CM.u > 0) cm_xr.post("/req/?rx=u", 'c=' + CM.c + '&i=' + CM.i + '&u=' + CM.u + '&n=' + n + '&l=' + GLanguage + '&x=' + ReqID.User() + '&m=1', User.Timeout, User.Timeout);
        } else {
            User.isLogin = true;
            cm_xr.post("/req/?rx=u", 'c=' + CM.c + '&i=' + CM.i + '&u=' + CM.u + '&n=' + n + '&l=' + GLanguage + '&x=' + ReqID.User(), User.CB, User.Timeout);
        }
    },
    CB: function(r) {
        User.Process = 0;
        eval("User.Read(" + r + ")");
    },
    Read: function(r) {
        if (r.rs == 0 || r.i != ReqID.UserID) return;
        var u = r.rs;
        CM.u = r.u;
        $('cm_users_body').innerHTML = "";

        User.Cnt = 0;
        User.Arr = new Array();
        for (var i = 0; i < u.length; i++) {
            User.Login(u[i], true);
        }
        User.DrawCnt();
    },
    Login: function(u, f) {
        if (f) {
            if (u.u in User.Arr) return;
            User.Arr[u.u] = { i: u.i, n: u.n };
            if ($("u" + u.u)) return;
            var s = "<img src='/images/icons/" + mc_Chara[u.i].f + "'><br />" + u.n + (u.l == "" ? "" : "<br /><span>" + Lang.T[u.l] + "</span>");
            var d = document.createElement('div');
            d.id = "u" + u.u; d.className = "user"; d.innerHTML = s;
            $('cm_users_body').insertBefore(d, $('cm_users_body').firstChild);
            User.Cnt++;
        } else {
            if (u.u in User.Arr) {
                delete User.Arr[u.u];
                if ($("u" + u.u)) $('cm_users_body').removeChild($("u" + u.u));
                User.Cnt--;
            }
        }
    },
    DrawCnt: function() { $('cm_users_head').innerHTML = Lang.T.LinCnt + " : " + Lang.T.People.replace("---", User.Cnt); },
    Timeout: function() {
        if (User.Process == 0) return;
        clearTimeout(User.Process); User.Process = 0;
    },
    Check: function() { if (!(CM.u in User.Arr) && Stage.Loaded) User.Sender(); }
}


var Msg = {
    Process: 0,
    TProcess: 0,
    Lang: "",
    Send: function() {
        if (isNull($("message").value)) { setTimeout(Msg.Restore, 1); return; }
        if (Msg.Process != 0) return;
        l();
        function l() { if (User.Process == 0) Msg.Trans(); else setTimeout(l, 100); }
    },
    Trans: function() {
        Msg.Process = setTimeout(Msg.Timeout, 20000);
        $("message").blur();
        $("message").disabled = true;
        Ctls.SendBtn.disabled();
        Ctls.Msg._focus = false;
        Ctls.Map.Start();
        Ctls.Scroll.Start();
        clearTimeout(CM.Timer.Main); CM.Timer.Main = 0;
        Emoji.close();


        if ($("message").value.length > 255) {
            $("message").value = $("message").value.substr(0, 255) + "...";
            $('message').style.color = "";
        }
        if (GLanguage == Lang.DLN) {
            Msg.Lang = $("message").value;
            Msg.SendExe();
        } else {
            Msg.Lang = "";
            var t;
            t = strip_smily($("message").value);
            if (isNull(t)) Msg.SendExe();
            else {
                Msg.TProcess = setTimeout("Msg.CB_Trans({error:true})", 5000);
                google.language.translate(t, GLanguage, Lang.DLN, Msg.CB_Trans);
            }
        }
    },
    CB_Trans: function(r) {
        if (Msg.TProcess == 0) return;
        clearTimeout(Msg.TProcess);
        Msg.TProcess = 0;
        if (!r.error) Msg.Lang = r.translation;
        Msg.SendExe();
    },
    SendExe: function() {

        var m = encodeURIComponent($("message").value);
        var ml = encodeURIComponent(Msg.Lang.substr(0, 255));
        var n = encodeURIComponent(CM.n);
        var f = encodeURIComponent(CM.f); CM.f = "";

        var lt = 0;
        var ln = 0;
        var z = 0;
        var t = 0;
        var s = '';
        var p;
        if (!CM.Mk.isHidden()) {
            p = CM.Mk.getLatLng();
            lt = p.lat();
            ln = p.lng();
            z = cm_map.getZoom();
            t = getGmapType(cm_map.getCurrentMapType());
        } else if (!CM.PMk.isHidden() && Paste.Apid > 0) {
            p = CM.PMk.getLatLng();
            lt = p.lat();
            ln = p.lng();
            z = cm_map.getZoom();
            t = getGmapType(cm_map.getCurrentMapType());
            s = '&a=' + Paste.Apid + "&q=" + encodeURIComponent(Paste.Desc);

        }
        cm_xr.post("/req/?rx=m", 'm=' + m + '&ml=' + ml + '&c=' + CM.c + '&u=' + CM.u + '&i=' + CM.i + '&lt=' + lt + '&ln=' + ln + '&z=' + z + '&t=' + t + '&n=' + n + '&r=' + CM.r + '&f=' + f + s + '&x=' + ReqID.Msg() + '&l=' + GLanguage, Msg.CB, Msg.Timeout);
    },
    CB: function(r) {
        $("file_form").reset();
        if (CM.f == "") File.Draw(0);
        if (!CM.PMk.isHidden() && Paste.Apid > 0) Paste.Cancel();
        $("message").value = "";
        $("message").disabled = false;
        Ctls.SendBtn.enabled();

        Ctls.Msg.Focus();
        clearTimeout(Msg.Process); Msg.Process = 0;
        eval("Msg.Draw(" + r + ");");
    },
    Timeout: function() {
        if (Msg.Process == 0) return;
        Msg.Process = 0;
        clearTimeout(Msg.Process);
        MsgBox.show("There is a possibility of failing in the transmission of the message. <br />Is this message transmitted again?(The message might be normally transmitted. )", { 1: "Send", 2: "Cancel" }, { title: "The error occurred.", icon: "x", func: c });

        function c(v) {
            if (v == 1) Msg.Send();
            else {
                Msg.Req();
                $("message").disabled = false;
                Ctls.SendBtn.enabled();
                Ctls.Msg.Focus();
            }
        }
    },
    Req: function() { jsonp('/req/?rx=r&c=' + CM.c + '&r=' + CM.r + '&x=' + ReqID.Msg(), "main_requestid"); },
    CBReq: function(r) { Msg.Draw(r); },
    Restore: function() { Ctls.Msg.Focus(); $("message").value = ''; },
    Draw: function(r) {
        if (r.rs.length > 0 && r.i == ReqID.MsgID) {
            var ch = $('chat');
            var res = r.rs;
            var ref = false;
            for (var i = 0; i < res.length; i++) {

                var rs = res[i];
                var id = parseInt(rs.r);
                var mk = rs.lt == 0 && rs.ln == 0 ? false : true;
                var s = "";
                var ap = rs.a > 0 ? { t: " <font class='pink'>" + MashMapGetTitle(rs.a) + " By " + MashMapGetAPIName(rs.a) + "</font>", i: MashMapGetMarker(rs.a, "style='height:20px;' align='absmiddle'") + '&nbsp;'} : { t: "", i: "" };

                var df = new Date(rs.d * 1000);
                var dn = new Date();
                var md = null;

                if (df.getDate() == dn.getDate() && df.getMonth() == dn.getMonth()) {
                    rs.d = df.getHours() + ":" + df.getMinutes() + ":" + df.getSeconds();
                } else {
                    rs.d = df.toLocaleString();
                }

                if (rs.k == 9) {
                    mk = false;
                    s += "<div class='cc_n'><img src='/images/icons/" + mc_Chara[rs.i].f + "' align='absmiddle'/><b>" + rs.n + "</b>  " + Lang.T.Join + "<span>(" + rs.d + (rs.l == "" ? "" : " / " + Lang.T[rs.l]) + ")</span></div>";
                    if (Stage.Loaded) User.Login({ n: rs.n, i: rs.i, u: rs.f, l: rs.l }, true); User.DrawCnt();
                } else if (rs.k == 8) {
                    mk = false;
                    s += "<div class='cc_n'><b>" + rs.n + "</b>  " + Lang.T.Leave + "<span>(" + rs.d + (rs.l == "" ? "" : " / " + Lang.T[rs.l]) + ")</span></div>";
                    if (Stage.Loaded) User.Login({ n: rs.n, i: rs.i, u: rs.f, l: rs.l }, false); User.DrawCnt();
                } else if (rs.k == 1) {
                    s += "<div class='cc_n'>" + rs.n + "&nbsp;<span>(" + rs.d + (rs.l == "" ? "" : " / " + Lang.T[rs.l]) + (mk && rs.t > 0 ? " - " + GmapTypeDef[rs.t].n : "") + ") <font class='pink'>Location By PlaceEngine</font></span></div>";
                    s += "<div class='cc_m' style='color:" + mc_Chara[rs.i].c + ";'>" + Lang.T.PEhere + "<br />" + rs.m + "</div>";
md = { n: rs.n, d: rs.d, i: rs.i, m: rs.m, f: rs.f, a: rs.a, q: rs.q, l: rs.l, ap: ap };

                } else {

                    var msg;
                    if (rs.k == 0 && strip_tags(rs.m) != "" && rs.l != "" && rs.l != GLanguage) {
                        msg = "<img src='/images/loading_s.gif'/>" + rs.m;
                        msg = "<span id='ccm" + id + "' title='" + Lang.T.Orginal + " : " + strip_tags(rs.m) + "'>" + msg + "</span>";
                    } else {
                        msg = rs.m;
                        msg = "<span id='ccm" + id + "'>" + msg + "</span>";
                    }

                    if (rs.f) s += "<div class='file_thumr' style='margin:0px; 0 '><a href='/files/" + rs.f + "' title='" + strip_tags(rs.m) + "' rel='lightbox' id='img" + id + "'><div style='background-image:url(/files/thumbs/60/" + rs.f + ");'></div></a></div>";
                    s += "<div class='cc_n'>" + rs.n + "&nbsp;<span>(" + rs.d + (rs.l == "" ? "" : " / " + Lang.T[rs.l]) + (mk && rs.t > 0 ? " / <font class='green'>" + GmapTypeDef[rs.t].n + "</font>" : "") + ")" + ap.t + "</span></div>";
                    s += "<div class='cc_m' style='color:" + mc_Chara[rs.i].c + ";'>" + ap.i + msg + "</div>";
                    if (rs.f) s = "<div style='clear:both;height:1px;'></div>" + s + "<div style='clear:both;height:1px;'></div>";

                    md = { n: rs.n, d: rs.d, i: rs.i, m: rs.m, f: rs.f, a: rs.a, q: rs.q, l: rs.l, ap: ap };
                }
if(Stage.Loaded || ( rs.k != 9 && rs.k != 8) ){
                var d = document.createElement('div');
                d.id = "cc" + id;
                d.className = "cc_o";
                d.innerHTML = s;
                d.setAttribute("onmouseover", atr("Ctls.Chat.onmouse(" + id + ",1)"));
                d.setAttribute("onmouseout", atr("Ctls.Chat.onmouse(" + id + ",-1)"));
                d.setAttribute("onclick", atr("Ctls.Chat.onclick(" + id + ")"));
                ch.appendChild(d);
}
                var maker = 0;



                if (mk) {
                    maker = new GMarker(new GLatLng(rs.lt, rs.ln), { icon: mc_Chara[rs.i].i });
                    cm_mkmng.addMarker(maker, 0);

                    GEvent.addListener(maker, "click",
			                GEvent.callbackArgs(maker, function(m) {
			                    Ctls.Map.TStop();
			                    McScroll.scrollTo("cc" + m, 'chat');
			                    GMark.Show(m, true);
			                    Player.StopAndPlay(m);
			                }, id));

                    ref = true;
                }

                cm_citem[id] = { m: maker, t: md, z: rs.z, v: 1, k: rs.k, mt: rs.t };

                CM.r = id;
                cm_citem_cnt++;

                if (cm_citem_cnt - cm_mkmngcnt > 100) {
                    cm_mkmngid = mc_each_array(cm_mkmngid, cm_citem);
                    if (cm_citem[cm_mkmngid].m != 0) {
                        cm_mkmng.removeMarker(cm_citem[cm_mkmngid].m);
                        cm_mkmng.addMarker(cm_citem[cm_mkmngid].m, cm_citem[cm_mkmngid].z);
                    }
                    cm_citem[cm_mkmngid].v = 0;
                    cm_mkmngcnt++;
                }
            }
            if (ref) {
                var am = !GMark.isHiddenInfoWindow();
                cm_mkmng.refresh();
                if (am && GMark.ActiveMk > 0) GMark.openInfoWindowHtmlExe(GMark.ActiveMk);
            }
        }


        if (CM.Timer.Main == 0) { CM.Timer.Main = setInterval(Msg.Req, 5000); };
        if (CM.mr < CM.r) { Player.Play(mc_each_array(CM.mr, cm_citem)); }
        if (CM.lmr < CM.r) { Gtrans.trans(mc_each_array(CM.lmr, cm_citem)); }

        User.Check();
    },
    MakeHTML: function(id) {
        var m = cm_citem[id];
        var rs = m.t;

        var s = "";
        if (m.k == 1) {
            s += "<div class='mk_250'>";
            s += "<div class='cc_n'>" + rs.n + "&nbsp;<span>(" + rs.d + (rs.l == "" ? "" : " / " + Lang.T[rs.l]) + ") <font class='pink'>Location By PlaceEngine</font></span></div>";
            s += "<div class='cc_m' style='color:" + mc_Chara[rs.i].c + ";'>" + Lang.T.PEhere + "<br />" + rs.m + "</div>";
            s += "</div><div class='mm_cdt'>Powerd By <a href='http://www.placeengine.com' target='_blank'><img src='/images/placeengine_s.gif' /></a></div>";
        } else {
            s += "<div class='cc_n'>" + rs.n + "&nbsp;<span>(" + rs.d + (rs.l == "" ? "" : " / " + Lang.T[rs.l]) + ")" + rs.ap.t + "</span></div>";
            if (rs.f) s += "<div class='mm_img'><a href='/files/" + rs.f + "' " + (isIE ? "onclick=\"$('img" + id + "').fireEvent('onclick');return false;\"" : "rel='lightbox'") + " title='" + strip_tags(rs.m) + "'><div style='background-image:url(/files/thumbs/140/" + rs.f + ");'></div></a></div>";
            s += "<div class='cc_m' style='color:" + mc_Chara[rs.i].c + ";'>" + "<span id='ccmk" + id + "'>" + rs.m + "</span></div>";
            if (rs.a > 0) s += "<div style='clear:both;background-color:#fff3f3;border:dotted 1px #ff9999;padding:3px;margin-top:5px;'>" + rs.q + "</div>";

            if (rs.f || rs.a > 0) s = "<div class='mk_340'>" + s + "</div>";
            else s = "<div class='mk_250'>" + s + "</div>";
        }
        return s;
    },
    ChkLen: function() {
        setTimeout(function() {
            $('message').style.color = $('message').value.length > 255 ? "#f00" : "";
        }, 255);
    }
}

var Gtrans = {
    mr: 0,
    Process: false,
    Timer: 0,
    trans: function(id) {
        if (id > 0 && Gtrans.Process == false && mc_in_array(id, cm_citem)) {
            Gtrans.Process = true;
            CM.lmr = id;
            var it = cm_citem[id];
            if (it.k == 0 && strip_tags(it.t.m) != "" && it.t.l != "" && it.t.l != GLanguage) {
                google.language.translate(it.t.m, it.t.l, GLanguage, Gtrans.CB_trans);
                Gtrans.Timer = setTimeout("Gtrans.CB_trans({error:true});", 5000);
            } else {
                Gtrans.GoNext(id);
            }
        }
    },
    CB_trans: function(r) {
        clearTimeout(Gtrans.Timer);
        var id = CM.lmr;
        var s = "";
        if (r.error) {
            $("ccm" + id).innerHTML = cm_citem[id].t.m;
        } else {
            s = r.translation;
            cm_citem[id].t.m = s;
            $("ccm" + id).innerHTML = s;
            if ($("ccmk" + id)) $("ccmk" + id).innerHTML = s;
        }
        Gtrans.GoNext(id);
    },
    GoNext: function(id) {
        Gtrans.Process = false;
        Gtrans.trans(mc_each_array(id, cm_citem));
    }
}


var GMark = {
    Close: false,
    CloseId: 0,
    Timer: 0,
    ActiveMk: 0,
    Show: function(i, f) {
        clearTimeout(GMark.Timer);
        var m = cm_citem[i];
        if (m.m == 0) return;
        if (GMark.ActiveMk == i && !GMark.isHiddenInfoWindow()) return;
        var l = GMark.CloseId;
        GMark.CloseId = i;
        if (!Ctls.Map._Play && !f) return;

        if (l != i && !GMark.isHiddenInfoWindow()) cm_map.closeInfoWindow();

        if ((Ctls.Map._Zoom && cm_map.getZoom() != m.z) || (m.v == 0 && cm_map.getZoom() < m.z)) cm_map.setCenter(m.m.getLatLng(), m.z);
        if (Ctls.Map._Zoom && GmapTypeDef[m.mt].o != cm_map.getCurrentMapType()) cm_map.setMapType(GmapTypeDef[m.mt].o);

        if (!GMark.Close || !isIE) GMark.openInfoWindowHtml(m, i);
        else cm_map.panTo(m.m.getLatLng());
    },
    openInfoWindowHtml: function(m, i) {
        var e = false, d;
        if (m.m.isHidden()) { cm_map.panTo(m.m.getLatLng()); GMark.Timer = setTimeout(a, 1); }
        else a();
        function a() {
            if (m.m.isHidden()) GMark.Timer = setTimeout(a, 100);
            else {
                d = GEvent.addListener(cm_map, 'movestart', function() { e = true; });
                GMark.openInfoWindowHtmlExe(i);
                GMark.Timer = setTimeout(s, 100);
            }
        }
        function s() {
            GEvent.removeListener(d); d = null;
            if (e) {
                var x = GEvent.addListener(cm_map, 'moveend', function() {
                    GMark.Timer = setTimeout(f, 1);
                    GEvent.removeListener(x);
                    x = null;
                });
                var c = GEvent.addListener(cm_map, 'click', function() {
                    if (x) { GEvent.removeListener(x); x = null; }
                    GEvent.removeListener(c); c = null;
                });
            } else f();
        }
        function f() { if (GMark.isHiddenInfoWindow()) GMark.openInfoWindowHtmlExe(i); }
    },
    openInfoWindowHtmlExe: function(i) {
        if (GMark.Close && isIE) {
            cm_map.panTo(cm_citem[i].m.getLatLng());
            return;
        }
        GMark.SetActiveInfoWindow(i);
        cm_citem[i].m.openInfoWindowHtml(Msg.MakeHTML(i));
    },
    SetActiveInfoWindow: function(i) {
        if (i == 0) GMark.CloseId = 0;
        GMark.ActiveMk = i;
    },
    AClose: function(c) {
        if (c && GMark.isHiddenInfoWindow()) GMark.CloseId = 0;
        GMark.Close = true;
        if (isIE) cm_map.closeInfoWindow();
    },
    AOpen: function() {
        GMark.Close = false;
        if (GMark.CloseId && isIE) GMark.Show(GMark.CloseId, true);
    },
    isHiddenInfoWindow: function() { var i = cm_map.getInfoWindow(); return i.isHidden(); }
}





var Player = {
    Process: false,
    ListProcess: false,
    Id: 0,
    _Mute: false,
    Arr: { 0: "", 8: "logout", 9: "login", 1: "koozyt" },
    Mute: function() { Player.MuteExe(Player._Mute ? false : true); },
    MuteExe: function(b) {
        Player._Mute = b; $f('player').mc_mute(b);
        $('muteimg').src = Player._Mute ? "/images/sound_off.gif" : "/images/sound_on.gif";
    },
    StopAndPlay: function(m) {
        Player.Stop();
        Player.PlayExe(m);
    },
    Stop: function() {
        $f('player').mc_stop();
        Player.Process = false;
        Player.ListProcess = false;
    },
    Play: function(m) {
        if (Player.Process || m < 0) return;
        if (mc_in_array(m, cm_citem)) {
            GMark.Show(m);
            if (Ctls.Scroll._Play) McScroll.scrollTo("cc" + m, 'chat');
            Player.ListProcess = true;
            Player.PlayExe(m);
        } else Player.PlayEnd(m);
    },
    PlayExe: function(m) {
        Player.Process = true;
        CM.mr = CM.mr > m ? CM.mr : m;
        Ctls.Chat.playing(Player.Id, -1);
        Player.Id = m;
        Ctls.Chat.playing(m, 1);

        if (cm_citem[m].k == 0) $f('player').mc_play(CM.c, m);
        else $f('player').mc_playse(m, Player.Arr[cm_citem[m].k]);
    },
    PlayEnd: function(m) {
        Player.Process = false;
        if (Player.ListProcess) Player.Play(mc_each_array(m, cm_citem));
    }
}
function mc_playend(m) { Player.PlayEnd(m); }


var File = {
    Process: false,
    Upload: function() {
        CM.f = "";
        var re_text = /\.jpg|\.gif|\.png|\.jpeg/i;
        var filename = $("file").value;
        if (filename.search(re_text) == -1) {
            MsgBox.show(Lang.T.FileCNot + "<br />(GIF,JPEG,PNG)");
            $("file_form").reset();
            File.Draw(0);
        } else {
            var d = document.createElement('iflame');
            d.id = "upload_iframe";
            d.style.display = "none";
            $("cm_upload").appendChild(d);
            $("file_form").submit();
            File.Draw(1);
            File.Process = true;
        }
    },
    CB: function(s, u, m, l) {
        $("upload_iframe").src = "";
        File.Process = false;
        $("file_form").reset();
        if (s) {
            var d = "";
            d = "<a href='/files/" + u + "' rel='lightbox'><div style='background-image:url(/files/thumbs/60/" + u + ");'></div></a>";
            $("file_thuml").innerHTML = d;

            d = Lang.T.FileSend + "<input type='button' id='updelete' name='updelete' value='" + Lang.T.Cancel + "' onclick='File.Cancel()' />";
            $("file_complete").innerHTML = d;
            CM.f = u;
            File.Draw(2);
            if (l != 0) {
                MsgBox.show(Lang.T.FileFGPS, { 1: Lang.T.Yes, 2: Lang.T.No }, { icon: "question", title: Lang.T.FileGPS, func: function(v) {
                    if (v == 1) {
                        Ctls.Map.TStop();
                        CM.Mk.setLatLng(new GLatLng(l[0], l[1]));
                        cm_map.setCenter(new GLatLng(l[0], l[1]), 17);
                    }
                }
                });
            }
        } else {
            MsgBox.show(m, null, { icon: 'critical' });
            CM.f = "";
            File.Draw(0);
        }
    },
    CBDel: function(s, m) {
        File.Process = false;
        if (!s) MsgBox.show(m, null, { icon: 'critical' });
        $("file_form").reset();
        CM.f = "";
        File.Draw(0);
    },
    Cancel: function() {
        File.Process = true;
        jsonp('/req/?rx=d&d=' + CM.f);
    },
    Draw: function(v) {
        if (File.Process) return;
        $("file_input").style.display = v == 0 ? "inline-block" : "none";
        $("file_check").style.display = v == 1 ? "inline-block" : "none";
        $("file_complete").style.display = v == 2 ? "inline-block" : "none";

        if (v == 2) {
            $('file_thuml').style.display = "block";
            $('message').style.width = "405px";
            $('pasting').style.width = "405px";
        } else {
            $('file_thuml').innerHTML = "";
            $('file_thuml').style.display = "none";
            $('message').style.width = "480px";
            $('pasting').style.width = "480px";
        }
    }
}



var Paste = {
    Desc: "",
    Apid: 0,
    Set: function(a) {
        var d = MashMaps.items[a];
        if (d) Paste.SetExe(d.d, d.t, d.i, d.m.getLatLng());
    },
    SetSearch: function() {
        var d = MashSearch.SelectedItem;
        if (d) Paste.SetExe(d.d, d.t, d.i, d.l);
    },
    SetExe: function(d, t, i, l) {
        CM.PMk.setLatLng(new GLatLng(l.lat() - MashMap_Margin, l.lng()));
        var a = eval(MashMapDef[i]);
        var c = MashMapGetMarker(i, "style='height:17px;'");
        c += c != "" ? "&nbsp;" : "";

        $('pasting').innerHTML = "<span id='ln_quote'>" + Lang.T.Quote + "</span>&nbsp;<input type='button' value='"+Lang.T.Cancel+"'onclick='Paste.Cancel();return false;' />&nbsp;<a href='javascript:void(0);' onclick='Paste.Click();return false;'>" + c + t + "</a>";
        Paste.Desc = d;
        Paste.Apid = i;
        Paste.Draw(1);
        Paste.Click();
    },
    Cancel: function() {
        Paste.Desc = "";
        Paste.Apid = "";
        Paste.Draw(0);
    },
    Click: function() {
        GMark.SetActiveInfoWindow(0);
        var s = Paste.Desc;
        s += "<div class='mk_pasteg'><span id='ln_quote_s'>" + Lang.T.SendAtc + "</span>&nbsp;<input type='button' value='" + Lang.T.Cancel + "'onclick='Paste.Cancel();return false;' /></div>"
        s = "<div class='mk_340'>" + s + "</div>";
        CM.PMk.openInfoWindowHtml(s);
    },
    Draw: function(v) {
        if (v == 0) {
            CM.PMk.closeInfoWindow();
            CM.PMk.hide();
            if (Ctls.Map._Cursor) {
                var l = CM.PMk.getLatLng();
                CM.Mk.setLatLng(new GLatLng(l.lat() + MashMap_Margin * 2, l.lng()));
                CM.Mk.show();
            }
            $('pasting').innerHTML = "";
            $('pasting').style.display = "none";
            $('message').style.height = "99px";
        } else {
            if (!Ctls.Map._Cursor) Ctls.Map.Cursor();
            CM.Mk.hide();
            CM.PMk.show();
            $('pasting').style.display = "block";
            $('message').style.height = "73px";
        }
    }
}

var View = {
    P: {
        form: true,
        chat: true,
        top: true
    },
    Chat: function(f) {
        if (McSlider.active) return;
        GMark.AClose(true);
        if (View.P.chat) {
            View.P.chat = false;
            $("chatimg").src = "/images/chat_hide.gif";
            McSlider.start("chat_w", { w: 400 }, function() { $("map_w").style.marginRight = "400px"; cm_map.checkResize(); e(); });
        } else {
            View.P.chat = true;
            $("map_w").style.marginRight = "5px";
            cm_map.checkResize();
            $("chatimg").src = "/images/chat_show.gif";
            McSlider.start("chat_w", { w: 5 }, e);
        }
        function e() { if (isIE) GMark.AOpen(); if (isset(f)) f(); }
    },
    Form: function(f) {
        if (McSlider.active) return;
        $("bottom_w").scrollTop = 0;
        GMark.AClose(true);
        if (View.P.form) {
            View.P.form = false;
            $("cm_tools").style.visibility = "visible";
            $("formimg").style.backgroundImage = "url('/images/form_hide.gif')"
            $("formimg").innerHTML = Lang.T.Hide +" "+ Lang.T.Form;
            $("message").disabled = false;
var v=$("message").value;
            McSlider.start("bottom_w", { h: 200 }, function() { Disp.Resize(); e(); $("message").focus();if(v==""){$("message").value=" ";$("message").value="";}});
        } else {
            View.P.form = true;
            $("message").blur();
            $("message").disabled = true;
            Emoji.close();
            Disp.Resize();
            $("formimg").style.backgroundImage = "url('/images/form_show.gif')"
            $("formimg").innerHTML = Lang.T.Show + " " + Lang.T.Form;
            McSlider.start("bottom_w", { h: 35 }, function() { $("cm_tools").style.visibility = "hidden"; e(); });
        }
        cm_map.checkResize();
        function e() { if (isIE) GMark.AOpen(); if (isset(f)) f(); }
    },
    Top: function(f) {
        if (McSlider.active) return;
        GMark.AClose(true);
        if (View.P.top) {
            View.P.top = false;
            McSlider.start("cm_head", { h: 70, time: 300, unit: 30 }, function() { Disp.Resize(); cm_map.checkResize(); e(); });
        } else {
            View.P.top = true;
            var h = (window.innerHeight || document.documentElement.clientHeight);
            h = h - (View.P.form ? 35 : 200);
            $("map").style.height = (h - 26) + "px";

            cm_map.checkResize();
            McSlider.start("cm_head", { h: 0, time: 300, unit: 30 }, e);
        }
        function e() { if (isIE) GMark.AOpen(); if (isset(f)) f(); }
    }
}





function InitializeGmap() {
    if (!$('map')) return false;
    cm_map = new GMap2($('map'));
    cm_map.addControl(cmGLargeMapControl);
    MTypeInit();
    cm_map.enableDoubleClickZoom();
    cm_map.enableContinuousZoom();
    new GKeyboardHandler(cm_map);
    cm_map.enableScrollWheelZoom();
    cm_map.addControl(new BtnChatHide());
    return true;
}
var cmGLargeMapControl = new GLargeMapControl();

function BtnChatHide() { }
BtnChatHide.prototype = new GControl();
BtnChatHide.prototype.getDefaultPosition = function() { return new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(0, 35)); }
BtnChatHide.prototype.initialize = function(m) {
    var d = document.createElement("div");
    d.innerHTML = "<img src='/images/spacer.gif' id='chatimg'>"
    GEvent.addDomListener(d, "click", function() { View.Chat(); });
    m.getContainer().appendChild(d);
    return d;
}



window.onunload = Stage.Unload;
var CHAMAP = 1;

