jQuery.noConflict();
(function($){
	
	//	Add text to the search form if nothing is entered 
	
	var text = "Looking for something?";
	$("#s").attr("value", text).addClass('inactive');
	
	$('#s').focus(function(){
		if($(this).attr("value") == text){ 
			$(this).attr("value", ""); 
		}
		$(this).removeClass('inactive');
	});
	
	$('#s').blur(function() {
		if($(this).attr("value") == ""){
			$(this).attr("value", text).addClass('inactive');
		}
	});
	
	// Center social network links
	$('#social-networks').css({
		'width': $('#social-networks').width() + 2,
		'display' : 'block'
	})
	
	/*
		If a post has a gallery, we want to add a rel="post-##" to it so
		that all posts open and can be viewed in the same lightbox.
		We also addClass('enlarge') to enable the lightbox on the gallery thumbs.
	*/
	$('.gallery').each(function(){
		var $url = $('a', this).attr('href');
		/* ***********************************************
		 * First test whether the link points to an image
		 * If it does, it won't have a trailing /
		 * *********************************************** */
		if($url.substring($url.length - 1) != '/'){
			// Add rel[post-id] and class="enlarge" to link
			var $postID = $(this).parent('.article').attr("id");
			$('a', this).attr('rel', $postID).addClass('enlarge');
		}
	});
	
	// Colorbox Init.
	$("a[rel='gallery'], a[rel='colorbox'], a.enlarge, .slides_container a").colorbox({
		maxWidth: '85%',
		maxHeight: '85%'	
	});
	
	// Slider Init.
	$('.slider').slides({
		play: 5000,
		pause: 2500,
		hoverPause: true,
		effect: 'fade',
		generatePagination: false
	});
	
	// Vertically center all images in the slider
	var $container = $('.slides_container').height();
	$('.slides_container img').each(function(){
		var $img = $(this).height();	
		if($img != $('.slides_container').height()){
			var $margin = ($container - $img)/2; 
			$(this).css('margin-top', $margin);
		}
	});
	
	// Tooltips
	$('.format-link .post-title a, .tooltip').tipsy();
	
	// Add enlarge icon to gallery items
	$('.gallery-icon a').append('<span class="enlarge-ic">Enlarge</span>');	
	
	// Display "Enlarge" icon on mouseover 
	$('a.enlarge, .slider').hover(function(){
		$('.enlarge-ic', this).fadeTo(400, 1);
	}, function(){
		$('.enlarge-ic', this).fadeTo(400, 0);
	});
	
	// Remove margins
	$('.simple-ads:nth-child(2n) img').addClass('last');
	
	// Add a backup for HTML5 audio
	$('audio').mediaelementplayer({
		'audioWidth': 400
	});
	
})(jQuery);
