//<![CDATA[

	//////////////////////////////////////////////////////////////////////
	/////////////multi quote for zetaboards by Viral/////////////
	//////////////////////////////////////////////////////////////////////

	var newVersion = 1.1;
	
	with(MultiQuoteZB){
		if(isNaN(version)){
			if(autoupdate){
				version = newVersion;
			} else {
				version = 1;
			}
		}
		if(autoupdate){
			version = newVersion;
		}
		var minus_quote = (minus) ? minus : "http://aquate.us/u/-m_quot.png";
		var plus_quote = (plus) ? plus : "http://aquate.us/u/multi_q.png";
	}
		
	$("body").append('<div id="ajaxReady" style="display:none;"> </div><div id="ajaxReady2" style="display:none;"> </div>');

	if(MultiQuoteZB.version >= 1.1){
		var mQF = false;
	}
	
	function trim (str) {
		var str = str.replace(/^\s\s*/, ''),
		ws = /\s/,
		i = str.length;
		while (ws.test(str.charAt(--i)));
		return str.slice(0, i + 1);
	}

	function finish(){
		if(MultiQuoteZB.version >= 1.1){
			mQF = true;
		}
		$("#multiQuote").val("").attr("disabled",false).attr("rows",quotes.length*2);
		for(i=0;i<quotes.length;i++){
			document.getElementById("multiQuote").value += quotes[i] + "\n\n";
		}
	}

	function startPost(){
		var posting = true;
		if(MultiQuoteZB.version >= 1.1){
			if(!mQF){
				var posting = confirm("The MultiQuote has not finished loading, are you sure you want to submit this post?");
			}
		}
		if(posting){
			var textareas = $("textarea");
			textareas[0].value = textareas[1].value + textareas[0].value;
			window.name = "";
			$("#realPost").click();
		}
	}

	function toggleQuote(post){
		if(post.src == plus_quote){
			$(post).fadeOut("fast",function(){
				post.src = minus_quote;
				window.name += $(this).prev().attr("href").split("&type=2&")[1]+"-multi-";
				$(post).fadeIn("fast");
			});
		} else {
			$(post).fadeOut("fast",function(){
				post.src = plus_quote;
				window.name = window.name.replace($(this).prev().attr("href").split("&type=2&")[1]+"-multi-","");
				$(post).fadeIn("fast");
			});
		}
	}


	if(location.href.match(/topic\/(\d+)/gi)){
		if(window.name!="" && RegExp.$1 != window.name.split("&t=")[1].split("&")[0]){
			window.name="";
		}
		$("tr.c_postfoot td.c_footicons span.right").each(function(){
			if(window.name.match($(this).children("a:last").prev().attr("href").split("&p=")[1].split("&qhash")[0])){
				var imglink = minus_quote;
			}else{
				var imglink = plus_quote;
			}
			$(this).children("a:last").before('<img src="'+imglink+'" style="cursor:pointer;" onclick="toggleQuote(this);" alt="Multi Quote" title="Multi Quote" /> ');
		});
	}
	else if(document.title.match("Posting reply to") && window.name!="" && document.getElementsByTagName("textarea").length!=2){

		var amount = window.name.split("-multi-");
		amount.pop();

		$("button#btn_preview").before('<textarea id="multiQuote" disabled="disabled">MultiQuote loading... (0/'+amount.length+')</textarea>');
		$("#c_postbtn button").attr("style","display:none;").attr("id","realPost").attr("accesskey","m").after('<button class="btn_default" id="multiPost" name="post_submit" tabindex="6" accesskey="s" onclick="startPost();" type="button">Post Reply</button>');
		
		var quotes = [];
		var q=0;

		var post_to_quote = null;
		a=0;
		function getPost(a){
			if(amount[a]){
				RawRz=(window.ActiveXObject) ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest()
				var uri = "?mode=2&type=2&f="+amount[a].split('f=')[1].split('&t=')[0]+"&t="+amount[a].split('&t=')[1].split('&p=')[0]+"&p="+amount[a].split('&p=')[1].split('&qhash')[0]+"&qhash="+amount[a].split('&qhash=')[1]+""
				RawRz.open("GET",uri,true)
				RawRz.onreadystatechange = function(){
					if(RawRz.readyState==4){
						document.getElementById("ajaxReady").innerHTML=RawRz.responseText;
						var post_to_quote = $("#ajaxReady #txt_quote").html();
						quote=(window.ActiveXObject) ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest()
						quote.open("GET","../single/?p="+amount[a].split('&p=')[1]+"&t="+amount[a].split('&t=')[1].split('&p=')[0],true)
						quote.onreadystatechange = function(){
							if(quote.readyState==4){
								document.getElementById("ajaxReady2").innerHTML=quote.responseText;
								var quote_user = trim($("#ajaxReady2 td.c_username").text());
								var full_quote = "[quote="+quote_user+"]"+post_to_quote+"[/quote]";
								var current = document.getElementById("multiQuote").value.split("... (")[1].split("/")[0] * 1 + 1;
								document.getElementById("multiQuote").value = "MultiQuote loading... ("+current+"/"+amount.length+")";
								quotes[q++] = full_quote;
								if(a<amount.length){
									getPost(a+1);
								}
								else{
									finish();
								}
							}
						}
						quote.send(null)

					}
				}
				RawRz.send(null)


			}
			else{
				finish();
			}
		}
		
		getPost(a);

	}else{
		window.name = "";
	}

//]]>
