/*
 *   Copyright 2005, John Wellesz '2072'
 *   All rights reserved
 */

// This global variable will contains the new created scriptsfiller object and will keep a reference
// so we can clean them when they have been used... (garbage collector)

var createdscripts = new Array();

// function DisplayHide  {{{
function DisplayHide (id, randnum)
{
    if (!CommentStatus[id]) {
	CommentStatus[id]= new CommentCLASS(id);
	CommentStatus[id].Create(randnum);
    } else if (! CommentStatus[id].loading) {
	CommentStatus[id].Delete(randnum);
	CommentStatus[id]=0;
    } else 
	alert ("Wait please...");
}
// }}}

function CommentCLASS (id)
{
    this.theid			= id;
    this.filecontainer	= document.getElementById("Table" + this.theid);
    this.created		= 0;
    this.commentContainer	= document.getElementById("Comment" + this.theid);
    this.CommentScriptCallURL   = script_dir + "/SITE/default_pages/fscomment.php";
    this.CommentScript		= 0;
    this.CommentScript_NUM_id	= 0;
    this.loading		= 0;
    this.sent			= 0;
    this.specrandomtoadd	= 0;
}

CommentCLASS.prototype.DeactivateJavascriptLinks = function () {
// This function is here to prevent a bug from IE6: when a dynamic script
// is loading, any call to javascript will cancel the load...
// So we disable the links...
// But another bug of MSIE prevent me to call that very function inside another one: If I do it the
// cache won't work anymore...
    document.getElementById("L1"+this.theid).href="#comm"+this.theid;
    document.getElementById("L2"+this.theid).href="#comm"+this.theid;
}

// CommentCLASS.prototype.Update = function  {{{
CommentCLASS.prototype.Update = function (argsarray) {
    if (!this.loading) {
	argsarray.unshift( "FileID=" + URLEncode(this.theid));
	argsarray.push("AdminAuthKey=" + URLEncode(AdminAuthKey));
	argsarray.push("AdminSessExpires=" + AdminSessExpires);
	argsarray.push("AdminName=" + URLEncode(AdminName));
	this.loading = 1; // Well we are loading here...

	var CommentScriptArguments = "?" + argsarray.join("&");

	randnum = Math.random() + "";
	randnum = parseInt(randnum.substr(3,6));
	this.CommentScript_NUM_id = randnum;
	//this.AddString("<br>"+this.CommentScriptCallURL + CommentScriptArguments); // XXX

	createdscripts[randnum] = loadAscript( this.CommentScriptCallURL + CommentScriptArguments , "Scriptfiller" + this.theid + randnum);

	this.CommentScript = createdscripts[randnum];
    }
}
// }}}

// CommentCLASS.prototype.Create = function  {{{
CommentCLASS.prototype.Create = function (randnum) {
    if ( ! this.created && ! this.loading) {
	this.created		= 1;
	//	this.DeactivateJavascriptLinks();
	//Thanks to ie again, I cannot call another function else cache won't work anymore
	document.getElementById("L1"+this.theid).href="#comm"+this.theid;
	document.getElementById("L2"+this.theid).href="#comm"+this.theid;
	this.commentContainer.innerHTML = "Loading...";
	var CommentScriptArguments = new Array("do=Show","randnum="+randnum);
	this.Update(CommentScriptArguments);
    }
}
// }}};

// CommentCLASS.prototype.Delete = function  {{{
CommentCLASS.prototype.Delete = function () {
    if (this.created) {
	this.commentContainer.className = "";
	document.removeAllChildNodes(this.commentContainer); // clear container
    }
}
// }}};

// CommentCLASS.prototype.Clean = function  {{{
CommentCLASS.prototype.Clean = function () {
    this.commentContainer.innerHTML = "";
}
// }}};

// CommentCLASS.prototype.Write = function  {{{
CommentCLASS.prototype.Write = function (string) {
    this.commentContainer.className = "FSBottomLine";
    this.commentContainer.innerHTML = string;
}
// }}};

// CommentCLASS.prototype.AddString = function  {{{
CommentCLASS.prototype.AddString = function (string) {
    this.commentContainer.className = "FSBottomLine";
    this.commentContainer.innerHTML += string;
}
// }}}


