$(function() {
	if ($('#site_bar').length) $('body').css('background-position', 'center ' + $('#site_bar').outerHeight() + 'px');
	
	$('#s').hint('inactive');
	
	// dynamically stretches navigation bar to fill width
	function adjustNavigationWidth(cycle) {
		if (typeof(cycle) == 'undefined') cycle = 0;
		
		$('nav#primary').css('overflow', 'hidden');  // hide overflow to prevent entire page from moving down until finished
		
		var navWidth = $('nav#primary > ul').width();
		var numLinks = $('nav#primary > ul > li').size();
		var linksWidth = 0;
		$('nav#primary > ul > li').each(function(i) {
			linksWidth += $(this).width();
		});
		linkPadding = Math.floor((navWidth - linksWidth) / (numLinks - 1));
		$('nav#primary > ul > li').not(':first').css({
			'padding-left': linkPadding,
			'background-position': Math.floor(linkPadding / 2 - 2) + 'px center'
		});
		
		if (cycle > 0) {
			index = cycle % numLinks;
			lessPadding = Math.floor(cycle / numLinks) + 1;
			$('nav#primary > ul > li:nth-child('+index+')').css('paddingLeft', parseInt($('nav#primary > ul > li:nth-child('+index+')').css('paddingLeft')) - lessPadding);
		}
		
		function doubleCheck() {
			if ($('ul.primary:first').height() > $('ul.primary:first > li:first').height() && cycle < numLinks) {
				adjustNavigationWidth(cycle + 1);  // still not right, try again
			}
			else {
				$('nav#primary').css('overflow', 'visible');  // perfect, make overflow (sub-menu) visible again
			}
		}
		
		setTimeout(doubleCheck, 100);
	}
	
	//adjustNavigationWidth();
	setTimeout(adjustNavigationWidth, 2000);
	setTimeout(adjustNavigationWidth, 4000); // once more just to be sure
	
	$('nav#primary>ul').superfish({
		hoverClass: 'hover',
		autoArrows: false,
		animation: {opacity: 'show'},
		speed: 0,
		dropShadows: false,
		onInit: function() {
			$('#header nav#primary>ul>li>ul>li ul').each(function() {
				$(this).css('left', $(this).parent().parent().width());
				$(this).parent().addClass('has-submenu');
			});
		}
	});
	
	$("form:not(.no-jquery-uniform) select, input:text:not(#s), input:checkbox, input:radio, input:file, textarea").uniform();
	
	$('.sponsorship:not(.extended) .duration:has(.to)').addClass('multiple');
	
	$('.audio > .toggle').click(function() {
		$parent = $(this).parent('.audio');
		if ($parent.hasClass('expanded')) {
			$parent.find('.content:first').slideUp(400, function() { $parent.removeClass('expanded'); });
		}
		else {
			$parent.find('.content:first').slideDown(400, function() { $parent.addClass('expanded'); });
		}
		return false;
	});
	
	$('.tabs a').click(function() {
		$tabs = $(this).parent().parent(); // ul.tabs
		$tabbed = $tabs.parent();
		$current = $tabs.siblings($tabs.find('.active a').attr('href'));
		$new = $tabs.siblings($(this).attr('href'));
		var difference = $new.height() - $current.height();
		
		if ($(this).attr('href') == $tabs.find('.active a').attr('href')) return false;
		
		// reset tabs, activate new
		$tabs.children('.active').removeClass('active');
		$(this).parent().addClass('active');
		
		// update URL
		document.location.hash = $(this).attr('href');
		
		/* animate height
		$tabbed.height($tabbed.height());
		$current.fadeOut(200, function() {
			$tabbed.animate({
				height: '+='+difference
			}, 200, null, function() {
				$new.fadeIn();
			});
		});
		*/
		$current.fadeOut(200, function() {
			$new.fadeIn();
		});
		
		return false;
	});
	
	if (document.location.hash) {
		$('.tabs a').each(function() {
			if ($(this).attr('href') == document.location.hash) $(this).trigger('click');
		});
	}
	
	// Featured
	if ($('#featured').length) {
		$('#featured').rotator({
			'controller': '#featured-controller',
			'images': '#featured-stack',
			'content': '#featured-content',
			'speed': 1000,
			'transition': 'horizontal',
			'easing': 'easeInOutQuart',
			'interval': true,
			'intervalDuration': 5000,
			'hoverPause': '#featured-stack, #featured-content'
		});
	}
	
	// Promos
	if ($('#promos').length) {
		$('#promos').rotator({
			'controller': '#promos-controller',
			'images': '#promos-stack',
			'content': false,
			'speed': 1000,
			'transition': 'horizontal',
			'easing': 'easeInOutQuart',
			'interval': true,
			'intervalDuration': 5000,
			'hoverPause': true
		});
		$('#promos .images li').removeAttr('id');
		
		var promoControllerWidth = 0;
		$('#promos .controller li').each(function() {
			promoControllerWidth += $(this).outerWidth(true);
		});
		$('#promos .controller').width(promoControllerWidth);
	}
	
	// Partners
	if ($('#partners').length) {
		$('#partners').rotator({
			'controller': false,
			'images': '#partners',
			'content': false,
			'speed': 1000,
			'transition': 'crossfade',
			'interval': true,
			'intervalDuration': 5000,
			'hoverPause': true
		});
	}

  // Calendar widget
  if ($('#mini-calendar').length) {
    $('#mini-calendar select').change(function() {
      newmonth = $('#mini-calendar select option:selected').val();
      $.cookie('month', newmonth);
      $.ajax({
        url: '/programs/cal/' + newmonth,
        cache: true,
        success: function(data) {
          $('tbody#calendar-body').replaceWith(data);
        }
      });
    });
    // If we have a cookie for 'month', make sure the minicalendar
    // reflects that month
    if ($.cookie('month')) {
      page_month = $('#mini-calendar select option:selected').val();
      if (page_month != $.cookie('month')) {
        $('#mini-calendar select').val($.cookie('month'));
        $('#mini-calendar select').change();
      }
    }
  }
});

