﻿var CookieHelper = {
    SetCookie: function(name, value, expiredays) {
        var todayDate = new Date();
        todayDate.setDate(todayDate.getDate() + expiredays);
        document.cookie = name + '=' + escape(value) + '; path=/; expires=' + todayDate.toGMTString() + ';domain=howlingsword.co.kr;';
    },
    GetCookie: function(name) {
        var nameOfCookie = name + "=";
        var x = 0;
        while (x <= document.cookie.length) {
            var y = (x + nameOfCookie.length);
            if (document.cookie.substring(x, y) == nameOfCookie) {
                if ((endOfCookie = document.cookie.indexOf(";", y)) == -1)
                    endOfCookie = document.cookie.length;
                return unescape(document.cookie.substring(y, endOfCookie));
            }
            x = document.cookie.indexOf(" ", x) + 1;
            if (x == 0)
                break;
        }
        return "";
    }
}
var PanelControl =
{
    SetPanelVisible: function(masterid, id) {
        $("[id^=" + masterid + "]").each(function(i) {
            $(this).css("display", "none");
        });
        $("#" + masterid + id).css("display", "");
    }
}


var StringHandler =
{
    StrReplacer: function(target, maxLen) {
        $(target).each(function(i) {

            if ($(this).text().length > maxLen) {
                $(this).text($(this).text().substring(0, maxLen) + "...");
            }
        });
    }
}

function setPng24(obj) {
//    obj.width = obj.height = 1;
//    obj.className = obj.className.replace(/\bpng24\b/i, '');
//    obj.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + obj.src + "',sizingMethod='image');"
//    obj.src = '/Images/Common/blank.gif';
    return '';
}


function SaveGameStartLog(n, type) {
    $.post(
        "/Account/SaveGameStartLog",
        { type: type, resultNo : n },
        null,
        "json"
    );
}


