﻿
function initMoving(target, position, topLimit, btmLimit) {
    if (!target)
        return false;


    var obj = target;
    obj.initTop = position;
    obj.topLimit = topLimit;
    obj.bottomLimit = Math.max(document.documentElement.scrollHeight, document.body.scrollHeight) - btmLimit - obj.offsetHeight;


    obj.style.position = "relative";
    obj.top = obj.initTop;
    obj.left = obj.initLeft;


    if (typeof (window.pageYOffset) == "number") {    //WebKit
        obj.getTop = function() {
            return window.pageYOffset;
        }
    } else if (typeof (document.documentElement.scrollTop) == "number") {
        obj.getTop = function() {
            return Math.max(document.documentElement.scrollTop, document.body.scrollTop);
        }
    } else {
        obj.getTop = function() {
            return 0;
        }
    }


    if (self.innerHeight) {    //WebKit
        obj.getHeight = function() {
            return self.innerHeight;
        }
    } else if (document.documentElement.clientHeight) {
        obj.getHeight = function() {
            return document.documentElement.clientHeight;
        }
    } else {
        obj.getHeight = function() {
            return 500;
        }
    }


    obj.move = setInterval(function() {
        if (obj.initTop > 0) {
            pos = obj.getTop() + obj.initTop;
        } else {
            pos = obj.getTop() + obj.getHeight() + obj.initTop;
            //pos = obj.getTop() + obj.getHeight() / 2 - 15;
        }


        if (pos > obj.bottomLimit)
            pos = obj.bottomLimit;
        if (pos < obj.topLimit)
            pos = obj.topLimit;


        interval = obj.top - pos;
        obj.top = obj.top - interval / 3;
        obj.style.top = obj.top + "px";
    }, 30)
}

/*
* flash activation
*/
function flashActivate() {
    var object = new String;
    var params = new String;
    var objParams = new String;
    var embedParams = new String;
    var objID = new String;

    this.init = function(objectID, flashURL, W, H) {
        objID = objectID;
        object = "<object id='" + objectID + "' width='" + W + "' height='" + H + "' classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000' codebase='http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0'>";
        object += "";
        objParams = "<param name='movie' value='" + flashURL + "' />";
        embedParams = "src='" + flashURL + "' ";
        embedParams += "name='" + objectID + "' ";
        embedParams += "width='" + W + "' height='" + H + "' ";
    };
    this.param = function(param, value) {
        params += "";
        objParams += "<param name='" + param + "' value='" + value + "' />";
        embedParams += param + "='" + value + "' ";
    };
    this.setAttr = function(param, value) {
        if (param.toLowerCase() == "flashvars") {
            getFlashMovieObject(objID).SetVariable(value.split("=")[0], value.split("=")[1]);
        } else {
            getFlashMovieObject(objID).setAttribute(param, value);
        }
    };
    this.load = function() {
        var embedTag = "<embed " + embedParams + "pluginspage='http://www.macromedia.com/go/getflashplayer' type='application/x-shockwave-flash' />";
        var objTag = object + objParams + embedTag + "</object>";
        //alert(objTag);
        document.write(objTag);
    };
};

function getFlashMovieObject(movieName) {
    if (window.document[movieName]) return window.document[movieName];
    if (navigator.appName.indexOf("Microsoft Internet") == -1) {
        if (document.embeds && document.embeds[movieName]) return document.embeds[movieName];
    } else {
        return document.getElementById(movieName);
    }
};


function MainMenu(menuName) {

    switch (menuName) {
        case "sub1_1":
            location.href = "/News/NewsList.aspx";          // 공지사항 페이지로 이동
            break;
        case "sub1_2":
        case "event_list":
            location.href = "/News/EventList.aspx";          // 이벤트 페이지로 이동
            break;
        case "sub1_3":
            location.href = "/News/NewsList.aspx?noticetype=3"; // 업데이트 페이지로 이동
            break;
        case "sub2_1": 
            location.href = "/Community/FreeBoard/FreeBoardList.aspx";   // 자유게시판 페이지로 이동 
            break; 
        case "sub2_2": 
            location.href = "/Community/ScreenShot/ScreenShotList.aspx";   // 스크린샷/동영상 페이지로 이동
            break;
        case "sub3_1":
        case "banner2":
            location.href = "/Guide/GameStart.aspx";   // 게임시작 페이지로 이동
            break;
        case "sub3_2": 
            location.href = "/Guide/GameControl.aspx";   // 조작법 페이지로 이동
            break;
        case "sub3_3":
            location.href = "/Guide/MapInfo.aspx";   // 지도정보 페이지로 이동
            break;
        case "sub3_4":
            location.href = "/Guide/WeaponInfo.aspx";   // 무기정보 페이지로 이동
            break;
        case "sub3_5":
            location.href = "/Guide/CharacterInfo.aspx";   // 캐릭터정보 페이지로 이동
            break;
        case "sub4_1": 
        case "banner1": 
            location.href = "/Download/Download.aspx";   // 다운로드 페이지로 이동 
            break;             
        case "sub5_1": 
        case "banner3": 
            location.href = "/Support/Faq/FaqList.aspx";   // 자주묻는질문 페이지로 이동 
            break; 
        case "sub5_2": 
        case "banner4": 
            location.href = "/Support/Inquiry/InquiryWrite.aspx";   // 1:1문의 페이지로 이동 
            break; 
        case "sub5_3": 
            location.href = "/Support/Inquiry/InquiryWrite.aspx?section=bug";   // 버그신고 페이지로 이동 
            break; 
        case "sub5_4": 
            location.href = "/Support/Policy/Policy.aspx";   // 운영정책 페이지로 이동
            break;
        case "banner5":         
            window.open("http://blog.metalslug.co.kr","blog");   // 배너 블로그 페이지로 이동 
            break;  
        case "index_go":
            location.href = "/Default.aspx";
            break;
        case "game_start": 
            RunGameStart(); // 게임 스타트버튼 링크
            break;        
        case "map1":
            location.href = "/Guide/MapInfo.aspx?maptype=1";
            break;
        case "map2":
            location.href = "/Guide/MapInfo.aspx?maptype=2";
            break;
        case "map3":
            location.href = "/Guide/MapInfo.aspx?maptype=3";
            break;
        case "map4":
            location.href = "/Guide/MapInfo.aspx?maptype=4";
            break;
        case "map5":
            location.href = "/Guide/MapInfo.aspx?maptype=5";
            break;            
        case "event_notice":
            location.href = "/News/NewsList.aspx?noticetype=2";
            break;
        case "open_event1":
            location.href = "/Event/1010/Open/EventMain.aspx";
            break;
        case "item_1":
            location.href = "/ItemShop/ShopList.aspx";
            break;
        case "item_2":
            location.href = "/ItemShop/MyItemBox.aspx";
            break;
        default:
            alert(menuName);
            break;
    }
}

