//----------------------------------------------------------------------
// File Name: generic.js
// Author: Edward Zhu (mail: v-edzhu@microsoft.com)
// Description:
//    The general javascript functions for the sites.
//----------------------------------------------------------------------

/*
* This function is to deal with the left side navigation bar click event.
*/
function toggle(element)
{
    var detail = element.parentNode.parentNode.parentNode.nextSibling;
    while (detail.nodeType == 3)
    {
        detail = detail.nextSibling;
    }
    if (detail.childNodes.length == 0 || detail.innerHTML.replace("\n", "").replace("\r", "").replace("/^ /", "").replace("/ $/", "") == "")
    	return;
    if (detail.className.indexOf("inactive")!=-1)
    {
        detail.className = detail.className.replace("inactive", "active");
        element.parentNode.parentNode.className = element.parentNode.parentNode.className.replace("collapse", "expand");
    }
    else if (detail.className.indexOf("active")!=-1)
    {
        detail.className = detail.className.replace("active", "inactive");
        element.parentNode.parentNode.className = element.parentNode.parentNode.className.replace("expand", "collapse");
    }
}

/*
*  This function is to folder/expand a bookmark in the long article in a content page.
*/
function folder(e)
{
    var content, classname;
    var element = e.parentNode;
    var parent = element.parentNode;
    
    switch (e.className)
    {
        case "bookmark_collapse":
            e.className = "bookmark_expand";
            break;
        case "bookmark_expand":
            e.className = "bookmark_collapse";
            break;
        default:
            e.className = "bookmark_collapse";
    }
    
    for (i = 0; i < parent.childNodes.length; i++)
    {
        content = parent.childNodes[i];
        
        if (content.nodeType == 1 && content.className.indexOf("segment_head") == -1)
        {
            classname = content.className;
            if (classname.indexOf("invisible")!=-1)
            {
                content.className = classname.replace("invisible", "visible");
            }
            else if (classname.indexOf("visible")!=-1)
            {
                content.className = classname.replace("visible", "invisible");
            }
            else
            {
                content.className = classname + " invisible";
            }
        }
    }
}

/*
* This part is to load the slides content for the slideshow in the homepage.
*/
getElementsByCssSelector = function(object, selector) {
    var sharp_delim = selector.indexOf("#");
    var dot_delim = selector.indexOf(".");
    var tagName = "", className = "", id = "";
    var elements = null;
    var result = new Array();

    // Devide them into several types.
    if (sharp_delim != -1) {
        tagName = selector.substr(0, sharp_delim);
        id = selector.substr(sharp_delim + 1);
    }
    else if (className != -1) {
        tagName = selector.substr(0, dot_delim);
        className = selector.substr(dot_delim + 1);
    }
    else {
        tagName = selector;
    }

    // Get all the elements followed the selector
    if (tagName != "") {
        elements = object.getElementsByTagName(tagName);
    }
    else {
        elements = getDescendant(object);
    }

    if (id != "") {
        result = loopSearch(elements, "id", id);
    }
    else if (className != "") {
        result = loopSearch(elements, "class", className);
    }
    else {
        result = elements;
    }
    
    return result;
}

loopSearch = function(o_Array, paraName, paraValue) {
    var value;
    var result = new Array();
    for (i = 0; i < o_Array.length; i++) {
        value = o_Array[i].attributes.getNamedItem(paraName);
        if (value.value == paraValue) {
            result = result.concat(o_Array[i]);
        }
    }

    return result;
}

getDescendant = function(object) {
    var result = new Array();

    if (object.childNodes.length == 0)
        return null;
    else {
        for (i = 0; i < object.childNodes.length; ++i) {
            result = result.concat(object.childNodes.item(i));
            if (getDescendant(object.children.item(i)) != null)
                result = result.concat(getDescendant(object.children.item(i)));
        }
    }

    return result;
}

