﻿$(document).ready(function () {
    loadFancyBox();

    // MAIN NAVIGATION drop down
    $('li.nav_items').hover(function () {
        var li_width = parseInt($(this).css('width'));
        var last_li_width = (li_width - 2)

        if ($(this).hasClass('first_link')) {
            $(this).find('ul').css('width', li_width);
            $(this).find('ul').css('left', '-1px');
        } else {
            $(this).find('ul').css('width', --li_width);
        }
        $(this).find('ul').stop(true, true).slideDown(300);
        if ($(this).find('ul').css('display') == 'block') {
            $(this).find('a:eq(0)').css('color', '#F50021');
        }
        $(this).find('.regions_dropdown:last').css('border-bottom', 'none');



    }, function () {

        $(this).find('ul').stop(true, true).hide();
        $(this).find('a:eq(0)').css('color', 'white');

    });

    $('.nav_items:last').css('width', '163px');

    // for dropdown on change language and regions
    var timeout = 300;
    var closetimer = '';

    $('.change').hover(function () {
        if (closetimer) {
            clearTimeout(closetimer);
        }
        $(this).find('ul').stop(true, true).slideDown(300);
        $(this).find('.regions_dropdown:last').css('border-bottom', 'none');
    },
    function () {
        var that = this;
        closetimer = setTimeout(mclose, timeout);
        function mclose() {
            $(that).find('ul').hide();
        }
    });

    $('li.change:eq(1)').find('ul.hide').addClass('area_dropdown');


    $('div.regions_article').each(function () {
        $(this).find('p.article_content').not(':first').css('width', 'auto');
    });

    var clickToOpen = $('.related_slideshows span.block')
    $(clickToOpen).click(function () {

        $(this).prev().toggle();
        $(this).html('view less &rsaquo;&rsaquo;');

        if ($(this).prev().css('display') == 'none') {
            $(this).html('view more &rsaquo;&rsaquo;');
        }
    });

    var program_location_table = $('#show_more_events').prev();
    var last_rows = $(program_location_table).find('tr:eq(3)').nextAll();
    $(last_rows).hide();
    // var show_less = $('#show_more_events').html('less events &rsaquo;&rsaquo;');
    // var show_more = $('#show_more_events').html('more events &rsaquo;&rsaquo;');

    $('#show_more_events').click(function () {
        $(last_rows).toggle();


        if ($(last_rows).is(':visible')) {
            $('#show_more_events').html('less events &rsaquo;&rsaquo;');
        }
        else {
            $('#show_more_events').html('more events &rsaquo;&rsaquo;');
        }

    });

    function loadFancyBox() {
        $("a.image-overlay").fancybox({
            'titlePosition': 'inside',
            'transitionIn': 'elastic',
            'transitionOut': 'elastic'
        });
    }



    $('.related_slideshows').each(function () {
        $(this).find('.slideshow_pics').slice(3).nextAll().hide();
    });


    $('.slideshow_wrapper').find("br:last").css('display', 'block');

    // this is for the slideshow pages where you can view more or less thumbnails slideshow2.aspx and video is an example

    // media_wrapper = $('div.media_wrapper')
    //  if (media_wrapper != 'undefined') {
    /*   $('.view_it').not(':first').toggle(function () {
    var further_rows = $(this).next().next().find('.slideshow_pics').slice(3).nextAll('div');
    //  alert('yes');
    $(further_rows).slideDown();
    $(this).text('[View fewer]')
    $('.slideshow_wrapper').find("br:last").css('display', 'block');
    }, function () {
    var further_rows = $(this).next().next().find('.slideshow_pics').slice(3).nextAll('div');
    $(further_rows).slideUp()
    $(this).text('[View more]')
    // $('.slideshow_wrapper').find("br:last").css('display', 'block');
    });*/

    /*  $('.view_it:first').toggle(function () {
    var further_rows = $(this).next().next().find('.slideshow_pics').slice(3).nextAll('div');
    $(further_rows).slideUp();
    $(this).text('[View more]')
    //   $('.slideshow_wrapper').find("br:last").css('display', 'block');
    }, function () {
    var further_rows = $(this).next().next().find('.slideshow_pics').slice(3).nextAll('div');
    $(further_rows).slideDown();
    $(this).text('[View fewer]')
    });*/
    //  }


    $('.view_it').toggle(function () {
        var further_rows = $(this).next().next().find('.slideshow_pics').slice(3).nextAll('div');
        //  alert('yes');
        $(further_rows).slideDown();
        $(this).text('[View fewer]')
        $('.slideshow_wrapper').find("br:last").css('display', 'block');
    }, function () {
        var further_rows = $(this).next().next().find('.slideshow_pics').slice(3).nextAll('div');
        $(further_rows).slideUp()
        $(this).text('[View more]')
        // $('.slideshow_wrapper').find("br:last").css('display', 'block');
    });




    // removing empty paragraphs that Ektron spits out because it's a silly goose
    $('p')
    .filter(function () {
        return $.trim($(this).text()) === ''
    })
    .remove()

    // this is for the three/four column pages that change Global Leadership team is an example
    $('div.four_columns').find('div.columns:last').css('margin-right', '0px');

    $('div.content_subsection').find('p:last').css('margin-bottom', '0px');

    //this is for article page that has two different size images css is based on size
    $('div.top_article_image a img').filter(function () {
        var width = $(this).width();
        if (width < 290) {
            $(this).parent('a').next('p').css('width', '230px');
        }

    })

    $('div.top_article_image > img').filter(function () {
        var width = $(this).width();
        if (width < 290) {
            $(this).next('p').css('width', '230px');
        }

    })

    //this is for article page that has two different size videos css is based on size
    $('div.top_article_image object').filter(function () {
        var width = $(this).width();
        return width < 290;
        /*  if (width < 290) {
        $(this).next('p').css('width', '230px');
        }*/

    }).next('p').css('width', '230px');

    // This is for jquery image map on right rail
    $('.world_map_container area').each(function () {

        // Assigning an action to the mouseover event
        $(this).mouseover(function (e) {
            var country_id = $(this).attr('id').replace('area_', '');
            $('#' + country_id).css('display', 'block');
        });

        // Assigning an action to the mouseout event
        $(this).mouseout(function (e) {
            var country_id = $(this).attr('id').replace('area_', '');
            $('#' + country_id).css('display', 'none');
        });

    });


    //if anchor tags contain text learn more, see or watch add that class to it
    $("a:contains('learn')").addClass('learn_more');
    $("a:contains('LEARN')").addClass('learn_more');
    $("a:contains('see')").addClass('learn_more');
    $("a:contains('SEE')").addClass('learn_more');
    $("a:contains('watch')").addClass('learn_more');
    $("a:contains('WATCH')").addClass('learn_more');


    //clearing some stuff on the actual old slideshow pages
    var sectionalheader = $('#articleLeft .sectional .sectionalHeader');
    if ($(sectionalheader).length > 0) {
        $('div.sectionalHeader h3').css('text-transform', 'capitalize');
    }
    else {
        var features_media_block = $('div.left_wrapper #articleLeft div.sectional')
        $(features_media_block).find(":nth-child(3n+1)").css('clear', 'both');
        $(features_media_block).find(":eq(0)").css('clear', 'none');
    }

    if ($('#regions_header').length) {
        $('#landingContent').css('background-color', 'white');
    }

    // removing background gifs on some of the old pages integrating into new regions styling
    if ($('#regions_main_content').length) {
        $('#articleContent').css('background-image', 'none');
        $('#maincont').css('background-image', 'none');
        $('#landingContent').css('background-image', 'none');
    }


    // for newsletter sign up


    $('.addthis_button_print img').remove();
    $('.addthis_button img').remove();






    /* $('add_print_button').hover(function () {
    $(this).html("<img src='/images/icons/print_icon_hover.png' />");

    }, function () {
    $(this).html("<img src='/images/icons/print_icon.png' />");
    });*/



    // 12/14/2011 New functionality view more for global events calendar
	var eventDetails = $('td.event_details p').find('span.show')	
	$('<span> ... </span>').insertAfter(eventDetails);  //elipses afterwards really? yes really!
    $('table tr td span.view_more').css({ 'display': 'block', 'cursor': 'pointer' }).toggle(function () {
        $(this).prev().slideDown('slow');
        $(this).text('View Less');
		$(this).prev().prev().hide();
        $(this).parent().parent().next().find('span.hide').show();

    }, function () {
        $(this).prev().slideUp('fast');
        $(this).text('View More');
		$(this).prev().prev().show();
        $(this).parent().parent().next().find('span.hide').hide();

    });
	
	// this is to bold any for more information within span tags... yes really
  function boldThisYo($el) {
   $el.contents().each(function () {

       if (this.nodeType == 3) { // Text only
           $(this).replaceWith($(this).text()
               .replace(/(For more information)/gi, '<strong>For more information</strong>'));
       } 
   });
}

boldThisYo($("span"));


    if ($('#holidayModal').css('display') == 'none' || $('div.modal_regions').css('display') == 'none') {
        $('#splash').css('display', 'none')   
    }
	
	



});
