// Copyright (c) 2004-2005 koikikukan All Rights Reserved.
// http://yujiro.dyndns.com/blog/koikikukan/
// License is granted if and only if this entire
// copyright notice is included. By Yujiro ARAKI.

// Ver1.00 initial version.
// Ver2.00 add the state maintenance function by cookie.
// Ver3.00 improve cookie processing.
// Ver3.01 corresponds to Mac+IE.
// Ver4.00 2005.03.31 add link display by block.
// Ver5.00 2005.08.22 add Ajax library.
// Ver5.01 2005.08.27 add flag of Ajax selection for subcategory.
// Ver5.01en 2005.11.20 for english.

function deleteValue(name, nameEQ) {
    var ca = document.cookie.split(';');
    var newData = new Array();

    // Repeat by cookie
    for(var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') {
            c = c.substring(1,c.length);
        }
        if (c.indexOf(nameEQ) == 0) {

            // Delete the corresponding name.
            var data = c.substring(nameEQ.length,c.length);
            var list = data.split('|');
            for(var x = 0; x < list.length; x++) {
                if (list[x] != name) {

                    // corresponds to Mac+IE
                    newData[newData.length] = list[x];
                }
            }
            return newData.join('|');
        }
    }

    // Return empty when cookie does not exist.
    return '';
}

function hasName(name, nameEQ) {
    var ca = document.cookie.split(';');
    for(var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') {
            c = c.substring(1,c.length);
        }
        if (c.indexOf(nameEQ) == 0) {
            var data = c.substring(nameEQ.length,c.length);
            var list = data.split('|');
            for(var x = 0; x < list.length; x++) {
                if (list[x] == name) {
                    return true;
                }
            }
            return false;
        }
    }
}

function createCookieByValue(name, onoff, days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    } else {
        expires = "";
    }

    // Once delete a name from cookie.
    var newOnData = deleteValue(name, 'sidebarMenuOn=');
    var newOffData = deleteValue(name, 'sidebarMenuOff=');

    // Set up a name as new data.
    if (onoff == 'on') {
        if (newOnData != '') {
            newOnData += '|' + name;
        } else {
            newOnData = name;
        }
    } else {
        if (newOffData != '') {
            newOffData += '|' + name;
        } else {
            newOffData = name;
        }
    }

    // Save cookie.
    document.cookie = "sidebarMenuOn=" + newOnData + expires + "; path=/";
    document.cookie = "sidebarMenuOff=" + newOffData + expires + "; path=/";
}

function readCookieByValue(name) {

    // Search a menu.
    // Return empty if there is nothing to both.
    if (hasName(name, 'sidebarMenuOn=')) {
        return 'on';
    }
    if (hasName(name, 'sidebarMenuOff=')) {
        return 'off';
    }
    return null;
}

// Get list number.
function getListCount(objLists, viewNum, idName, linkNumber, trackbackNumber, rightMarkForListNumber, subCategoryCount, leftMarkForListNumber, countTag, offsetValue) {

    var objItems;
    var href;
    var commentCounter = 0;

    // List of subCategory.
    if (subCategoryCount && (idName.indexOf('subcategories') == 0)) {
        objItems = objLists.getElementsByTagName('li');

    // Use specified tag.
    } else if (countTag == 'li') {
        objItems = objLists.getElementsByTagName(countTag);

    // else(a element)
    } else {
        objItems = objLists.getElementsByTagName('a');
    }

    // Recent Comments
    if (idName == 'comment') {
        for (i = 0; i < objItems.length; i++) { // Repeat a tag.
            href = objItems[i].getAttribute("href");
            if(href.indexOf('#') == -1){ // Count if '#' exists in a href attribute.
                commentCounter++;
            }
        }
    }

    // Trackback
    var counter;
    if (idName == 'trackback') {
        commentCounter = objItems.length / trackbackNumber;
    }

    // Subtract the unnecessary number of links.
    if (idName.indexOf('link') == 0) {
        var linkName;
        for (j = 0; j < linkNumber; j++) {
            linkName = 'link' + (j + 1);
            if (idName == linkName) {
                counter = objItems.length - offsetValue[j];
            }
        }
    } else {

        // Subtract the value of a counter from 'Recent Comments'
        counter = objItems.length - commentCounter;
    }

    // decoration counter
    if (leftMarkForListNumber) {
        counter = leftMarkForListNumber + counter;
    }
    if (rightMarkForListNumber) {
        counter = counter + rightMarkForListNumber;
    }

    return counter;
}