var PopupHelper =
{
    SecureRoot: "",
    IsAuthenticated: false,
    IsTester: false,
    IsFirst: true,
    IsLauncherBlock: false,
    Init: function(url, isAuth, isTester) {
        this.SecureRoot = url;
        this.IsAuthenticated = isAuth;
        this.IsTester = (isTester === "true");
        setInterval(function() { PopupHelper.IsLauncherBlock = false; }, 5000);
    },
    GoFindID: function() {
        PopupHelper.fn_pop(PopupHelper.SecureRoot + "/Account/SearchAccount", "420px", "498px");

    },
    RunGame: function() {

        if (PopupHelper.IsAuthenticated == "false") {
            alert('로그인 해주세요.');
            return;
        }
        else if (PopupHelper.IsAuthenticated == "notaccess") {
            alert('게임 이용 약관에 동의 하셔야 합니다.'); 
        }
        else if (window.navigator.userAgent.indexOf('IE') > -1 && PopupHelper.IsLauncherBlock === false) {

            if (PopupHelper.IsFirst == true) {
                $(objectName).appendTo("body");
                PopupHelper.IsFirst = false;
            }
            try {
                var aaaa = document.WebLauncher.IsExecutable("zz");
                if (aaaa === 0) {
                    PopupHelper.IsLauncherBlock = true;
                    $.post(
                    "/Account/GetLauncherKey",
                    { type: "acdb" },
                    function(result) {

                        if (result == null) {
                            alert('하울링쏘드 플레이를 위해서 주소창 아래부분에 있는 메시지를 클릭하시고 \n\n [ActiveX 컨트롤] 을 설치해주세요! \n');
                            return;
                        }
                        else if (result == '') {
                            alert('인증에 실패하였습니다.');
                            return;
                        }
                        else if (result.LcKey.indexOf("[") > -1) {
                            alert(result.LcKey);
                            return;
                        }
                        else if (result.Result === 0) {


                            var n = document.WebLauncher.HsExecute(result.Param);
                            // 결과로그 저장
                            SaveGameStartLog(n, "ac");
                            if (n == 1 || n == -2) {
                                if (confirm("클라이언트 다운로드 페이지로 이동하시겠습니까?") == true) {
                                    location.href = "/DataRoom/Client";
                                }
                            }
                        }
                        else {
                            alert('인증에 실패하였습니다.');
                        }
                    },
                    "json"
                );
                }
                else {
                    alert('게임이 이미 실행중입니다.');
                }
            }
            catch (e) {
                // alert(e);
                //alert('하울링쏘드 플레이를 위해서 주소창 아래부분에 있는 메시지를 클릭하시고 \n\n [ActiveX 컨트롤] 을 설치해주세요! \n');
            }

        }
        else if (PopupHelper.IsLauncherBlock === true) {
            alert('게임이 이미 실행중입니다.');
        }
        else {
            alert('인터넷 익스플로러 이외의 브라우저에서는 실행이 불가능 합니다.');
        }
    }
    ,
    RunGameTest: function() {
        //alert("2010년 2월 중 오픈베타 예정입니다.");

        //setInterval(function() { PopupHelper.IsLauncherBlock = false; }, 5000);
        var nowDate = new Date();
        var currentTime = nowDate.getTime() + 5;

        if (PopupHelper.IsAuthenticated == false) {
            alert('로그인 해주세요.');
            return;
        }
        else if (window.navigator.userAgent.indexOf('IE') > -1 && nowDate.getTime() < currentTime) { //&& PopupHelper.IsLauncherBlock === false) {

            if (PopupHelper.IsFirst == true) {
                $('<OBJECT id="WebLauncher" codeBase="http://image.howlingsword.co.kr/launcher/starter/20100223/HsWebLauncher.cab#version=10,2,23,11" height="0" width="0" classid="clsid:B33E4AAB-3500-4715-8A18-81D5D69C30D7" VIEWASTEXT></OBJECT>').appendTo("body");
                PopupHelper.IsFirst = false;
            }
            try {

                if (document.WebLauncher.IsExecutable("zz") === 0) {
                    //PopupHelper.IsLauncherBlock = true;
                    $.post(
                    "/Account/GetLauncherKey",
                    { type: "acdb" },
                    function(result) {

                        if (result == null) {
                            alert('하울링쏘드 플레이를 위해서 주소창 아래부분에 있는 메시지를 클릭하시고 \n\n [ActiveX 컨트롤] 을 설치해주세요! \n');
                            return;
                        }
                        else if (result == '') {
                            alert('인증에 실패하였습니다.');
                            return;
                        }
                        else if (result.LcKey.indexOf("[") > -1) {
                            alert(result.LcKey);
                            return;
                        }
                        else if (result.Result === 0) {


                            var n = document.WebLauncher.HsExecute(result.Param);
                            // 결과로그 저장
                            SaveGameStartLog(n, "ac");
                            if (n == 1) {
                                if (confirm("클라이언트 다운로드 페이지로 이동하시겠습니까?") == true) {
                                    location.href = "/DataRoom/Client";
                                }
                            }
                        }
                        else {
                            alert('인증에 실패하였습니다.');
                        }
                    },
                    "json"
                );
                }
                else {
                    alert('게임이 이미 실행중입니다.');
                }
            }
            catch (e) {
                // alert(e);
                //alert('하울링쏘드 플레이를 위해서 주소창 아래부분에 있는 메시지를 클릭하시고 \n\n [ActiveX 컨트롤] 을 설치해주세요! \n');
            }

        }
        else if (nowDate.getTime() < currentTime) {//else if (PopupHelper.IsLauncherBlock === true) {
            alert('게임이 이미 실행중입니다.');
        }
        else {
            alert('인터넷 익스플로러 이외의 브라우저에서는 실행이 불가능 합니다.');
        }
    },

    GoFindPW: function() {
        PopupHelper.fn_pop(PopupHelper.SecureRoot + "/Account/SearchAccount", "420px", "498px");

    },
    GoMember: function() {
        if (PopupHelper.IsAuthenticated == "true") {
            location.href = "/Account/CheckEdit";
        }
        else {
            alert("로그인 해주세요");
        }

    },
    GoTesterPage: function() {
        if (PopupHelper.IsAuthenticated === true && PopupHelper.IsTester === true) {
            location.href = "/Tester/List";
        }
        else if (PopupHelper.IsAuthenticated === true) {
            alert("테스터 분들께 공개 될 예정 입니다.");

        }
        else {
            alert("테스터 게시판에 입장하기 위해 로그인 해주세요.");

        }
    }
    ,
    fn_pop: function(url, h, w) {
        window.open(url, '하울링쏘드', 'height=' + h + ',width=' + w + ',menubar=no,toolbar=no,location=no,resizable=no,status=no,scrollbars=no,top=300,left=700');
    }
}


