 

	function btway_post(el) {
		var _s = el.id.split('-');
		el.idnum = _s[1];
		el.view_btn = $(el).find('.btway_post_switch')[0];
		  if (el.view_btn) {
			el.view_btn.that = el;
			$(el.view_btn).click(function() {
				var that = this.that;
				that.view_mode();
				return false;
			});
		  }
		el.edit_btn = $(el).find('.btway_post_switch')[1];
		  if (el.edit_btn) {
			el.edit_btn.that = el;
			$(el.edit_btn).click(function() {
				var that = this.that;
				that.edit_mode();
				return false;
			});
		  }
		el.zone_view = $(el).find('.btway_post_view')[0];
		el.zone_edit = $(el).find('.btway_post_edit')[0];

		el.view_mode = function() {
			$(this.view_btn).addClass('btway_post_switch_on');
			$(this.edit_btn).removeClass('btway_post_switch_on');
			$(this.zone_edit).slideUp(240);
			$(this.zone_view).html('<div style="padding:1em;"><span style="font-size:2em;"> <img src="http://btway.com/?&o=594&f=routine-2134" border="0" style="height:1em;; "> </span></div>');
			  $.get('../naked_post.html?&btway=' + this.idnum, function(data) {
				var _s = this.url.split('=');
				var _idnum = _s[_s.length-1];
				var _el = $('#btway-' + _idnum)[0];
				$(_el.zone_view).html(data);
				btway_init($(_el.zone_view).find('.btway')[0]);
			  });
			$(this.zone_view).slideDown(240);
		}

		el.edit_mode = function() {
			$(this.edit_btn).addClass('btway_post_switch_on');
			$(this.view_btn).removeClass('btway_post_switch_on');
			$(this.zone_view).slideUp(240);
			$(this.zone_edit).slideDown(240);
		}
	}

	$(document).ready(function() {

	  $('.btway').each(function() {
		btway_init(this);
	  });

	  $('.btway_post').each(function() {
		btway_post(this);
	  });

	});

	function btway_init(el) {
		$(el).find('ul').each(function() {
			$(this).css('display','inline');
			$(this).css('margin','0px');
			$(this).css('padding','0px');
			$(this).css('list-style-type','none');
		});

		$(el).find('li').each(function() {
		  	$(this).css('display','inline');
		  	$(this).css('margin','0px !important');
		  	$(this).css('padding','0px');
			this.show = function() {
				$(this).children('.pretext').css('display','none');
				$(this).children('.text').css('display','inline');
				$(this).children('.text').animate({'opacity':1},320);
			}
			this.hide = function() {
				$(this).children('.text').css('opacity','0');
				$(this).children('.text').css('display','none');
				$(this).children('.pretext').css('display','inline');
			}
			this.hide();
		});

		$(el).find('span.pretext').html('&nbsp;+&nbsp;');
		$(el).find('span.pretext').css('cursor','pointer');
		$(el).find('span.pretext').css('background','#ccf');
		$(el).find('span.pretext').css('padding','2px');
		$(el).find('span.pretext').css('border','1px solid #999');
		$(el).find('span.pretext').click(function(e) {
			var el = this.parentNode;
			el.show();
			e.stopPropagation();
		});

		$(el).find('span.text').css('cursor','crosshair');
		$(el).find('span.text').css('padding','1px');
		$(el).find('span.text').click(function(e) {
			var el = this.parentNode;
			el.hide();
			e.stopPropagation();
		});

		$(el).find('a').click(function(e) {
		  	e.stopPropagation();
		});

		$(el).css('line-height','1.5em');
		  $(el).find('.chapter').css('margin-bottom','1em');

		$(el).find('.chapter').each(function() {
			btway_label($(this).find('.text')[0],0);
			btway_colorize($(this).find('.text')[0],0);
		  	$($(this).find('ul')[0]).children('li').each(function() {
			  	this.show();
		  	});
		});
	}

	function btway_label(el,depth) {
		return true;
		if (typeof depth !== 'number') {
			var depth = 0;
		}
		var label = depth;
		$(el).attr('title',label);
		$(el).children('ul').children('li').children('.text').each(function() {
			btway_label(this,depth+1);
		});
	}

	function btway_colorize(el,depth) {
		if (typeof depth !== 'number') {
			var depth = 0;
		}
		var color = '';
		switch (depth) {
			case 0:
				color = ''; break;
			case 1:
				color = '#fcf'; break;
			case 2:
				color = '#ffc'; break;
			case 3:
				color = '#cff'; break;
			case 4:
				color = '#fcc'; break;
			case 5:
				color = '#cfc'; break;
			case 6:
				color = '#ccf'; break;
			case 7:
				color = '#ddd'; break;

			case 8:
				color = '#fcf'; break;
			case 9:
				color = '#ffc'; break;
			case 10:
				color = '#cff'; break;
			case 11:
				color = '#fcc'; break;
			case 12:
				color = '#cfc'; break;
			case 13:
				color = '#ccf'; break;
			case 14:
				color = '#ddd'; break;
		}
		if (color !== '') {
			$(el).css('background',color);
		}
		$(el).children('ul').children('li').children('.text').each(function() {
			btway_colorize(this,depth+1);
		});
	}

	$('.delete_link').click(function() {
		// each delete link is within a parent container called "otype-idnum"; the delete link itself should be called "delete:otype-idnum".
		var pElId = this.id.replace('delete:','');
		var c = confirm('Really delete?  There is no undo.');
		if (c == true) {
			var _s = pElId.split('-');
			var otype = _s[0];
			var idnum = _s[1];
			$.post('http://' + document.domain + '/?&siteformat=xml&elId=' + pElId, {
				// delete the btway post
				  '1_posttype':'delete',
				  '1_orb':otype,
				  '1_where':otype + '.id = ' + idnum,
				// delete news items about it by this user
				  '2_posttype':'delete',
				  '2_orb':'n',
				  '2_where':"n.name = '/btway-" + idnum + "'"
			}, function(data) {
				var _s = this.url.split('=');
				var el = document.getElementById(_s[_s.length-1]);
				 var spacer_id = el.id + '-spacer';
				$(el).slideUp(360);
				$('#' + spacer_id).hide();
				// how many records now match?
				var _n = document.getElementById('num_records');
				if (_n !== null) {
					var num_records = $(_n).html();
					var num_records = num_records - 1;
					if (num_records == 1) {
						var _txt = "Result";
					}
					else {
						var _txt = "Results";
					}
					$(_n).html(num_records);
					$('#records_match').html(_txt);
				}
			});
		}
		return false;
	});

