﻿var replaceHeadlineDataArray = new Array();

function RHDataItem(ItemID, Width, Height, ImageUrl, AltText) {
	this.ItemID = ItemID;
	this.Width = Width;
	this.Height = Height;
	this.ImageUrl = ImageUrl;
	this.AltText = AltText;
}

function ColorByName(ColorText) {
    ColorText = ColorText.toLowerCase();

    var result = 1; // black
    if (ColorText == 'fg_oper')
        result = 4;
    if (ColorText == 'fg_konzert')
        result = 5;
    if (ColorText == 'fg_jungeoper')
        result = 6;

    return result;
}


function FontSizeByName(Size) {
    Size = Size.toLowerCase();

    var result = 2; // h2
    if (Size == 'h3')
        result = 3;
    if (Size == 'zitat')
        result = 4;

    return result;
}

function ContainerWidthByName(Size) {
    Size = Size.toLowerCase();

    var result = "1";
    if (Size.toLowerCase().indexOf("66") >= 0)
        result = "2";
    if (Size.toLowerCase().indexOf("50") >= 0)
        result = "3";
    if (Size.toLowerCase().indexOf("33") >= 0)
        result = "4";

    return result;
}

function ReplaceFormatedHeadline(ItemID, Width, Height, ImageUrl, AltText, FontSize, Color) {
	var vgColor = ColorByName(Color); 
    var hgColor = "0";
    var textSize = FontSizeByName(FontSize);

    var item = document.getElementById(ItemID);
    if ((item != null) && (typeof (item) != 'undefined')) {
        var node = item.parentNode;
        var sizeName = "s";
        while ((node != null) && (typeof (node) != 'undefined')) {
            var className = node.className;
            if ((className != null) && (typeof (className) != 'undefined')) {
                var idx = className.toLowerCase().indexOf("content");
                if (idx >= 0) {
                    sizeName = className;
                    break;
                }

            }
            node = node.parentNode;
        }

        var containerWidth = ContainerWidthByName(sizeName);
        //alert(vgColor + "#" + textSize + "#" + containerWidth + "#" + sizeName);

        var format = textSize.toString() + vgColor.toString() + hgColor.toString() + containerWidth.toString() + "_";
        ImageUrl = ImageUrl.replace("FORMAT_", format);
        //alert(ImageUrl);
        ReplaceHeadlineWithinLink(ItemID, Width, Height, ImageUrl, AltText);
    }
}

function ReplaceHeadline(ItemID, Width, Height, ImageUrl, AltText) {
	replaceHeadlineDataArray.push(new RHDataItem(ItemID, Width, Height, ImageUrl, AltText));
}

function ReplaceHeadlineWithinLink(ItemID, Width, Height, ImageUrl, AltText) {
	replaceHeadlineDataArray.push(new RHDataItem(ItemID, Width, Height, ImageUrl, AltText));
}

function DoReplaceHeadlineWithinLink(ItemID, Width, Height, ImageUrl, AltText) {
	var item = document.getElementById(ItemID);
    if ((item != null) && (typeof (item) != 'undefined')) {
        var itemFound = false;
        for (child in item.childNodes) {
            var tagName = item.childNodes[child].tagName;
            if ((tagName != null) && (typeof (tagName) != 'undefined') && tagName.toUpperCase() == 'A') {
                internalReplaceHeadline(item.childNodes[child], Width, Height, ImageUrl, AltText);
                itemFound = true;
                break;
            }
        }
        if (!itemFound) {
            internalReplaceHeadline(item, Width, Height, ImageUrl, AltText);
        }
    }
}

function internalReplaceHeadline(Item, Width, Height, ImageUrl, AltText) {
    if ((Item != null) && (typeof (Item) != 'undefined')) {
        var width = '';
        var height = '';
        if (Width > 0)
            width = ' width="' + Width.toString() + '"';
        if (Height > 0)
            height = ' height="' + Height.toString() + '"';
        Item.innerHTML = '<img' + width + height + ' border="0" alt="' + AltText + '" src="' + ImageUrl + '">';
    }
}

function ReplaceAllHeadlines() {
	for (i in replaceHeadlineDataArray) {
		var item = replaceHeadlineDataArray[i];
		DoReplaceHeadlineWithinLink(item.ItemID, item.Width, item.Height, item.ImageUrl, item.AltText);
	}
}