$().ready(
    function() {
        if (navigator.appVersion.indexOf("MSIE 6.0") > -1 &&
            navigator.appVersion.indexOf("MSIE 8.0") < 1) {
            $("img.png24[src$='.png'],input.png24[src$='.png']").each(
                function(e) {
                    this.width = this.height = 1;
                    this.className = this.className.replace(/\bpng24\b/i, '');
                    this.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + this.src + "',sizingMethod='image');"
                    this.src = '/Images/Common/blank.gif';
                    return '';
                }
            );
        }

        var cnt = 0;
        $("img.cmdRollover").each(
            function(e) {
                if ($(this).attr("id") === '' || $(this).attr("id") == null) {
                    $(this).attr("id", "CLImageRollover" + cnt);
                    cnt++;
                }
                var currentElement = $(this);
                $(this).wrap("<span id='" + $(this).attr("id") + "container'></span>")
                    .after('<img src="' + $(this).attr("src").replace("off", "on") + '" id="' + $(this).attr("id") + '_on" style="display:none;" />');
                $("#" + $(this).attr("id") + "container").hover(
                    function() {
                        currentElement.css("display", "none");
                        $("#" + currentElement.attr("id") + "_on").css("display", "");
                    },
                    function() {
                        currentElement.css("display", "");
                        $("#" + currentElement.attr("id") + "_on").css("display", "none");
                    }
                ).css("cursor", "pointer");
            }

        );

        
        
        PanelControl.SetPanelVisible("Community", "PlayKnowhow");
        $("#btnComm1container").click(
            function(){
                SetCommunityPanel(1);
            }
        );
        $("#btnComm2container").click(
            function(){
                SetCommunityPanel(2);
            }
        );
        $("#btnComm3container").click(
            function() {
                SetCommunityPanel(3);
            }
        );
        try {
            $('#btnGoTop').scrollFollow({ offset: 102, speed: 700 });
        }
        catch (e) {

        }
    }

);

    var Launcher = {
    Init: function() {
        
    },
    RunGame: function() {
        //alert('CBT 기간에는 [자료실 > 클라이언트] 에서 \n\n클라이언트 다운로드 받아서 설치 후 실행 해주시기 바랍니다.');
        alert('2010년 2월 중 오픈베타 예정입니다.');
    }
}
function PopCouponClickEventHandler() {
    //alert("현재 등록 가능한 쿠폰이 존재 하지 않습니다.");
    //    var mem_hdReslutCD = $("input:#hdReslutCD");
    //    var mem_divRegister = $("div:#divRegister");
    //    var mem_divSearch = $("div:#divSearch");
    //    var mem_txtCoupon = $("input:#txtCoupon");
    //        
    //    if ($("input:#hdIsLogin").val() == "False") {
    //        alert("로그인을 하신 후 이용 가능 합니다.");
    //        return;
    //    }

    //    if (mem_hdReslutCD.val() == "200" || mem_hdReslutCD.val() == "100") {
    //        alert("이미 2차테스터에 당첨되셨습니다.");
    //        return;
    //    }

    //    mem_divRegister.show();
    //    mem_txtCoupon.focus();
    //    mem_txtCoupon.val("");
    location = "/Event/Read?EventNo=21";
}

function NotLookingForADay() {
    var todayDate = new Date();
    var todayDateString = todayDate.getFullYear() + "-" + GetTwoValue(todayDate.getMonth() + 1) + "-" + GetTwoValue(todayDate.getDate());
    todayDate.setDate(todayDate.getDate() + 1);

    document.cookie = 'exIntro=' + escape(todayDateString) + ';expires=' + todayDate.toGMTString() + ';domain=' + domain + ";path=/";

    location = "/main";
}

function GetTwoValue(value) {
    return value < 10 ? "0" + value : "" + value;
}

