Cufon.replace('#slogan, #navigation a, a.twitter', { fontFamily: 'Gotham Medium', hover: true });

jQuery(function($) {
	jQuery.fn.center = function(loaded) {
	    var obj = this;
	    body_width = parseInt($(window).width());
	    body_height = parseInt($(window).height());
	    block_width = parseInt(obj.width());
	    block_height = parseInt(obj.height());
	    
	    left_position = parseInt((body_width/2) - (block_width/2)  + $(window).scrollLeft());
	    if (body_width < block_width) { left_position = 0 };
	    top_position = parseInt((body_height/2) - (block_height/2) + $(window).scrollTop());
	    if (body_height < block_height) { top_position = 0 };
	    
	    if(!loaded) {
	        
	        obj.css({'position': 'absolute'});
	        obj.css({'left': left_position, 'top': top_position});
	        $(window).bind('resize', function() { 
	            obj.center(!loaded);
	        });
	        $(window).bind('scroll', function() { 
	            obj.center(!loaded);
	        });
	        
	    } else {
	        obj.stop();
	        obj.css({'position': 'absolute'});
	        obj.animate({'left': left_position, 'top': top_position}, 200, 'easeOutExpo');
	    }
	}
	
	jQuery.fn.equalize_height = function() {
		var collection = this;
		var h = 0;
		collection.each(function() {
			if ($(this).height() > h) {
				h = $(this).height();
			}
		});
		collection.each(function() {
			$(this).height(h);
		});
	}
	
	$('.blink, #s').focus(function () {
		if ($(this).val() == $(this).attr('title')) {
			$(this).val('');
		}
	});
	
	$('.blink, #s').blur(function () {
		if ($(this).val() == '') {
			$(this).val($(this).attr('title'));
		}
	});

	function equalize_height(collection) {
		var h = 0;
		$(collection).each(function() {
			if ($(this).height() > h) {
				h = $(this).height();
			}
		});
		$(collection).each(function() {
			$(this).height(h);
		});
	}
	
	$('.popup').each(function() {
		$(this).center();
	});
	$('.featured-sections .section').equalize_height();
	$('.blog-content, .blog-sidebar').equalize_height();
	$('#books').height($('#books').parent().height());
	$('#navigation li:last, .featured-sections .section:last').addClass('last');
	$('.widget a').each(function() {
		$(this).append('<span class="orangearrow">&nbsp;</span>');
	});
	
	var popup_fade_speed = 200;
	
	$(document).click(function(e) {
		if ($('.popup:visible').length) {
			if (!$(e.target).parents('.popup').length && !$(e.target).hasClass('popup')) {
				$('.popup:visible').fadeOut(popup_fade_speed);
			}
		}
	});
	
	$('.articles .col .black-arrow').click(function() {
		if ($('.popup:animated').length) {
			return false;
		}
		var col = $(this).parents('.col:eq(0)');
		var id = col.attr('id');
		if ($('#articles-' + id).is(':visible')) {
			return false;
		}
		$('.popup').fadeOut(popup_fade_speed);
		$('#articles-' + id).fadeIn(popup_fade_speed);
		return false;
	});
	
	$('.video-links a').click(function() {
		$('.videos .placeholder').hide();
		var id = $(this).attr('id');
		if ($('#play-' + id).is(':visible')) {
			return false;
		}
		$('.videos .video').hide();
		$('#play-' + id).show();
		return false;
	});
	
	$('.widget_search #s').val('Search').attr('title', 'Search');
	$('.widget_search #searchsubmit').val('»');
});
