/**
 * Add new item in news
 */
function newsAddItem(instance, position)
{
    if (confirm("Would you like to add a new article?")) {
        ApOnListAddEditor.add("newsList_" + instance, (position == "last" ? "last" : "first"));
        $("newsNoItems_" + instance).style.display = "none";
    }
}

/**
 * Add new new link to a news item
 */
function newsAddLink(id, instance, type)
{
    if (confirm("Would you like to add a link to the end of this article?")) {
        var data = new Array();
        data["type"] = type;
        ApOnListAddEditor.add("newsItemContent_" + id + "_" + instance, null, data);
        $("newsAddButtons_" + id + "_" + instance).style.display = "none";
    }
}

function newsAddSublink(id, instance)
{
    if (confirm("Would you like to add a new link at the end of this article?")) {
        ApOnListAddEditor.add("newsLinkList_" + id + "_" + instance, $("newsLinkListAddBefore_" + id + "_" + instance));
    }
}


/**
 * Value function for the news article details
 */
function newsGetArticleOptions(additionalData)
{
    var data = new Array();
    if (newsGetArticleOptions.faces) {
        if (newsGetArticleOptions.faces[additionalData.instance]) {
            data["face"] = newsGetArticleOptions.faces[additionalData.instance];
        }
    }
    return data;
}

newsGetArticleOptions.setInstanceFace = function(instance, face)
{
    if (!newsGetArticleOptions.faces) {
        newsGetArticleOptions.faces = new Array();
    }
    newsGetArticleOptions.faces[instance] = face;
}

/**
 * Updates the other pictures when resizing
 */
function newsUpdatePictureWidth(size, objectId)
{
    newsUpdatePictureSizes("width", size, objectId);
}

/**
 * Updates the other pictures when resizing
 */
function newsUpdatePictureHeight(size, objectId)
{
    newsUpdatePictureSizes("height", size, objectId);
}

/**
 * Generic update for width/height resizing
 */
function newsUpdatePictureSizes(dimension, size, objectId)
{
    var touchedObject = $(objectId);
    if (!touchedObject) {
        return;
    }
    //identify the parent list
    var parent = touchedObject;
    while (parent = parent.parentNode) {
        if (parent.nodeName == "UL") {
            break;
        }
    }
    if (!parent) {
        //no parent found
        return;
    }
    var images = parent.getElementsByTagName("IMG");
    for (var i = 0; i < images.length; i++) {
        if (images[i] != touchedObject) {
            if (dimension == "width") {
                images[i].style.width = size + "px";
            } else if (dimension == "height") {
                images[i].style.height = size + "px";
            }
        }
    }
}

function newsOptionsGetValues(additionalData)
{
    var data = new Array();
    if($("chkNewsShowTitle_" + additionalData.instance)) {
        data["showTitle"] = $("chkNewsShowTitle_" + additionalData.instance).checked;
    }
    var orderSelect = $("selNewsOrder_" + additionalData.instance);
    if (orderSelect) {
        data["order"] = orderSelect.options[orderSelect.selectedIndex].value;
    }
    var modeSelect = $("selNewsMode_" + additionalData.instance);
    if (modeSelect) {
        data["mode"] = modeSelect.options[modeSelect.selectedIndex].value;
    }
    return data;
}