// CommentCLASS.prototype.SubmitForm = function  {{{
CommentCLASS.prototype.SubmitForm = function (form) {
    if (! this.loading && ! this.sent) {
	this.sent = true;
	form.submitbutton.disabled=true;
	this.DeactivateJavascriptLinks();
	var GetArgsArray = gatherFormDatainArray(form);
	randnum = Math.random() + "";
	randnum = parseInt(randnum.substr(3,6));
	GetArgsArray.unshift("rand=" + randnum);
	GetArgsArray.unshift("do=SubmitForm");
	this.AddString("<b>Sending Data, please wait...</b><br>\n");
	this.Update(GetArgsArray);
    } else alert ("Take it easy!");
}
// }}}

CommentCLASS.prototype.ShowForm = function (randnum, adminedit) {
    if (!this.loading) {
	document.deleteNodes(document.getElementById("showf"+this.theid));
	//this.DeactivateJavascriptLinks();
	document.getElementById("L1"+this.theid).href="#comm"+this.theid;
	document.getElementById("L2"+this.theid).href="#comm"+this.theid;
	var ArgsArray = new Array();
	ArgsArray.push("do=ShowForm");
	ArgsArray.push("randnum=" + randnum);
	ArgsArray.push("adminedit=" + adminedit);
	this.AddString("<b>Loading, please wait...</b><br>\n");
	this.Update(ArgsArray);
    }
}

CommentCLASS.prototype.Show = function () {
    if (!this.loading) {
	document.deleteNodes(document.getElementById("show"+this.theid));
	//this.DeactivateJavascriptLinks();
	document.getElementById("L1"+this.theid).href="#comm"+this.theid;
	document.getElementById("L2"+this.theid).href="#comm"+this.theid;
	var ArgsArray = new Array();

	randnum = Math.random() + "";
	randnum = parseInt(randnum.substr(3,6));

	ArgsArray.push("do=Show");
	ArgsArray.push("rand=" + randnum);
	this.AddString("<b>Loading, please wait...</b><br>\n");
	this.Update(ArgsArray);
    }
}

// Internal use functions - ScriptIsReady - deleteNodes - removeAllChildNodes - loadAscript - DeleteScript {{{
// CommentCLASS.prototype.ScriptIsReady = function  {{{

// var specrandomtoadd = 0;
CommentCLASS.prototype.ScriptIsReady = function () {
    this.loading = 0;
    var randpiece = 0;
    if (this.specrandomtoadd) randpiece = this.specrandomtoadd;

    document.getElementById("L1"+this.theid).href="javascript:DisplayHide('"+this.theid+"', '"+randpiece+"' );";
    document.getElementById("L2"+this.theid).href="javascript:DisplayHide('"+this.theid+"', '"+randpiece+"' );";
    // specrandomtoadd = 0;
}
// }}}

// document.deleteNodes = function {{{
document.deleteNodes = function(node) {
    while (node.childNodes.length > 0) {
	node.removeChild(node.childNodes[0]);
    }
    node.parentNode.removeChild(node);// the object ask to its parent to kill him :-( - very sad.
}
// }}}

// document.removeAllChildNodes = function {{{
document.removeAllChildNodes = function(node) {
    while (node.childNodes.length > 0) {
	node.removeChild(node.childNodes[0]);
    }
}
// }}}

// function loadAscript  {{{
function loadAscript (url,id)
    //--#Works in IE, Mozilla (inspired from http://www.xs4all.nl/~zanstra/inTec/safariIdea/script.htm )
{
    if (url.substr(0,1) != "/") return; // A litlle protection against stupid hackers...

    var script=document.createElement('SCRIPT');
    var head=document.getElementsByTagName('HEAD')[0];
    script.id = id;
    script.language='javascript';
    script.src=url;
    script.defer='defer';
    script = head.appendChild(script);
    return script;
}
// }}}

// document.DeleteScript = function  {{{
document.DeleteScript = function (scriptnum) {
    document.deleteNodes(createdscripts[scriptnum]);
    createdscripts[scriptnum]=null;
}
// }}}
// }}}



