if (!TL) { var TL = {}; }
if (!TL.components) { TL.components = {}; }


/**
* Use $j or jQuery when referencing the jQuery object
*/
var $j = jQuery.noConflict();


/**
* Live events that do not need to be bound on DOM ready
*/

$j('input.button').live('mouseover mouseout', function(e){
    (e.type === 'mouseover') ? $j(this).addClass('hover') : $j(this).removeClass('hover');
});

$j('input.txt-signup').live('focus', function(){
    if (this.value == 'email address') { this.value = ''; }
});

// Search text input - submit on Enter
$j('input.txt-search').live('keydown', function(e){
    if (e.which === 13) {
        e.preventDefault();
        this.value = $j.trim(this.value);
        if (this.value !== "") {
            SearchBtnSubmit();
        }
    }
});

$j('input.txt-search').live('focus', function(){
    if (this.value == 'search') { this.value = ''; }
});

$j('#slider .quiz input[type=radio]').live('click', function(e){
    e.preventDefault();
    var selector = ($j(this).parents('span').hasClass('correct') === true ) ? 'Correct' : 'Wrong';
    $j(this)
        .parents('.quiz') // traverse up the dom to div.quiz
        .find('> div').hide() // hide all children of div.quiz
        .end() // return the set of matched elements to its previous state (div.quiz)
        .find('div[id$='+selector+']').show() // show the div for Correct or Wrong
        .find('.content p:first').attr('tabindex', '-1').focus(); // Focus for accessibility
});


/**
* Featured Videos
*/
function selectVideo(id) {
    var i;
    for(i=1; i<= 3; i++)
    {
        if (id != i)
        {
             $j('#video'+i).hide();
             $j('#videoTitle'+i).removeClass('selectedVideoTitle').addClass('normalVideoTitle');
        }else{
            $j('#video'+id).show();
            $j('#videoTitle'+i).removeClass('normalVideoTitle').addClass('selectedVideoTitle');
        }
    }
}

/**
* Initilize main features content slider
*/
$j(window).load(function(){
    if ($j('#slider').length > 0) {
    
        // Add slider navigation
        var total_width = 0,
            list = $j('<ul/>'),
            l = $j('#slider li.item').length,
            next = $j('<a/>')
                        .attr('title', 'Next Feature')
                        .attr('href', '#')
                        .addClass('next')
                        .html('Next Feature'),
            prev = $j('<a/>')
                        .attr('title', 'Previous Feature')
                        .attr('href', '#')
                        .addClass('prev')
                        .html('Previous Feature');
        
        // If there is only one panel and it's a feature article, remove the bottom drop shadow
        if (l === 1 && $j('#slider .item').hasClass('article')) {
                $j('#slider .nav').css('background', 'none');
        }
        
        // If there is only one panel, slider is not required
        if (l <= 1) { return false; }
        
        $j('#slider li.item').each(function(i){
            var active = (i === 0) ? 'active' : '',
                item = $j('<li/>').addClass(active),
                anchor = $j('<a/>')
                                .attr('title', 'Feature '+(i+1))
                                .attr('href', '#')
                                .html('Feature '+(i+1));
            item.append(anchor);
            list.append(item);
            total_width += $j(this).outerWidth(true);
            
            // set a width now that content is loaded
            $j(this).width($j(this).width());
        });
        
        // Set the scrollable width equal to the total width of all panels (plus 10px padding)
        $j('#slider .panels ul').width((total_width+10));
        
        $j('#slider .nav').append(list).prepend(prev).append(next);
        $j('#slider .nav').wrapInner('<div class="wrap"/>');
        
        // make slider nav visible
        $j('#slider .nav').css('visibility', 'visible');
        
        var opts = {
            target:'.panels',
            items: 'li',
            navigation:'.nav li a',
            next: '.nav .next',
            prev: '.nav .prev',
            constant:false,
            start:0,
            interval:($j('body').hasClass('index')) ? 7000 : 0,
            force:true,
            stop:true,
            lock:false,
            duration:1000,
            easing:'easeOutExpo',
            onBefore:function( e, elem, $pane, $items, pos ) {
                $j('#slider .nav li').removeClass('active');
                $j('#slider .nav li:eq('+pos+')').addClass('active');
            }
        }
        
        $j('#slider').serialScroll(opts);
    }
});

/**
* Initialize Galleria photo galleries on article pages
*/
$j(window).load(function(){
    $j('#article .galleria').galleria({ width: 700, height: 378, autoplay: 6000 });
});