function newsSwitch(instance, itemId, selector, auto)
{
    //switch item
    var list = $("newsList_" + instance);
    var selectedItem = $("newsItem_" + itemId + "_" + instance);
    //mark the object
    if (newsObject.list[instance]) {
        newsObject.list[instance].currentItemId = selector.id;
    }

    if (isDefined("ApOnEditors")) {
        //hide all editors for the list
        ApOnEditors.hideEditorsFor(list, true);
        ApOnEditors.update();
        ApOnEditors.reposition();
    }

    //enable switch
    var switchList = $("newsSwitch_" + instance);
    var prefix = "newsSwitch_";
    for(var i = 0; i < switchList.childNodes.length; i++) {
        var child = switchList.childNodes[i];
        if (child.nodeType == 1) {
            if (child.id.substr(0, prefix.length) == prefix) {
                if (child == selector) {
                    child.className = "selected";
                } else {
                    child.className = "";
                }
            }
        }
    }

    if (newsSwitch.fader) {
        newsSwitch.fader.stop();
    }

    var maxFrames = 40;
    var middle = maxFrames / 2;
    var animationFunction = function (frame)
    {
        if (frame < middle) {
            //hide the current item (by hiding the entire list)
            var opacity = ((middle - frame) / middle);
            if (!document.all) {
                list.style.opacity = opacity;
            } else {
                list.style.filter = "alpha(opacity=" + Math.round(opacity * 100) + ")";
            }
        } else if (frame == middle) {
            //hide all items, prepare selected item
            for(var i = 0; i < list.childNodes.length; i++) {
                var child = list.childNodes[i];
                if (child.nodeType == 1) {
                    if (child.id == "newsItem_" + itemId + "_" + instance) {
                        child.style.display = "block";
                    } else {
                        child.style.display = "none";
                    }
                }
            }
            //reshow list
            if (!document.all) {
                list.style.opacity = 1;
                selectedItem.style.opacity = 0;
            } else {
                list.style.filter = "alpha(opacity=100)";
                selectedItem.style.filter = "alpha(opacity=0)";
            }
        } else {
            //show the new item
            var opacity = ((frame - middle) / middle);
            if (!document.all) {
                selectedItem.style.opacity = opacity;
            } else {
                selectedItem.style.filter = "alpha(opacity=" + Math.round(opacity * 100) + ")";
            }
        }
        if (frame == maxFrames) {
            if (isDefined("ApOnEditors")) {
                ApOnEditors.unhideEditorsFor($("newsItem_" + itemId + "_" + instance), true);
                ApOnEditors.update();
                ApOnEditors.reposition();
            }
            if (newsObject.list[instance]) {
                if (newsObject.list[instance].running) {
                    if (auto) {
                        newsObject.list[instance].goOn();
                    } else {
                        newsObject.list[instance].stop();
                    }
                }
            }
        }
    }
    newsSwitch.fader = new ApOnAnimation(0, maxFrames, 40, "linear", animationFunction);
    newsSwitch.fader.start();
}

function newsAutoStart()
{
    newsPrepare();
    for(key in newsObject.list) {
        newsObject.list[key].start();
    }
}

function newsPrepare()
{
    //detect all instances
    var hiddenFields = document.body.getElementsByTagName("INPUT");
    for(var i = 0; i < hiddenFields.length; i++) {
        if (hiddenFields[i].type == "hidden") {
            if (hiddenFields[i].name == "newsInstance") {
                var item = new newsObject(hiddenFields[i].value);
            }
        }
    }
}

function newsStart(instance)
{
    newsObject.list[instance].start(true);
}

function newsObject(instance)
{
    this.instance = instance;
    if (!newsObject.list) {
        newsObject.list = new Array();
    }
    newsObject.list[this.instance] = this;
    this.running = false;
}

newsObject.prototype.start = function (force) {
    if (force) {
        this.running = true;
        this.switchToNext();
    } else {
        this.goOn();
    }
}

newsObject.prototype.goOn = function ()
{
    this.running = true;
    //take a break before switching to the next one
    setTimeout("newsObject.list['" + this.instance + "'].switchToNext()", 8000);
}

newsObject.prototype.switchToNext = function ()
{
    //detect next item
    var list = $("newsSwitch_" + this.instance);
    var prefix = "newsSwitch_";
    //current item
    var currentItemFound = false;
    var currentItemId = "newsSwitch_" + this.currentItemId + "_" + this.instance;
    var nextItem = "";
    //first item
    var firstItem = "";
    var items = list.getElementsByTagName("SPAN");
    for (var i = 0; i < items.length; i++) {
        if (items[i].id.substr(0, prefix.length) == prefix) {
            if (firstItem == "") {
                firstItem = items[i].id;
                if (!this.currentItemId) {
                    this.currentItemId = firstItem;
                }
            }
            if (currentItemFound) {
                //this is it, the next item
                nextItem = items[i].id;
                break;
            }
            if (items[i].id == this.currentItemId) {
                currentItemFound = true;
            }
        }
    }
    if (nextItem != "") {
        this.jumpToItem(nextItem);
    } else if (firstItem != "") {
        this.jumpToItem(firstItem);
    }
}

newsObject.prototype.getIdFromSwitchId = function (switchId)
{
    var regexp = /^newsSwitch_([0-9]+)_/;
    var arr = regexp.exec(switchId);
    return arr[1];
}

newsObject.prototype.jumpToItem = function (switchId)
{
    var id = this.getIdFromSwitchId(switchId)
    //detect the item id
    if (id != "") {
        //do an auto switch
        newsSwitch(this.instance, id, $("newsSwitch_" + id + "_" + this.instance), true);
    }
}

newsObject.prototype.stop = function ()
{
    this.running = false;
}
