﻿
function ColorByName(ColorText) {
    ColorText = ColorText.toLowerCase();
    if (ColorText.length > 8 && ColorText.substr(0,8) == 'vg_color')
        ColorText = ColorText.substr(8);
    
    var result = 7;
    if (ColorText == 'grey')
        result = 8;
    if (ColorText == 'red')
        result = 1;
    if (ColorText == 'orange')
        result = 2;
    if (ColorText == 'darkblue')
        result = 6;
    if (ColorText == 'brightblue')
        result = 5;
    if (ColorText == 'pink')
        result = 3;
    if (ColorText == 'yellow')
        result = 4;
    if (ColorText == 'white')
        result = 0;

    return result;
}

function FontSizeByName(Size) {
    Size = Size.toLowerCase();
    
    var result = 1;
    if (Size == 'h2')
        result = 2;
    if (Size == 'h3')
        result = 3;
    if (Size == 'h4')
        result = 4;

    return result;
}

function ContainerWidthByName(Size) {
    Size = Size.toLowerCase();
    
    var result = 1;
    if (Size == 'm')
        result = 2;
    if (Size == 'l')
        result = 3;
    if (Size == 'xl')
        result = 4;
    if (Size == 'xxl')
        result = 5;

    return result;
}



function ReplaceFormatedHeadline(ItemID, Width, Height, ImageUrl, AltText, FontSize, Color, BackColor) {
    var vgColor = ColorByName(Color);
    var bgColor = ColorByName(BackColor);
    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("textcontent_width");
                if(idx >= 0)
                {
                    var s = className.toLowerCase().substr(idx + 17);
                    if (s.indexOf(" ") >= 0)
                        s = s.substr(0, s.indexOf(" "));
                    sizeName = s;
                    break;
                }
            
            }
            node = node.parentNode;
        }

        var containerWidth = ContainerWidthByName(sizeName);
        //alert(vgColor + "#" + bgColor + "#" + textSize + "#" + containerWidth);

        var format = textSize.toString() + vgColor.toString() + bgColor.toString() + containerWidth.toString() + "_";
        ImageUrl = ImageUrl.replace("FORMAT_", format);
        //alert(ImageUrl);
        ReplaceHeadline(ItemID, Width, Height, ImageUrl, AltText);
    }
}

function ReplaceHeadline(ItemID, Width, Height, ImageUrl, AltText) {
    var item = document.getElementById(ItemID);
    internalReplaceHeadline(item, Width, Height, ImageUrl, AltText);
}

function ReplaceHeadlineWithinLink(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 + '">';
    }
}