/*
    multis (More Useful Lingual Tricky Innocent Selector)
    version 1.0 Copyright (C) 2006-2007 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

*/

_multis.prototype = new _fss('multis','More Useful Lingual Tricky Innocent Selector','1.0g','2006-2007 Tagata Akira','http://www.tagata.org/software/');

_multis.prototype._multisElementTop = null;
_multis.prototype._multisClassInline = 'ml-inline-box';
_multis.prototype._multisClassBlock = 'ml-block-box';
_multis.prototype._multisSelectLangLabel = 'Lang ';
_multis.prototype._multisLangLabelObject = null;
_multis.prototype._multisFormId = 'ml-form';
_multis.prototype._multisOpId = 'ml-operation';

// 1.0g
_multis.prototype._multisCookieName = _multis.prototype._fssName();
_multis.prototype._multisCookieDays = 90;

_multis.prototype._multisLangLable = function (languageCode) {
	if (this._multisLangLabelObject) {
		if (languageCode in this._multisLangLabelObject) {
			return this._multisLangLabelObject[languageCode];
		}
	}
	return languageCode;
}

_multis.prototype._multisStart = function () {
	this._log('_multisStart(): entered',0);

	this._multisLang = new Object;
	this._multisSelectLang = document.createElement('select');
	this._multisSelectLang.$o = this;
	this._multisSelectLang.onchange = this._multisChangeLang;

	this._multisForm = this.elementIdentified(this._multisFormId,'form');

	this._log('_multisStart(): '+'this._multisFormId:'+this._multisFormId+', this._multisForm._fssPredefined:'+this._multisForm._fssPredefined+', this._multisForm._fssGenerated:'+this._multisForm._fssGenerated,0);

	if (!this._multisForm._fssPredefined) {
		this._multisOpElm = this.elementIdentified(this._multisOpId,'div');
		this._multisOpElm.appendChild(this._multisForm);

		if (this._multisOpElm._fssGenerated) {
			// insert generated op element into the top of body
			document.body.insertBefore(this._multisOpElm,document.body.firstChild);
		}

		this._log('_multisStart(): '+'this._multisOpId:'+this._multisOpId+', this._multisOpElm._fssPredefined:'+this._multisOpElm._fssPredefined+', this._multisOpElm._fssGenerated:'+this._multisOpElm._fssGenerated,0);
	}


	// collect elements with lang attr and inline/block class

	this.searchClass([this._multisClassInline,this._multisClassBlock],
		function (elt,className) {
			var lang = elt.getAttribute('lang');
			if (!lang)
				return;
			if (this._multisLang[lang] === undefined) {
				this._multisLang[lang] = new Object;
				this._log('_multisStart(): collect lang:'+lang,0);
			}
			if (className == this._multisClassInline) {
				if (!this._multisLang[lang].inline) {
					this._multisLang[lang].inline = new Array;
				}
				this._multisLang[lang].inline[this._multisLang[lang].inline.length] = elt;
				this._log('_multisStart(): found inline lang:'+lang,0);
			}
			else if (className == this._multisClassBlock) {
				if (!this._multisLang[lang].block) {
					this._multisLang[lang].block = new Array;
				}
				this._multisLang[lang].block[this._multisLang[lang].block.length] = elt;
				this._log('_multisStart(): found block lang:'+lang,0);
			}
		},
		'*',this._multisElementTop);

	// get last lang from cookie
//	this._multisCookieName = _multis.prototype._fssName();	// 1.0g
	var lastLang = this.readCookie(this._multisCookieName);

	// generate options for select

	var lastLangIndex = 0;
	for (var lang in this._multisLang) {
		this._log('_multisStart(): edit opt lang:'+lang,0);
		var opt = document.createElement('option');
		opt.appendChild(document.createTextNode(this._multisLangLable(lang)));
		opt.value = lang;
		this._multisSelectLang.appendChild(opt);
		if (lang == lastLang)
			lastLangIndex = this._multisSelectLang.options.length -1;
	}
	this._log('_multisStart(): lastLangIndex:'+lastLangIndex,0);
	this._multisSelectLang.selectedIndex = lastLangIndex;


	this._multisForm.appendChild(document.createTextNode(this._multisSelectLangLabel));
	this._multisForm.appendChild(this._multisSelectLang);


	this._multisSelectLang.onchange.call(this._multisSelectLang);

	_multis.prototype._fssInsertUrl();
};

_multis.prototype._multisChangeLangEnter = function () { }
_multis.prototype._multisChangeLangExit = function () { }

_multis.prototype._multisChangeLang = function () {
	this.$o._log('this._multisChangeLang(): this.value:'+this.value+', this.selectedIndex:'+this.selectedIndex,0);

	this.$o._multisChangeLangEnter(this);

//	alert('this.$o._multisCookieName:'+this.$o._multisCookieName+', this.$o._multisCookieDays:'+this.$o._multisCookieDays);
	// set cookie
	this.$o.createCookie(this.$o._multisCookieName,this.value,this.$o._multisCookieDays);

	if (this._multisElementTop) {
		if (this._multisElementTop.getAttribute('lang')) {
			this._multisElementTop.setAttribute('lang',this.value);
			this.$o._log('_multisChangeLang(): top lang '+this.value,0);
		}
	}
	else {
		var html = document.documentElement;
		if (html.getAttribute('xml:lang')) {
			html.setAttribute('xml:lang',this.value);
			this.$o._log('_multisChangeLang(): html xml:lang '+this.value,0);
		}
		if (html.getAttribute('lang')) {
			html.setAttribute('lang',this.value);
			this.$o._log('_multisChangeLang(): html lang '+this.value,0);
		}
	}

	for (var lang in this.$o._multisLang) {
		this.$o._log('this._multisChangeLang(): lang:'+lang,0);
		var display;
		if (this.$o._multisLang[lang].inline) {
			if (this.value == lang) {
				display = 'inline';
			}
			else {
				display = 'none';
			}
			for (var i = 0; i < this.$o._multisLang[lang].inline.length; i++) {
				this.$o._multisLang[lang].inline[i].style.display = display;
			}
		}
		if (this.$o._multisLang[lang].block) {
			if (this.value == lang) {
				display = 'block';
			}
			else {
				display = 'none';
			}
			for (var i = 0; i < this.$o._multisLang[lang].block.length; i++) {
				this.$o._multisLang[lang].block[i].style.display = display;
			}
		}
	}
	this.$o._multisChangeLangExit(this);
};

function _multis(elementTop,classInline,classBlock,selectLangLabel,formId,opId) {
	if (elementTop)
		this._multisElementTop = elementTop;
	if (classInline)
		this._multisClassInline = classInline;
	if (classBlock)
		this._multisClassBlock = classBlock;
	if (selectLangLabel)
		this._multisSelectLangLabel = selectLangLabel;
	if (formId)
		this._multisFormId = formId;
	if (opId)
		this._multisOpId = opId;
}

