﻿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();
    //if (ColorText.length > 8 && ColorText.substr(0, 8) == 'vg_color')
        //ColorText = ColorText.substr(8);

    var result = 1;
    if (ColorText == 'fg_oper')
        result = 3;
    if (ColorText == 'fg_ballett')
        result = 4;
    if (ColorText == 'fg_schauspiel')
        result = 5;

    return result;
}


function FontSizeByName(Size) {
    Size = Size.toLowerCase();

    var result = 2;
    if (Size == 'h1')
        result = 2;
    if (Size == 'teaser')
        result = 3;

    return result;
}

function ContainerWidthByName(Size) {
    Size = Size.toLowerCase();

    var result = "01";
    if (Size == 'multicol_width1')
        result = "03";
    if (Size == 'multicol_width2')
        result = "04";

    return result;
}



function ReplaceFormatedHeadline(ItemID, Width, Height, ImageUrl, AltText, FontSize, Color) {
    var vgColor = ColorByName(Color);
    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("multicol_width");
                if (idx >= 0) {
                    var s = className.toLowerCase().substr(idx, 15);
                    if (s.indexOf(" ") >= 0)
                        s = s.substr(0, s.indexOf(" "));
                    sizeName = s;
                    break;
                }

            }
            node = node.parentNode;
        }

        var containerWidth = ContainerWidthByName(sizeName);
        //alert(vgColor + "#" + textSize + "#" + containerWidth);

        var format = textSize.toString() + vgColor.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);
	}
}
