/**
 * @require prototype.js
 * @require scriptaculous.js
 */
var atomSpec = {

    /**
     * @var object
     * @access private
     */
    specIndex: {
        "atomTextConstruct"     : "S3.1",
        "atomPersonConstruct"   : "S3.2",
        "atomName"              : "S3.2.1",
        "atomUri"               : "S3.2.2",
        "atomEmail"             : "S3.2.3",
        "atomDateConstruct"     : "S3.3",
        "atomFeed"              : "S4.1.1",
        "atomEntry"             : "S4.1.2",
        "atomContent"           : "S4.1.3",
        "atomAuthor"            : "S4.2.1",
        "atomCategory"          : "S4.2.2",
        "atomContributor"       : "S4.2.3",
        "atomGenerator"         : "S4.2.4",
        "atomIcon"              : "S4.2.5",
        "atomId"                : "S4.2.6",
        "atomLink"              : "S4.2.7",
        "atomLogo"              : "S4.2.8",
        "atomPublished"         : "S4.2.9",
        "atomRights"            : "S4.2.10",
        "atomSource"            : "S4.2.11",
        "atomSubtitle"          : "S4.2.12",
        "atomSummary"           : "S4.2.13",
        "atomTitle"             : "S4.2.14",
        "atomUpdated"           : "S4.2.15"
    },

    /**
     * @var string
     * @access private
     */
    currentStyle: "",

    /**
     * @var integer
     * @access private
     */
    currentPos: 0,

    /**
     * @var string
     * @access private
     */
    history: ["spec-toc"],

    /**
     * @param string strStyle
     * @return void
     * @access public
     */
    show: function (strStyle)
    {
        if (!strStyle) {
            var rexRegExp = new RegExp('.*atom-spec=([^;]*).*');
            if (document.cookie && unescape(document.cookie).match(rexRegExp)) {
                strStyle = unescape(document.cookie).replace(rexRegExp,'$1');
            } else {
                strStyle = "all";
            }
        }
        if (strStyle != this.currentStyle) {
            /*
            $("container").setOpacity(0);
            */
            if (strStyle == "chart") {
                $("controller").show();
                $("left").show();
                $("right").hide();
                $("left").style.width = "100%";
                $("spec-container").innerHTML = "";
            } else if (strStyle == "spec") {
                $("controller").hide();
                $("left").hide();
                $("right").show();
                $("right").style.width = "100%";
                $("spec-container").innerHTML = $("specification").innerHTML;
                Event.stopObserving("spec-container", "click", this.specLinkOnClick, false);
            } else {
                $("controller").show();
                $("left").show();
                $("right").show();
                $("left").style.width = "49%";
                $("right").style.width = "50%";
                $("spec-container").innerHTML = $("spec-toc").innerHTML;
                Event.observe("spec-container", "click", this.specLinkOnClick, false);
            }
            this.currentStyle = strStyle;
            document.cookie = 'atom-spec=' + escape(strStyle) + ';path=' + location.pathname;
            /*
            new Effect.Appear($("container"), {duration:0.3});
            */
        }
    },

    /**
     * @param object elmElement
     * @param string  strId
     * @return void
     * @access public
     */
    showElement: function(elmElement, strId)
    {
        elmElement = this.getTargetElement(elmElement);
        elmElement.innerHTML = $(strId).innerHTML;
        elmElement.innerHTML = $(strId).innerHTML;
        new Effect.toggle(elmElement, "blind", {duration:0.3});
        if ($("right").visible() && this.specIndex[strId]) {
            this.showSpec(this.specIndex[strId], true);
        }
    },

    /**
     * @param string strId
     * @param boolean bolApplyEffect
     * @return void
     * @access public
     */
    showSpec: function (strId, bolApplyEffect)
    {
        if (strId != this.history[this.currentPos]) {
            if (this.currentPos < this.history.length -1) {
                this.history = this.history.slice(0, this.currentPos +1);
            }
            this.history[this.history.length] = strId;
            this.currentPos++;
            $("spec-container").innerHTML = $(strId).innerHTML;
            /*
            if (bolApplyEffect) {
                $("spec-container").setOpacity(0);
            }
            $("spec-container").innerHTML = $(strId).innerHTML;
            if (bolApplyEffect) {
                new Effect.Appear($("spec-container"), {duration:0.3});
            }
            */
        }
    },

    /**
     * @param string strPos
     * @return void
     * @access public
     */
    showHistory: function (strPos)
    {
        if (strPos == 'back') {
            if (this.currentPos > 0) {
                this.currentPos--;
                $("spec-container").innerHTML = $(this.history[this.currentPos]).innerHTML;
            } else {
                this.currentPos = 0;
            }
        } else if (strPos == 'forth') {
            if (this.currentPos < this.history.length -1) {
                this.currentPos++;
                $("spec-container").innerHTML = $(this.history[this.currentPos]).innerHTML;
            } else {
                this.currentPos = this.history.length -1;
            }
        }
    },

    /**
     * @param object elmElement
     * @return object
     * @access private
     */
    getTargetElement: function (elmElement)
    {
        elmElement = elmElement.parentNode;
        while (!elmElement.tagName || elmElement.tagName.toLowerCase() != "div") {
            elmElement = elmElement.parentNode;
        }
        var objDivNodes = elmElement.getElementsByTagName("div");
        return objDivNodes[0];
    },

    /**
     * @param object evtEvent
     * @return void
     * @access private
     */
    specLinkOnClick: function (evtEvent) {
        var elmElement = Event.element(evtEvent);
        if (elmElement.href) {
            var strBase = location.href.replace(/#.+/, "")
            var strId = elmElement.href.replace(strBase, "");
            if (strId.indexOf("#") == 0) {
                strId = strId.replace("#", "");
                atomSpec.showSpec(strId.replace("#", ""), true);
            } else {
                window.open(elmElement.href);
            }
        }
        Event.stop(evtEvent);
    }
}

