var timeoutID;

function addFav() {
    var thisurl = window.location;
	var heading;
	var h1;
	
	if (document.getElementsByTagName) {
		h1 = document.getElementsByTagName('h1')[0];
		if (h1) {
			heading = h1.innerHTML;
		} 
		else {
			heading = 'SQL Book';
		}
	}
	else {
		heading = 'SQL Book';
	}
	
	if (window.sidebar){ // FF
	    window.sidebar.addPanel(heading, thisurl, "");
	}
    else if(window.opera && window.print){ // opera
	    var elem = document.createElement('a');
	    elem.setAttribute('href',thisurl);
	    elem.setAttribute('title',heading);
	    elem.setAttribute('rel','sidebar');
	    elem.click();
    } 
    else if(document.all) { // IE
	    window.external.AddFavorite(thisurl, heading);
    }	
}

function showToolsMenu() {
	clearTimeout(timeoutID);
	timeoutID = setTimeout("toggleToolsMenu(true)", 200);
}

function hideToolsMenu() {
	timeoutID = setTimeout("toggleToolsMenu(false)", 300);
}

function toggleToolsMenu(isShow) {
	if(document.getElementById) {
		var m = document.getElementById('ToolsMenu');
		var sty = m.style;
		if (isShow == true) {
			sty.display = 'block';
		}
		else {
			sty.display = 'none';
		}
	}
}

function socBook(socSite) {
	var sendurl;
	var thisurl = window.location;
	var heading;
	var h1;
	
	if (document.getElementsByTagName) {
		h1 = document.getElementsByTagName('h1')[0];
		if (h1) {
			heading = h1.innerText;
		} 
		else {
			heading = 'SQL Book SQL tutorials, data warehousing and business intelligence';
		}
	}
	else {
		heading = 'SQL Book SQL tutorials, data warehousing and business intelligence';
	}
	
	thisurl = urlEncode(thisurl);
	heading = urlEncode(heading);

	if (socSite == 'Digg') {
		sendurl = 'http://www.digg.com/submit?phase=2&url=' + thisurl + '&title=' + heading + '&bodytext=&topic=programming';
	} else if (socSite == 'Delicious') {
		sendurl = 'http://del.icio.us/post?url=' + thisurl + '&title=' + heading;
	} else if (socSite == 'Technorati') {
		sendurl = 'http://technorati.com/faves?add=' + thisurl;
	} else if (socSite == 'BlogThis')  {
		sendurl = 'http://new.blogger.com/blog_this.pyra?t=' + heading + '&u=' + thisurl + '&n=' + heading;
	} 
	
	if (sendurl != '') {
		//alert(sendurl);
		document.location.href = sendurl;	
	}	
}

function urlEncode(str) {
	var s;
	if (str != '') {
		s = escape(str);
		s = s.replace("+", "%2B");
		s = s.replace("/", "%2F"); 
	}
	//alert(str);
	return s;	
}
