﻿/**
* DF javascript framework v0.9
*
* @author Jang Ki Woong
**/

/*
* enable image cache for less than internet explorer 6
*/
try { document.execCommand('BackgroundImageCache', false, true); } catch (e) { }

(function() {
    var DF = {
        /**
        * Verify when the DOM is ready.
        * Callback is the anonymous function to execute when the dom is ready
        **/
        ready: function(callback) {
            var done = false;
            var checkLoaded = setInterval(function() { if (document.body && document.getElementById) { done = true; } }, 10);
            var checkInter = setInterval(function() { if (done) { clearInterval(checkLoaded); clearInterval(checkInter); callback(); } }, 10);
        },

        /**
        * Extend method
        * @function
        * @param	{ Element, Object }
        * @return	{ none }
        **/
        extend: function(target, source) {
            for (var i in source) {
                target[i] = source[i];
            }
        },

        /**
        * @function
        * @param	{ Element }
        * @return	{ String }
        **/
        tag: function() {
            return arguments[0].tagName.toLowerCase();
        },

        /**
        * @function
        * @param	{ String }
        * @return	{ Element | Array }
        **/
        $: function() {
            var elems = [];
            var el = null;
            var regx = new RegExp(/^([a-z]+)?([#|.])([a-z0-9]+)$/i);

            for (var i = 0; i < arguments.length; i++) {
                el = arguments[i];
                if (typeof el === "string") {
                    if (regx.test(el)) {
                        if (RegExp.$2 == "#") {
                            el = document.getElementById(RegExp.$3);
                            elems.push(el);
                        } else if (RegExp.$2 == ".") {
                            var pattern = new RegExp("(^| )" + RegExp.$3 + "( |$)");
                            var e = document.getElementsByTagName((RegExp.$1) ? RegExp.$1 : "*");
                            for (var x = 0; x < e.length; x++) {
                                if (pattern.test(e[x].className)) {
                                    elems.push(e[x]);
                                }
                            }
                        }
                    } else {
                        var e = document.getElementsByTagName(el);
                        for (var x = 0; x < e.length; x++) {
                            elems.push(e[x]);
                        }
                    }
                }
            }

            for (var i = 0; i < elems.length; i++) {
                DF.extend(elems[i], DF.Element);
            }
            //elems.each = DF.Array.each;
            return (elems.length > 1) ? elems : elems[0];
        }
    };

    DF.Element = {
        /**
        * @function
        * @param	{ String }
        * @return	{ Object }
        **/
        child: function() {
            return this.getElementsByTagName(arguments[0]);
        },

        /**
        * @function
        * @param	{ _none }
        * @return	{ _none }
        **/
        show: function() {
            var s = this.style;
            var b = "block";
            var l = { p: b, div: b, form: b, h1: b, h2: b, h3: b, h4: b, ol: b, ul: b, fieldset: b, dl: b, dt: b, dd: b, td: "table-cell", th: "table-cell", li: "list-item", table: "table", thead: "table-header-group", tbody: "table-row-group", tfoot: "table-footer-group", tr: "table-row", col: "table-column", colgroup: "table-column-group", caption: "table-caption" };
            try {
                s.display = (typeof l[DF.tag(this)] === "string") ? l[DF.tag(this)] : "inline";
            } catch (e) {
                s.display = b;
            }
            return this;
        },
        /**
        * @function
        * @param	{ _none }
        * @return	{ _none }
        **/
        hide: function() {
            this.style.display = "none";
            return this;
        },
        /**
        * @function
        * @param	{ String }
        * @return	{ _none }
        **/
        addClass: function(classname) {
            var el = this;
            if (this.hasClass(classname)) return this;
            el.className = (el.className + " " + classname).replace(/^\s+/, "");
            return this;
        },

        /**
        * @function
        * @param	{ String }
        * @return	{ _none }
        **/
        removeClass: function(classname) {
            var el = this;
            el.className = el.className.replace(classname, "").replace(/^\s+|\s+$/, "");
            return this;
        },

        /**
        * @function
        * @param	{ String }
        * @return	{ boolean }
        **/
        hasClass: function(classname) {
            return (this.className).indexOf(classname) > -1;
        },

        /**
        * @function
        * @param	{ Object | String }
        * @return	{ none }
        **/
        css: function() {
            var el = this;
            if (typeof arguments[0] === "object") {
                DF.extend(this.style, arguments[0]);
            } else {
                el.style[arguments[0]] = arguments[1];
            }
            return this;
        }
    };


    DF.Array = {
        /**
        * @function
        * @param	{ Array }
        * @return	{ _none }
        **/
        each: function(callback) {
            if (typeof callback != "function") throw new TypeError();
            for (var i = 0; i < this.length; i++) {
                if (i in this) {
                    callback.call(this[i], []);
                }
            }
            return this;
        },

        object2Array: function(obj) {
            var arr = [];
            if (typeof obj !== "object") return obj;
            for (var i in obj) {
                arr.push([i, obj[i]]);
            }
            return arr;
        }
    };

    DF.extend(Array.prototype, DF.Array);
    window.$ = DF.$;
    document.ready = DF.ready;
})();


document.ready(function() {
    if ($(".boardPaging")) {
        if ($(".boardPaging").child("span").length < 1) return false;
        $(".boardPaging").child("span")[0].className = "first-child";
    }
});

/*
* 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);
    }
};

/**
* recently article tabs initializing
**/
var RecentNotice = {
    init: function(w, o, m) {
        var p = w.child("ul");
        var c = o.child("a");

        for (var i in c) {
            (function(idx) {
                c[i].onclick = function() {
                    RecentNotice.reset(c, p);

                    this.parentNode.className = "current";
                    if (idx != 0) this.parentNode.style.marginLeft = "-1px";
                    $(this.href.match("\#[A-Z]+")[0]).style.display = "block";
                    m.href = this.rel;
                    return false;
                };
            })(i);
        }
    },
    reset: function(c, p) {
        for (var i in c) {
            if (/[0-9]/.test(i)) {
                c[i].parentNode.className = "";
                if (i != 0) c[i].parentNode.style.marginLeft = 0;
            }
        }
        for (var x = 0; x < p.length; x++) {
            if (p[x].id != "recentTab") p[x].style.display = "none";
        }
    }
};

/*
* id / pw background setting
*/
function setBackground(o) {
    if (o.value) return;
    o.className = o.className.replace(" onfocus", "");
};
function removeBackground(o) {
    o.className = o.className + " onfocus";
};

/*
* initialize board content preview on board list
*/
var boardPreview = {
    init: function(obj) {
        obj.l = this.getElements(obj.getElementsByTagName("a"));
        obj.p = this.getElements(obj.getElementsByTagName("blockquote"));

        for (var i in obj.l) {
            (function(idx) {
                obj.l[i].onmouseover = function() {
                    bpos = { "x": obj.offsetLeft, "y": obj.offsetTop };
                    lpos = { "x": this.parentNode.offsetLeft, "y": this.parentNode.offsetTop };
                    obj.p[idx].style.top = bpos.y + lpos.y + 24 + "px";
                    obj.p[idx].style.left = bpos.x + lpos.x + 10 + "px";
                    obj.p[idx].style.display = "block";
                };

                obj.l[i].onmouseout = function() {
                    obj.p[idx].style.display = "none";
                };
            })(i);
        }
    },
    getElements: function(obj) {
        tmp = [];
        j = 0;

        for (i = 0; i < obj.length; i++) {
            if (obj[i].className != "preview") continue;
            tmp[j] = obj[i];
            j++;
        }

        return tmp;
    }
};

/*
* initialize FAQ list
*/
var faqList = {
    init: function(obj, list, answer) {
        if (!obj || list.length <= 0 || answer.length <= 0) return;

        obj.faqQ = [];
        obj.faqA = answer;

        var j = 0;
        for (i = 0; i < list.length; i++) {
            if (list[i].className != "question") continue;
            obj.faqQ[j] = list[i];
            j++;
        }

        for (var x in obj.faqQ) {
            (function(idx) {
                obj.faqQ[idx].onclick = function() {
                    this.className = (this.className == "on") ? "" : "on";
                    obj.faqA[idx].style.display = (obj.faqA[idx].style.display == "block") ? "none" : "block";
                    return false;
                }
            })(x);
        }
    }
};

/*
* moveable layer popup
*/
var LayerMover = function(handler) {
    handler.onmouseup = onDrop;
    handler.onmousedown = function(e) {
        var e = e || event;
        absolute = { x: e.clientX, y: e.clientY };
        relative = { x: parseInt(handler.offsetLeft) - absolute.x, y: parseInt(handler.offsetTop) - absolute.y };
        prezIndex = parseInt(handler.style.zIndex);
        zIndex = parseInt(handler.style.zIndex) + 100;

        handler.style.left = handler.offsetLeft;
        handler.style.marginLeft = "0px";
        handler.style.zIndex = zIndex;

        document.onmouseup = onDrop;
        document.onmousemove = onMove;
        document.onselectstart = function() { return false; };
    };
    handler.ondragstart = function() { return false; };

    function onMove(e) {
        var e = e || event;
        handler.style.left = (e.clientX + relative.x) + "px";
        handler.style.top = (e.clientY + relative.y) + "px";
        handler.style.cursor = "move";

        if (parseInt(handler.style.left) <= 0) handler.style.left = "0px";
        if (parseInt(handler.style.top) <= 0) handler.style.top = "0px";
    };
    function onDrop(e) {
        document.onmousemove = null;
        document.onmouseup = null;
        document.onselectstart = null;
        handler.style.cursor = "default";
        handler.style.zIndex = prezIndex;
    };
};