
function generate_button() {
	button = document.createElement("span");
	button.textContent = "filter";

	/*
	class = document.createAttribute("class");
	class.value = "filter_button";	
	button.setAttributeNode(class);
	*/

	// style
	//button.style.cssFloat = "right";
	button.style.margin = "1px";
	button.style.border = "1px dashed gray";

	// event color
	button.addEventListener("mouseover", set_button_style_hover, false);
	button.addEventListener("mouseout", set_button_style_normal, false);

	// event color
	button.addEventListener("mouseover", popup_menu, false);
	button.addEventListener("mouseout", popdown_menu, false);

	return button;
}

function mouseon_menu_elem(elem) {
	document.getElementById("FilterMenu").style.display = "block";
	elem.style.background = "#ff9900";
	elem.style.color = "#000000";
	elem.style.cursor = "pointer";
}

function mouseout_menu_elem(elem) {
	elem.style.background = "white";
	elem.style.color = "";
}

function mouseout_menu() {
	document.getElementById("FilterMenu").style.display = "none";	
}

function click_menu(domain, type) {
	if(type == "drop") {
		uri = location.href + ' -site:' + domain;
	} else if(type == "only") {
		uri = location.href + ' site:' + domain;
	} else if(type == "new") {
		uri = 'history-search:site:' + domain;
	}
	location.href = uri;
}

function popup_menu(evt) {
	uri = this.parentNode.getElementsByTagName("a")[0].getAttribute("href");
	DomainRegex = new RegExp('://([^/]+)/', "");
	uri.match(DomainRegex);
	domain = RegExp.$1;

	menu = '<div onmouseout="mouseout_menu();">';

	menu += '<div style="background:white;" onmouseover="mouseon_menu_elem(this);" onmouseout="mouseout_menu_elem(this);"'
	+ ' onclick="click_menu(\'' + domain + '\', \'drop\');">Drop ' + domain + ' from result</div>';
	menu += '<div style="background:white;" onmouseover="mouseon_menu_elem(this);" onmouseout="mouseout_menu_elem(this);"'
	+ ' onclick="click_menu(\'' + domain + '\', \'only\');">Show only ' + domain + '</div>';
	menu += '<div style="background:white;" onmouseover="mouseon_menu_elem(this);" onmouseout="mouseout_menu_elem(this);"'
	+ ' onclick="click_menu(\'' + domain + '\', \'new\');">Find ' + domain + ' from History</div>';

	menu += '</div>';

	FilterMenu = document.getElementById("FilterMenu");
	//FilterMenu.style.virticalAlign = "middle";

	FilterMenu.innerHTML = menu;
	FilterMenu.style.display = "block";

	// position setting need to place after display is block.
	left = this.offsetLeft + this.offsetWidth - FilterMenu.offsetWidth;
	if( left < 0 ) left = 0;
	FilterMenu.style.left = left + "px";
	FilterMenu.style.top = this.offsetTop + this.offsetHeight - 1 + "px";

}

function popdown_menu() {
	document.getElementById("FilterMenu").style.display = "none";
}

function set_button_style_hover() {
	this.style.border = "1px dashed orange";
}

function set_button_style_normal() {
	this.style.border = "1px dashed gray";
}

function insert_button(header_div) {
	// header_div.parentNode.insertBefore(generate_button(), header_div.nextSibling);
	header_div.appendChild(generate_button());
}

function addFilterMenu() {
	menu = document.createElement("div");
	menu.setAttribute("id", "FilterMenu");
	menu.style.display = "none";
	menu.style.position = "absolute";

	document.getElementsByTagName("body")[0].appendChild(menu);
}

function search_title() {
	// debug
	// var log = document.getElementById('log');

	var divObjs = document.getElementsByTagName("div");
	for( var i=0; i < divObjs.length; i++ ) {
		var current = divObjs[i];
		switch( current.getAttribute("class") ) {
		case "header":
			insert_button(current);
			//current.style.cssFloat = "left";
			// show result in 1 line.
			//current.style.whiteSpace = "nowrap";
			//current.style.overflow = "hidden";
			//current.style.maxWidth = "100%";
			break;
		case "summary":
			//current.style.clear = "both";
			// fix current css
			//current.style.borderTop = "solid 1px black";
			break;
		}
	}

	addFilterMenu();

	// debug
	//log.innerHTML = out;
}
// out = '';
window.onload = search_title;