/**
* Setup the Secondary nav dropdown (Plan a Smarter Trip)
*/
$j(window).load(function(){

    // Creates the overflow scroll
    var menuHeight = $j('#ddmenu .content').outerHeight();
    $j('#ddmenu ul').height(menuHeight);
    $j('#ddmenu .dropdown').height(menuHeight*2);
    
    $j('#ddmenu ul').scrollTo('100%', {duration:0});
    
    var openDDMenu = function() {
            $j('#ddmenu ul').css('top', '100%').stop(true).scrollTo('0%', {
                easing: 'easeOutExpo',
                duration:1000
            });
            $j('#ddmenu .past').addClass('opened');
            $j('#ddmenu .toggle:first').addClass('toggleon');
        },
        closeDDMenu = function() {
            $j('#ddmenu ul').stop(true).scrollTo('100%', {
                duration:500,
                easing: 'easeInExpo',
                onAfter:function(){
                    $j('#ddmenu ul').css('top', '-9999px');
                    $j('#ddmenu .toggle:first').removeClass('toggleon');
                    $j('#ddmenu .past').removeClass('opened');
                }
            });
        },
        hover_id = null;
    
    
    if (Modernizr.touch === true) {
        $j('#ddmenu .toggle')
            .css('display', 'inline-block')
            .bind('touchstart', function(e){ e.preventDefault(); });
        $j('#ddmenu .toggle').bind('touchend', function(e){
            e.preventDefault();
            if ($j('#ddmenu ul').css('top') == '100%') {
                closeDDMenu();
            } else {
                openDDMenu();
            }
        });
    } else {
        // Add a hover delay to prevent opening menu by accident
        $j('#ddmenu').hover(function(){
            hover_id = setTimeout(function(){
                hover_id = null;
                openDDMenu();
            }, 500);
            
        },
        function(){
            if (hover_id === null) {
                closeDDMenu();
            } else {
                clearTimeout(hover_id);
            }
        });
        
        $j('#ddmenu .past').click(function(e){
            e.preventDefault();
            location.href = $j(this).find('.title a').attr('href');
        });
    }
});


TL.components.Search = function() {
    return {
        submit: function() {
            var tzOffset = -new Date().getTimezoneOffset();
            var searchString = $j.trim(document.getElementById('ctl02_ctl01_TXTQuery').value);

            location.href = '/site-info/search-results.aspx?&amp;lcid=9&amp;q=' + encodeURIComponent(searchString) + '&amp;t=' + tzOffset;
        },
        submitDocLib: function() {
            var form = document.forms[0];
            form.action = "/site-info/search-results.aspx";
            
            var el = document.createElement("input");
            el.type = "hidden";
            el.name = "docLib";
            el.value = "1";

            form.appendChild(el);
            form.submit();
        },
        update: function() {
            $j('.CesSearch .result-list > span:nth-child(even)').addClass('even');
        }
    }
} ();

// Shortcut
var tl = TL.components ;






$j(document).ready(function(){

    if ($j('body').hasClass('ie6')) {
        IE6Container.append(IE6Message);
        $j('body').append(IE6Container);
    }


    $j('body').removeClass('no-js');
    
    
    // Terms and Privacy page scroll animation
    $j('a[href=#top]').click(function() {
        $j(window).scrollTo('0%', { duration: 500 });
        return false;
    });
    
    /**
    * Articles
    */
    
    // insert a span tag into blockquotes for styling purposes
    $j('#article blockquote').wrapInner('<span/>');
    // add row striping for article tables and comments
    $j("#article table:not(.no-stripes) tbody tr:nth-child(even), #comments .comment:nth-child(even)").addClass("even");
    
    
    if (Modernizr.borderradius === false) {
        // round left sides
        $j('.side-highlight, .featured-videos, #article blockquote').each(function(){
            $j(this).cornerz({corners:'tl bl',fixIE:false});
        });
    }
    
    // Misc. round corners
    $j('#article .hero, #slider .article').cornerz({corners:'tl bl'});

    // Font resizer for Articles
    if ($j('#article').length > 0) {
        $j('#fontsizer').jfontsizer({ applyTo: '#article', changesmall: '2', changelarge: '2', expire: 30 });
        $j('#btnFontResize').click(function(){
            var next = $j('#fontsizer a.fsactive').next('a').trigger('click');
            if (next.length == 0) {
                $j('#fontsizer a').first().trigger('click');
            }
        });
    }

});

function SearchBtnSubmit()
{
    tl.Search.submit();
}

var IE6Container = $j("<div/>").addClass('ie6-container');
var IE6Message = $j('<p/>')
                        .addClass('msg')
                        .html("Just as there are sustainable ways to travel, there's more sustainable ways to view the internet! <br />Please upgrade your browser to the latest version and come back to see us in our full glory.  We'll be waiting! <br />Visit <a href=\"http://www.microsoft.com/windows/internet-explorer/\">http://www.microsoft.com/windows/internet-explorer/</a> to upgrade");
