// only run when jQuery is present
// won't be present if SezWho plugin is not loaded
// and no other process has loaded it
// ALL THIS IS REQUIRED FOR COMMENTS TO POST AND NEST PROPERLY
if(typeof jQuery != 'undefined') {
	/**
	 * Comment nesting and reply form
	 */
	var sz_fm = false; // inline form 
	var sz_ci = false; // inline form textarea
	var sz_cpi = false; // inline parent value
	// Bring the comment form to the target comment
	sz_add_comment = function(comment_id,id) {
		if(sz_fm.css('display') == 'block') {
			if(jQuery.browser.msie) {
				sz_fm.hide('normal',function(){ 
						if(jQuery.browser.version < 7) { sz_fix_ie_textarea_size('close'); }
						sz_check_comment_open(comment_id,id,sz_fm); 
					});
			}
			else {
				sz_fm.slideUp('normal',function(){ sz_check_comment_open(comment_id,id,sz_fm); });
			}			
		}
		else { sz_move_comment_form(comment_id,id,sz_fm); }
	}

	// check to see if the current comment already has an open form
	sz_check_comment_open = function(comment_id,id,elem) {
		if(elem.attr('comment_open') !== comment_id) { sz_move_comment_form(comment_id,id,elem); }
		else { elem.attr('comment_open',''); }
	}

	// move the inline comment form to the requested comment
	sz_move_comment_form = function(comment_id,id,elem) {
		sz_cpi.val(id);
		elem.attr('comment_open',comment_id); // set a variable to tell us which comment we're attaching to
		if(jQuery.browser.msie) {
			elem.insertAfter(jQuery('#'+comment_id+'>.body>.sz_footer')).show('normal',function() {
					if(jQuery.browser.version < 7) { sz_fix_ie_textarea_size('open'); }
				});
		}
		else {
			elem.insertAfter(jQuery('#'+comment_id+'>.body>.sz_footer')).slideDown('normal');
		}
		// flash the form if available
		if(jQuery('window').jquery >= '1.2.3') { elem.css({ 'background-color': '#fff' }).animate({ backgroundColor: '#f3f3f3' },500); }
	}
	
	// hard resize textarea for IE 6, otherwise it jumps when text is input
	sz_fix_ie_textarea_size = function(type) {
		if(type == 'open') { sz_ci.css({ 'width':(sz_ci.parent('label').width()-10)+'px' }); }
		else { sz_ci.css({'width':'98%'}); }
	}

	// put the comment form back in the default location
	sz_cancel_add = function() {
		if(jQuery.browser.msie) {
			jQuery('#sz_comment_form_inline').hide('normal',function(){
					jQuery('#sz_comment_form_inline').insertAfter(jQuery('#sz_comment_form'));
					if(jQuery.browser.version < 7) { sz_fix_ie_textarea_size('close'); }
				});	
		}
		else {
			jQuery('#sz_comment_form_inline').slideUp('normal',function(){
					jQuery('#sz_comment_form_inline').insertAfter(jQuery('#sz_comment_form'));
				});			
		}	
		sz_ci.val('');
		sz_cpi.val(0);
	}

	// submit form
	sz_submit_comment = function(smb,form) {
		// do minor validation here before submit
		sz_fm.submit();
	}

	jQuery(window).ready(function(){
			// duplicate the subscription status checkbox/link to the inline comment form
			if(jQuery('.subscribe-to-comments')) {
				jQuery('.subscribe-to-comments').clone(true).insertAfter('#comment_inline_label');
			}
			if(!sz_cpi) { sz_cpi = jQuery('#sz_comment_parent_inline'); }
			if(!sz_ci) { sz_ci = jQuery('#comment_inline'); }
			if(!sz_fm) { sz_fm = jQuery('#sz_comment_form_inline'); }
		});
}
else {
	var sz_fm = false;
	var sz_mvs = false;
	var sz_ci = false;
	var sz_cpi = false;
	// no frills JS code
	sz_add_comment = function(comment_id,id) {
		if(!sz_fm) { sz_fm = document.getElementById('sz_comment_form_inline'); }
		if(!sz_ci) { sz_ci = document.getElementById('comment_inline'); }
		if(!sz_cpi) { sz_cpi = document.getElementById('sz_comment_parent_inline'); }
		if(!sz_mvs) { sz_move_comment_subscription(); }
		
		if(sz_fm.style.display == 'block') {
			sz_fm.style.display = 'none';
			sz_check_comment_open(comment_id,id,sz_fm);
		}
		else { sz_move_comment_form(comment_id,id,sz_fm); }
	}
	
	sz_cancel_add = function() {
		sz_fm.style.display = 'none';
		sz_ci.value = '';
		sz_cpi.value = '0';		
	}
	
	sz_check_comment_open = function(comment_id,id,elem) {
		if(elem.comment_open && elem.comment_open !== comment_id) { sz_move_comment_form(comment_id,id,elem); }
		else { elem.comment_open = ''; }
	}
	
	sz_move_comment_form = function(comment_id,id,elem) {
		sz_cpi.value = id;
		elem.comment_open = comment_id;
		document.getElementById(comment_id).appendChild(elem);
		sz_fm.style.display = 'block';
	}
	
	sz_submit_comment = function(smb,form) {
		// eventually do some minor validation here
		document.commentforminline.submit();
	}
	
	sz_move_comment_subscription = function() {
		var ss = document.getElementById('sz_comment_form').getElementsByTagName('p');
		var ss_clone = false;
		for(i=0;i<ss.length;i++) {
			if(ss[i].className == 'subscribe-to-comments') { ss_clone = ss[i].cloneNode(true); }
		}
		if(ss_clone !== false) { document.getElementById('commentforminline').appendChild(ss_clone); }
		sz_mvs = true; 
	}
}
