// IFES Namespace
var IFES = {

    preventDefault: function(e) {
        e.preventDefault();
    },

    MainNav: {
        init: function() {
            $("#main-nav").superfish(
				{
				    delay: 1500,
				    speed: 'fast',
				    pathClass: 'selected',
				    autoArrows: false,
				    dropShadows: false
				}
			);
        }
    },

    PostRenderStyling: function() {

        // in some cases, the slideshow will get inserted into a column that is too narrow.
        // here, we move it out and on top of that column so it fits well. REVISIT:If you can think
        // of a better way to do with (perhaps with a new template?) please replace this.
        if ($('#slideshow').parents('.leftCol-content').length > 0) {
            $('.leftCol-content').before($('#featureSlideShowContainer'));
        }

        $('#slideshow').css("visibility", "visible");

        if ($.browser.msie && ($.browser.version < 7)) {

            // adjust the width of the main-nav submenu items dynamically since CSS can't seem to come through here.
            $("#main-nav .sub-menu-container .sub-menu-item span").each(
				function(i, item) {
				    var navLink = $(item);
				    var width = navLink.text().length * 7.5; // figure out how many letters are in the link
				    navLink.parent().css("width", width + "px"); // adjust width to # of letters.
				});
        }

    },

    BrowseByNav: {
        init: function() {
            $("#browseby-nav").superfish(
				{
				    delay: 1500,
				    speed: 'fast',
				    pathClass: 'selected',
				    autoArrows: false,
				    dropShadows: false
				}
			);
        }
    },

    DropDown: {
        handleDropDownTitleClick: function(e) {
            $(e.target).parent().toggleClass('open');
        },
        init: function() {
            $('.dropdown .title').click(this.handleDropDownTitleClick);
        }
    },

    Slideshow: {
        init: function() {

            var slideshow = $("#slideshow").jcarousel(
            // slideshow options
					{
					auto: 8, // number of seconds to show each slide
					animation: 0, // the speed of the slide transition
					circular: true,
					wrap: "both",
					scroll: 1, // step one slide at a time
					buttonNextHTML: '<span class="slideControl">Next &#9658;</span>',
					buttonPrevHTML: '<span class="slideControl">&#9668; Previous</span>',
					buttonPlayHTML: '<span class="slideControl pause">Pause</span>',
					itemVisibleInCallback: IFES.Slideshow.updateSlideshowCaptionHeight,

					// setup the play/pause button
					initCallback: function(jc, state) {
					    if (state == 'init') {

					        // if there's only one slide, hide the controls, and disable autoplay.
					        if (jc.options.size <= 1) {
					            $('.slideControl').css("display", "none");
					            jc.options.auto = 0;
					        }

					        jc.startAutoOrig = jc.startAuto;

					        jc.startAuto = function() {
					            if (!jc.paused) {
					                jc.startAutoOrig();
					            }
					        }

					        jc.pause = function() {
					            jc.paused = true;
					            jc.stopAuto();
					        };

					        jc.play = function() {
					            jc.paused = false;
					            jc.startAuto();
					        };

					        $('.jcarousel-play').click(function(e) {
					            var btn = $(e.currentTarget);

					            if (btn.hasClass("pause")) {
					                btn.text("Play");
					                btn.removeClass("pause");
					                $('.jcarousel-prev').css("margin-right", "90px");
					                jc.pause();
					            } else {
					                btn.text("Pause");
					                btn.addClass("pause");
					                $('.jcarousel-prev').css("margin-right", "100px");
					                jc.play();
					            }
					        });

					    }
					    jc.play();
					}
	}
				);

        },

        // update the height of the slideshow based on the contents after slide change.			
        updateSlideshowContentHeight: function(carousel, slide, index, action) {
            var carousel = $(slide).parent();
            carousel.css("height", $(slide).height());
        },

        updateSlideshowCaptionHeight: function(carousel, slide, index, action) {
            // grab the slide
            var slide = $(slide);

            // get the caption height
            var captionHeight = $(slide.find('.caption span')).height();

            // update the caption conatiner height
            $('#slideshow .caption').height(captionHeight);

            // adjust the position of the caption based on it's height				
            $('#slideshow .caption').css('top', -captionHeight - 10);

            // update the caption top margin
            $('#slideshow .slideDetails').css('top', -captionHeight - 10);

            // update slideshow height
            $('#slideshow').css("height", $('#slideshow').height());
        }
    },

    ElectionNewsFeed: {
        init: function() {
            $("#electionNewsFeed").simplyScroll(
					{
					    className: 'electionNewsFeed',
					    horizontal: false,
					    pauseOnHover: true,
					    autoMode: "loop",
					    speed: 1,
					    frameRate: 15
					}
				);
        }
    },

    WorldMap: {
        init: function() {

            // upon region clicked, show popup message		
            $("#world_map").click(function(e) {
                var id = e.target.id;

                // calculate the dimensions of the popup message
                var height = $('.popup_msg').height();
                var width = $('.popup_msg').width();
                var leftVal = e.pageX - (width / 2) + "px";
                var topVal = e.pageY - (height / 2) + "px";

                // clear out existing popups				
                $('.popup_msg').hide();

                // show the region's popup
                if (id == "americas") {
                    $("#americas_popup").css({ left: leftVal, top: topVal }).show();
                } else if (id == "africa") {
                    $("#africa_popup").css({ left: leftVal, top: topVal }).show();
                } else if (id == "middleeast") {
                    $("#middleeast_popup").css({ left: leftVal, top: topVal }).show();
                } else if (id == "eurasia") {
                    $("#eurasia_popup").css({ left: leftVal, top: topVal }).show();
                }
            });

        }
    },

    init: function(e) {

        //prevent default behaviour for anchors without a URL.
        $("[href=\"#\"]").click(IFES.preventDefault);

        IFES.MainNav.init();
        IFES.Slideshow.init();
        IFES.PostRenderStyling();
        IFES.DropDown.init();
        IFES.ElectionNewsFeed.init();
        IFES.BrowseByNav.init();
        IFES.WorldMap.init();

        // Setup photo gallery js when needed.
        if ($('.photoGallery-content').length) {
            $.getScript('/js/numberedslides.js', function() { IFES.PhotoGallery.init(); });
        }
    }
};

$(document).ready(IFES.init);

