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

This script is included when the CommentPress theme is active.

---------------------------------------------------------------
*/

// browser detection
var msie = $.browser.msie;
var msie6 = $.browser.msie && $.browser.version == "6.0";

// page defaults to 'not-highlighted'
var page_highlight = false;






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





// set up comment headers
function setup_comment_headers() {

	// set pointer 
	$('div.comments_container h3').css( 'cursor', 'pointer' );

	/** 
	 * @description: comment page headings click
	 * @todo: 
	 *
	 */
	$('div.comments_container h3').click( function() {
	
		// are comments on paragraphs allowed?
		if ( cp_para_comments_enabled == '1' ) {
		
			// toggle next paragraph_wrapper
			$(this).next('div.paragraph_wrapper').slideToggle();
			
		}
		
		// get text_sig
		var text_sig = $(this).attr( 'id' ).split('para_heading-')[1];
		
		// did we get one at all?
		if( typeof( text_sig ) != 'undefined' ) {
		
			// if not the whole page...
			if( text_sig != '' ) {
	
				// get text block
				var textblock = $('#textblock-' + text_sig);
				
				// is this element highlighted?
				var is_highlighted_para = $.is_highlighted_para( textblock );
				
				// highlight this paragraph
				$.highlight_para( textblock );
				
				// only scroll if switching highlight on
				if ( !is_highlighted_para ) {
				
					// if IE6, then we have to scroll #wrapper
					if ( msie6 ) {
					
						// scroll wrapper to title
						$('#wrapper').scrollTo( textblock.position().top - scrollOffset, {duration: adjust_scroll_speed} );
			
					} else {
					
						// only scroll if not mobile
						if ( cp_is_mobile == '0' ) {
					
							// scroll
							$.scrollTo( textblock.position().top - scrollOffset, adjust_scroll_speed );
							
						}
						
					}
					
					// only scroll if not mobile
					if ( cp_is_mobile == '0' ) {
					
						// scroll comment area to para heading
						$('#comments_minimiser').scrollTo( $('#para_heading-' + text_sig), {duration: adjust_scroll_speed} );
						
					}
					
				}
				
			} else {
			
				// only scroll if page is not highlighted
				if ( page_highlight === false ) {
			
					// scroll to page title
					var myScroll = $('.post').position().top - scrollOffset - scrollOffsetTitle;
					
					// make sure it's > 0
					if ( myScroll < 0 ) { myScroll = 0; }
					
					// if IE6, then we have to scroll #wrapper
					if ( msie6 ) {
					
						// scroll wrapper to title
						$('#wrapper').scrollTo( myScroll, {duration: adjust_scroll_speed} );
			
					} else {
					
						// only scroll if not mobile
						if ( cp_is_mobile == '0' ) {
						
							// scroll
							$.scrollTo( myScroll, adjust_scroll_speed );
							
						}
						
					}
					
					// only scroll if not mobile
					if ( cp_is_mobile == '0' ) {
					
						// scroll comment area to para heading
						$('#comments_minimiser').scrollTo( $('#para_heading-' + text_sig), {duration: adjust_scroll_speed} );
						
					}
					
				}
				
				// toggle page highlight flag
				page_highlight = !page_highlight;
				
			}
			
		} // end defined check
		
	});

}






/** 
 * @description: clicking on the comment permalink
 * @todo: 
 *
 */
function enable_comment_permalink_clicks() {

	$('.comment_permalink').click( function(e) {
	
		// get comment id
		var comment_id = this.href.split('#')[1];
	
		// scroll comment area to comment
		$('#comments_minimiser').scrollTo( $('#'+comment_id), {duration: adjust_scroll_speed} );
		
		// --<
		return false;
		
	});

}






