// JavaScript Document
function switchAds() {
	var $ads = $('div#footer-panel-right div.image-advertisement');
	var $active = $ads.filter('.active-ad');
	if ( $active.length == 0 ) $active = $('div#footer-panel-right div.image-advertisement:first');

    var $next =  $active.siblings('.image-advertisement').eq(0).length ? $active.siblings('.image-advertisement').eq(0)
        : $('div#footer-panel-right div.image-advertisement:first');
	
	$active.addClass('last-active-ad');
	
	$next.css({opacity:0.0})
			.addClass('active-ad')
			.animate({opacity:1.0},1000,function() {
				$active.removeClass('active-ad last-active-ad');
			});
}

$(document).ready(function() {
	// Show/hide department shortcuts.
	$('div#resource-shortcuts-img a').click(function() {
		$('div#resource-shortcuts-underlay').toggle();
		$('div#resource-shortcuts').toggle();
		return false;
	});
	
	$('div#resource-shortcuts-underlay').click(function() {
		$('div#resource-shortcuts').hide();
		$('div#resource-shortcuts-underlay').hide();
		return false;
	});
	$('div#resource-shortcuts').mouseover(function() {
		$('div#resource-shortcuts-underlay').mouseover(function() {
			$('div#resource-shortcuts').hide();
			$('div#resource-shortcuts-underlay').hide();
			$(this).unbind('mouseover');
		});
	});
	
	// Handle search type switching.
	$('div#resource-search-type-selector a').click(function() {
		var new_type = $(this).attr('rel');
		
		//$('input#resource-search-type').val(new_type);
		$('input#edit-sitesearch').val(new_type);
		$('div#resource-search-type-selector a').removeClass();
		$(this).addClass('active');
		return false;
	});

	// Direct user to e-mail link for a given page.
	$('a.email-page').click(function() {
		var emailSubject = encodeURI("DSA Student Affairs: "+$('h1.title').text());
		var emailBody = '';
		if ($('div#node-teaser').text())
		{
			//emailBody += encodeURI($('div#node-teaser').text())+'%0D%0A%0D%0A';
			emailBody += encodeURI('I found this great article on the DSA website:')+'%0D%0A%0D%0A';
		}
		emailBody += encodeURI($('h1.title').text())+'%0D%0A';
		emailBody += encodeURI(location.href);
		
		var emailAddress = 'mailto:?subject='+emailSubject+'&body='+emailBody;
		location.href = emailAddress;
		
		return false;
	});

	$('a.email-homepage').click(function() {
		var nid = $(this).attr('rel');
		
		var emailSubject = encodeURI("DSA Student Affairs: "+$('#homepage-node-title-'+nid).text());
		//var emailBody = encodeURI($('#homepage-node-teaser-'+nid).text())+'%0D%0A%0D%0A'+encodeURI($(this).attr('href'));
		var emailBody = encodeURI('I found this great article on the DSA website:')+'%0D%0A%0D%0A';
		emailBody += encodeURI($('#homepage-node-title-'+nid).text())+'%0D%0A';
		emailBody += encodeURI($(this).attr('href'));
		var emailAddress = 'mailto:?subject='+emailSubject+'&body='+emailBody;
		location.href = emailAddress;
		
		return false;
	});
	
	// make external links open new window
	$('a[rel="external"]').attr('target','_blank');

        // story node image switching
        $('a.story-image-thumb').click(function() {

		$("div#story-images div.story-image").hide();
		$('div#story-image-'+$(this).attr('rel')).fadeIn("fast");

		return false;
	});

	// rotate footer ads
	var $ads = $('div#footer-panel-right div.image-advertisement');
	if ($ads.length > 0 ) {
		$('div#footer-panel-right div.image-advertisement:first').addClass('active-ad');
		setInterval(switchAds,5000);
	}
});