// HTML SLicer
// version 0.1
// 2008-02-02
// Copyright (c) 2007, Viktor Kelemen
// Released under the GPL license
// http://www.gnu.org/copyleft/gpl.html
//
// --------------------------------------------------------------------
//
// This is a Greasemonkey user script.  To install it, you need
// Greasemonkey 0.3 or later: http://greasemonkey.mozdev.org/
// Then restart Firefox and revisit this script.
// Under Tools, there will be a new menu item to "Install User Script".
// Accept the default configuration and install.
//
// --------------------------------------------------------------------
//
// ==UserScript==
// @name          HTML Slicer
// @namespace     http://yikulju.wikidot.com
// @description   Mixing the DOM
// @include       http://*
// @include		  https://*
// ==/UserScript==


function doChangeAttr(node1,node2,attr)	{
	
	var a = node1[attr];
	node1[attr]=node2[attr];
	node2[attr]=a;
}

function doRandomizeChildrenNodes(root)	{
	
	var childnodes = root.childNodes;
	var children = [];
	
	if (childnodes.length==0) return;
	
	for(var i = 0; i < childnodes.length; i++)
	{
  		if(childnodes[i].nodeType == 1) { 
			childnodes[i].style.margin="0";
			childnodes[i].style.padding="0";
			childnodes[i].style.display="inline";
						
			doRandomizeChildrenNodes(childnodes[i]);	 
		}
		children.push(childnodes[i]);
	}
	
	if (children.length>0)	{
		for (var i=0;i<10;i++)	{
			var i1 = Math.floor(Math.random()*children.length);
			var i2 = Math.floor(Math.random()*children.length);
			
			doChangeAttr(children[i1],children[i2],"id");
			doChangeAttr(children[i1],children[i2],"class");

			
			root.insertBefore(children[i1],children[i2]);
		}
	}	
}


function findNode(root)	{
	
	
	if (root.nodeType != 1)	{
		return root;
	}
	
	
	if (!root.hasChildNodes())	{ 
		return root; 
	}
	
	var childnodes = root.childNodes;
	
	var b = Math.floor(Math.random()*10);
	var i1 = Math.floor(Math.random()*childnodes.length);
		
	if (b<6) { 	
		return findNode(childnodes[i1]);
	} else	{
		return childnodes[i1];
	}

}

function doSlicing()	{

	setTimeout(doRandomizeChildrenNodes(document.body), 1000);

}

doSlicing();