wmd_options = { autostart: false };
    
var instances = [];

function add_wmd_bar(form_id, textarea_id, preview_id) {
	/***** Make sure WMD has finished loading *****/
	if (!Attacklab || !Attacklab.wmd) {
		alert("WMD hasn't finished loading!");
		return;
	}
	// build the dom elements
	var form = document.getElementById(form_id);
	var textarea = document.getElementById(textarea_id);
	var previewDiv = document.getElementById(preview_id);
	
	/***** build the preview manager *****/
	var panes = {input:textarea, preview:previewDiv, output:null};
	var previewManager = new Attacklab.wmd.previewManager(panes);

	/***** build the editor and tell it to refresh the preview after commands *****/
	var editor = new Attacklab.wmd.editor(textarea,previewManager.refresh);

	// save everything so we can destroy it all later
	instances.push({ta:textarea, div:previewDiv, ed:editor, pm:previewManager});
}

function createInstance(id, text) {
	/***** Make sure WMD has finished loading *****/
	if (!Attacklab || !Attacklab.wmd) {
		alert("WMD hasn't finished loading!");
		return;
	}

	// build the dom elements
	var form = document.getElementById(id);
	form.style.margin = "0px 0px 10px 0px"

	var textarea = document.createElement("textarea");
	textarea.rows = "10";
	textarea.value = text;
	
	var submit = document.createElement("input");
	submit.type = "button";
	submit.name = "submit";
	submit.value = "submit";
	
	var cancel = document.createElement("input");
	cancel.type = "button";
	cancel.name = "cancel";
	cancel.value = "cancel";
	cancel.style.margin = "0px 0px 0px 10px"
	
	var previewDiv = document.createElement("div");
	form.appendChild(previewDiv);
	form.appendChild(textarea);
	form.appendChild(submit);
	form.appendChild(cancel);
	

	/***** build the preview manager *****/
	var panes = {input:textarea, preview:previewDiv, output:null};
	var previewManager = new Attacklab.wmd.previewManager(panes);

	/***** build the editor and tell it to refresh the preview after commands *****/
	var editor = new Attacklab.wmd.editor(textarea,previewManager.refresh);

	// save everything so we can destroy it all later
	instances.push({ta:textarea, div:previewDiv, ed:editor, pm:previewManager});
}

function destroyInstance() {
	var inst = instances.pop();

	if (inst) {

		/***** destroy the editor and preview manager *****/
		inst.pm.destroy();
		inst.ed.destroy();
	
		// remove the dom elements
		inst.ta.parentNode.removeChild(inst.ta);
		inst.div.parentNode.removeChild(inst.div);

	}
}


(function($) {
	$.fn.stripHtml = function() {
		var regexp = /<("[^"]*"|'[^']*'|[^'">])*>/gi;
		
		return $(this).val().replace(regexp,"[HTML REMOVED]");
	}
})(jQuery);

markdown2html = function(element, text){
	if(text == ''){
		element.hide("slow");
		return;
	}
	
	converter = new Showdown.converter();
	html = converter.makeHtml(text);
	
	html = "<div>\n\t" + html + "\n</div>";
	
	element.html(html);
	element.show();
	
	return true
};

nl2br = function(str) {
	return (str + '').replace(/([^>]?)\n/g, '$1'+ '<br />' +'\n');
}


jQuery(function() {
    $("li.r_comment").mouseover(function(){
		$(this).css("color", "#666666");
		$(this).css("cursor", "pointer")
	})
	$("li.r_comment").mouseout(function(){
		$(this).css("color", "");
		$(this).css("cursor", "default")
	})
	$("li.r_comment").click(function(){
		var id = $(this).attr("name");
		$.post("/comment/target", {id: id}, function(value) {
			window.location = value
		});
	})
	
	$("a.follow").live("click", function(event){
		var _this = $(this)
		var id = $(this).attr("name");
		$.post("/blog/follow", {id: id}, function(value) {
			_this.replaceWith("<a class=\"following fr\"  href=\"\" name=\"" + id + "\">Stop Following</a>")
		});
		event.preventDefault();
	});
	
	$("a.following").live("click", function(event){
		var _this = $(this)
		var id = $(this).attr("name");
		$.post("/blog/stop_following", {id: id}, function(value) {
			_this.replaceWith("<a class=\"follow fr\" href=\"\" name=\"" + id + "\">Follow</a>")
		});
		event.preventDefault();
	});
	
});

function areArraysEqual(array1, array2) {
   var temp = new Array();
   if ( (!array1[0]) || (!array2[0]) ) { // If either is not an array
      return false;
   }
   if (array1.length != array2.length) {
      return false;
   }
   // Put all the elements from array1 into a "tagged" array
   for (var i=0; i<array1.length; i++) {
      key = (typeof array1[i]) + "~" + array1[i];
   // Use "typeof" so a number 1 isn't equal to a string "1".
      if (temp[key]) { temp[key]++; } else { temp[key] = 1; }
   // temp[key] = # of occurrences of the value (so an element could appear multiple times)
   }
   // Go through array2 - if same tag missing in "tagged" array, not equal
   for (var i=0; i<array2.length; i++) {
      key = (typeof array2[i]) + "~" + array2[i];
      if (temp[key]) {
         if (temp[key] == 0) { return false; } else { temp[key]--; }
      // Subtract to keep track of # of appearances in array2
      } else { // Key didn't appear in array1, arrays are not equal.
         return false;
      }
   }
   // If we get to this point, then every generated key in array1 showed up the exact same
   // number of times in array2, so the arrays are equal.
   return true;
}