// set up submit button
function setup_submit_button() {

	// only if we haven't got ajax-comments enabled
	if ( typeof(ajax_comments) == 'undefined' ) {
	
		/** 
		 * @description: hijack submit button
		 * @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 );
				
			}



			// submit form
			document.getElementById('commentform').submit();



			// test for tinyMCE
			if ( $.is_object( tinyMCE ) ) {
	
				// reload tinyMCE
				tinyMCE.execCommand('mceAddControl', false, 'comment');
			
			}
			
	
	
			// --<
			return false;
			
		});
	
	}

}






/** 
 * @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 = $('#comments_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();
	
		// store initial width
		//var w = content.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 ) {
			
				// get text ratio
				//var textsize = parseInt( 100 * ( me.width() / w ) );
			
				// have the text change size
				//content.css( 'font-size', textsize + '%' );

				// have the comments 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' );
				book_info.css( 'width', ( me.width() + book_info_diff ) + 'px' );

			},
			
			// on stop... (note: this doesn't fire on the first go in Opera!)
			stop: function( event, ui ) {			
				
				/*
				// store the font size in cookie
				$.cookie( 
				
					'cp_content_font_size', 
					parseInt( content.css( 'font-size') ).toString(), 
					{ expires: 28, path: cp_cookie_path } 
					
				);
				*/
				
				// 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: 
	 *
	 */
	$('#comments_sidebar').each( function(i) {
	
		// assign vars
		var me = $(this);
		var page_wrapper = $('#page_wrapper');
		var wrapper = $('#comments_minimiser');
		var header = $('#comments_header');
		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' );
		
	});






	// if there is an anchor in the URL (only on non-special pages)
	var url = document.location.toString();
	
	// do we have a comment permalink?
	if ( url.match('#comment-' ) ) {
	
		// open the matching block

		// get comment ID
		var comment_id = url.split('#comment-')[1];
		//alert( comment_id );
		
		// get array of parent paragraph_wrapper divs
		var para_wrapper_array = $('#comment-' + comment_id)
									.parents('div.paragraph_wrapper')
									.map( function () {
										return this;
									});

		// did we get one?
		if ( para_wrapper_array.length > 0 ) {
		
			// get the item
			var item = $(para_wrapper_array[0]);
			
			// show block
			item.show();
			
			// only scroll if not mobile
			if ( cp_is_mobile == '0' ) {
			
				// scroll comment area to comment
				$('#comments_minimiser').scrollTo( $('#comment-' + comment_id), {duration: 0} );
				
			}
			
			// get previous para-heading
			var para_heading = item.prev();
			
			// find text_sig
			var text_sig = para_heading.attr('id').split('-')[1];
			
			// if not the whole page...
			if( text_sig != '' ) {
	
				// get text block
				var textblock = $('#textblock-' + text_sig);
				
				// highlight this paragraph
				$.highlight_para( textblock );
				
				// if IE6, then we have to scroll #wrapper
				if ( msie6 ) {
				
					// scroll wrapper to paragraph
					$('#wrapper').scrollTo( textblock.position().top - scrollOffset, {duration: 1} );
		
				} else {
				
					// only scroll if not mobile
					if ( cp_is_mobile == '0' ) {
					
						// scroll page
						$.scrollTo( textblock.position().top - scrollOffset, 1 );
							
					}
				
				}
				
			} else {
				
				// only scroll if page is not highlighted
				if ( page_highlight === false ) {
				
					// scroll to page title
					var myScroll = $('.post').position().top; - scrollOffset - scrollOffsetTitle;
					
					//alert(myScroll);
					
					// make sure it's > 0
					if ( myScroll < 0 ) { myScroll = 0; }
					
					// if IE6, then we have to scroll #wrapper
					if ( msie6 ) {
					
						// scroll wrapper to title
						$('#wrapper').scrollTo( myScroll, {duration: 1} );
			
					} else {
					
						// only scroll if not mobile
						if ( cp_is_mobile == '0' ) {
						
							// scroll to title
							$.scrollTo( myScroll, 1 );
							
						}
						
					}
					
				}
				
				// toggle page highlight flag
				page_highlight = !page_highlight;
				
			}
			
		}
		
	} else {
		
		/** 
		 * @description: loop through the little comment icons checking for paragraph permalink
		 * @todo: 
		 *
		 */
		$('.commenticon').each( function(i) {
		
			// get text signature
			var text_sig = $(this).attr('id');
			
			// do we have a paragraph permalink?
			if ( url.match('#' + text_sig ) ) {
			
				// toggle next item_body
				$('#para_heading-' + text_sig).next('div.paragraph_wrapper').slideToggle();
				
				// only scroll if not mobile
				if ( cp_is_mobile == '0' ) {
				
					// scroll comment area to para heading
					$('#comments_minimiser').scrollTo( $('#para_heading-' + text_sig), {duration: 1} );
				
				}
				
				// get text block
				var textblock = $('#textblock-' + text_sig);
				
				// highlight this paragraph
				$.highlight_para( textblock );
				
				// if IE6, then we have to scroll #wrapper
				if ( msie6 ) {
				
					// scroll wrapper to title
					$('#wrapper').scrollTo( textblock.position().top - scrollOffset, {duration: 1} );
		
				} else {
				
					// only scroll if not mobile
					if ( cp_is_mobile == '0' ) {
					
						// scroll
						$.scrollTo( textblock.position().top - scrollOffset, 1 );
						
					}
					
				}
				
			}
			
		});
	
	}






	/*
	 ------------------------------------------------------------------------------------------
	 End of Procedure, begin Events
	 ------------------------------------------------------------------------------------------
	 */





	// set up comment headers
	setup_comment_headers();
	





	/** 
	 * @description: clicking on the little comment icon
	 * @todo: 
	 *
	 */
	$('.commenticon').click( function(e) {
	
		// get text signature
		var text_sig = $(this).attr('id');
		
		// toggle next item_body
		$('#para_heading-' + text_sig).next('div.paragraph_wrapper').slideToggle();
		
		// clear other highlights
		$.unhighlight_para();

		// only scroll if not mobile
		if ( cp_is_mobile == '0' ) {
		
			// scroll comment area to para heading
			$('#comments_minimiser').scrollTo( $('#para_heading-' + text_sig), {duration: adjust_scroll_speed} );
		
		}
		
		// did we get a text_sig?
		if ( text_sig != '' ) {
		
			// get text block
			var textblock = $('#textblock-' + text_sig);
			
			// is this element highlighted?
			var is_highlighted_para = $.is_highlighted_para( textblock );
			
			// highlight this paragraph
			$.highlight_para( textblock );
			
			// only scroll if highlight was off
			if ( !is_highlighted_para ) {
			
				// if IE6, then we have to scroll #wrapper
				if ( msie6 ) {
				
					// scroll wrapper to title
					$('#wrapper').scrollTo( textblock.position().top - scrollOffset, {duration: adjust_scroll_speed} );
		
				} else {
				
					// only scroll if not mobile
					if ( cp_is_mobile == '0' ) {
					
						// scroll
						$.scrollTo( textblock.position().top - scrollOffset, adjust_scroll_speed );
						
					}
					
				}
				
			}
			
		}
		
	});






	/** 
	 * @description: clicking on the paragraph permalink
	 * @todo: 
	 *
	 */
	$('.para_permalink').click( function(e) {

		// --<
		return false;
		
	});





	// enable them...
	enable_comment_permalink_clicks();






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

		// has the sidebar window been minimised?
		if ( $.cookie('cp_sidebar_minimised') == 'y' ) {
		
			var flag = 'n';
		
		} else {
		
			var flag = 'y';
		
		}
		
		// store flag in cookie
		$.cookie( 
		
			'cp_sidebar_minimised', 
			flag, 
			{ expires: 28, path: cp_cookie_path } 
			
		);
		
	});

	
	
	
	
	// set up submit button
	setup_submit_button();
	
	
	



});