// Set list number.
function setListNumber(buffer, listCount, menuTitle, listNumberPosition, tlspace) {

    // Display list number ahead.
    if (listNumberPosition) {
        buffer[buffer.length] = listCount;
        if (tlspace) {
            buffer[buffer.length] = tlspace;
        }
        buffer[buffer.length] = menuTitle;

    // Display list number back.
    } else {
        buffer[buffer.length] = menuTitle;
        if (tlspace) {
            buffer[buffer.length] = tlspace;
        }
        buffer[buffer.length] = listCount;
    }
}

// Set menu title and list number(for block).
function setMenuTitleForBlock(viewNum, buffer, listCount, menuTitle, listNumberPosition, tlspace) {

    // Display list number.
    if (viewNum) {
        setListNumber(buffer, listCount, menuTitle, listNumberPosition, tlspace);

    // No display list number.
    } else {
        buffer[buffer.length] = menuTitle;
    }
}

// Set menu title + list number.
function setMenuTitle(viewNum, buffer, listCount, menuTitle, listNumberPosition, tlspace) {

    // Display list number.
    if (viewNum) {

        // Display list number ahead.
        if (listNumberPosition) {
            buffer[buffer.length] = listCount;
            if (tlspace) {
                buffer[buffer.length] = tlspace;
            }
        }

        buffer[buffer.length] = menuTitle;

        // Display list number back.
        if (!listNumberPosition) {
            if (tlspace) {
                buffer[buffer.length] = tlspace;
            }
            buffer[buffer.length] = listCount;
        }

    // No display list number.
    } else {
        buffer[buffer.length] = menuTitle;
    }
}