Slideshow = function(slideArray) {
    var slide_div = document.getElementById("slideshow");

    var image_div = getElementsByCssSelector(slide_div, "div.image")[0];
    var index_div = getElementsByCssSelector(slide_div, "div.navigator_bar")[0];
    var curr_index;
    var _t = this;

    createSlices = function() {
        var slice, image, link;
        for (i = 0; i < slideArray.slides.length; ++i) {
            slice = document.createElement("div");
            slice.className = "slice inactive";
            if (slideArray.slides[i].url != "") {
                link = document.createElement("a");
                link.setAttribute("href", slideArray.slides[i].url);
            }
            image = document.createElement("img");
            image.setAttribute("src", slideArray.slides[i].imgsrc);
            image.setAttribute("alt", slideArray.slides[i].alt);

            if (link != null) {
                link.appendChild(image);
                slice.appendChild(link);
                image_div.appendChild(slice);
            }
            else {
                slice.appendChild(image);
                image_div.appendChild(slice);
            }
            slice = image = link = null;
        }
    };

    createTableStruct = function(alignment) {
        var index_table, index_body, row;
        index_table = document.createElement("table");
        index_table.align = alignment;
        index_table.appendChild(document.createElement("thead"));

        index_body = document.createElement("tbody");
        row = document.createElement("tr");
        index_body.appendChild(row);
        index_table.appendChild(index_body);

        index_table.appendChild(document.createElement("tfoot"));

        return index_table;
    };

    genIndexLink = function(c, index) {
        var td = document.createElement("td");
        td.className = "unselected";
        var link = document.createElement("a");
        link.onclick = function(e) { slides.setIndex(index); };
        link.className = "slide_nav";
        link.innerHTML = c;

        td.appendChild(link);

        return td;
    }

    genIndexArrow = function(img, alt, index) {
        var td = document.createElement("td");
        td.className = "unselected";
        var link = document.createElement("a");
        link.onclick = function(e) { slides.setIndex(index); };
        link.className = "slide_nav";
        var image = document.createElement("img");
        image.src = img;
        image.alt = alt;

        link.appendChild(image);
        td.appendChild(link);

        return td;
    }

    createControl = function() {
        var table = createTableStruct("center");
        var row = table.getElementsByTagName("tr")[0];
        row.appendChild(genIndexArrow("http://www.vanceinfo.com/static/img/hp/arrow_left.jpg", "left_arrow", 0));
        for (i = 0; i < slideArray.slides.length; ++i) {
            row.appendChild(genIndexLink(i + 1, i + 1));
        }

        row.appendChild(genIndexArrow("http://www.vanceinfo.com/static/img/hp/arrow_right.jpg", "right_arrow", slideArray.slides.length + 1));

        index_div.appendChild(table);
    };

    this.initSlides = function() {
        curr_index = 0;
        createSlices();
        createControl();
        _t.setIndex(1);
    };

    this.setIndex = function(index) {
        image_div.childNodes.item(curr_index).className = image_div.childNodes.item(curr_index).className.replace(" active", " inactive");
        var tds = index_div.getElementsByTagName("td");
        tds[curr_index + 1].className = "unselected";
        if (index == 0) {
            --curr_index;
        }
        else if (index == index_div.getElementsByTagName("tr")[0].childNodes.length - 1) {
            ++curr_index;
        }
        else {
            curr_index = index - 1;
        }

        if (curr_index < 0) {
            curr_index = slideArray.slides.length - 1;
        }
        curr_index = curr_index % slideArray.slides.length;
        image_div.childNodes.item(curr_index).className = image_div.childNodes.item(curr_index).className.replace(" inactive", " active");
        tds[curr_index + 1].className = "selected";
    }

    this.goNext = function() {
        this.setIndex(slideArray.slides.length + 1);
    }

    this.dispose = function() {
        slide_div = image_div = index_div = slices = null;
        curr_index = -1;
    }
}

function loadslides(slideArray) {
    slides = new Slideshow(slideArray);
    slides.initSlides();
    setTimeout("rotate(5000);", 5000);
}

function rotate(time) {
    slides.goNext();
    setTimeout("rotate(" + time + ")", time);
}
