//-----------
// add bookmark
//-----------

// hatena
function addSbmHatena() {
	return void(window.open('http://b.hatena.ne.jp/add?mode=confirm&title='+escape(document.title)+'&url='+escape(location.href)));
}

// Google
function addSbmGoogle() {
	return void(window.open('http://www.google.com/bookmarks/mark?op=add&bkmk='+encodeURIComponent(location.href)+'&title='+encodeURIComponent(document.title)));
}

// Yahoo!
function addSbmYahoo() {
	return void(window.open('http://bookmarks.yahoo.co.jp/bookmarklet/showpopup?t='+encodeURIComponent(document.title)+'&u='+encodeURIComponent(window.location.href)+'&opener=bm&ei=UTF-8'));
}

// del.icio.us
function addSbmDelicious() {
	return void(window.open('http://del.icio.us/post?url='+encodeURIComponent(location.href)+';title='+encodeURIComponent(document.title)));
}

// livedoor
function addSbmLiveDrooClip() {
	return void(window.open('http://clip.livedoor.com/clip/add?link='+encodeURIComponent(location.href)+'&title='+encodeURIComponent(document.title)+'&tags='+''+'&notes='+''+'&jump=ref'));
}

// Buzzurl
function addSbmBuzzurl() {
	return void(window.open('http://buzzurl.jp/config/add/confirm?url='+encodeURIComponent(location.href)+'&title='+encodeURIComponent(document.title)));
}

// newsing
function addSbmNewsing() {
	return void(window.open('http://newsing.jp/add?'+'url='+encodeURIComponent(location.href)));
}

//-----------
// get bookmark count
//-----------

// hatena
function getSbmHatenaCount(i,c) {
	var template='<img src="http://b.hatena.ne.jp/entry/image/small/{P1}" {CLASS} {ID} />';
	document.write( makeSbmCountTag(template, i , c, location.href ) );
	return;
}

// Google
function getSbmGoogleCount(i,c) {
	return;
}

// Yahoo!
function getSbmYahooCount(i,c) {
	var template='<img src="http://num.bookmarks.yahoo.co.jp/image/small/{P1}" {CLASS} {ID} />';
	document.write( makeSbmCountTag(template, i , c, location.href ) );
	return;
}

// del.icio.us
function getSbmDeliciousCount(i,c) {
	var template='<script type="text/javascript" src="http://badges.del.icio.us/feeds/json/url/blogbadge?hash={P1}&callback=getSbmDeliciousCountCallBack"></script>';
	document.write( makeSbmCountTag(template, i , c, MD5_hexhash(location.href) ) );
	return;
}
function getSbmDeliciousCountCallBack(json){
	if ( json != '' ) {
		var c = json[0].total_posts;
		var v = '';
		if (c == 1 ) {
			v = c + 'Person';
		}
		else if ( c > 1 ) {
			v = c + 'Persons';
		}	
		document.write( v );
	}
};

// livedoor
function getSbmLiveDrooClipCount(i,c) {
	var template='<img src="http://image.clip.livedoor.com/counter/small/{P1}" {CLASS} {ID} />';
	document.write( makeSbmCountTag(template, i , c, location.href ) );
	return;
}

// Buzzurl
function getSbmBuzzurlCount(i,c) {
	var template='<img src="http://api.buzzurl.jp/api/counter/{P1}" {CLASS} {ID} />';
	document.write( makeSbmCountTag(template, i , c, location.href ) );
	return;
}

// newsing
function getSbmNewsingCount(i,c) {
	var template='<iframe src="http://newsing.jp/newsingit?url={P1}&title={P2}" scrolling="no" marginheight="0" marginwidth="0" frameborder="0" height="18" width="122" style="margin:0;padding:0;width:122px;height:18px;" {CLASS} {ID} ></iframe>';
	document.write( makeSbmCountTag(template, i , c, location.href, document.title ) );
	return;
}

// make SbmCountTag
function makeSbmCountTag(){
	var t = makeSbmCountTag.arguments[0];
	var i = makeSbmCountTag.arguments[1];
	var c = makeSbmCountTag.arguments[2];
	var n = makeSbmCountTag.arguments.length;

	if ( typeof c == "undefined" ) {
		c = "";
	}
	else {
		c = ' class="' + c + '"';
	}
	if ( typeof i == "undefined" ){
		i = "";
	}
	else {
		i = ' id="' + i + '"';
	}

	t = t.replace('{CLASS}',c);
	t = t.replace('{ID}',i);

	for ( l = 3; l < n; l++ ){
		var r = '{P' + ( l - 2 ) + '}';
		t = t.replace(r,makeSbmCountTag.arguments[l]);
	}
	
	return t;
}
