$(window).load(function () {
    $("div#topnav a.active").before('<img src="/images/top-nav-tab-left.png" />').after('<img src="/images/top-nav-tab-right.png" />').css('padding', '0 8px');
    $(".contentbar").before('<span class="contentbarleft"></span>').after('<span class="contentbarright"></span>');
    $("div#topnav li").hover(
        function () {
            if(!$(this).children('a').hasClass('temphover') && !($(this).children('a').hasClass('active') || $(this).children('a').hasClass('parent'))){
                $(this).children('a').before('<img src="/images/top-nav-tab-left.png" class="navtempimage" />')
                    .after('<img src="/images/top-nav-tab-right.png" class="navtempimage" />')
                    .addClass('active')
                    .addClass('temphover')
                    .css('padding','0 8px');
            }
        },
        function () {
            if($(this).children('a').hasClass('temphover')){
                $(this).children("img").remove();
                $(this).children('a').removeClass('active').removeClass('temphover').css('padding','0 13px');
            }
        }
    );

    
    if ($("div#topnav li a.parent") != null){
        $("div#topnav li a.parent").before('<img src="/images/top-nav-tab-left.png" class="navtempimage" />')
            .after('<img src="/images/top-nav-tab-right.png" class="navtempimage" />')
            .css('padding','0 8px');
    }
    
    //flash text replacement
    $('#hometitle').flash(
        { 
            src: '/js/Helvetica_Neue_Lt_Std_Thin_Extended.swf', 
            flashvars: { 
                css: [
                    '* { color: #4c6d6b; }',
                    'a { color: #4c6d6b; text-decoration: none; }',
                    'a:hover { text-decoration: underline; }'
                ].join(' ')
            }
        },
        { update: false },
        function (htmlOptions) {
            htmlOptions.flashvars.txt = this.innerHTML;
            this.innerHTML = '<div>'+this.innerHTML+'</div>';
            var $alt = $(this.firstChild);
            htmlOptions.height = $alt.height();
            htmlOptions.width = $alt.width();
            $alt.addClass('alt');
            $(this)
                .addClass('flash-replaced')
                .prepend($.fn.flash.transform(htmlOptions));                        
        }
    );
    $('#pagetitle').flash(
        { 
            src: '/js/Helvetica_Neue_Lt_Std_Thin_Extended.swf', 
            flashvars: { 
                css: [
                    '* { color: #4d6e6c; }',
                    'a { color: #4d6e6c; text-decoration: none; }',
                    'a:hover { text-decoration: underline; }'
                ].join(' ')
            }
        },
        { update: false },
        function (htmlOptions) {
            htmlOptions.flashvars.txt = this.innerHTML;
            this.innerHTML = '<div>'+this.innerHTML+'</div>';
            var $alt = $(this.firstChild);
            htmlOptions.height = $alt.height();
            htmlOptions.width = $alt.width();
            $alt.addClass('alt');
            $(this)
                .addClass('flash-replaced')
                .prepend($.fn.flash.transform(htmlOptions));                        
        }
    );
    $('#white').flash(
        { 
            src: '/js/Helvetica_Neue_Lt_Std_Thin_Extended.swf', 
            flashvars: { 
                css: [
                    '* { color: #FFFFFF; }',
                    'a { color: #FFFFFF; text-decoration: none; }',
                    'a:hover { text-decoration: underline; }'
                ].join(' ')
            }
        },
        { update: false },
        function (htmlOptions) {
            htmlOptions.flashvars.txt = this.innerHTML;
            this.innerHTML = '<div>'+this.innerHTML+'</div>';
            var $alt = $(this.firstChild);
            htmlOptions.height = $alt.height();
            htmlOptions.width = $alt.width();
            $alt.addClass('alt');
            $(this)
                .addClass('flash-replaced')
                .prepend($.fn.flash.transform(htmlOptions));                        
        }
    );



    $('h4#unknown').flash(
        { 
            src: '/js/Helvetica_Neue_Lt_Std_Thin_Extended.swf', 
            flashvars: { 
                css: [
                    '* { color: #002b2f; }',
                    'a { color: #ffffff; text-decoration: none; }',
                    'a:hover { text-decoration: underline; }'
                ].join(' ')
            }
        },
        { update: false },
        function (htmlOptions) {
            htmlOptions.flashvars.txt = this.innerHTML;
            this.innerHTML = '<div>'+this.innerHTML+'</div>';
            var $alt = $(this.firstChild);
            htmlOptions.height = $alt.height();
            htmlOptions.width = $alt.width();
            $alt.addClass('alt');
            $(this)
                .addClass('flash-replaced')
                .prepend($.fn.flash.transform(htmlOptions));                        
        }
    );
    
    //chrome will fire this too soon and it won't read the heights accurately so a short
    //delay will take care of it.
    setTimeout('adjustLeftColumn()',500);

    
    // This will fix the png transparencies for IE.
    $(document).pngFix(); 

    if ($('#nav4').children().length > 0) {
        $('#nav4').jcarousel({
            vertical: true,
            scroll: 14,
            visible: 15,
	        initCallback: setCarouselToActive,
        	itemLastInCallback: shortcutNextDisable
        });
    }

    if ($('#nav4').children().length > 0
        || $('#related_block').children().length > 0
        || $('#related').children().length > 0
        || $('#rightcolumn > img').length > 0
        || $('#rightcolumn > h4').length > 0) {
//        $('#rightcolumn').css('height', $('.maincontentbody').css('height'));
        var jcarouselHeight = $('.jcarousel-skin-ephib').css('height').substr(0, $('.jcarousel-skin-ephib').css('height').length - 2);
        var mainContentHeight = $('.maincontentbody').css('height').substr(0, $('.maincontentbody').css('height').length - 2);
        $('#rightcolumn').css('height', Math.max(jcarouselHeight, mainContentHeight) + 50);
    }
    else {
        $('#rightcolumn').remove();
    }

     if ($('#nav4').children().length == 0){
         $('#nav4').remove();
     }


    // Remove the div if there are no items.
    if ($.browser.msie && $.browser.version < 8) {
        if ($('div#topsubnav').children(0).children().length == 0) {
            $('div#topsubnav').css('display', 'none');
        }
    }

    if (location.href.match(/your-world.html/)) {
        $('#topsubnav').parent().remove();
    }
});


function setCarouselToActive(carousel, state) {
	var active = carousel.list.children(':has(".active")');
	if (active.length > 0) {
		var i;
		for (i = 0; i < carousel.list.children().length; i++) {
			if (carousel.list.children()[i] == active[0]) {
				break;
			}
		}
		carousel.options.start = Math.max(1, i - 3);
	}
}

function shortcutNextDisable(carousel, li, index) {
	if (index == carousel.list.children().length) {
		if (carousel.buttonNext.css('display') != 'none')
			carousel.buttonNext.toggle();
	} else if (carousel.buttonNext.css('display') == 'none') {
		carousel.buttonNext.toggle();
	}
}

//size the left column so that it extends down to the footer
function adjustLeftColumn(){
    var contentRightHeight = $("div#contentright").outerHeight();
    var contentLeftHeight = $("div#contentleft").outerHeight();
    if(contentRightHeight > contentLeftHeight)
        $("div#contentleft").height(contentRightHeight);
}

function displayMap(src){
  $.modal('<iframe src="' + src + '" width="500" height="400" style="border:0" target="_blank" scrolling="no">', {
        closeHTML:"",
        containerCss:{
                backgroundColor:"#fff",
                borderColor:"#fff",
                height:400,
                padding:0,
                width:600
        },
        overlayClose:true
  });
}

