/**
* @author Jang Ki Woong
**/

/*
* enable image cache for less than internet explorer 6
*/
try { document.execCommand('BackgroundImageCache', false, true); } catch (e) { }

/*
*  BLITZ2
*/

$(document).ready(function () {

    /* board list fold/unfold event */
    $("li:not(.blocked) > div.boardArticle").click(function () {

        var min_height = 75;
        var max_height = $(this).find('dl.content').height() + $(this).find('div.commentWrap').height() + 13;
        var $parent = $(this).parent();

        if ($parent.hasClass('on')) {
            $parent.removeClass('on');
            $(this).animate({ height: min_height }, 150, function () {
                parent.iframeResize($(document).find('body').height());                
            });

        } else {
            $parent.addClass('on');
            $(this).animate({ height: max_height }, 150, function () {                                
                parent.iframeResize($(document).find('body').height());
            });
        }
    });

    /* stop event - follow / function button */
    $(".boardArticle > p a").click(function (event) {
        event.stopImmediatePropagation();
    });

    $(".boardArticle dl.content > dd a").not('[lang]').click(function (event) {
        event.stopImmediatePropagation();
    });

    //    /* stop event - content link */
    //    $(".boardArticle dl.content > dd a").click(function (event) {
    //        event.stopImmediatePropagation();                
    //        return false;
    //    });


    /* each list hover effect */
    $(".boardList > li:not(.header)").hover(function () {
        $(this).addClass('hover');
    }, function () {
        $(this).removeClass('hover');
    });
    /* each comment item background style */
    $("ul.commentList > li:odd").addClass('odd');


    /* set footer game grade icon */
    new setGradeIcon().hide();

});

/*
* footer game grade icon
*/
function setGradeIcon() {
    var self = this;
    this.object = $("#gradeICON");

    $("#footer p.grade").hover(function () {
        self.object.fadeIn();
    }, function () {
        self.hide();
    });
};
setGradeIcon.prototype.hide = function () {
    this.object.fadeIn().animate({ 'opacity': 1 }, 3000, function () { $(this).fadeOut(); });
};

/*
* rolling text
*/
function rollText(options) {
    var self = this;
    this.object = $(options.visual);
    this.roller = $(options.roller);
    this.text = $(options.text);
    this.margin = options.margin;
    this.width = this.text.width();
    this.speed = options.speed || 10;
    this.delay = options.delay || 100;

    if (this.width > this.object.width()) {
        this.text.clone().appendTo(this.text);
        this.play();
    }
};
rollText.prototype.play = function () {
    var self = this;
    this.roller.animate({ marginLeft: ((this.width + (this.margin * 2)) * -1) }, (this.speed * this.width),
			function () {
			    self.roller.css({ marginLeft: (self.margin * -1) });
			    self.timer = setTimeout(function () { self.play(); }, self.delay);
			}
		);
};
rollText.prototype.stop = function () {
    clearTimeout(this.timer);
};

/*
* initialize tabs
*/
function initTabs(options) {
    var self = this;
    this.object = $(options.tab);
    this.tabs = $(options.items);
    this.items = this.tabs.children("a");
    this.init = options.init || 0;

    this.items.click(function () {
        var tab = self.getRealID($(this));
        self.hideTabs();
        self.tabs.eq($(this).parent().index()).addClass("current");
        $("#" + tab).show();
        return false;
    });
};
initTabs.prototype.hideTabs = function () {
    var self = this;
    this.items.each(function () {
        var tab = self.getRealID($(this));
        $("#" + tab).hide();
    });
    this.tabs.each(function () {
        $(this).removeClass("current");
    });
};
initTabs.prototype.getRealID = function (id) {
    return $(id).attr("href").replace("#", "");
};

