$(document).ready(function () {
    $(".tabs .tab-pane").hide(); // Hide tab panes
    $(".tabs .tab-pane:first").show(); // Show first
    $(".tabs li:first").addClass('active');

    $(".tabs ul li").click(function () {
        $(".tabs li").removeClass('active');
        $(this).addClass('active');
        var index = $(".tabs li").index(this) + 2;
        $(".tabs .tab-pane").hide(); // Hide tab panes
        $(".tabs .tab-pane:nth-child(" + index + ")").show(); // Show clicked tab-pane
    });

    $("#login-tab").toggle(function () {
        $("#login-display").show();
        $("#forgot-password-display").hide();
        $("#login-box").animate({ right: '-13px' }, 400);
        $("#login-box").css('box-shadow', '0 0 4px 1px #9e9e9e');

    },
	function () {
	    $("#login-box").animate({ right: '-318px' }, 400, function () {
	        $("#login-box").css('box-shadow', 'none');
	    });
	});

    $("#font-size-toggle span").click(function () {
        $("#font-size-toggle span").removeClass('active');
    });
    $("#font-size-toggle #small").click(function () {
        $(this).addClass('active');
        $("#main-content, #main-content-plain, .caption").attr("style", "");
    });
    $("#font-size-toggle #medium").click(function () {
        $(this).addClass('active');
        $("#main-content, #main-content-plain, .caption").attr("style", "font-size:125% !important; line-height:1.5em;");
    });
    $("#font-size-toggle #large").click(function () {
        $(this).addClass('active');
        $("#main-content, #main-content-plain, .caption").attr("style", "font-size:150% !important; line-height:1.5em;");
    });



    // Login / Forgot password
    SetForgetPasswordOnClick();

    // Mini Slider 

    function miniSlider(minPadding) {
        var miniSliderWidth = $('.mini-slider').width() - 84;
        var imgWidthTotal = 0;
        var totalImages = 0;
        $(".mini-slider li").each(function (i) {
            imgWidthTotal += $('img', this).width(); // add image widths together.
            imgWidthTotal += minPadding; // add padding

            //alert("imgWidthTotal = "+imgWidthTotal+" totalImages= "+totalImages+" miniSliderWidth= "+miniSliderWidth)

            if (imgWidthTotal < miniSliderWidth) {
                totalImages += 1;
                return true;
            } else {
                totalImages -= 1;
                imgWidthTotal -= $('img', this).width() + minPadding; // remove current image
                return false;
            }
        });

        //alert(totalImages)
        var padding = minPadding / 2;
        var remainder = (miniSliderWidth - imgWidthTotal + padding) / 2;

        $('.mini-slider a').css({
            'padding-left': padding,
            'padding-right': padding
        });


        $('.mini-slider a:first').css({ 'padding-left': remainder });
        $('.mini-slider a:eq(' + totalImages + ')').css({ 'padding-right': remainder });
    }

    miniSlider(30);

    $('.mini-slider #next').click(function () {
        li = $('.mini-slider ul li:first').detach();
        li.appendTo('.mini-slider ul');
        miniSlider(30);
    });
    $('.mini-slider #prev').click(function () {
        li = $('.mini-slider ul li:last').detach();
        li.prependTo('.mini-slider ul');
        miniSlider(30);
    });

    $('#slider').gallery({
        'speed': 1000,
        'cycleSpeed': 7000,
        'cycle': true,
        'pagerType': false,
        'teasers': '#teasers div',
        'captionType': '.slide-copy'
    });

});

function SetForgetPasswordOnClick() {
    $("#forgot-password").click(function (event) {
        event.preventDefault(); // stop # link from jumping page
        $("#login-display").hide();
        $("#forgot-password-display").show();
    });
}

function SetActiveMainNav(navdiv, navpath) {
    try {
        if (navpath == undefined || navpath == '') {
            $("#" + navdiv + " a").removeClass('active');
            $($("#" + navdiv + " a").get(0)).addClass('active');
            return;
        }
        $("#" + navdiv + " a").removeClass('active');
        $("#" + navdiv + " a").each(function () {
            if (this.pathname == navpath || this.pathname == '/' + navpath) {
                $(this).addClass('active');
            }
        });
    } catch (err) { }
}

function recordOutboundLink(link, category, action) {
    _gat._getTrackerByName()._trackEvent(category, action);
    setTimeout('document.location = "' + link.href + '"', 100);
}

function openFaceBookShare(url) {
    //var url = encodeURIComponent(url);
    window.open('http://www.facebook.com/sharer.php?u=' + url);
}

function openTwitterShare(url) {
    //var url = encodeURIComponent(url);
    window.open('http://twitter.com/home?' + url);
}

function openLinkedInShare(url) {
    //var url = encodeURIComponent(url);
    window.open('http://www.linkedin.com/shareArticle?mini=true&' + url);
}