function RunGameStart() {
    var isLogin = GetCookie("FPSCamp");
    
    if (isLogin == "" || isLogin == null) {
        alert('로그인 후 이용 가능합니다.');
        document.getElementById("txtUserID").focus();
        return false;
    }
    else {
        var Installed = false;
        try {
            var xObj = new ActiveXObject("DragonflyControl.DragonflyCtrl.1");
            if (xObj)
                Installed = true;
        }
        catch (ex) {
        }

        if (Installed) { // 설치된경우
            try {
                frames['ifrmGame'].location.href = "/Main/GameOn/GameStart.aspx";
            }
            catch (ex) {
            }
        }
        else {
            top.location.href = "/Main/GameOn/ActiveXGuide.aspx";
        }
    }
}

/**** 비로그인 & 로그인 박스 포커싱 ****/
function IsNotLoginBoard() {
    alert('로그인을 하셔야 게시물을 작성할 수 있습니다.');
    document.getElementById("txtUserID").focus();
}

function IsNotLoginComment() {
    alert('꼬릿글 작성을 위해 먼저 로그인을 해 주세요.');
    document.getElementById("txtUserID").focus();
}

function IsNotPAgreeBoard() {
    alert('게시물 작성을 위해 먼저 부모님의 동의가 필요합니다.\r\n로그인 박스의 동의센터 바로 가기 버튼을 눌러주세요.');
    return false;
}

function IsNotPAgreeVote() {
    alert('투표를 위해 먼저 부모님의 동의가 필요합니다.\r\n로그인 박스의 동의센터 바로 가기 버튼을 눌러주세요.');
    return false;
}

function IsNotPAgreeComment() {
    alert('꼬릿글 작성을 위해 먼저 부모님의 동의가 필요합니다.\r\n로그인 박스의 동의센터 바로 가기 버튼을 눌러주세요.');
    return false;
}

/**** 문자열 공백 제거 ****/
//var xx = "  포인트 : 30,000 결제 성공  ";
//alert(xx);
//alert("<" + xx.trim() + ">");
//alert("<" + xx.trim("포인트:,") + ">");
//alert("<" + xx.ltrim("포인트: ") + ">");
//alert("<" + xx.rtrim("결제성공 ") + ">"); 

String.prototype.trim = function(chars) {
    if (chars) {
        var str = "[" + chars + "\\s]+";
        return this.replace(new RegExp(str, "g"), "");
    }
    return this.replace(/^\s+|\s+$/g, "");
}
String.prototype.ltrim = function(chars) {
    if (chars) {
        var str = "^[" + chars + "\\s]+";
        return this.replace(new RegExp(str, "g"), "");
    }
    return this.replace(/^\s+/, "");
}
String.prototype.rtrim = function(chars) {
    if (chars) {
        var str = "[" + chars + "\\s]+$";
        return this.replace(new RegExp(str, "g"), "");
    }
    return this.replace(/\s+$/, "");
}   
/**** 한글 바이트 체크 하기 ****/
String.prototype.bytes = function() {
    var str = this;
    return str.replace(/[^\x00-\x7F]/g, "xx").replace(/([<\x0D])/g, function($0, $1) { return ($1 == "<") ? "&lt;" : "<br>" + $1; }).length;    
}

String.prototype.cut = function(len, tail) {
    var str = this;
    var l = 0;
    for (var i = 0; i < str.length; i++) {
        l += (str.charCodeAt(i) > 128) ? 2 : 1;
        if (l > len) return str.substring(0, i) + tail;
    }
    return str;
}

// 쿠키 설정
function SetCookie(name, value, expiredays) {
    var todayDate = new Date();
    todayDate.setHours(todayDate.getHours() + expiredays);
    document.cookie = name + "=" + escape(value) + "; path=/; expires=" + todayDate.toGMTString() + ";"
}

// 쿠키 불러오기
function GetCookie(name) {
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1) {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    } else
        begin += 2;
    var end = document.cookie.indexOf(";", begin);
    if (end == -1)
        end = dc.length;
    return unescape(dc.substring(begin + prefix.length, end));
}
