//////////////////////////////////
//  ZetaBoards AJAX Quick-Edit  //
//  Finished on Nov 29th, 2007  //
//    Written by Chocolate570   //
//    Current Version: 2.0.0    // 
//  Do Not Edit The Following!  //
//Browsers: IE 7, FF 1.5-2, Op 9//
//////////////////////////////////
//<!--                          //
//Start Submit, Load & Cancel   //
//////////////////////////////////
function postSize(a){
         var b=$("#c_post-text").css("height");
         b=parseInt(b.split("px")[0])-50+(100*a);
         b=(b<175)?175:b;
         b=b+"px";
         $("#c_post-text").css("height",b);
}
function sub(mel) {
	//First fetch the value of the textarea, and then fetch all of the inputs.
	text=$(".quickedit").val();
        $(".quickedit").wrap("<div></div>").attr("disabled",true).css("background","url(http://aquate.us/u/ajax-lo3.gif) no-repeat bottom right");
        $(".quickedit").parent().animate({
          opacity: .75
        },{duration:750});
	//Bug fix :: Following three are for first-post editing.
	ztitle=$("#hO table tbody tr td").children("[@name=title]").val();
	ztags=$("#hO table tbody tr td").children("[@name=tags]").val();
	zdesc=$("#hO table tbody tr td").children("[@name=description]").val();
	zf=$("#hO").children("[@name=f]").val();
	zsd=$("#hO").children("[@name=sd]").val();
	zp=$("#hO").children("[@name=p]").val();
	zxc=$("#hO").children("[@name=xc]").val();
	zt=$("#hO").children("[@name=t]").val();
	zqhash=$("#hO").children("[@name=qhash]").val();
	zpg=$("#hO").children("[@name=pg]").val();
	zx=$("#hO").children("[@name=x]").val();
	ztype=$("#hO").children("[@name=type]").val();
	zmode=$("#hO").children("[@name=mode]").val();
	//Send the information to the server.
	$(".quickedit").attr("readonly",true);
	//^Set the textarea locked until data is sent.
	$.post(main_url+"post/?",{mode:zmode,post:text,type:ztype,sd:zsd,f:zf,xc:zxc,t:zt,qhash:zqhash,p:zp,pg:zpg,x:zx,title:ztitle,description:zdesc,tags:ztags,emo:1},function() { 
		sing=main_url+"single/?p="+zp+"&t="+zt;
		$.get(sing,function(dat) {
                          td=(document.all)?'TD':'td';
                          re=new RegExp('<'+td+' class="c_post">','i');
                          le=new RegExp('</'+td+'>','i');
                          $(mel).parent().html(dat.split(re)[1].split(le)[0]);
			  window.onbeforeunload=null;
		});
	});
}
function load(f) {
	//Retrieve Edit Button URL
	t=$(f).parent().parent().next().next().children("td:eq(1)").children("span:eq(0)").html();
        //$(f).parent().parent().next().next().children("td:eq(1)").children("span:eq(0)").remove();
	t=t.split("href=\"")[1].split("\"")[0];
	t=t.replace(/&amp;/gi,"&");
	//Bit of a bug fix...those darn browsers replace & with &amp;
	$.get(t,function(data) {
		//Start the AJAX request
		curHTML=data;
		document.getElementById("hO").innerHTML="<input type='hidden' name='mode' value='3' />"+data.split("<input type='hidden' name='mode' value='3' />")[1].split("<td id='c_emot' rowspan='2'>")[0];
		curHTML=curHTML.split('id="c_post-text">')[1].split("</textarea>")[0];
		//Okay, we've got the post value--let's insert it.
		$(".quickedit").html(curHTML);
		$(".quickedit").attr("readonly",false);
		//^That part was so the textarea can be edited again.
	});
	window.onbeforeunload=function() {
		return "You currently have a quickedit box open. If you continue off the page, your browser will not save your edit.";
	};
}
function cancel(el,htm) {
	//This function really could be better, but for v1, it has to do.
	//The cancel button currently has to send a huge string of HTML to cancel();a no-no.
	$("qpost").attr("id","");
	$(el).parent().html(unescape(htm));
	window.onbeforeunload=null;
}
i=0;
//^ My way of using the .each as a for loop with accessible counter.
$("<div id='hO'></div>").appendTo("body").hide();
$("<div id='hA'></div>").appendTo("body").hide();
if(!window.location.href.match(/announcement/i)) {
$("img[@alt*=Edit]").each(function(){
	$(this).parent().parent().parent().parent().prev().prev().children(".c_post").dblclick(function() {
		//We've just started setting the double-click-on-the-post event.
		if($("textarea.quickedit").length>0 && $(this).children(".quickedit").length<1){
			//Uh oh--more than one quickedit open? I don't think so.
			alert("You can only have one quick edit box open at a time. Please cancel/submit the first one before continuing to this one.");
			return false;
		} 
		if($(this).children("textarea").length=="0"){
			//Complete the following if there's not already a quickedit box in the current post.
			holdHTML=escape(this.innerHTML);
			this.id="qpost";
			//Okay...this is another part that needs to be fixed.
			//I've never liked huge strings of HTML. But it's sorta necessary. 
                        $(this).html("<textarea name='post' style='padding-bottom:1px;height:223px;' id='c_post-text' class='quickedit' rows='1' readonly='true'>Loading Post...</textarea><br><input type='button' id='me' value='Submit'  onclick='sub(this)' style='float:left;' /> <input type='button' value='Cancel' onclick='cancel(this,\""+holdHTML+"\")' style='float:left;padding-left:1px;' /><div id='c_post-resize'><span onclick='postSize(1)'>+</span><span onclick='postSize(0)'>-</span></div>");
			load($("#me"));
			//^ Load the textarea value.
		}
		else {
			//If you've double clicked BUT the textarea is already open:
			if(confirm("Are you sure you'd like to close this?")) {
                          $("#qpost").attr("id","");
			  //Reset IDs and what not.
		          $(this).html(unescape(holdHTML));
			  //Reinstate HTML
                        }
		    window.onbeforeunload=null;
		}
	});
	i++;
});}
//////////////////////////////////
//!-->                          //
//End code.                     //
//////////////////////////////////