﻿if(!window.jsyFlashGame) window.jsyFlashGame = {};
jsyFlashGame.Comment = Class.Create();
jsyFlashGame.Comment.prototype = {
    initialize:function(){
        //this.selfPart = this.createHtmlForm($D("divComment"));
        this.selfForm = $D("divCommentForm");
        //var divCommentBody = this.createHtmlDiv("divCommentBody", this.selfPart);
        this.commentPageIndex = 0;
        this.commentPageSize = 8;
        this.commentPagerVisibleCount = 9;
        this.commentLoad();
        this.commentInit();
    },
    commentInit:function(){
        var divNickNamePart = this.createHtmlDiv(undefined, this.selfForm, "Petname");
        var divNickNameTitle = this.createHtmlDiv(undefined, divNickNamePart, "left4");
        var divNickNameInput = this.createHtmlDiv(undefined, divNickNamePart, "right")
        var txtNickName = this.createHtmlInputText("txtNickName", divNickNameInput, "input1");
        txtNickName.setAttribute("maxlength", "20");
        jsy.attachEvent(txtNickName, "onkeypress", this.validInputLen(txtNickName, 20));
        jsy.attachEvent(txtNickName, "onchange", this.validInputLen(txtNickName, 20));
        divNickNameTitle.innerHTML = "昵称：";
        
        var divFacePart = this.createHtmlDiv(undefined, this.selfForm, "Petname");
        var divFaceTitle = this.createHtmlDiv(undefined, divFacePart, "left4");
        var divFaceInput = this.createHtmlDiv(undefined, divFacePart, "right");
        var divFaceRadio = this.createHtmlFaceRadio("rdoUserFace", divFaceInput);
        divFaceTitle.innerHTML = "表情：";
        
        var divCommentPart = this.createHtmlDiv(undefined, this.selfForm, "Petname");
        var divCommentTitle = this.createHtmlDiv(undefined, divCommentPart, "left4");
        var divCommentInput = this.createHtmlDiv(undefined, divCommentPart, "right");
        var labelCommentTextArea = this.createHtmlLabel(undefined, divCommentInput);
        var txtComment = this.createHtmlInputTextArea("txtComment", labelCommentTextArea, "input2");
        jsy.attachEvent(txtComment, "onkeypress", this.validInputLen(txtComment, 200));
        jsy.attachEvent(txtComment, "onchange", this.validInputLen(txtComment, 200));
        
        var divSubmitPart = this.createHtmlDiv(undefined, this.selfForm, "Petname");
        var divSubmitTitle = this.createHtmlDiv(undefined, divSubmitPart, "left4");
        var divSubmitInput = this.createHtmlDiv(undefined, divSubmitPart, "right");
        var imgSubmit = this.createHtmlImage("imgCommentSubmit", divSubmitInput, window.__localHost + "images/an_4.gif");
        var labelSubmitWait = this.createHtmlLabel("labelCommentSubmitWait", divSubmitInput);
        labelSubmitWait.appendChild(document.createTextNode("评论写入中，请稍后..."));
        imgSubmit.style.border = 0;
        imgSubmit.style.cursor = "pointer";
        labelSubmitWait.style.display = "none";

        jsy.attachEvent(imgSubmit, "onclick", this.submit_click(this));
        this.commentValid = new pageValidate();
        this.commentValid.alertType = 0;
        this.commentValid.regRequiredField(txtNickName, undefined, "请输入昵称！", undefined);
        this.commentValid.regRequiredField(txtComment, undefined, "请输入评论！", undefined);
    },
    commentLoad:function(){
        var xmlHandle = new jsy.xmlHandle();
        var ajax = new jsy.Ajax();
        var divCommentBody = $D("divCommentBody");
        divCommentBody.style.height = divCommentBody.offsetHeight;
        divCommentBody.innerHTML = "";
        xmlHandle.createXml("parameters");
        xmlHandle.addChildNode(xmlHandle.rootElement, "pageindex", this.commentPageIndex);
        xmlHandle.addChildNode(xmlHandle.rootElement, "pagesize", this.commentPageSize);
        xmlHandle.addChildNode(xmlHandle.rootElement, "gameid", __game);
        ajax.execLoaded(this.ajax_commentListComplete, this.ajax_commentListError, this);
        ajax.xmlHttp.open("post", window.__localHost + "callajax/comment_list.aspx?id=" + jsy.rndTimeNum());
        ajax.xmlHttp.send(xmlHandle.xmlDoc);
    },
    getCommentPager_1:function(pageIndex, pageSize, visiblePagerCount, count, s){
        var sc = s || this;
        var pageCount = parseInt(count / pageSize);
        if(parseFloat(pageCount) < parseFloat(count) / parseFloat(pageSize)){
            pageCount++;
        }
        sc.commentPageCount = pageCount;
        var loopLeft = l = parseInt(visiblePagerCount / 2);
        var loopRight = r = visiblePagerCount - loopLeft;
        if(pageIndex + 1 <= l) loopRight = r + (l - (pageIndex + 1));
        if(pageIndex + 1 + r >= pageCount) loopLeft = l + (pageIndex + 1 + r - pageCount);
        var pageText = "";
        for(var i = 0; i < loopLeft; i++){
            if(i==0)
                pageText = "<a href=\"javascript://\" onclick=\"comment.gotoPageIndex("+ (pageIndex - i) +")\" class=\"T02\">" + (pageIndex + 1 - i) + "</a>" + pageText;
            else
                pageText = "<a href=\"javascript://\" onclick=\"comment.gotoPageIndex("+ (pageIndex - i) +")\">" + (pageIndex + 1 - i) + "</a>" + pageText;
            if(pageIndex - i <= 0) break;
        }
        for(var i = 0; i < loopRight; i++){
            if(pageIndex + 1 + i >= pageCount) break;
            pageText += "<a href=\"javascript://\" onclick=\"comment.gotoPageIndex("+ (pageIndex + 1 + i) +")\">" + (pageIndex + 1 + i + 1) + "</a>";
        }
        pageText = "<a href=\"javascript://\" onclick=\"comment.gotoPageIndex("+(pageIndex - 1)+")\" class=\"T03\">上一页</a>" + pageText;
        pageText = "<a href=\"javascript://\" onclick=\"comment.gotoPageIndex(0);\" class=\"T03\">首页</a>" + pageText;
        pageText += "<a href=\"javascript://\" onclick=\"comment.gotoPageIndex("+(pageIndex + 1)+")\" class=\"T03\">下一页</a>";
        pageText += "<a href=\"javascript://\" onclick=\"comment.gotoPageIndex("+(pageCount - 1)+")\" class=\"T03\">尾页</a>";
        pageText += "<span>" + count + "条/共" + pageCount + "页</span>";
        return pageText;
    },
    validInputLen:function(inInput, maxLen){
        return function(ev){
            var e = ev || event;
            if(inInput.value.length > maxLen){
                inInput.value = inInput.value.substring(0, maxLen);
                jsy.cancelEvent(e);
            }
        }
    },
    getCommentPager:function(pageIndex, pageSize, count, s){
        var sc = s || this;
        var pageCount = parseInt(count / pageSize);
        if(parseFloat(pageCount) < parseFloat(count) / parseFloat(pageSize)){
            pageCount++;
        }
        sc.commentPageCount = pageCount;
        var selectPager = document.createElement("select");
        selectPager.id = "drpCommentPager";
        var i = 0;
        do{
            selectPager.options[i] = new Option("第" + (i + 1) + "页", i);
            if(i == s.commentPageIndex) selectPager.options[i].selected = true;
            i++;
        }
        while(i < pageCount);
        return selectPager;
    },
    pagerChange:function(s){
        return function(){
            var p = parseInt($D("drpCommentPager").value);
            s.gotoPageIndex(p);
        }
    },
    gotoPageIndex:function(pageIndex){
        if(pageIndex < 0){
            return;
        }
        if(pageIndex >= this.commentPageCount){
            return;
        }
        if(pageIndex != this.commentPageIndex){
            this.commentPageIndex = pageIndex;
            this.commentLoad();
        }
    },
    createHtmlInputText:function(id, parent, css){
        var htmlInputText = this.createHtmlInput("text", id);
        htmlInputText.id = id;
        parent.appendChild(htmlInputText);
        if(css) htmlInputText.className = css;
        return htmlInputText;
    },
    createHtmlInputTextArea:function(id, parent, css){
        var htmlInputTextArea = this.createHtmlInput(undefined, id, "textarea");
        htmlInputTextArea.id = htmlInputTextArea.name = id;
        parent.appendChild(htmlInputTextArea);
        if(css) htmlInputTextArea.className = css;
        return htmlInputTextArea;
    },
    createHtmlInputButton:function(id, parent, value, css){
        var htmlInputButton = this.createHtmlInput("button", id);
        htmlInputButton.setAttribute("value", value);
        htmlInputButton.setAttribute("id", id);
        parent.appendChild(htmlInputButton);
        if(css) htmlInputButton.className = css;
        return htmlInputButton;
    },
    createHtmlImage:function(id, parent, imgUrl, css){
        var htmlImage = document.createElement("img");
        htmlImage.src = imgUrl;
        if(id != undefined) htmlImage.id = id;
        parent.appendChild(htmlImage);
        if(css) htmlImage.className = css;
        return htmlImage;
    },
    createHtmlHref:function(id, parent, text, url, css){
        var htmlHref = document.createElement("a");
        if(url != undefined) htmlHref.setAttribute("href", url);
        if(text != undefined) htmlHref.innerHTML = text;
        if(id != undefined) htmlHref.setAttribute("id", id);
        if(css) htmlHref.setAttribute("className", css);
        parent.appendChild(htmlHref);
        return htmlHref;
    },
    createHtmlDiv:function(id, parent, css){
        var htmlDiv = document.createElement("div");
        if(id != undefined) htmlDiv.id = id;
        parent.appendChild(htmlDiv);
        if(css) htmlDiv.className = css;
        return htmlDiv;
    },
    createHtmlLabel:function(id, parent, css){
        var htmlLabel = document.createElement("label");
        if(id != undefined) htmlLabel.id = id;
        parent.appendChild(htmlLabel);
        if(css) htmlLabel.className = css;
        return htmlLabel;
    },
    createHtmlForm:function(parent){
        var htmlInputForm = document.createElement("form");
        parent.appendChild(htmlInputForm);
        return htmlInputForm;
    },
    createHtmlFaceRadio:function(name, parent){
        var ulList = document.createElement("ul");
        var faceAry = this.commentFaceList();
        for(var i = 0; i < faceAry.length; i++){
            var liList = document.createElement("li");
            var htmlInputRadio = i == 0 ? this.createHtmlInputRadio(name, true) : this.createHtmlInput("radio", name);
            var htmlImage = document.createElement("img");
            htmlInputRadio.setAttribute("id", name + "_" + i);
            htmlInputRadio.setAttribute("value", faceAry[i].value);
            htmlImage.src = faceAry[i].img;
            htmlImage.align = "absmiddle";
            liList.appendChild(htmlInputRadio);
            liList.appendChild(htmlImage);
            ulList.appendChild(liList);
        }
        parent.appendChild(ulList);
    },
    createHtmlInput:function(type, name, tag){
        var htmlInput = null;
        var tagName = tag || "input";
        try{
            htmlInput = document.createElement("<"+tagName+" name=\""+name+"\" />");
        }catch(e){
            htmlInput = document.createElement(tagName);
            htmlInput.name = name;
        }
        if(type != undefined) htmlInput.setAttribute("type", type);
        return htmlInput;
    },
    createHtmlInputRadio:function(name, isChecked){
        var htmlInput = null;
        var tagName = "input";
        try{
            htmlInput = document.createElement("<"+tagName+" name=\""+name+"\" checked=\""+isChecked+"\" />");
        }catch(e){
            htmlInput = document.createElement(tagName);
            htmlInput.name = name;
            htmlInput.checked = true;
        }
        htmlInput.setAttribute("type", "radio");
        return htmlInput;
    },
    commentFaceList:function(){
        return new Array(
            new this.commentFace(0, window.__localHost + "images/bqtu_1.gif"),
            new this.commentFace(1, window.__localHost + "images/bqtu_2.gif"),
            new this.commentFace(2, window.__localHost + "images/bqtu_3.gif"),
            new this.commentFace(3, window.__localHost + "images/bqtu_4.gif"),
            new this.commentFace(4, window.__localHost + "images/bqtu_5.gif"),
            new this.commentFace(5, window.__localHost + "images/bqtu_6.gif"),
            new this.commentFace(6, window.__localHost + "images/bqtu_7.gif"),
            new this.commentFace(7, window.__localHost + "images/bqtu_8.gif"),
            new this.commentFace(8, window.__localHost + "images/bqtu_9.gif"),
            new this.commentFace(9, window.__localHost + "images/bqtu_10.gif"),
            new this.commentFace(10, window.__localHost + "images/bqtu_11.gif"),
            new this.commentFace(11, window.__localHost + "images/bqtu_12.gif"),
            new this.commentFace(12, window.__localHost + "images/bqtu_13.gif"),
            new this.commentFace(13, window.__localHost + "images/bqtu_14.gif"),
            new this.commentFace(14, window.__localHost + "images/bqtu_15.gif"),
            new this.commentFace(15, window.__localHost + "images/bqtu_16.gif")
        )
    },
    commentFaceImage:function(faceList, faceId){
        for(var i = 0; i < faceList.length; i++){
            if(faceList[i].value == faceId){
                return faceList[i].img;
            }
        }
        return faceList[0].img;
    },
    commentFace:function(value, imgUrl){
        this.img = imgUrl;
        this.value = value;
    },
    getFaceValue:function(){
        var objs = document.getElementsByName("rdoUserFace");
        for(var i = 0; i < objs.length; i++){
            if(objs[i].checked){
                return objs[i].value;
            }
        }
    },
    ajax_commentAddComplete:function(_ajax, _xml, s){
        var ret = _xml.getText(_xml.rootElement);
        if(parseInt(ret) == 1){
            $D("txtComment").value = "";
            alert("评论发表成功！");
        }else{
            alert("评论发表失败！");
        }
        $D("imgCommentSubmit").style.display = "";
        $D("labelCommentSubmitWait").style.display = "none";
        s.commentLoad();
    },
    ajax_commentAddError:function(){
        alert("评论发表失败！");
        $D("imgCommentSubmit").style.display = "";
        $D("labelCommentSubmitWait").style.display = "none";
    },
    setCommentTop:function(liParent){
        var liList = document.createElement("li");
        liList.setAttribute("className", "nob");
        liList.innerHTML = "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 如果您觉得好玩.请把本站网站www.5qg.com告诉您的朋友,大家一起玩游戏";
        var divClear = document.createElement("div");
        divClear.style.clear = "both";
        liList.appendChild(divClear);
        liParent.appendChild(liList);
    },
    ajax_commentListComplete:function(_ajax, _xml, s){
        var ret = parseInt(_xml.rootElement.attributes.getNamedItem("ret").value);
        if(ret == 1){
            var divCommentBody = $D("divCommentBody");
            var elements = _xml.rootElement.getElementsByTagName("CommentList");
            var ulList = document.createElement("ul");
            divCommentBody.appendChild(ulList);
            s.setCommentTop(ulList);
            for(var i = 0; i < elements.length; i++){
                //var divCommentList = s.createHtmlDiv(undefined, divCommentBody);
                var nickName = _xml.getText(elements[i].getElementsByTagName("NickName")[0]);
                var ip = _xml.getText(elements[i].getElementsByTagName("Ip")[0]);
                var writeTime = _xml.getText(elements[i].getElementsByTagName("WriteTime1")[0]);
                var content = __strFilter.filter(_xml.getText(elements[i].getElementsByTagName("Content")[0]));
                var userFace = _xml.getText(elements[i].getElementsByTagName("UserFace")[0]);
                var liList = document.createElement("li");
                var divFace = s.createHtmlDiv(undefined, liList, "left1");
                var imgFace = s.createHtmlImage(undefined, divFace, s.commentFaceImage(s.commentFaceList(), parseInt(userFace)));
                var divContent = s.createHtmlDiv(undefined, liList, "right");
                var b = document.createElement("b");
                var span1 = document.createElement("span");
                var span2 = document.createElement("span");
                var br = document.createElement("br");
                divContent.appendChild(b);
                divContent.appendChild(span1);
                divContent.appendChild(br);
                divContent.appendChild(span2);
                b.innerHTML = nickName;
                span1.innerHTML = "来自：" + ip + " 发表于：" + writeTime;
                span2.innerHTML = content.replaceAll("<", "&lt;").replaceAll(">", "&gt;").replaceAll(" ", "&nbsp;").replaceAll("\n", "<br />");
                liList.appendChild(divFace);
                liList.appendChild(divContent);
                var divClear = s.createHtmlDiv(undefined, liList);
                divClear.style.clear = "both";
                ulList.appendChild(liList);
                if(i + 1 == elements.length) liList.setAttribute("className", "nob");
            }
            divCommentBody.style.height = ulList.offsetHeight + "px";
            $D("imgCommentSubmit").style.display = "";
            $D("labelCommentSubmitWait").style.display = "none";
            var divPager = $D("divCommentPager");
            divPager.innerHTML = "";
            divPager.appendChild(s.getCommentPager(s.commentPageIndex, s.commentPageSize, parseInt(_xml.rootElement.attributes.getNamedItem("count").value), s));
            var hrefMore = s.createHtmlHref(undefined, divPager, "查看全部评论", window.__localHost + "comment/game.aspx?game=" + jsy.urlEncode(__game));
            hrefMore.setAttribute("target", "_blank");
            hrefMore.style.marginLeft = 8;
            s.createHtmlImage(undefined, divPager, window.__localHost + "images/222.gif");
            jsy.attachEvent($D("drpCommentPager"), "onchange", s.pagerChange(s));
        }else{
            alert("因为网络原因出错，请稍后再试！");
        }
    },
    ajax_commentListError:function(){
        alert("获取评论时错误！");
    },
    submit_click:function(s){
        return function(){
            if(!s.commentValid.pageValid()){
                alert(s.commentValid.errorInfo("\n"));
                return;
            }
            var xmlHandle = new jsy.xmlHandle();
            var ajax = new jsy.Ajax();
            xmlHandle.createXml("parameters");
            xmlHandle.addChildNode(xmlHandle.rootElement, "nickname", $D("txtNickName").value);
            xmlHandle.addChildNode(xmlHandle.rootElement, "userface", s.getFaceValue());
            xmlHandle.addChildNode(xmlHandle.rootElement, "comment", $D("txtComment").value);
            xmlHandle.addChildNode(xmlHandle.rootElement, "gameid", __game);
            $D("imgCommentSubmit").style.display = "none";
            $D("labelCommentSubmitWait").style.display = "";
            ajax.execLoaded(s.ajax_commentAddComplete, s.ajax_commentAddError, s);
            ajax.xmlHttp.open("post", window.__localHost + "callajax/comment_add.aspx?id=" + jsy.rndTimeNum());
            ajax.xmlHttp.send(xmlHandle.xmlDoc);
        }
    },
    getDateTimeFormat:function(d){
        var date = d.substring(0, d.indexOf("T"));
        var time = d.substring(d.indexOf("T") + 1, d.indexOf("+"));
        return date + " " + time;
    }
}
jsyFlashGame.loadGame = Class.Create();
jsyFlashGame.loadGame.prototype = {
    initialize:function(){
        this.gameUrl = window.__swfHost + window.__game__;
        this.divGamePlayer = $D("divGameBody");
        this.bodyWidth = parseInt(this.divGamePlayer.offsetWidth);
        this.bodyHeight = parseInt(this.divGamePlayer.offsetHeight);
        this.gameBody = document.createElement("div");
        this.loadBody = document.createElement("div");
        this.gameBody.style.position = "absolute";
        this.gameBody.style.margin = 0;
        this.gameBody.style.zIndex = 1;
        this.divGamePlayer.appendChild(this.gameBody);
        this.divGamePlayer.appendChild(this.loadBody);
        if(window.__game) this.gameVisit();
        //this.gameInit();
	setTimeout(this.gameInit(), 5000);
        jsy.attachEvent($D("hrefFullScreen"), "onclick", this.gameFullScreen(this));
        jsy.attachEvent($D("spanJsyFavorite"), "onclick", this.jsyFavorite(this));
        jsy.attachEvent($D("hrefCopyUrl"), "onclick", this.gameUrlCopyt(this));
        $D("spanJsyFavorite").style.cursor = "pointer";
        //if(this.gameUrl != ""){this.loadInit();}
    },
    complete:function(){
        this.divGamePlayer.removeChild(this.loadBody);
    },
    loadInit:function(){
        var so = new SWFObject(window.__localHost + "jsyobject/gameloader.swf", "objGameLoader", this.bodyWidth, this.bodyHeight, "9", "#000000");
        so.addVariable("id", this.gameUrl);
        so.addParam("allowFullScreen","true");
        so.addParam("allowScriptAccess","sameDomain");
        so.write(this.loadBody);
    },
    gameInit:function(){
	var _this = this;
	return function(){
	        var so = new SWFObject(_this.gameUrl, "objGamePlayer", 565, 450, 7);
	        so.addParam("allowFullScreen","true");
	        so.addParam("allowScriptAccess","sameDomain");
	        so.write(_this.divGamePlayer);
	        setTimeout(_this.gameDownProgress(_this), 200);
	}
    },
    gameVisit:function(){
        var date = new Date();
        var longHour = 12;
        var visit = jsy.cookie.get("JsyGameHistory");
        var visitList = visit == null ? new Array() : visit.split(",");
        for(var i = 0; i < visitList.length; i++){
            var t = visitList[i].split("|");
            var t1 = t[0];
            var t2 = new Date(t[1]);
            if(unescape(t1) == __game){
                if((date - t2) / 3600 / 1000 < longHour){
                    return;
                }
            }
        }
        jsy.addScript(window.__localHost + "gamevisit/gamevisit.aspx?game=" + jsy.urlEncode(__game));
        visitList.push(escape(__game) + "|" + date);
        var loop = visitList.length - 40;
        if(loop < 0) loop = 0;
        visit = "";
        for(var i = loop; i < visitList.length; i++){
            var t = visitList[i].split("|");
            var t1 = t[0];
            var t2 = new Date(t[1]);
            if((date - t2) / 3600 / 1000 > longHour) continue;
            if(visit == ""){
                visit = visitList[i];
            }else{
                visit += "," + visitList[i];
            }
        }
        jsy.cookie.set("JsyGameHistory", visit, 365);
    },
    gameFullScreen:function(s){
        return function(ev){
            var e = ev || event;
            var gameFullScreen = window.open(s.gameUrl);
            jsy.cancelEvent(e);
        }
    },
    jsyFavorite:function(s){
        return function(){
            window.external.addFavorite('http://www.5qg.com/','休闲小游戏');
        }
    },
    gameUrlCopyt:function(s){
        return function(ev){
            var e = ev || event;
            if(window.clipboardData){
                window.clipboardData.clearData();
                var pageUrl = window.location.toString();
                var pageUrl = pageUrl.substring(0, pageUrl.indexOf(".html") + 5);
                window.clipboardData.setData("Text", pageUrl);
                alert("网址复制成功！");
            }else{
                alert("您的浏览器不支持复制功能");
            }
            jsy.cancelEvent(e);
        }
    },
    gameDownProgress:function(s){
        return function(){
            var loadProgress = 0;
            var imgDownProgress = $D("imgDownProgress");
            var divDownProgress = $D("divDownProgress");
            try{
                var objGame = $D("objGamePlayer");
                loadProgress = parseFloat(objGame.PercentLoaded());
            }catch(error){}
            imgDownProgress.style.width = parseInt((loadProgress / 100 * 418)).toString() + "px";
            divDownProgress.innerHTML = loadProgress;
            var span1 = document.createElement("span");
            span1.innerHTML = "%";
            divDownProgress.appendChild(span1);
            if(loadProgress != 100){
                setTimeout(s.gameDownProgress(s), 200);
            }else{
                divDownProgress.innerHTML = "<p>加载完毕</p>";
            }
        }
    }
}
var __jsyFlashGame = new jsyFlashGame.loadGame();
var __gameFavorite = new gameFavorite();
var comment = new jsyFlashGame.Comment();
jsy.attachEvent($D("hrefGameFavorite"), "onclick", __gameFavorite.add_event(__gameFavorite));
