$(document).ready(function() {

// Function to clear the search field and return it to the default state 
// that it started in. The base code comes from:
// http://bassistance.de/2007/01/23/unobtrusive-clear-searchfield-on-focus/
	$.fn.search = function() {
		return this.focus(function() {
			if( this.value == this.defaultValue ) {
				this.value = "";
			}
		}).blur(function() {
			if( !this.value.length ) {
				this.value = this.defaultValue;
			}
		});
	};
	$("#edit-search-block-form-keys").search();
	
	// Block hide/show function
	$(".block.hidden .content").hide();
	$(".block.hidden .title a").toggle(function() {          
			$(this).parents('.title').next('.content').slideDown('slow');
		}, function() {
			$(this).parents('.title').next('.content').slideUp('slow');
		});
	
	// Store Menu hide/show function
	$(".sub").hide();
	$(".downarrow").toggle(function() {
			$(this).siblings('.sub').slideDown('fast');
		}, function() {
			$(this).siblings('.sub').slideUp('fast');
		});
		
	$("div.view-data-field_excerpt_value").hide();

	$('a.show-more').click(function()
	  {
	  $(this).parent().parent('div').next('div').slideToggle('slow');
		return false;
	    
	  }
	);
	// Media - more options
	$('#more_options').hide();
	$('a.more_options').toggle(function() {
			$("#more_options").slideDown('slow');
		}, function() {
			$("#more_options").slideUp('slow');
		});
	// Popup for media viewer
	$('a.popup.video').click(function()
		{
			window.open(this.href, "iiw_v_player","menubar=1,resizable=1,width=660,height=350");
			return false;
		}
	);
	$('a.popup.audio').click(function()
		{
			window.open(this.href, "iiw_a_player","menubar=1,resizable=1,width=350,height=110");
			return false;
		}
	);
	
	
	// JQuery captions
	$('.news img[@title]').each( function() { 
		imgtitle = $(this).attr('title'); 
		imgwidth = $(this).attr('width');
		imgalign = $(this).attr('align');
		if(imgwidth > '300'){
			imgalign = 'center';
		}
		if (!imgalign){
			imgalign = 'right';
		}
		$(this).wrap('<div class="imgcaption img-'+ imgalign +'" style="width: '+imgwidth+'px;">').after('<p>' + imgtitle + '</p>');  
	});
	
	// add print page link - since it can only work for JS, only add it for JS users
  $('<li></li>') // create list item
      .addClass('print plink')
      .append('<a class="print" href="javascript:window.print();">Print this Episode</a>') 
      .insertAfter('ul.watch li.listen');

});
