/**
 * Fancy script sets up the page and runs the code in the div.js elements
 */
$(function(){
  $('div.toolTip').each(function() {
    var target = $('.target, .alt-target', this);
	  var htmlSource = $('<div />').append(target.clone()).html();
	  htmlSource = $('<div class="html-source"></div>');
	  var html = $('<div class="html-link"></div>').click(function(){
	    if ($.browser.msie) {
	      // ugh!!! effing IE
	      $(this).parent().find('.html-source').toggle();
	    }
	    else {
	      $(this).parent().find('.html-source').slideToggle();
	    }
			return false;
		});
		var jsLink = $('<div class="html-link"></div>').click(function(){
		  if ($.browser.msie) {
		    $(this).parent().find('.jsContent').toggle();
		  }
		  else {
		    $(this).parent().find('.jsContent').slideToggle();
		  }
			return false;
		});
		
		var cssSource = $('style', this);
		if (cssSource.length) {
		  cssSource = $('<div class="css-source"></div>');
			var css = $('<div class="css-link">').click(function(){
			  if ($.browser.msie) {
					$(this).parent().find('.css-source').toggle();
			  }
			  else {
			    $(this).parent().find('.css-source').slideToggle();
			  }
				return false;
			});
		}
		
		var jsSource = $('.jsContent', this);
		$(jsSource).before(html);
		$(jsSource).before(htmlSource);
		textareaWrap(htmlSource);
		$('textarea', htmlSource).data('target', target[0]).change(function(){
		  var newTarg = $($(this).val()).get(0);
		  $($(this).data('target')).replaceWith(newTarg);
		  $(this).data('target', newTarg);
		});
		htmlSource.hide();
		
		if (typeof css != 'undefined') {
		  $(jsSource).before(css);
			$(jsSource).before(cssSource);
			textareaWrap(cssSource);
			cssSource.hide();
		}
		
		$(jsSource).before(jsLink);
		//eval the source!
		eval(jsSource.html());
		textareaWrap(jsSource, true);
		$('textarea', jsSource).change(function(){
		  eval($(this).val());
		});
		jsSource.hide();
	});
	
	// add BeautyTip to js and html-source textareas

});

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

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