// Execute folding(specified tag)
function FoldNavigationByTagName(idName, initMode, viewNum, countTag, sp) {

//--------------------------------------------------------
// Configuration
//--------------------------------------------------------

//--------------------------------------------------------
// Setting data for title of menu
//--------------------------------------------------------

var linkType = 'block';

//-----------------------
// speed of fold
//-----------------------

var speed = 'normal';

// speed of fold for subcategory(only speed:'slow')
// normal：'normal'
// slow：'slow'
var subcategorySpeed = 'normal';

//-----------------------
// fold mark
//-----------------------

// display mark
// display：true
// no display：false
var displayMark = false;

// fold mark
// up：closing mark
// down：opening mark
var openMarkForSideBarMenu  = '+';
var closeMarkForSideBarMenu = '-';

// Position of mark(only display mark)
// ahead of title：true
// back of title：false
// fold mark on a left end or a right edge. ：true
var preMarkForSideBarMenu = true;

//---------------
// list number
//---------------

// Position list number.
// ahead of title：true
// back og title：false
var listNumberPosition = true;

// Mark that bundles list number.
var leftMarkForListNumber = '';
var rightMarkForListNumber = '';

// Number of menus for which link number subtraction is necessary.
// When there is no subtracted menu:0
var linkNumber = 2;

// Offset value of subtracted each menu.
var offsetValue = new Array(linkNumber);
offsetValue[0] = 3;
offsetValue[1] = 2;

// Divisors of number of trackbacks
var trackbackNumber = 2;

// Method of subcategory list number calculation.
// using the li element：true
// using the a element：false
var subCategoryCount = true;

//-------------------
// Display position correction
//-------------------

// Title display position correction flag.
// correct：true
// No correct:false
var modificationFlag = true;

// Direction of title display position correction.
// corrects it right：true
// corrects it left：false
var centeringPosition = false;

// Title display position correction offset
var offsetForCentering = 0;

// Space of title and fold mark
var offsetForTitleAndMark = 0;

// Space of title and number of lists
var offsetForTitleAndLinkNumber = 1;

//--------------------------------------------------------
// Set data for subcategory list
//--------------------------------------------------------

// subcategory flag
// Effective：true
// Invalidity：false
var subCategory = true;

// Fold mark for subcategory.
// up：closing mark
// down：opning mark
var openMarkForSubCategories  = '+';
var closeMarkForSubCategories = '-';

// Fold mark insertion position for subcategory.
// ahead of category：true
// back of category：false
var preMarkForSubCategory = false;

// Title of subcategory and space of mark.
var offsetForTitleAndMarkOfSubcategory = 0;

//--------------------------------------------------------
// Set data for state maintenance.
//--------------------------------------------------------

// state maintenance flag.
// Effective：true
// Invalidity：false
var holdState = true;

//--------------------------------------------------------

    var openMark;  // The mark for opening, when having closed
    var closeMark; // The mark for closing, when open

    var idTitle = Array(idName,'name').join('');
    var idList = Array(idName,'list').join('');
    var objTitle = this.document.getElementById(idTitle);
    var objLists = this.document.getElementById(idList);

    if (!objTitle || !objLists) return;

    // folding mark
    openMark = openMarkForSideBarMenu;
    closeMark = closeMarkForSideBarMenu;
    if (subCategory) {
        if (idName.indexOf('subcategories') == 0) {
            openMark = openMarkForSubCategories;
            closeMark = closeMarkForSubCategories;
        }
    }

    var dispMode = objLists.style.display;
    if (!dispMode) {

        // Hold a fold-up state to cookie.
        if (holdState) {
            var cookie_initMode = readCookieByValue(idName);
            if(cookie_initMode){
                initMode = cookie_initMode;
            }
            createCookieByValue(idName, initMode, 365);
        }

        // start tag
        var buffer = new Array();
        var tmpText;

        // change start method
        if(sp != 'dummy'){
            speed = sp;
        }
        if(speed == 'slow') {
            tmpText = Array('FoldNavigationSlowly(',"'",idName,"','chng','');return(false);").join('');
        } else {
            tmpText = Array('FoldNavigation(',"'",idName,"','chng','');return(false);").join('');
        }
        var startTag = Array('<a class="foldmark" href="#" onclick="',tmpText,'" onkeypress="',tmpText,'">').join('');

        // end tag
        var endTag = '</a>';

        // display mark
        var foldMark = (initMode == 'off') ? openMark : closeMark;

        // title of menu
        var menuTitle = objTitle.innerHTML;

        // padding
        var padding = '';
        for (k = 0; k < offsetForCentering; k++) {
            padding += '&nbsp;';
        }

        // space for title and number of link
        var tlspace = '';
        for (l = 0; l < offsetForTitleAndLinkNumber; l++) {
            tlspace += '&nbsp;';
        }

        // space for title and mark
        var tmspace = '';
        for (l = 0; l < offsetForTitleAndMark; l++) {
            tmspace += '&nbsp;';
        }

        // space for title of subcategory and mark
        var scspace = '';
        for (l = 0; l < offsetForTitleAndMarkOfSubcategory; l++) {
            scspace += '&nbsp;';
        }

        // list number
        var listCount;
        if (viewNum) {
            listCount = getListCount(objLists, viewNum, idName, linkNumber, trackbackNumber, rightMarkForListNumber, subCategoryCount, leftMarkForListNumber, countTag, offsetValue);
        }

        // generate title with folding mark

        // subcategory list
        if (subCategory && (idName.indexOf('subcategories') == 0)) {
            if (preMarkForSubCategory) {
                buffer[buffer.length] = startTag;
                buffer[buffer.length] = foldMark;
                buffer[buffer.length] = endTag;
                if (scspace) {
                    buffer[buffer.length] = scspace;
                }
                buffer[buffer.length] = menuTitle;
            } else {
                buffer[buffer.length] = menuTitle;
                if (scspace) {
                    buffer[buffer.length] = scspace;
                }
                buffer[buffer.length] = startTag;
                buffer[buffer.length] = foldMark;
                buffer[buffer.length] = endTag;
            }

        // title of menu
        } else {

            // decide generating pattern(generate buffer)
            if (linkType == 'unblock') {

                // ahead of mark
                if (preMarkForSideBarMenu) {

                    // set folding mark
                    buffer[buffer.length] = startTag;
                    buffer[buffer.length] = foldMark;
                    buffer[buffer.length] = endTag;

                    // fix display position
                    if(modificationFlag && centeringPosition) {
                        buffer[buffer.length] = padding;
                    }

                    // space for title and mark
                    if(offsetForTitleAndMark) {
                        buffer[buffer.length] = tmspace;
                    }

                    // set title
                    setMenuTitle(viewNum, buffer, listCount, menuTitle, listNumberPosition, tlspace);

                    // fix display position
                    if(modificationFlag && !centeringPosition) {
                        buffer[buffer.length] = padding;
                    }

                // back of mark
                } else {

                    // fix display position
                    if(modificationFlag && centeringPosition) {
                        buffer[buffer.length] = padding;
                    }

                    // set title
                    setMenuTitle(viewNum, buffer, listCount, menuTitle, listNumberPosition, tlspace);

                    // space for title and mark
                    if(offsetForTitleAndMark) {
                        buffer[buffer.length] = tmspace;
                    }

                    // set folding mark
                    buffer[buffer.length] = startTag;
                    buffer[buffer.length] = foldMark;
                    buffer[buffer.length] = endTag;

                    // fix display position
                    if(modificationFlag && !centeringPosition) {
                        buffer[buffer.length] = padding;
                    }
                }

            // for block display
            } else {
                buffer[buffer.length] = startTag;

                // display mark
                if (displayMark) {

                    // ahead of mark
                    if (preMarkForSideBarMenu) {
                        buffer[buffer.length] = foldMark;

                        // space for title and mark
                        if(offsetForTitleAndMark) {
                            buffer[buffer.length] = tmspace;
                        }

                        setMenuTitleForBlock(viewNum, buffer, listCount, menuTitle, listNumberPosition, tlspace);

                    // back of mark
                    } else {
                        setMenuTitleForBlock(viewNum, buffer, listCount, menuTitle, listNumberPosition, tlspace);

                        // space for title and mark
                        if(offsetForTitleAndMark) {
                            buffer[buffer.length] = tmspace;
                        }
                        buffer[buffer.length] = foldMark;
                    }

                // No display mark
                } else {
                    setMenuTitleForBlock(viewNum, buffer, listCount, menuTitle, listNumberPosition, tlspace);
                }
                buffer[buffer.length] = endTag;
            }
        }

        // set data to object
        objTitle.innerHTML = buffer.join('');

        // set style
        objLists.style.display = (initMode == 'off') ? 'none' : 'block';

    } else if (initMode == 'chng') {
        // change folding mark
        var objMarks = objTitle.getElementsByTagName('a');
        for (i = 0; i < objMarks.length; i++) {
            if (objMarks[i].className == 'foldmark') {

                if(linkType == 'block' && displayMark) {
                    var title = objMarks[i].firstChild.nodeValue;
                    if(dispMode == 'none'){
                        title = title.replace(openMark, closeMark);
                    } else {
                        title = title.replace(closeMark, openMark);
                    }
                    objMarks[i].firstChild.nodeValue = title;
                } else if((linkType == 'unblock') || (subCategory && (idName.indexOf('subcategories') == 0))) {
                    objMarks[i].firstChild.nodeValue = (dispMode == 'none') ? closeMark : openMark;
                }
            }
        }

        // folding speed
        if(sp != 'dummy'){
            speed = sp;
        }
        if(speed == 'slow' || (idName.indexOf('subcategories') == 0 && subcategorySpeed == 'slow')) {
            if(dispMode == 'none') {
                element = $(idList);
                options = {
                    afterFinish: function(effect) {
                        Element.undoClipping(effect.element);
                        Element.show(effect.element); // must fix prototype.js
//                        element.style.display = 'block';
                    }
                };
                Effect.BlindDown(element, options);
            } else {
                element = $(idList);
                options = {
                    afterFinish: function(effect) {
                        Element.hide(effect.element);
                    }
                };
                Effect.BlindUp(element, options);
            }
        } else {

            // set style
            objLists.style.display = (dispMode == 'none') ? 'block' : 'none';
        }

        // Hold a fold-up state to cookie, when a change occurs.
        if (holdState) {
            if (dispMode == 'none') {
                createCookieByValue(idName, 'on', 365);
            } else {
                createCookieByValue(idName, 'off', 365);
            }
        }
    }
}

// Execute folding.
function FoldNavigation(idName, initMode, viewNum) {
    FoldNavigationByTagName(idName, initMode, viewNum, 'dummy', 'dummy');
}

function FoldNavigationSlowly(idName, initMode, viewNum) {
    FoldNavigationByTagName(idName, initMode, viewNum, 'dummy', 'slow');
}
