/* 
===============================================================
Table of Contents
===============================================================
AUTHOR			: Christian Wach <needle@haystack.co.uk>
LAST MODIFIED	: 16/07/2009
REQUIRES		: jquery.js
---------------------------------------------------------------
*/

// call page setup function
cp_page_setup( 'archive' );






/** 
 * @description: define what happens when the page is ready
 * @todo: 
 *
 */
$(document).ready( function() {






	/** 
	 * @description: sets up the main column, if the id exists
	 * @todo: 
	 *
	 */
	$('#page_wrapper').each( function(i) {
	
		// assign vars
		var me = $(this);
		var content = $('#content');
		var sidebar = $('#archive_sidebar');
		var toc_dropdown = $('#toc_dropdown');
		var book_header = $('#book_header');
		var book_nav_wrapper = $('#book_nav_wrapper');
		var book_nav = $('#cp_book_nav');
		var book_info = $('#cp_book_info');
				
		// calculate gap to sidebar
		var l = sidebar.css( 'left' );
		var gap = l.substring( 0, (l.length - 2) ) - me.width();
	
		// if Opera...
		if ( $.browser.opera ) {
		
			// set the position of #content to avoid alsoResize bug
			content.css( 'position', 'static' );
		
		}

		// make page wrapper resizable
		me.resizable({ 
		
			handles: 'e',
			minWidth: page_wrapper_min_width,
			alsoResize: '#content',
			
			// while resizing...
			resize: function( event, ui ) {
			
				// have the sidebar follow
				sidebar.css( 'left', ( me.width() + gap ) + 'px' );

				// have the toc dropdown follow
				toc_dropdown.css( 'width', me.width() + 'px' );

				// have the book header follow
				book_header.css( 'width', ( me.width() + sidebar.width() + book_header_diff ) + 'px' );

				// have the book nav wrapper, book nav and book info follow
				book_nav_wrapper.css( 'width', ( me.width() + sidebar.width() + book_header_diff ) + 'px' );
				book_nav.css( 'width', ( me.width() + book_nav_diff ) + 'px' ); // diff in css
				book_info.css( 'width', ( me.width() + book_info_diff ) + 'px' ); // diff in css + width of l/r/ buttons

			},
			
			// on stop... (note: this doesn't fire on the first go in Opera!)
			stop: function( event, ui ) {			
			
				// store this width in cookie
				$.cookie( 
				
					'cp_container_width', 
					me.width().toString(), 
					{ expires: 28, path: cp_cookie_path } 
					
				);
				
				// store location of sidebar in cookie
				$.cookie( 
				
					'cp_sidebar_left', 
					sidebar.position().left.toString(), 
					{ expires: 28, path: cp_cookie_path } 
					
				);
				
			}
			
		});

	});






	/** 
	 * @description: sets up the table of contents, if the id exists
	 * @todo: 
	 *
	 */
	$('#archive_sidebar').each( function(i) {
	
		// assign vars
		var me = $(this);
		var wrapper = $('#archive_wrapper');
		var header = $('#archive_header');
		var page_wrapper = $('#page_wrapper');
		var book_header = $('#book_header');
		var book_nav_wrapper = $('#book_nav_wrapper');
	
		// make sidebar resizable
		me.resizable({ 
		
			handles: 'e',
			minWidth: sidebar_min_width,
			
			// while resizing...
			resize: function( event, ui ) {
			
				// have the book header follow
				book_header.css( 'width', ( me.width() + page_wrapper.width() + book_header_diff ) + 'px' );

				// have the book nav wrapper, book nav and book info follow
				book_nav_wrapper.css( 'width', ( me.width() + page_wrapper.width() + book_header_diff ) + 'px' );

			},
			
			stop: function( event, ui ) {			
			
				// store state in cookie
				$.cookie( 'cp_sidebar_width', me.width().toString(), { expires: 28, path: cp_cookie_path } ); 
			
			}
			
		});



		// get the necessary height to reach the bottom of the viewport
		var to_bottom = $.get_sidebar_height( me, header, wrapper );
	
		// set toc_wrapper height
		wrapper.css( 'height', to_bottom + 'px' );
		
	});






	/** 
	 * @description: clicking on the minimise archive icon
	 * @todo: 
	 *
	 */
	$('#cp_minimise_archive').click( function() {
	
		// toggle next div
		$(this).parent().next().slideToggle();
		
	});

	
	
	



});

