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

// ------------------------------------------------------------
// do we want AJAX comment submission?

// set flag
var comment_via_ajax = false;

// ------------------------------------------------------------
// define fixed layout variables -> these are the settings in the CSS file

// #page_wrapper { width: }
var page_wrapper_min_width = 447;

// difference between #page_wrapper + #sidebar and #book_header width
var book_header_diff = 35;

// difference between page wrapper and #book_nav width
var book_nav_diff = 35;

// difference between page wrapper and #book_info width
var book_info_diff = -47;

// #content { width: } // NO LONGER USED
var content_min_width = 440;

// #archive_sidebar/#comments_sidebar { width: }
var sidebar_min_width = 370;

// ------------------------------------------------------------
// When the tools column is shown/hidden...  // NO LONGER USED

// init speed of expansion
var content_expand_speed = 1400;

// set speed of contraction
var content_contract_speed = 300;

// ------------------------------------------------------------
// Set these offsets to tweak scroll positions

// set scroll offset for page elements
var scrollOffset = -10;

// set additional scroll offset for page title
var scrollOffsetTitle = 130;

// set default scroll speed
var adjust_scroll_speed = 1000;

// ------------------------------------------------------------






/** 
 * @description: define what happens before the page is ready - avoid flash of content
 * @todo: 
 *
 */
function cp_page_setup( sidebar ) {

	// init styles
	var styles = '';



	// wrap with js test
	if ( document.getElementById ) {
	
		// open style declaration
		styles += '<style type="text/css" media="screen">';

	
	
		// avoid flash of all-comments hidden elements
		styles += 'ul.all_comments_listing div.item_body { display: none; }';
	


		// are subpages to be shown?
		if ( cp_show_subpages == '0' ) {
		
			// avoid flash of hidden elements
			styles += '#' + sidebar + '_wrapper ul li ul { display: none; }';
		
			// avoid flash of hidden elements in dropdown TOC
			styles += '#toc_dd_wrapper ul li ul { display: none; }';
		
		}
		


		// is this the archive sidebar?
		if ( sidebar == 'archive' ) {
		
			// avoid flash of hidden comment form
			styles += '#toc_minimiser { display: none; }';
		
		}
		


		// is this the comments sidebar?
		if ( sidebar == 'comments' ) {
		
			// are comments on paragraphs allowed?
			if ( cp_para_comments_enabled == '1' ) {
			
				// avoid flash of hidden comments
				styles += '.paragraph_wrapper { display: none; }';
				
			}
	
			// avoid flash of hidden comment form
			styles += '#respond { display: none; }';
		
			// has the sidebar window been minimised?
			if ( $.cookie('cp_sidebar_minimised') == 'y' ) {
			
				// set visibility of comments
				styles += '#comments_minimiser { display: none; }';
	
			}
		
		}
		


		// has the sidebar window been changed?
		if ( $.cookie('cp_sidebar_width') ) {
		
			// set width of sidebar
			styles += '#' + sidebar + '_sidebar, #toc_sidebar { width: ' + $.cookie('cp_sidebar_width') + 'px; }';

		}
	
		// has the sidebar window been changed?
		if ( $.cookie('cp_sidebar_left') ) {
		
			// set width of sidebar
			styles += '#' + sidebar + '_sidebar, #toc_sidebar { left: ' + $.cookie('cp_sidebar_left') + 'px; }';

		}
	


		// has the content column been changed?
		if ( $.cookie('cp_container_width') ) {

			// get value
			var cp_container_width = parseInt( $.cookie('cp_container_width') );

			// did we get a sidebar cookie?
			if ( $.cookie('cp_sidebar_width') ) {
				var cp_sidebar_width = parseInt( $.cookie('cp_sidebar_width') );
			} else {
				var cp_sidebar_width = sidebar_min_width;
			}

			// set header wrapper width
			var book_header_width = cp_container_width + cp_sidebar_width + book_header_diff;
			styles += '#book_header { width: ' + book_header_width + 'px; }';

			// set book nav wrapper width to the same as book_header
			styles += '#book_nav_wrapper { width: ' + book_header_width + 'px; }';

			// set container width
			styles += '#page_wrapper { width: ' + cp_container_width + 'px; }';

			// set toc width
			styles += '#toc_dropdown { width: ' + cp_container_width + 'px; }';

			// set book nav width
			var book_nav_width = cp_container_width + book_nav_diff;
			styles += '#book_nav div#cp_book_nav { width: ' + book_nav_width + 'px; }';

			// set book info width
			var book_info_width = cp_container_width + book_info_diff;
			styles += '#book_nav div#cp_book_info { width: ' + book_info_width  + 'px; }';

			// set font size
			//styles += '#content { font-size: ' + $.cookie('cp_content_font_size') + '%; }';

		}
		


		// close style declaration
		styles += '</style>';

	}
	
	
	
	// write to page now
	document.write( styles );
	
}






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






	/** 
	 * @description: clicking on the contents button
	 * @todo: 
	 *
	 */
	$('#btn_contents').click( function() {
	
		// toggle next div
		$('#toc_dropdown').slideToggle();
		
		// --<
		return false;
		
	});

	
	
	



	/** 
	 * @description: chapter page headings click
	 * @todo: 
	 *
	 */
	$("#toc_dropdown ul#toc_dd_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: define what happens when the page is unloaded
 * @todo: 
 *
 */
$(window).unload( function() { 

	// debug
	//alert('Bye now!'); 
	
});