// JavaScript Document
if (typeof RTS != 'object')
	var RTS = {};

RTS.ffRebuild = {
	init: function() {

		var me = this;
		var links = [];
		var stack = null;

		if (typeof $ != 'function') {
			if (typeof console != 'undefined' && typeof console.info == 'function')
				console.info('[ffRebuild] jQuery\'s undefined, can\'t run.');
			return;
		}

		$('a').each(function() {
			var you = $(this);
			if (me.equals(you,stack)) {

				var found = false;
				for (var i in links)
					if (me.equals(links[i],stack))
						found = true;
				if (!found) {
					var cpt = 0;
					var mozFound = false;
					var parent = you.parent();
					var realStack = null;
					parent.find('a').each(function() {
						var it = $(this);
						if (me.equals(it,stack)) {
							if (it.attr('_moz-rs-heading') != undefined) {
								/*if (!realStack)
									realStack = it;*/
								mozFound = true;
							}
							if (it.html() != stack.html())
								cpt ++;
						}
					});
					if (!realStack)
						realStack = stack;
					if (cpt > 1 && mozFound) {
						links.push(realStack);
					}
					else if (cpt > 1 && !mozFound) {
						cpt = 0;
						parent.children('a').each(function() {
							var it = $(this);
							if (me.equals(it,stack))
								cpt ++;
						});
						if (cpt > 1) {
							links.push(realStack);
						}
					}
				}
			} else
				stack = you;
		});

		for (var i in links) {
			var parent = links[i].parent();
			var currentLink = links[i];
			var html = '';
			var cpt = 0;
			var firstLinkDetected = false;
			var stack = [];
			var idClass = 'ffRebuild-'+me.randomString();

			parent.children().each(function() {
				var you = $(this);
				if (!firstLinkDetected && me.isLink(you)) {
					
					if (me.equals(you,currentLink)) {
						currentLink = you;
						firstLinkDetected = true;
					}
				}
				if (firstLinkDetected && cpt > 0) {
					if (me.isLink(you)) {
						if (me.equals(you,currentLink)) {
							if (stack.length > 0)
								for (var j = 0 ; j < stack.length ; j++)
									stack[j].addClass(idClass);
							stack = [];
							you.before('<div class="'+idClass+' ffRebuild-toRemove">'+you.html()+'</div>');
							you.remove();
						}
					} else {
						var found = false;
						you.find('a').each(function() {
							var it = $(this);
							if (me.equals(it,currentLink)) {
								if (stack.length > 0)
									for (var j = 0 ; j < stack.length ; j++)
										stack[j].addClass(idClass);
								stack = [];
								you.addClass(idClass);
								it.before(it.html());
								it.remove();
							}
							found = true;
						});
						if (!found)
							stack.push(you);
					}
				}
				if (firstLinkDetected)
					cpt++;
			});
			parent.find('.'+idClass).each(function() {
				var you = $(this);
				if (you.hasClass('ffRebuild-toRemove')) {
					currentLink.append(you.html());
					you.remove();
				} else {
					you.removeClass(idClass);
					currentLink.append(you);
				}
			});

			if (typeof console != 'undefined' && typeof console.info == 'function') {
				console.info('[ffRebuild] fixed link :');
				console.info(currentLink);
			}
		}
	},
	isLink: function(n) {
		var me = this;
		if (typeof n != 'object' || !n)
			return false;
		return (n.attr('nodeName').toLowerCase() == 'a') ? true : false;
	},
	equals: function(n1, n2) {
		var me = this;
		if (typeof n1 != 'object' || !n1)
			return false;
		if (typeof n2 != 'object' || !n2)
			return false;

		var onclick1 = (n1.attr('onclick')) ? n1.attr('onclick').toString() : '';
		var onclick2 = (n2.attr('onclick')) ? n2.attr('onclick').toString() : '';

		var toReturn = (n1.attr('nodeName') == n2.attr('nodeName')
		&&  n1.attr('class') == n2.attr('class')
		&&  n1.attr('title') == n2.attr('title')
		&&  n1.attr('href') == n2.attr('href')
		&&  onclick1 == onclick2) ? true : false;

		/*if (toReturn == true)
			if (typeof console != 'undefined' && typeof console.info == 'function')
				console.info(n1.attr('class')+" ; "+n1.attr('title')+" ; "+n1.attr('href'));*/
		return toReturn;
	},
	randomString: function() {
		var chars = new Array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','0','1','2','3','4','5','6','7','8','9');
		var str = '';
		for (var i = 0; i < 32; i ++)
			str += chars[Math.floor(Math.random()*chars.length)];
		return str;
	}
};

RTS.ffRebuild.init();
