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

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






/** 
 * @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 = $('#toc_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: 
	 *
	 */
	$('#toc_sidebar').each( function(i) {
	
		// are subpages to be shown?
		if ( cp_show_subpages == '0' ) {
		
			// hide all subpages
			$('#toc_wrapper ul li ul').hide();
			
		}
		


		// assign vars
		var me = $(this);
		var wrapper = $('#toc_wrapper');
		var header = $('#toc_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 );
	
		//alert( to_bottom );
		
		// set toc_wrapper height
		wrapper.css( 'height', to_bottom + 'px' );
		
	});






	/** 
	 * @description: chapter page headings click
	 * @todo: 
	 *
	 */
	$("#toc_wrapper ul#toc_list li a").click( function(e) {
	
		// are our chapters pages?
		if ( cp_toc_chapter_is_page == '0' ) {
		
			// no, find child lists of the enclosing <li>
			var myArr = $(this).parent().find('ul');
			
			//alert( 'has ' + myArr.length + ' children' );	

			// do we have a child list?
			if( myArr.length > 0 ) {
			
				// are subpages to be shown?
				if ( cp_show_subpages == '0' ) {
				
					// toggle next list
					$(this).next('ul').slideToggle();

				}
			
				// --<
				return false;
				
			}
		
		}
		
	});





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

	
	
	



	/** 
	 * @description: hijack submit button (active on 'general comments' page)
	 * @todo:
	 *
	 */	
	$('#submit').click( function(e) {
		
		// test for tinyMCE
		if ( $.is_object( tinyMCE ) ) {
		
			// set value of comment textarea to content
			tinyMCE.triggerSave();

			// unload tinyMCE
			tinyMCE.execCommand('mceRemoveControl', false, 'comment');

		}



		// if we want to show values
		if ( 1 == 2 ) {
		
			// check value of comment parent hidden input
			var mycpID = $('input#comment_post_ID').val();
			alert( 'submitted comment_post_ID: ' + mycpID );
			
			// check value of comment parent hidden input value
			var myparent = $('input#comment_parent').val();
			alert( 'submitted comment_parent: ' + myparent );
	
			// check value of comment text signature hidden input value
			var mysig = $('input#text_signature').val();
			alert( 'submitted text_signature: ' + mysig );
			
		}
		
		
		
		// do we want to use AJAX?
		if ( comment_via_ajax ) {
		
			// try and submit via AJAX
			$.submit_via_ajax( ajax_script_url );
			
		} else {
	
			// submit form
			document.getElementById('commentform').submit();
			
		}
        
		
		
		// test for tinyMCE
		if ( $.is_object( tinyMCE ) ) {

			// reload tinyMCE
			tinyMCE.execCommand('mceAddControl', false, 'comment');
		
		}
		


		// --<
		return false;
			
	});



});

