jQuery(document).ready(function(){
	/** Sub-page dropdown menu functions */
	jQuery('#head .navi>li').hover(
		function(){
			jQuery(this).children('ul').slideDown('fast');
		},
		function(){
			jQuery(this).children('ul').slideUp('fast');
		}
	);
	jQuery('#head .navi>li ul>li').hover(
		function(){
			var v_offset = 0;
			var h_offset = jQuery(this).width();
			jQuery(this).children('ul').css('top',v_offset).css('left',h_offset).fadeIn('fast');
		},
		function(){
			jQuery(this).children('ul').fadeOut('fast');
		}
	);
	
	/** Dynamic transcript display for the_comic_transcript() */
	jQuery('.comic-transcript-title').toggle(
		function(){
			jQuery(this).siblings('.comic-transcript').slideDown('fast');
		},
		function(){
			jQuery(this).siblings('.comic-transcript').slideUp('fast');
		}
	);
	
	/** Auto select the embed comic code generated by the_comic_embed() */
	jQuery('.embed-title').click(function(){
		jQuery(this).siblings('p').children('.comic-embed-code').focus();
	});
	jQuery('.comic-embed-code').focus(function(){
		this.select();
	});
	
	/** Jump to the selected comic/chapter for dropdown_comics() */
	jQuery('select.dropdown-comics').change(
		function(){
			if(jQuery(this).attr('value') != 0){
				window.location = jQuery(this).attr('value');
			}
		}
	);
	
	/** Dynamic archive display for comic_archive() */
	jQuery('.comic-library>li>a').removeAttr('href');
	jQuery('.comic-series-chapters>li>a').removeAttr('href');
	jQuery('.comic-volume-chapters>li>a').removeAttr('href');
	jQuery('.comic-library>li>a').toggle(
			function(){
				if(jQuery(this).siblings('ol').is('.comic-series-volumes')){
					jQuery(this).siblings('.comic-series-volumes').slideDown('fast');
				}
				else{
					jQuery(this).siblings('.comic-volume-chapters').slideDown('fast');
				}
			},
			function(){
				if(jQuery(this).siblings('ol').is('.comic-series-volumes')){
					jQuery(this).siblings('.comic-series-volumes').slideUp('fast');
				}
				else{
					jQuery(this).siblings('.comic-volume-chapters').slideUp('fast');
				}
			}
	);
	jQuery('.comic-series-volumes>li>a').toggle(
		function(){
			jQuery(this).siblings('.comic-volume-chapters').slideDown('fast');
		},
		function(){
			jQuery(this).siblings('.comic-volume-chapters').slideUp('fast');
		}
	);
	jQuery('.comic-volume-chapters>li>a').toggle(
		function(){
			jQuery(this).siblings('.comic-chapter-pages').slideDown('fast');
		},
		function(){
			jQuery(this).siblings('.comic-chapter-pages').slideUp('fast');
		}
	);
	
	/** Set the comic bookmark */
	jQuery('#inkblot-bookmark .bookmark-this').click(function(){
		createCookie('inkblot-bookmark',window.location,365);
		jQuery(this).siblings('.goto-bookmark').fadeIn();
		jQuery(this).siblings('.clear-bookmark').fadeIn();
	});
	
	/** Jump to the comic bookmark */
	jQuery('#inkblot-bookmark .goto-bookmark').click(function(){
		window.location = readCookie('inkblot-bookmark');
	});
	
	/** Clear the comic bookmark */
	jQuery('#inkblot-bookmark .clear-bookmark').click(function(){
		createCookie('inkblot-bookmark','',-1);
		jQuery(this).fadeOut();
		jQuery(this).siblings('.goto-bookmark').fadeOut();
	});
 
	/** Reveal the goto and clear links if a comic bookmark is set. */
	if(readCookie('inkblot-bookmark')){
		jQuery('#inkblot-bookmark .goto-bookmark').show(0);
		jQuery('#inkblot-bookmark .clear-bookmark').show(0);
	}
	
	/** AJAX-ify the Submit Transcript form. */
	jQuery('#comic-transcript-form input[name=comic_trans_submit]').attr('value','2');
	jQuery('#comic-transcript-form').ajaxForm({target: '#comic-trans-message'});
});

/** Cookie functions used for the Comic Bookmark widget */
function createCookie(name,value,days){
	if (days){
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name){
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++){
		var c = ca[i];
		while (c.charAt(0)==' ')
			c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0)
			return c.substring(nameEQ.length,c.length);
	}
	return null;
}