Cufon.replace('.tours-list .box h3, .events-block h3, #subnav .title h2, #timeline-short .head h3, .papers h3');

$(function() {
	
	$('#timeline .nav a').live('click', function() {
		var href = $(this).attr('href');
		var position = $(href).position().left;
		$('#timeline .periods').animate({
			'left' : -1*parseInt(position)
		}, 'fast', function() {
			checkTimlinePosition();
		});
		return false;
	});
	
	$('#coda-slider').codaSlider({
    	dynamicArrows: false,
		dynamicTabs: false
   	});
   	
   	$('#helper .coda-nav li a').live('click', function() {
   		return false;
   	});
	
	$('.wp-caption, #footer .gallery a').facebox();	
	
	$(document).bind('loading.facebox', function() {
		$('.caption-gallery .thumbs a').live('click', function() {
			$(this).parents('.caption-gallery:eq(0)').find('.thumbs a').removeClass('active');
			$(this).addClass('active');
			var href = $(this).attr('href');
			var big_image = $(this).parents('.caption-gallery:eq(0)').find('.big img');
			big_image.fadeOut(function() {
				$(this).attr('src', href).fadeIn();
			});
			var big_cap = $(this).parents('.caption-gallery:eq(0)').find('.big p');
			big_cap.fadeOut(function() {
				$(this).attr('src', href).fadeIn();
			});
			return false;
		});
	})
	
	$('#timeline .nav .prev, #timeline .nav .next').live('click', function() {
		if ($(this).hasClass('prev')) {
			$('#timeline .periods').animate({
				'left' : "+=45px"
			}, 'fast', function() {
				checkTimlinePosition();
			});
		};
		if ($(this).hasClass('next')) {
			$('#timeline .periods').animate({
				'left' : "-=45px"
			}, 'fast', function() {
				checkTimlinePosition();
			});
		};
		return false;
	});
	
	function checkTimlinePosition() {
		var currentPosition = parseInt($('#timeline .periods').position().left) - 45;
		var idx = Math.ceil(Math.abs(currentPosition/450)) -1;
		$('#timeline .nav li').removeClass('active');
		$('#timeline .nav li').eq(idx).addClass('active');
	};
	
	maptip();
	
	$.fixpng({
		'blank_src' : 'css/images/empty.gif'
	});
	
	$('#slider ul').jcarousel({
		'scroll' : 1,
		'auto' : 8,
		'wrap' : 'both',
		itemFirstInCallback: mycarousel_itemFirstInCallback,
		initCallback: carousel_initCallback,
        buttonNextHTML: null,
        buttonPrevHTML: null
	});
	
	$('#timeline-short .slider').jcarousel({
		'scroll' : 1,
		'start' : 2,
		itemFirstInCallback: timeline_itemFirstInCallback,
		initCallback: timeline_carousel_initCallback
	});
	
	function timeline_itemFirstInCallback(carousel, item, idx, state) {
		$('#timeline-short .years li a').removeClass('active');
		$('#timeline-short .years li a').eq(idx-1).addClass('active');
	}
	
	function timeline_carousel_initCallback(carousel) {
		$('#timeline-short .years li a').bind('click', function() {
			$('#timeline-short .years li a').not($(this)).removeClass('active');
			$(this).addClass('active');
	        carousel.scroll(jQuery.jcarousel.intval(jQuery(this).parent().index() + 1));
	        return false;
	    });
	}
	
	$('.events-block ul li').hover(function() {
		$(this).addClass('hover');
	}, function() {
		$(this).removeClass('hover');
	});
	
	$('.events-block-wider li a.toggler').live('click', function() {
		$('.events-block-wider li .cnt').not($(this).parents('li:eq(0)').find('.cnt')).slideUp();
		$(this).parents('li:eq(0)').find('.cnt').slideToggle();
		$('.events-block-wider li a.toggler').not($(this)).removeClass('toggler-active');
		$(this).toggleClass('toggler-active');
		return false;
	});
	
	function carousel_initCallback(carousel) {
		$('#slider .nav a').bind('click', function() {
			$('#slider .nav a').not($(this)).removeClass('active');
			$(this).addClass('active');
	        carousel.scroll(jQuery.jcarousel.intval(jQuery(this).text()));
	        return false;
	    });
	}
	
	function mycarousel_itemFirstInCallback(carousel, item, idx, state) {
		$('#slider .nav a').removeClass('active');
		$('#slider .nav a').eq(idx-1).addClass('active');
	};
	
	$('.field').focus(function () {
		if ($(this).val() == $(this).attr('title')) 
		{
			$(this).css('color', '#000').val('');
		}
	}).blur(function () {
		if ($(this).val() == '') {
			$(this).css('color', '#acacac').val($(this).attr('title'));
		}
	});
	
	$('input[type=text], textarea').focus(function () {
		if(this.title==this.value) {
            this.value = '';
        }
		$(this).addClass('field-focus');
	}).blur(function () {
		if(this.value=='') {
            this.value = this.title;
        }
		$(this).removeClass('field-focus');
	});
	
	$('#search .field').focus(function () {
		$(this).parents('.field-holder:eq(0)').addClass('field-holder-focus');
	}).blur(function () {
		$(this).parents('.field-holder:eq(0)').removeClass('field-holder-focus');
	});
});

this.maptip = function(){       
    $("#interactive-map .hotspot").hover(function(e) {
        var html = $($(this).attr('href')).html();
        $("body").append("<div id='maptip'>"+ html +"</div>");
        xOffset = parseInt($('#maptip').outerHeight()) + 10;
        yOffset = -130;
        $("#maptip")
            .css("top",(e.pageY - xOffset) + "px")
            .css("left",(e.pageX + yOffset) + "px")
            .fadeIn("fast");        
    },
    function(){
        $("#maptip").remove();
    }); 
    $("#interactive-map .hotspot").mousemove(function(e){
        $("#maptip")
            .css("top",(e.pageY - xOffset) + "px")
            .css("left",(e.pageX + yOffset) + "px");
    });
};
