/**
 * Fancy script sets up the page and runs the code in the div.js elements
 */
jQuery(function(){
  jQuery('div.example').each(function() {
    var target = jQuery('.target, .alt-target', this);
	  var htmlSource = jQuery('<div />').append(target.clone()).html();
	  htmlSource = jQuery('<div class="html-source">' + escapeHTMLEncode(htmlSource) + '</div>');
	  var html = jQuery('<div class="html-link"><a href="#">html</a></div>').click(function(){
	    if (jQuery.browser.msie) {
	      // ugh!!! effing IE
	      jQuery(this).parent().find('.html-source').toggle();
	    }
	    else {
	      jQuery(this).parent().find('.html-source').slideToggle();
	    }
			return false;
		});
		var jsLink = jQuery('<div class="html-link"><a href="#">javascript</a></div>').click(function(){
		  if (jQuery.browser.msie) {
		    jQuery(this).parent().find('.js').toggle();
		  }
		  else {
		    jQuery(this).parent().find('.js').slideToggle();
		  }
			return false;
		});
		
		var cssSource = jQuery('style', this);
		if (cssSource.length) {
		  cssSource = jQuery('<div class="css-source">' + cssSource.html() + '</div>');
			var css = jQuery('<div class="css-link"><a href="#">css</a></div>').click(function(){
			  if (jQuery.browser.msie) {
					jQuery(this).parent().find('.css-source').toggle();
			  }
			  else {
			    jQuery(this).parent().find('.css-source').slideToggle();
			  }
				return false;
			});
		}
		
		var jsSource = jQuery('.js', this);
		jQuery(jsSource).before(html);
		jQuery(jsSource).before(htmlSource);
		textareaWrap(htmlSource);
		jQuery('textarea', htmlSource).data('target', target[0]).change(function(){
		  var newTarg = jQuery(jQuery(this).val()).get(0);
		  jQuery(jQuery(this).data('target')).replaceWith(newTarg);
		  jQuery(this).data('target', newTarg);
		});
		htmlSource.hide();
		
		if (typeof css != 'undefined') {
		  jQuery(jsSource).before(css);
			jQuery(jsSource).before(cssSource);
			textareaWrap(cssSource);
			cssSource.hide();
		}
		
		jQuery(jsSource).before(jsLink);
		//eval the source!
		eval(jsSource.html());
		textareaWrap(jsSource, true);
		jQuery('textarea', jsSource).change(function(){
		  eval(jQuery(this).val());
		});
		jsSource.hide();
	});
	
	// add BeautyTip to js and html-source textareas
	jQuery('div.js textarea, div.html-source textarea').bt('This is live code. Feel free to edit and try out new options. Click (or tab) out of the textarea to see the results of your edits. If things go awry, reload the page.', {positions: 'right', trigger: ['focus', 'blur'], overlap: 8, width: 150, cssStyles: {fontSize: '11px'}});
});

function textareaWrap(obj, escape) {
  var html = jQuery(obj).html();
  if (escape) {
    html = escapeHTMLEncode(html);
  }
	jQuery(obj).empty().append(jQuery('<textarea />').append(html));
	var jQuerytextarea = jQuery('textarea', obj);
	jQuerytextarea.width('100%');
	var scrollHeight = jQuerytextarea.get(0).scrollHeight || 50;
	jQuery('textarea', obj).css({height: scrollHeight});
}

function escapeHTMLEncode(str) {
	var div = document.createElement('div');
	var text = document.createTextNode(str);
	div.appendChild(text);
	return div.innerHTML;
 }

