/* default fancybox options */
var fancybox_overlay_opacity = 0.65, fancybox_padding = 0;
var default_fancybox_options = {
	'overlayOpacity' : fancybox_overlay_opacity,
	'padding' : fancybox_padding,
	'easingIn' : 'easeOutBack',
	'easingOut' : 'easeInBack'
}

var cancel_reset_nav = false;

jQuery(function($)
{

	/* set up tabs */
	$('.tabs').tabs({
		show : set_up_lightboxes //sets up lightbox handler on tabs
	});
	
	/* set auto-clear fields */
	set_up_autoclear_fields();
	
	/* main nav */
	/* handle label for zip code field for find representative */
	$('#find_representative label').click(function(event){
		$(this).prev().find(':text').val('').attr('maxlength', '5').focus();
		event.stopPropagation();
		event.preventDefault();
	});
	
	$(':input.zip_code').click(function(event){
		set_maxlength_on_field($(this), 5);
		event.stopPropagation();
		event.preventDefault();
	});
	
	/* handle form submission to find representative */
	$('#find_representative :submit').click(function(event){
		$(this).closest('form').submit();
	});
	
	/* shows more content indicator in main nav */
	var main_nav = $('#main_nav');
	var more_nav_content = $('#more_nav_content');
	var more_hover_obj = null;
	
	
	/* even heights of "more"content */
	main_nav.children('li').children('a.more').hover(
		function()
		{
			if (more_hover_obj)
			{
				cancel_reset_nav = true;
				
				if ($('#more_nav_content:animated').length > 0)
					more_nav_content.css('bottom', '0px');
				else
					if (more_nav_content.css('bottom') != '0px') show_more_content_indicator();
				
				more_hover_obj = null;
			}
			else
				show_more_content_indicator();
		},
		function()
		{
			more_hover_obj = $(this);
			cancel_reset_nav = false;
			setTimeout('reset_more_nav_content()', 500);
		}
	).click(function(event){
		var element = $(this);
		element.next().toggle().end().parent().toggleClass('active');
		
		/* hide other subnav more content divs and de-activate other links */
		$.each(main_nav.children('li.more'), function(index, li){
			if (li.id != element.parent().attr('id'))
				$(li).removeClass('active').children('div.more').hide();
		});
		
		/* even heights of content divs */
		if (element.parent().hasClass('active'))
		{
			var content_div_heights = [], content_divs = [];
			$.each(element.next().find('div.content'), function(i, content_div)
			{
				var content_div = $(content_div);
				content_div_heights.push($(content_div).height());
				content_divs.push(content_div);
			});
			
			if (content_div_heights.length > 1) 
			{
				var tallest_content_div = get_highest_value(content_div_heights);
				$.each(content_divs, function(i, content_div)
				{
					$(content_div).height(tallest_content_div);
				});
			}
		}
		
		/* add margin to top of content div to push it below the "more" content */
		var content_margin;
		element.parent().hasClass('active') ? content_margin = (element.next().height() + 10) : content_margin = 5;
		$('#content').css('margin-top', content_margin + 'px');
		
		event.preventDefault();
		return false;
	});
	
	/* close more click handler */
	$('li.close_more a').click(function(event){
		main_nav.children('li.more').removeClass('active').children('div.more').hide();
		$('#content').css('margin-top', '5px');
		
		event.preventDefault();
		return false;
	});
	
	/* set up lightboxes */
	set_up_lightboxes();
	
	/* set up font-size increase/decrease */
	var text_size = $('.text_size');
	var text_size_links = text_size.children('a');
	text_size_links.click(function(event){
		text_size_links.removeClass('active');
		$(this).addClass('active');		
		
		event.preventDefault();
	});
	
	
	/* set up external links */
	$("a.external, #polling a[href*='http']").click(function(event){
		window.open(this.href);
		event.preventDefault();
		return false;
	});
	
	
    // Set up share links.
    $('.social_links a.share').click(function () {
        var ellipsize = function (message, url) {
            if(message.length + url.length + 1 > 140) {
                message = message.substr(0, 140 - url.length - 2) + '\u2026';
            }
            return message + ' ' + url;
        };

        var urlToShare = document.location.toString().replace(/comments\/$/, '')
        var $shareLinks = $(this).siblings('.share_links');

        $shareLinks.find('a[name=fb_share]').attr('share_url', urlToShare);
        var $fbScript = $('<script type="text/javascript"></script>');
        $fbScript.attr('src', 'http://static.ak.fbcdn.net/connect.php/js/FB.Share');
        $('head').append($fbScript);

        var $twitterLink = $shareLinks.find('a.twitter_share');
        $twitterLink.attr('href', 'http://twitter.com/home?status=' +
            encodeURIComponent(ellipsize(document.title, urlToShare)));

        BitlyCB.shortenResponse = function (data) {
            var hash = data.results[urlToShare].userHash;
            var shortURL = 'http://bit.ly/' + hash;
            $twitterLink.attr('href', 'http://twitter.com/home?status=' +
                encodeURIComponent(ellipsize(document.title, shortURL)));
            if(!$(this).closest('.social_links').hasClass('inline')) {
                BitlyClient.stats(hash, 'BitlyCB.statsResponse');
            }
        };

        BitlyCB.statsResponse = function (data) {
            if(data.results.clicks) {
                var s = data.results.clicks == 1 ? '' : 's';
                $twitterLink.siblings('.tweet_count')
                    .before('<br/>')
                    .text('(' + data.results.clicks + ' tweet click'+s+')');
            }
        };

        BitlyClient.shorten(urlToShare, 'BitlyCB.shortenResponse');

        $(this).fadeOut('normal', function () {
            $shareLinks.fadeIn();
        });

        return false;
    });
	
	
	/* even heights of footer element containers */
	footer_elements = ['#staff_writers', '#footer_topics', '#offsite_outlets'];
	footer_element_heights = [];
	$.each(footer_elements, function(index, element){
		footer_element_heights.push($(element).height());
	});
	
	tallest_footer_element = get_highest_value(footer_element_heights);
	$.each(footer_elements, function(index, element){
		$(element).height(tallest_footer_element);
	});
	
	
	/* event handler to hide message cue */
	setTimeout("fade_out_elements('.message')", 3000);
});

function reset_more_nav_content()
{
	if (!cancel_reset_nav)
	{
		$('#more_nav_content').animate({
			'bottom' : 5
		}, 'fast');
	}
}

function show_more_content_indicator()
{
	$('#more_nav_content').animate({
		'bottom': 0
	}, 'fast');
}

