$(document).ready(function(){

	// PNG fix (IE6)
	$(document).pngFix();

	// open in new window when a rel="external"
	$('a[rel=external]').each(function() {
		if ($(this).attr('rel') == 'external') {
			$(this).attr('target', '_blank');
		}
	});

	// scroll content
	$(".slide-callout .scroll").css('height', '125px').css('overflow', 'hidden').jScrollPane({ scrollbarWidth: 4, scrollbarMargin: 20 });
	
	$('#masthead-nav li').hover(function() {
	    $(this).addClass('highlight');
	}, function() {
	    $(this).removeClass('highlight');
	});
	
	// Ride the carousel...
    $("#masthead-slides").jcarousel({
        scroll: 1,
        initCallback: carousel_initCallback,
        buttonNextHTML: null,
        buttonPrevHTML: null
    });

	// calendar date switcher

	$('#calendar thead th:not(.title) a').live("click", function() { 
	
		$.ajax({
		   type: "GET",
		   url: $(this).attr('href'),
		   cache: false,
		   success: function(data){	   		
		   		$('#calendar-data').html(data);	   
		   }
		 });
	
		return false; 
	});
	
	// calendar pop-up

	$('#calendar tbody td a').live("click", function() { 
	
		$.ajax({
		   type: "GET",
		   url: $(this).attr('href'),
		   cache: false,
		   success: function(data){	   		
		   		$('#calendar-popup-data').html(data).css('overflow', 'hidden');		   		
		   		$('#calendar-popup').fadeIn(500);		   
		   }
		 });
	
		return false; 
	});
	
	$('#calendar-popup-exit').css('opacity', .90);
	
	$('#calendar-popup-exit a').live("click", function() { 
		$('#calendar-popup').fadeOut('fast');
		return false; 
	});
	
	$('a#scroll-down').live("mouseover", function() {
		dHeight = $("#calendar-popup-data").height();
		pHeight = $("#calendar-popup-data .conferences").height();
		diffHeight = pHeight - dHeight;
		duration = Math.floor(diffHeight / '.05');
		if (pHeight > dHeight) {
			$("#calendar-popup-data .conferences").animate({"marginTop": -diffHeight + "px"}, duration);
		}
	});

	$('a#scroll-down').live("mouseout", function() {
		$("#calendar-popup-data .conferences").stop();
	});

	$('a#scroll-up').live("mouseover", function() {
		$("#calendar-popup-data .conferences").animate({"marginTop": "0px"}, 1000);
	});

	$('a#scroll-up').live("mouseout", function() {
		$("#calendar-popup-data .conferences").stop();
	});
	
});

/* ----- supporting functions (callbacks etc...) ----- */

function carousel_initCallback(carousel) {
	$('#masthead-nav li:first').addClass('active');
    $('#masthead-nav a').bind('click', function() {
    	$('#masthead-nav li').removeClass('active');
    	$(this).parent('li').addClass('active');
        carousel.scroll(jQuery.jcarousel.intval($(this).attr('rel')));
        return false;
    });
};