/*
 guess_loop

*/

new function(){

function $(id){
	return document.getElementById(id)
}

function guess_loop(node){
	var r = domdump(node,[], 0);
	var path = r.map(function(v){return v.path});
	var c = {};
	path.map(function(v){c[v]=++c[v]||1});
	var keys = [];
	for(var key in c){keys.push(key)}
	keys.sort(function(a,b){return c[b]-c[a]});
	var hilight_path = {};
	var hilight_color = ["#F00", "#0F0", "#00F"];
	alert(keys.slice(0,3).map(function(v){
		return c[v] + ": " + v;
	}).join("\n"));
	keys.slice(0,3).forEach(function(v,i){
		hilight_path[v] = hilight_color[i];
	});
	r.map(function(v){
		if(hilight_path[v.path]){
			v.el.style.border = "2px solid " + hilight_path[v.path];
			// console.log(v.el);
		}
	});
	/* 
	$('result').value = keys.slice(0,5).map(function(v){
		return c[v] + ": " + v;
	}).join("\n") + "\n-----\n" + r.toSource().replace(/},/g, "},\n");
	*/
}

function domdump(target, ary, level, path){
	path = path || [];
	var fc = "firstChild";
	var ns = "nextSibling";
	var len = target.childNodes.length;
	var count_by_tagname = {};
	for(var i=0;i<len;i++){
		var el = target.childNodes[i];
		var tag = el.tagName;
		count_by_tagname[tag] = count_by_tagname.hasOwnProperty(tag) ? count_by_tagname[tag] + 1 : 0;
		if(el.nodeType != 1){continue;}
		var dump = {
			level : level,
			path  : "/" + path.concat(tag).join("/"),
			tag   : tag,
			cl    : ((el.className)?el.className:""),
			id    : ((el.id)?el.id:""),
			text  : ((el[fc])?((el[fc].nodeValue)?el[fc].nodeValue:""):""),
			el : el
		};
		ary.push(dump);
		if(el[fc] && el.childNodes.length > 0){
			level++;
			var cnt = count_by_tagname[tag];
			path.push(el.tagName + '['+cnt+']');
			domdump(el,ary,level, path);
			path.pop();
			level--;
		}
	}
	return ary;
}

guess_loop(document.body);

}
