//functions
//footer carousel callbacks
function footerCarouselInit(carousel) {
	$('#footer-right').bind('click', function() {
		carousel.next();
		return false;
	});

	$('#footer-left').bind('click', function() {
		carousel.prev();
		return false;
	});
}

function footerCarouselItemFirstIn(carousel, item, idx, state) {
	if(idx != 1)
	    $('#footer-left').css({ 'background' : 'url(/media/images/footer_left_active.png) no-repeat', 'cursor' : 'pointer' });
	else
	    $('#footer-left').css({ 'background' : 'url(/media/images/footer_left_inactive.png) no-repeat', 'cursor' : 'auto' });
}

function footerCarouselLoad(carousel, item, evt) {
	var fcw = $('#footer-carousel-list').width();
	var fcl = $('#footer-carousel-list').css('left');
	fcl = parseInt(fcl.substr(0,fcl.search('px')));
	
	if((fcw + fcl) === $('#footer-carousel').width())
	    $('#footer-right').css({ 'background' : 'url(/media/images/footer_right_inactive.png) no-repeat', 'cursor' : 'auto' });
	else if($('#footer-carousel').width() > $('#footer-carousel-list').width())
		$('#footer-right').css({ 'background' : 'url(/media/images/footer_right_inactive.png) no-repeat', 'cursor' : 'auto' });
	else
		$('#footer-right').css({ 'background' : 'url(/media/images/footer_right_active.png) no-repeat', 'cursor' : 'pointer' });
}

//toggle chapter list overlay
var chapterOverlayVisible = false;

function toggleChapterFeeds() {
    if(chapterOverlayVisible) {
        chapterOverlayVisible = false;

        $('.chapterfeed-overlay').remove();
        $('#chapters-see-all').html('See All');
        $('#chapter-feed-items').css({ 'overflow': 'hidden', 'height': '80px' });
        $('#interior-chapter-feeds').css({ 'z-index': '1' });
    } else {
        var body = document.body, html = document.documentElement;
        var height = Math.max( body.scrollHeight, body.offsetHeight, html.clientHeight, html.scrollHeight, html.offsetHeight );
        chapterOverlayVisible = true;

        $('<div class="chapterfeed-overlay"></div>').appendTo(document.body);
        $('.chapterfeed-overlay').height(height);
        $('.chapterfeed-overlay').click(function () {
            toggleChapterFeeds();
        });
        $('#chapters-see-all').html('x Close');
        $('#chapter-feed-items').css({ 'overflow': 'visible', 'height': '0px' });
        $('#interior-chapter-feeds').css({ 'z-index': '3000' });
    }
}

//login box toggle
function toggleLogin() {
    var body = document.body, html = document.documentElement;
    var height = Math.max(body.scrollHeight, body.offsetHeight, html.clientHeight, html.scrollHeight, html.offsetHeight) - 26;
    $('.login-overlay').height(height);
    $('.login-overlay').toggle();
    if ($('.login-overlay').is(':visible'))
        $('#headerLoginLink').addClass('login-link-active');
    else
        $('#headerLoginLink').removeClass('login-link-active');
}

//audience ajax overlay
function loadIAma(url, linkTitle) {
    $('.iama-interiorpage').html('');
    $('#head-iama-container').hide();
    $('<iframe />', {
        'name': 'audience-frame',
        'id': 'audience-frame',
        'class': 'audience-frame',
        'scrolling': 'no',
        'frameborder': '0',
        'width': '830',
        'src': url
    }).appendTo('.iama-interiorpage');
    $('.iama-overlay').show();
    $('.audience-name').html(linkTitle);
    var body = document.body, html = document.documentElement;
    var height = Math.max(body.scrollHeight, body.offsetHeight, html.clientHeight, html.scrollHeight, html.offsetHeight) - 26;
    $('.iama-overlay').height(height);
}

function closeIAmaOverlay() {
    $('.iama-overlay').hide();
    $('.audience-name').html('&#0133;');
}

