var lastMarginTop = 0;
var theFollower;
var origPos = 0;

$(document).ready(function () {
    setupAutoClears();
    setupForms();
    setupCarousel();
    //setupFollower($('#caseStudiesContainer'), $('#rightContent'));
    setupFollower($('.BlogFeatures'), $('.BlogStreamNarrow'));
    setupFold();
    setupFancybox();
});

function setupFancybox() {
    $('#subLaunch, #subscribetext a').fancybox({
        'titleShow': false
    });
}

function setupFollower(follower, followee) {
    //if ($('#caseStudiesContainer').length > 0 && $('#rightContent').children().length == 1)
    if ($(follower).length > 0 && $(followee).length > 0) {
        if ($(follower).height() < $(followee).height()) {
            theFollower = follower;
            setInterval("follow()", 10);
        }
    }
}

function follow() {    
    var rightContentPos = $(theFollower).offset();

    if ($(window).scrollTop() > rightContentPos.top) {
        origPos = rightContentPos.top;
        $(theFollower)/*.parent()*/.css({
            'position': 'fixed',
            'top': 0,
            'left': rightContentPos.left + 'px'
        });
    }
    else if($(window).scrollTop() <= origPos)
    {
        $(theFollower)/*.parent()*/.css({
            'position' : 'relative',
            'left' : 0
        });
    }
}

function setupCarousel() {
    if (typeof $(".carousel").children("ul").jcarousel == 'function') {
        $('.carousel').children("ul").each(function() {
            $(this).parent().children('.next, .prev').css('display', 'block');
            $(this).children('li').children('.textOverlay').css('opacity', '0.8');
            $(this).children('li').children('.textOverlay').css({ 'paddingRight': '30px', 'width': '420px' }).append('<div class="show-hide" rel="show"></div>');
            
            $(this).children('li').children('.textOverlay').children('.show-hide').css('backgroundPosition','bottom left').click(function() {
                if ($(this).attr('rel') == 'show') {
                    $(this).css('backgroundPosition', 'top left');
                    $(this).attr('rel', 'hide');
                    
                    $(this).parent().animate({
                        'bottom': '-' + ($(this).parent().outerHeight() - 29) + 'px'
                    },
                    1000,
                    'easeInOutCubic',
                    null);
                }
                else {
                    $(this).css('backgroundPosition', 'bottom left');
                    $(this).attr('rel', 'show');
                    $(this).parent().animate({
                        'bottom': '0px'
                    },
                    1000,
                    'easeInOutCubic',
                    null);
                }
            });

            $(this).jcarousel({
                wrap: 'circular',
                scroll: 1,
                initCallback: mycarousel_initCallback,
                buttonNextHTML: null,
                buttonPrevHTML: null,
                animation: 1000,
                easing: 'easeInOutCubic'
            });
        });
    }
}

function mycarousel_initCallback(carousel) {
    $(carousel.container).parent().parent().children('.carouselControl').children('li').children('a').click(function() {
        $(this).parent().parent().children('li').removeClass('selected');
        $(this).parent().addClass('selected');
        carousel.scroll(jQuery.jcarousel.intval($(this).attr('rel')));
        return false;
    });

    $(carousel.container).parent().children('.next').bind('click', function() {
        carousel.next();
        return false;
    });

    $(carousel.container).parent().children('.prev').bind('click', function() {
        carousel.prev();
        return false;
    });
};

function setupForms() {
    $(window).keypress(function(e) {
        if (e.which == 13)
            return false;
        else
            return true;
    });
    $('.form').find('input').keydown(function(e) {
        if (e.which == 13) {
            if ($(this).parents('.form').find('.formEnter')[0].tagName.toLowerCase() == 'a')
                window.location = $(this).parents('.form').find('.formEnter').attr('href')
            $(this).parents('.form').find('.formEnter').trigger('click');
        }
    });
}

function setupAutoClears() {
    $(".autoClear").focus(function() {
        if (!$(this).attr('originalVal')) {
            $(this).attr('originalVal', $(this).val());
            $(this).val('');
        }
    }).blur(function() {
        if ($.trim($(this).val()).length == 0) {
            $(this).val($(this).attr('originalVal'));
            $(this).removeAttr('originalVal');
        }
    });
}

function setupFold() {
    $("#pagefold").hover(function() {
        $("#pagefold img , .msg_block").stop()
		.animate({
		    width: '307px',
		    height: '319px'
		}, 500);
    }, function() {
        $("#pagefold img").stop()
		.animate({
		    width: '70px',
		    height: '72px'
		}, 220);
        $(".msg_block").stop()
		.animate({
		    width: '70px',
		    height: '70px'
		}, 200);
    });
}
