var url = location.pathname;
// check URL for language param
// swap language param and redirect

function langToggle(url){
    var language = url.match('/en/');
    if (language != null) {
        url = url.replace('/en/', "/fr/");
    }
    else {
        url = url.replace('/fr/', "/en/");
    };
    window.location = url;
};

// quick hide experts panel
document.getElementsByTagName('html')[0].className = 'hideFast';

function PanelNav(){
    var isActive;
    var panelId = location.hash;
    // Inject rounded corners for content menu rollover
    var cornerArray = ['topLeft', 'topRight', 'botLeft', 'botRight'];
    $(cornerArray).each(function(){
        var spanClass = this;
        $('div.content_menu ul li a').each(function(){
            var span = $('<span>').addClass(spanClass);
            $(this).append(span);
        });
    });
    if (panelId.length < 1) {
        resizeAndShowPanels(panelId);
    }
    else {
        resizeAndShowPanels(panelId)
    };
    // setup click functionality
    $('div.content_menu a').click(function(){
        isActive = $(this).attr('active');
        if (isActive != 'true') {
            panelId = $(this).attr('href');
            resizeAndShowPanels(panelId);
        }
    });
};

function resizeAndShowPanels(panelId){
    if (panelId.length < 1) {
        $('.expandable').animate({
            'height': $('.experts:first').height() + 200
        }, 200, 'swing');
        $('.expandable .info').animate({
            'height': $('.experts:first').height() + 15
        }, 300, 'swing', function(){
            $('.experts:first').css({
                'display': 'block'
            }).animate({
                'opacity': 1
            }, 600, 'linear');
        })
    }
    else {
        $('.expandable').css({
            'height': $(panelId).height() + 200
        }, 200, 'swing');
        
        $('div.content_menu a').removeAttr('active').removeClass('active');
        $('div.content_menu a[href="' + panelId + '"]').attr({
            'active': 'true',
            'class': 'active'
        });
        
        $('.experts').css({
            display: 'none',
            opacity: 0
        });
        
        $('.expandable .info').animate({
            'height': $(panelId).height() + 15
        }, 300, 'swing', function(){
            $(panelId).css({
                'display': 'block'
            }).animate({
                'opacity': 1
            }, 600, 'linear');
        })
    }
    
}

function deepLinker(menuWrapper){
    var deepLink = location.pathname;
    $(menuWrapper + ' a[href="' + deepLink + '"]').addClass('active');
}

function animatePods(){
    $('ul.pods li').animate({
        'margin': "0px"
    });
    $('ul.pods li.staggered').animate({
        'margin': "30px 0 0 0"
    });
}



// shows full window overlay.
//will also block user from interacting with page elements with a lesser z-index
function showFullOverlay(noticeID, opacityVal, color){
    $('select').addClass("hide_select"); // This is for IE6 only
    var overlayHeight = $(document).height();
    var overlayWidth = $(document).width();
    $('div.overlay').css({
        display: "block",
        height: overlayHeight,
        width: overlayWidth,
        opacity: opacityVal,
        backgroundColor: color
    });
    $('div#' + noticeID).css({
        display: "block"
    })
};

function hideFullOverlay(noticeID){
    $('select').removeClass("hide_select"); // This is for IE6 only
    $('div.overlay').css({
        display: "none"
    });
    $('div#' + noticeID).css({
        display: "none"
    })
};

//open out going links (http:// prefix) in a new window
function openHTTPlinksInNewWindow(){
    $('a[href^="http://"]').each(function(){
        $(this).attr({
            target: "_blank",
            title: $(this).attr('title') + ' - Opens in a new window.'
        });
    });
}

function getConfirmationPage(){
    if (url.match('error.html')) {
        $('#Submit').click(function(){
            $(this).attr({
                'href': $(this).attr('href') + $.trim($('#pin').val())
            });
        })
    }
    else {
        $('#Submit').attr({
            'target': '_blank'
        }).click(function(){
            $(this).attr({
                'href': tempHref + $.trim($('#pin').val())
            });
        })
    }
}

function aimatePods(){
    if (!navigator.appName.match("Microsoft Internet Explorer")) {
        var i = 0;
        var e = 0;
        $('ul.pods li').css({
            'opacity': 0
        });
        for (i = 0; i <= 2; i++) {
            $('ul.pods').css({
                'visibility': 'visible'
            }).animate({
                'opacity': 1
            }, 300, 'swing', function(){
                $('ul.pods li:eq(' + e + ')').animate({
                    'opacity': 1
                }, 800, 'swing');
                e++;
            })
        }
    }
    else {
        $('ul.pods').css({
            'visibility': 'visible'
        })
    }
}



var tempHref;
$(document).ready(function(){

    aimatePods()
    // set active links
    deepLinker('#Navigation')
    // attach target="_blank" to out going links
    openHTTPlinksInNewWindow()
    // language toggle
    $('a.langToggle').click(function(){
        langToggle(url);
        return false;
    });
    // get confirmation page
    getConfirmationPage();
    // experts/kids panel nav functionality
    PanelNav();
    
    tempHref = $('#Submit').attr('href');
});
