$(document).ready(function()
{
    /*Hide level 1 menuitem (MIGHT NOT BE USED ON KONTAKT OS)*/
    var hideFirstMenuItem = $("#toptoolbar").get(0);
    if (hideFirstMenuItem != null && hideFirstMenuItem.id == "toptoolbar")
    {
        var $menu = $("#toolbarHolder").get(0);
        $firstMenuItem = $($menu).children().eq(0).children().eq(0).hide();
    }

    ReplaceOpensInNewWindowIcon();

    if (swfobject.hasFlashPlayerVersion("8.0.0"))
    {
        $('#tabs').addClass("sIFR-hasFlash");
        $('#tabsOut').addClass("sIFR-hasFlashTabsOut");

        /* Tabs menu */
        $('.sIFR-hasFlash a').each(function(i)
        {
            var counter = i;
            var point = $(this).offset();
            var title = this.innerHTML;
            var height = $(this).height();
            var width = $(this).width();

            this.id = "elemnt" + i;

            var selected = "";
            if ($(this).parent().get(0).className.substr(0, 7) == "current")
            {
                selected = "selected";
            }

            var flashvars = {
                txt: encodeURIComponent(this.innerHTML),
                link: this.href,
                color: '0xffffff',
                hovercolor: '0xC5A654',
                objid: this.id,
                isSelected: selected
            };

            var params = {
                wmode: "transparent",
                scale: 'noscale',
                salign: 'tl'
            };

            var attributes = {};

            swfobject.embedSWF("/sitemod/themes/gridPB/stylesheets/txt.swf", flashvars.objid, width, 50, "8.0.0", "expressinstall.swf", flashvars, params, attributes);
        }
		)

        /* Headline */
        $('#lidMainHeadline').each(function(q)
        {
            var counter = q;
            var point = $(this).offset();
            var title = this.innerHTML;
            var height = $(this).height();
            var width = $(this).width();

            this.id = "elemnts" + q;

            var flashvars = {
                txt: encodeURIComponent(this.innerHTML),
                color: '0xffffff',
                objid: this.id,
                fontSize: 32,
                txtwidth: width
            };

            var params = {
                wmode: "transparent",
                scale: 'noscale',
                salign: 'tl'
            };

            var attributes = {};

            // Adjust for inconsistencies between html font and flash font (These two lines might be different on kontakt os)
            height += 16;
            swfobject.embedSWF("/sitemod/themes/gridPB/stylesheets/sim_txt.swf", flashvars.objid, 410, height, "8.0.0", "expressinstall.swf", flashvars, params, attributes);
        }
		)

        /* Headline in marketscommens pages*/
        $('.pbCommentHeadline').each(function(q)
        {
            var counter = q;
            var point = $(this).offset();
            var title = this.innerHTML;
            var height = $(this).height();
            var width = $(this).width();

            this.id = "elemnts" + q;

            var flashvars = {
                txt: encodeURIComponent(this.innerHTML),
                color: '0xffffff',
                objid: this.id,
                fontSize: 32,
                txtwidth: width
            };

            var params = {
                wmode: "transparent",
                scale: 'noscale',
                salign: 'tl'
            };

            var attributes = {};

            // Adjust for inconsistencies between html font and flash font (These two lines might be different on kontakt os)
            height += 16;
            swfobject.embedSWF("/sitemod/themes/gridPB/stylesheets/sim_txt.swf", flashvars.objid, 410, height, "8.0.0", "expressinstall.swf", flashvars, params, attributes);
        }
		)		

        /* Left side menu */
        var topMenuItemNoLink = $('.topmenu span.selected');
        var topMenuItemWithLink = $('.topmenu a');

        if (topMenuItemNoLink.length > 0)
        {
            setTopMenu(topMenuItemNoLink[0]);
        }
        else if (topMenuItemWithLink.length > 0)
        {
            setTopMenu(topMenuItemWithLink[0]);
        }
    }

    /* Frontpage bottom boxes headline*/
    $('div.pbfrontbottombackgroundimageandtext703headlineInner, div.pbfrontbottomtext693Container683headlineInner, div.pbfrontbottomtext683ContainerheadlineInner, div.pbfrontbottomMarketComment1493ContainerheadlineInner, div.pbfrontbottomMarketComment1483ContainerheadlineInner').each(function(q)
    {
        var counter = q;
        var point = $(this).offset();
        var title = this.innerHTML;
        var height = $(this).height();
        var width = $(this).width();
        var linkUrl = "";
        var linkTarget = "";
        var text = this.innerHTML;


        // Adjust for inconsistencies between html font and flash font
        height += 11;

        this.id = "fpBoxes" + q;

        var flashvars = {
            txt: encodeURIComponent(text),
            color: '0xffffff',
            objid: this.id,
            fontSize: 20,
            txtwidth: width
        };


        var $kids = $(this).children();
        if ($kids != null && $kids[0] != null)
        {
            linkUrl = $kids[0].href;
            linkTarget = $kids[0].target;
            text = $kids[0].innerHTML;

            flashvars = {
                txt: encodeURIComponent(text),
                color: '0xffffff',
                objid: this.id,
                fontSize: 20,
                txtwidth: width,
                link: linkUrl,
                target: linkTarget
            };
        }


        var params = {
            wmode: "transparent",
            scale: 'noscale',
            salign: 'tl'
        };

        var attributes = {};
        //swfobject.embedSWF("/sitemod/themes/gridPB/stylesheets/sim_txt.swf", flashvars.objid, 244, height, "8.0.0", "expressinstall.swf", flashvars, params, attributes);
        swfobject.embedSWF("/sitemod/themes/gridPB/stylesheets/sim_txt.swf", flashvars.objid, width, height, "8.0.0", "expressinstall.swf", flashvars, params, attributes);
    }
		)

    //Insert dummy element to redraw content
    $("#toolbarHolder").append(document.createElement('div'));

});

function setTopMenu(tp)
{
    var point = $(tp).offset();
    var title = tp.innerHTML;
    var height = $(tp).height();
    var width = $(tp).width();

    var linkUrl = window.location;

	if(tp.tagName == "A")
	{
		linkUrl = tp.href;
	}

    //This might not be used on kontakt os
    height += 11;
    
    tp.id = "topmenuFlash";
    var flashvars = {
        txt: encodeURIComponent(tp.innerHTML),
        color: '0xffffff',
        objid: tp.id,
        fontSize: 18,
        txtwidth: 190,
        link: linkUrl,
        target: '_self'
    };

    var params = {
        wmode: "transparent",
        scale: 'noscale',
        salign: 'tl'
    };

    var attributes = {};

    swfobject.embedSWF("/sitemod/themes/gridPB/stylesheets/sim_txt.swf", flashvars.objid, width, height, "8.0.0", "expressinstall.swf", flashvars, params, attributes);
    //swfobject.embedSWF("/sitemod/themes/gridPB/stylesheets/sim_txt.swf", flashvars.objid, width, 50, "8.0.0", "expressinstall.swf", flashvars, params, attributes);
}


function setsize(id, width)
{
    var obj = document.getElementById(id)
    if (obj)
    {
        obj.width = width;
    }
}

function DivLink(url, target)
{
    if (target == "_blank")
    {
        window.open(url);
    }
    else
    {
        window.location = url;
    }
}

// Changes the opens new window picture
function ReplaceOpensInNewWindowIcon()
{
    $(".newwindow").each(function(w) {
        this.src = "/sitemod/themes/gridPB/images/OpenNewWindowIcon.gif";
    });

}