//resizing main container based upon child items to prevent footer issues
function resizeMain() {
    var wideHeight = $('.wide').height();
    var interiorLeftHeight = $('.interior-left').height();
    var fullInspirationHeight = $('.full-inspiration').height();
    var interiorRightHeight = $('.interior-right').height();
    var convLeftHeight = $('.conversations-left').height();

    if (wideHeight != null && wideHeight > 0) {
        //landing page height
        if ($('.interior-main.landing').length) {
            if ($('.wide').height() < $('.narrow').height()) {
                $('.interior-right').height($('.narrow').height());

		if(interiorLeftHeight > $('.interior-right').height())
			$('#main').height(interiorLeftHeight + 105);

	    }
            else {
		if(interiorLeftHeight > $('.interior-right').height())
			$('#main').height(interiorLeftHeight + 205);

                $('.interior-right').height($('.wide').height());
	    }
            return;
        }

        //resources page height
        if ($('.interior-main.resources').length) {
            if ($('.wide').height() < $('.narrow').height()) {
                $('.interior-right').height($('.narrow').height());
	    }
            else {
		if(interiorLeftHeight > $('.interior-right').height())
			$('#main').height(interiorLeftHeight + 205);

                $('.interior-right').height($('.wide').height());
	    }
            return;
        }

        //if we're on an interior page and the left column is taller than our wide widgets...
        if (interiorLeftHeight != null && interiorLeftHeight > wideHeight) {
            $('#main').height(interiorLeftHeight + 105);
            return;
        } else {
            if (interiorRightHeight != null && interiorRightHeight > wideHeight) {
                $('#main').height(interiorRightHeight + 105);
                return;
            } else if (fullInspirationHeight == null) {
                $('#main').height(wideHeight + 105);
            }
        }

        //if inspiration is taller than wide height
        if (fullInspirationHeight != null && fullInspirationHeight > wideHeight) {
            $('#main').height(fullInspirationHeight + 105);
            return;
        } else {
            $('#main').height(wideHeight + 105);
        }

        //about page height
        if ($('.about-main').length) {
            if ($('.about-left').height() > $('.about-right').height())
                $('#main').height($('.about-left').height() + 105);

            return;
        }

        //events page height
        if ($('.events-main').length) {
            if (($('.events-right .wide').is(':hidden')) || ($('.events-right .wide').is(':visible') && $('.events-right .wide').height() < $('.narrow').height())) {
                $('.events-right').height($('.narrow').height() + 50);
            }
            if ($('.events-left').height() > $('.events-main').height()) {
                $('.events-main').height($('.events-left').height() + 105);
            }
            return;
        }

        //profile page height
        if ($('.profile-main').length) {
            if (($('.narrow').height() > $('.wide').height()) || !$('.wide').is(':visible')) {
                $('.profile-right').height($('.narrow').height() + 40);
            }
            if ($('.profile-right').height() > $('.profile-left').height())
                $('.profile-main').height($('.profile-right').height());

            $('#main').height($('.profile-main').height() + 105);
            return;
        }

        //conversations page height
        if (convLeftHeight != null && convLeftHeight > wideHeight) {
            $('#main').height(convLeftHeight + 105);
            return;
        } else {
            $('#main').height(wideHeight + 105);
        }
    } else if (interiorLeftHeight != null) {
        $('#main').height(interiorLeftHeight + 105);
    } else if ($('.about-left') != null) {
        $('#main').height($('.about-left').height() + 105);
    }
}

//completely loaded failsafe for slower browsers
$(window).load(function () {
    resizeMain();
    $('#footer-carousel').width($(window).width() - 65);
});

//doc rdy
$(function () {

    //main height
    resizeMain();

    //header login
    if ($('#headerLoginLink') != null) {
        $('#headerLoginLink').click(function () {
            toggleLogin();
            return false;
        });
    }

    $('.login-overlay').click(function () {
        toggleLogin();
    });

    $('.login-container').click(function (e) { e.stopPropagation(); });

    $('.username-box').click(function () {
        if ($('.username-box').val() == 'User Name')
            $('.username-box').val('');
    });

    $('.password-box').click(function () {
        if ($('.password-box').val() == 'Password')
            $('.password-box').val('');
    });

    //window resize
    $(window).resize(function () {
        //resize main height on window resize
        resizeMain();

        //resize carousel on window resize
        $('#footer-carousel').width($(window).width() - 65);

        //if width of window is more than carousel, disable next button
        if ($('#footer-carousel').width() > $('#footer-carousel-list').width())
            $('#footer-right').css({ 'background': 'url(/media/images/footer_right_inactive.png) no-repeat' });
    });

    //set width on carousel based upon window width - buttons
    $('#footer-carousel').width($(window).width() - 65);

    //carousel on footer
    $('#footer-carousel ul li').each(function () {
        $(this).css({ 'width': 'auto' });
    });

    $('#footer-carousel-list').jcarousel({
        //Move one each time button is pressed
        scroll: 1,
        initCallback: footerCarouselInit,
        //This tells jCarousel NOT to autobuild prev/next buttons
        buttonNextHTML: null,
        buttonPrevHTML: null,
        //Callbacks to enable/disable the prev/next buttons
        itemFirstInCallback: footerCarouselItemFirstIn,
        itemLoadCallback: footerCarouselLoad
    });

    //iama dropdown
    $('.audience-selected').click(function () {
        $('#head-iama-container').toggle();
    });

    $('#head-iama-container a').click(function () {
        loadIAma($(this).attr('href'), $(this).attr('title'));
        return false;
    });

    $('.iama-overlay').click(function () {
        closeIAmaOverlay();
    });

    //chapter feeds overlay
    $('#chapters-see-all').click(function () {
        toggleChapterFeeds();
        return false;
    });

    //header search
    $('#headSearchInput').focus(function () {
        if (this.value == "SEARCH")
            this.value = "";
    });

    $('#headSearchInput').blur(function () {
        if (this.value == "")
            this.value = "SEARCH";
    });

    $('#headSearchInput').keypress(function (e) {
        if ((e.keyCode || e.which) == 13) {
            var searchString = "";
            if ($(this).val() != null && $(this).val() != "")
                searchString = escape($(this).val());
            window.location = '/search.aspx?searchtext=' + searchString;
            return false;
        }
        return true;
    });

    //comment input
    $('.comment-input').focus(function () {
        if (this.value == "Share your thoughts")
            this.value = "";
    });

    $('.comment-input').blur(function () {
        if (this.value == "")
            this.value = "Share your thoughts";
    });
});
