var globals = null;

function Querystring(qs) { // optionally pass a querystring to parse
	this.params = new Object()
	this.get=Querystring_get
	
	if (qs == null)
		qs=location.search.substring(1,location.search.length)

	if (qs.length == 0) return

// Turn <plus> back to <space>
// See: http://www.w3.org/TR/REC-html40/interact/forms.html#h-17.13.4.1
	qs = qs.replace(/\+/g, ' ')
	var args = qs.split('&') // parse out name/value pairs separated via &
	
// split out each name=value pair
	for (var i=0;i<args.length;i++) {
		var value;
		var pair = args[i].split('=')
		var name = unescape(pair[0])

		if (pair.length == 2)
			value = unescape(pair[1])
		else
			value = name
		
		this.params[name] = value
	}
}

function Querystring_get(key, default_) {
	// This silly looking line changes UNDEFINED to NULL
	if (default_ == null) default_ = null;
	
	var value=this.params[key]
	if (value==null) value=default_;
	
	return value
}

function DisplayComments(postid,url) {

	var chaine;
	chaine = url.indexOf("page", 0);
	var myTransition = new Fx.Transition(Fx.Transitions.Expo, 3);

	if ($('commentslist-'+postid) && (chaine == -1)) {	
		var myFx = new Fx.Style('commentslist-'+postid, 'opacity',{duration:900,transition: myTransition.easeOut}).start(1,0).chain(function(){ $('comments-'+postid).empty(); });				
	} else {
		
		var log = $('comments-'+postid).empty().addClass('ajax-loading');
		
		new Ajax(url, {
			method: 'get',
			update: $('comments-'+postid),
			evalScripts:true,
			onComplete: function() {
			var myFx = new Fx.Style('commentslist-'+postid, 'opacity',{duration:900,transition: myTransition.easeIn});
			myFx.start(0,1).chain(function(){log.removeClass('ajax-loading');});
			}
		}).request();	
	}
}

function DisplayForm(url) {
	
	new Ajax(url, {
		method: 'get',
		update: $('TheForm'),
		evalScripts:true
	}).request();
	
	var myFx = new Fx.Style('TheForm', 'opacity').start(0,1);

}

function BackupPostData(ident) {
	globals = null;
	if (typeof(ident) == 'undefined') {
	globals = $('postlink').toQueryString();
	} else {
	globals = $('postlink-'+ident).toQueryString();
	}
}

function PostForm(url) {

var log = $('TheForm').empty().addClass('ajax-loading');

 var request = new Ajax(url, {
 				method: 'post',
 				postBody:$('postlink'), 
 				update: log,
 				data: globals,
 				evalScripts:true,
 				onComplete: function() {
				var qs = new Querystring(globals);
					
				log.removeClass('ajax-loading');

 				}
 				}).request();	
}

function BackupFormData(postid) {
globals = null;
globals = $('postcom').toQueryString();
isSpamAjax(globals);
}

function isSpamAjax(mydata) {

var result;

var ask = new Ajax(root_uri+"index.php5?action=spamcheck", {
 				method: 'get',
 				data: mydata,
 				onSuccess: function() {
 									 result = 'spam';									
 									},
 				onFailure: function() {
 									 result = 'clean';
 									}
			}).request().chain(function() {													
													if (result == 'spam') {
														globals += "&spamming=1";
													} else {
														globals += "&spamming=1";
													}
												});
}

function PostCommentsForm(postid,url,commenturl) {

	var log = $('log').empty().addClass('ajax-loading');

 	var request = new Ajax(url, {
 				method: 'post',
 				postBody:$('postcom'), 
 				update: log,
 				data: globals,
 				evalScripts:true,
 				onComplete: function() {
 				log.removeClass('ajax-loading');
 				
				var qs = new Querystring(globals);
				
					if (qs.get("Author") && qs.get("Text") && qs.get("spamming") != 1) {
						RefreshPost(postid);
						DisplayComments(postid,commenturl);				
						MOOdalBox.close();	
					}
				
 				}
 				}).request();
}

function RefreshPost(postid) {
	
	new Ajax(root_uri+"index.php5?action=refresh&id="+postid, {
		method: 'get',
		update: $('body-'+postid),
		evalScripts:true
	}).request();
	
	var myFx = new Fx.Style('body-'+postid, 'opacity').start(0,1);
}

function ReportBroken(postid) {
	new Ajax(root_uri+"index.php5?action=broken&id="+postid, {
		method: 'get',
		evalScripts:true,
		onComplete: function() {			
				RefreshPost(postid);
		}
	}).request();
}

/*
function CloneLink(){

	$$('a.previewlink').each(
    	function(lien, i){
  	  						
						var clone1 = lien.clone();
						var clone2 = lien.clone().setProperties({
            onclick: 'window.open(this.href); return false;'}).setHTML('<img src="http://moisson.faucheur.org/img/icon_link.gif" alt="icon" title="Ouvrir ce lien dans une nouvelle fen&ecirc;tre" style="border: none; margin:0 4px;" />');
						
						clone2.removeClass('previewlink');
							
						var finish = new Element("span");
							
						clone1.injectInside(finish);	
						clone2.injectInside(finish);					
						lien.replaceWith(finish); 
  	}
  );

}

window.addEvent('domready', CloneLink);
*/