/* 
	listepal.js
	listes palindromes
	(C) 2009 Jean-Paul Davalan
	http://jeux-et-mathemetiques.davalan.org/jeux/alpha/palindromes/index.html
*/

function __transf(t) {
        var s =t.replace(/\s+/g,"")
        s = s.toLowerCase()
        s = s.replace(/[àáâãäå]/g,"a")
        s = s.replace(/æ/g,"ae")
        s = s.replace(/½/g,"oe")
        s = s.replace(/ç/g,"c")
        s = s.replace(/[èéêë]/g,"e")
        s = s.replace(/[ìíîï]/g,"i")
        s = s.replace(/[ðòóôõö]/g,"o")
        s = s.replace(/ñ/g,"n")
        s = s.replace(/[ùúûü]/g,"u")
        s = s.replace(/[ýÿ]/g,"y")
        s = s.replace(/\W+/gi,"")
        return s
}

function __rota(s) {
	var u=""
	for(var i=0; i<s.length;i++) {
		u = s.charAt(i)+u
	}
	return u
}

function __ispal(s) {
        var l= s.length-1
        for(var i=0;i<l-i;i++) {
                if(s.charAt(i) != s.charAt(l-i)) 
		return false
        }
        return true
}

function __liste() {
	this.g=[]
	this.d=[]
	this.ng=0
	this.nd=0
	this.s=""
	this.mod=true
	
	this.init = function(u, m) {
		var v = __transf(u)
		if(m==true) {
			this.g[0]=u
			this.ng=1
			this.nd=0
			this.s=v
			this.mod = true
		} else {
			this.d[0]=u
			this.ng=0
			this.nd=1
			this.s=__rota(v)
			this.mod = false
		}
	}
	
	this.clone = function() {
		var ls = new __liste()
		ls.ng=this.ng
		ls.nd=this.nd
		for(var i=0; i<this.ng; i++) {
			ls.g[i] = this.g[i]
		}
		for(var i=0; i<this.nd; i++) {
                        ls.d[i] = this.d[i]
                }
		ls.s=this.s
		ls.mod=this.mod
		return ls
	}

	this.unique = function(u) {
		for(var i=0; i< this.ng; i++) {
			if(u == this.g[i]) {
				return false
			}
		}
                for(var i=0; i< this.nd; i++) {
                        if(u == this.d[i]) {
                                return false
                        }
                }
		return true
	}

	this.compat = function(v) {
		if(this.s=="") 
			return true
		var w = (this.mod==false)? v : __rota(v)
		for(var i=0; i<this.s.length && i<w.length; i++) {
			if(this.s.charAt(i) != w.charAt(i)) {
				return false
			}
		}
		return true
	}

	this.add = function(u, v) {
		var tl=this.s.length, l=v.length, w = (this.mod==true)? __rota(v) : v
		if(this.mod==true) {
			this.d.push(u)
			this.nd++
		} else {
			this.g.push(u)
			this.ng++
		}
		
		if(tl < l) { 
			this.mod = ! this.mod
			this.s = w.slice(tl)
		} else {
			this.s = this.s.slice(l)
		}
		if(__ispal(this.s)) {
			this.affiche()
		}
	}
	this.clr = function() {
		delete(this.g)
		delete(this.d)
		this.s=null
		delete(this)
	}

	this.cherche = function() {
//if(nbmaxi>0 && nb>nbmaxi) return
		for(var i=0; i<ldico; i++) {
			var u = __dico[i], v=__transf(u)   
			if(v!=null && v!="" && this.unique(u) && this.compat(v)) {
				var ls = this.clone()
				ls.add(u, v)
				ls.cherche()
				ls.clr()
			}
		}	
	}

	this.affiche = function() {
		nb++
//if(nbmaxi>0 && nb>nbmaxi) return

		var s = ''
		for(var i=0; i<this.ng; i++) {
			if(this.g[i]!="") {
				s += this.g[i]+", "
			}
		}
                for(var i=this.nd-1; i>=0; i--) {
			if(this.d[i]!="") {
                        	s += this.d[i]
				if(i>0) {
					s += ", "
				}
			}
                }

		s = s.replace(/\,+/g,",")
		s = '# -------------- ' + nb + '\n'+ s
		print(s)
	}
}

var __dico = null
var ldico=0
var laliste=null
var sortie= ""
var nb=0

function palindromes(s1, s2, s3) {
	var s = s1
	nb=0
	s = s.replace(/\n/g," ");
	s = s.replace(/\s+\,/g,",");
	s = s.replace(/\,\s+/g,", ");
	s = s.replace(/\,\s*\,/g,",");
	s = s.replace(/\,+$/g,",");
	s = s.replace(/^\,+/g,",");
	if(__dico != null) {
		delete(__dico)
	}
	__dico = s.split(/\s*\,\s+/g)
	ldico = __dico.length
	for(var i= ldico-1; i>0; i--) {
		var j = Math.floor(Math.random()*(i+1))
		var ss = __dico[i]
		__dico[i] = __dico[j]
		__dico[j] = ss
	}
	if(laliste!=null) {
		laliste.clr()
	}
	sortie=""
	laliste = new __liste()
	laliste.init(s2, s3)
	laliste.cherche()
	print("\n"+nb+"\n")
}


//palindromes(listemots, mot, logic)