/*
* initialize image slider 
*/
function imageSlider(options) {
    var self = this;
    this.object = $(options.object);
    this.items = this.object.children("li");
    this.navi = $(options.navi);
    this.width = options.width;
    this.listmax = options.listmax;
    this.length = this.items.length;

    this.navi.eq(0).click(function () {
        self.action("prev");
    });
    this.navi.eq(1).click(function () {
        self.action("next");
    });
};
imageSlider.prototype.action = function (dir) {
    var self = this;
    var index = Math.round(parseInt(this.object.css('marginLeft')) / this.width * -1);

    if (dir == "next") {
        if (index >= (self.length - self.listmax)) return false;
        var left = -index * this.width - this.width;
        this.object.append(this.items.eq(index).clone());
        this.object.animate({ marginLeft: left }, 500);
    }
    if (dir == "prev") {
        if (index <= 0) return false;
        var left = parseInt(this.object.css('marginLeft')) - (this.width * -1);
        this.object.animate({ marginLeft: left }, 500);
    }
};

/*
* 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);
    }
};

/*
* id / pw background setting
*/
function setBackground(o) {
    if (o.value) return;
    o.className = o.className.replace(/(onfocus)/gi, "");
};
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).find("a"));
        obj.p = this.getElements($(obj).find("blockquote"));

        for (var i in obj.l) {
            (function (idx) {
                $(obj.l[idx]).mouseover(function () {
                    bpos = $("#content").offset();
                    lpos = $(this).parent().offset();
                    $(obj.p[idx]).css({ "top": (lpos.top - bpos.top + 30), "left": (lpos.left - bpos.left) });
                    $(obj.p[idx]).show();
                });

                $(obj.l[idx]).mouseout(function () {
                    $(obj.p[idx]).hide();
                });
            })(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";

        offset = { "left": parseInt(handler.style.left), "top": parseInt(handler.style.top),
            "x": parseInt(handler.offsetWidth), "y": parseInt(handler.offsetHeight),
            "width": parseInt(document.documentElement.clientWidth), "height": parseInt(document.documentElement.clientHeight) + parseInt(document.documentElement.scrollTop)
        };

        if (offset.left <= 0) handler.style.left = "0px";
        if (offset.top <= 0) handler.style.top = "0px";
        if (offset.left + offset.x >= offset.width) handler.style.left = offset.width - offset.x + "px";
        if (offset.top + offset.y >= offset.height) handler.style.top = offset.height - offset.y + "px";
    };
    function onDrop(e) {
        document.onmousemove = null;
        document.onmouseup = null;
        document.onselectstart = null;
        handler.style.cursor = "auto";
        handler.style.zIndex = prezIndex;
    };
};

/*
* top button
*/
var topButton = function (handler, min, max) {
    //if(navigator.appName == "Microsoft Internet Explorer" && navigator.appVersion.indexOf("MSIE 6.0") != -1) {
    //handler.style.display = "none";
    //}

    var posY;
    var height = document.documentElement.scrollHeight;

    handler.onclick = function () { window.scrollTo(0, 0); return false; };
    window.onscroll = function () {
        pastY = posY || min;
        posY = document.documentElement.scrollTop;
        posY = (posY >= (height - max)) ? (height - max) : posY;
        posY = (posY <= min) ? min : posY;

        handler.style.top = posY + "px";

        if (posY >= (height - max)) {
            posY = (height - max);
        }
    };
};

var idtimer = {};
var key = 0;
function flashMethodCall(id, fnName, args) {

    if (document.all) {
        var obj = null;
        if (document.getElementById(id) && document.getElementById(id)[fnName]) {

            window.clearTimeout(idtimer[id + key]);

            idtimer[id + key] = null;
            key = 0;

            obj = document.getElementById(id);                                        
            if (args.length === 1) obj[fnName](args[0]);
            else if (args.length === 2) obj[fnName](args[0], args[0]);
            else if (args.length === 3) obj[fnName](args[0], args[1], args[2]);            
        }
        else {
            idtimer[id + key] = window.setTimeout(function () { flashMethodCall(id, fnName, args); }, 100);
        }
    }
    else {
        if (document.embeds && document.embeds[id][fnName]) {
            window.clearTimeout(idtimer[id + key]);

            idtimer[id + key] = null;
            key = 0;
                                    
            if (args.length === 1) document.embeds[id][fnName](args[0]);
            else if (args.length === 2) document.embeds[id][fnName](args[0], args[0]);
            else if (args.length === 3) document.embeds[id][fnName](args[0], args[1], args[2]);            
        }
        else {
            idtimer[id + key] = window.setTimeout(function () { flashMethodCall(id, fnName, args); }, 100);
        }
    }

    key += 1;
};

