/*

    fss (Free Software Signature)
    version 1.0 Copyright (C) 2006 Tagata Akira

    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License as
    published by the Free Software Foundation; either version 2 of the
    License, or (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
    02110-1301 USA

*/

_fss.prototype._fss = _fss;

_fss.prototype._fss('fss','Free Software Signature','1.0e','2006 Tagata Akira','http://www.tagata.org/software/');

function _fss(name,desc,ver,copy,web,contact) {

	_fssObj.prototype = {
		name:	'n/a',
		desc:	'n/a',
		ver:	'n/a',
		copy:	'n/a',
		web:	'n/a',
		contact:'n/a'
	}
	function _fssObj() { }

	this._fssObj = new _fssObj;
	if (name)
		this._fssObj.name = name;
	if (desc)
		this._fssObj.desc = desc;
	if (ver)
		this._fssObj.ver = ver;
	if (copy)
		this._fssObj.copy = copy;
	if (web)
		this._fssObj.web = web;
	if (contact)
		this._fssObj.contact = contact;

}

_fss.prototype._fssName = function () { return this._fssObj.name }
_fss.prototype._fssDesc = function () { return this._fssObj.desc }
_fss.prototype._fssVer = function () { return this._fssObj.ver }
_fss.prototype._fssCopy = function () { return this._fssObj.copy }
_fss.prototype._fssWeb = function () { return this._fssObj.web }
_fss.prototype._fssContact = function () { return this._fssObj.contact }

_fss.prototype._fssHtml = '_fss.html';
_fss.prototype._fssUrlStyle = 'padding-left: 1ex; font-size: smaller;';
_fss.prototype._fssUrlMargin = '0 0 0 1ex';
_fss.prototype._fssUrlFont = 'smaller serif';
_fss.prototype._fssElementIdToBeInserted = '_fss-siglink-box';
_fss.prototype._fssTagNameToBeInserted = 'div';
_fss.prototype._fssInsertedSig = {};
_fss.prototype._fssInsertUrl = function (eltId) {
	if ('name' in this._fssObj) {
		var elementId = this._fssElementIdToBeInserted;
		if (eltId) {
			elementId = eltId;
		}
		var elementIdToBeInserted = this.elementIdentified(elementId,this._fssTagNameToBeInserted);
		if (elementIdToBeInserted._fssGenerated) {
			document.body.appendChild(elementIdToBeInserted);
		}

		if (this._fssObj['name'] in _fss.prototype._fssInsertedSig)
			return;
		_fss.prototype._fssInsertedSig[this._fssObj['name']] = true;

		// insert signature of myself only the first ime
		_fss.prototype._fssInsertUrl();

		var url = this._fssUriElement(this._fssObj);
//		url.setAttribute('style',this._fssUrlStyle);
		url.style.margin = this._fssUrlMargin;
		url.style.font = this._fssUrlFont;
		elementIdToBeInserted.appendChild(url);
	}
}
_fss.prototype._fssUriElement = function (fssObj) {
	ret = document.createElement('a');
	ret.appendChild(document.createTextNode(fssObj.name));
	function objectToQuery(o) {
		var query = '';
		for (var p in o) {
			if (query != '')
				query += '&amp;';
			query += p+'='+o[p];
		}
		return query;
	}
	var href = encodeURI(this._prependInstallDirectory(this._fssHtml)+'?'+objectToQuery(fssObj));
	var title = 'what is '+fssObj.name+'?';
	ret.setAttribute('href',href);
	ret.setAttribute('title',title);
	return ret;
}

_fss.prototype._log = function () { }; // dummy

// common use method for inferitanced objects
_fss.prototype.elementIdentified = function (id,tagName) {
	var elt = id? document.getElementById(id): null;
	if (elt) {
		if (elt._fssGenerated) // check generated ever
			elt._fssGeneratedEver = true;
		else
			elt._fssPredefined = true;
	}
	else {
		elt = document.createElement(tagName);
		elt.id = id;
		elt._fssGenerated = true;
	}
	return elt;
}

_fss.prototype._isLocal = window.location.protocol == 'file:';

_fss.prototype.searchClass = function (classNames,f,tagName,top) {
	if (!top) var top = document;
	if (!tagName) var tagName = '*';
	this._log('searchClass(): top:'+top+', tagName:'+tagName,0);
	var all = top.getElementsByTagName(tagName);
	this._log('searchClass(): all.length:'+all.length,0);
	for (var i = 0; i < all.length; i++) {
		if (all[i].className) {

			// HTML4.01 white space
			//  * ASCII space (&#x0020;)
			//  * ASCII tab (&#x0009;)
			//  * ASCII form feed (&#x000C;)
			//  * Zero-width space (&#x200B;)
			var classNamesSplit = all[i].className.split(/[\u0020\u0009\u000C\u200B]+/);
			for (j = 0; j < classNamesSplit.length; j++) {
				for (k = 0; k < classNames.length; k++) {
					if (classNamesSplit[j] == classNames[k]) {
						f.call(this,all[i],classNames[k]);
						this._log('searchClass(): class found:'+classNames[k],0);
					}
				}
			}
		}
	}
}

////////////////////////////////////////////////////////////////
// xxxCookie from http://www.quirksmode.org/
////////////////////////////////////////////////////////////////
_fss.prototype.createCookie = function (name,value,days)
{
	if (days)
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
//		var expires = "; expires="+date.toGMTString();
		var expires = "; expires="+date.toUTCString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

_fss.prototype.readCookie = function (name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

_fss.prototype.eraseCookie = function (name)
{
	this.createCookie(name,"",-1);
}

////////////////////////////////////////////////////////////////

_fss.prototype._jlOffStartup	= false;
_fss.prototype._jlHideJlogButtonOnStartup = false;
_fss.prototype._jlogSetUp = function (ini,obj,opId,
		offStartup,hideJlogButtonOnStartup) {
	if (typeof _jlog != 'undefined') {
		if (offStartup) {
			this._jlOffStartup = offStartup;
		}
		if (hideJlogButtonOnStartup) {
			this._jlHideJlogButtonOnStartup = hideJlogButtonOnStartup;
		}
		this.jl = new _jlog(function () { ini.call(obj); },opId);
		this._log =
			function (s,level) { this.jl._jlPuts(s,level); };
	}
}

_fss.prototype._installDirectory = null;
_fss.prototype._prependInstallDirectory = function (path) {
	if (this._installDirectory && this._installDirectory.length && this._installDirectory.charAt(this._installDirectory.length - 1) == '/') {
		return this._installDirectory + path;
	}
	return path;
}

