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');}
	);
	
	//Auto select the embed comic code
	jQuery('.embed-comic-code').click(
		function(){jQuery(this).select();}
	);
	
	//Jump to the selected comic/chapter for dropdown listings
	jQuery('select.comic-dropdown').change(
		function(){
			if(jQuery(this).attr('value') != 0){
				window.location = jQuery(this).attr('value');
			}
		}
	);
	
	//Comic library functions for chapter archives
	jQuery('.comic-library>li>a').removeAttr('href');
	jQuery('.comic-volume-chapters>li>a').removeAttr('href');
	jQuery('.comic-library>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');
		}
	);
	
	//Comic transcript functions
	jQuery('.transcript-title').toggle(
		function(){
			jQuery(this).siblings('.transcript').slideDown('fast');
		},
		function(){
			jQuery(this).siblings('.transcript').slideUp('fast');
		}
	);
	
	//Sets 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();
	});
	
	//Returns to the comic bookmark
	jQuery('#inkblot-bookmark .goto-bookmark').click(function(){
		window.location = readCookie('inkblot-bookmark');
	});
	
	//Clears the comic bookmark
	jQuery('#inkblot-bookmark .clear-bookmark').click(function(){
		createCookie('inkblot-bookmark','',-1);
		jQuery(this).fadeOut();
		jQuery(this).siblings('.goto-bookmark').fadeOut();
	});
	
	//If there's a comic bookmark set we show the "goto" and "clear" links
	if(readCookie('inkblot-bookmark')){
		jQuery('#inkblot-bookmark .goto-bookmark').show(0);
		jQuery('#inkblot-bookmark .clear-bookmark').show(0);
	}
});

//Cookie Functions - necessary for the Comic Bookmark functions
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;
}