/** ブックマーク対象URL */
var target_url = location.href;

/** ブックマーク対象タイトル */
var target_title = document.title;

/** ドロップダウン部のID */
var dropdownid = "bookmarkDropdown";

/** アイコン画像のID */
var imageid = "bookmark_image float_right";

/** アイコン画像 */
var image_url='/img/btn_bookmark.gif';

/** ロード対象URL */
var link_url='';

/** 非表示タイマー */
var hide_timer = 0;

/** ブックマーク一覧 */
var bookmark_list = new Array();
						//表示名,ブックマークURL,アイコン
bookmark_list[ 0 ]	= [ 'Yahoo!ブックマーク', 'http://bookmarks.yahoo.co.jp/action/bookmark?t=<!--title-->&u=<!--url-->', '/img/icon_yahoo.gif' ];
bookmark_list[ 1 ]	= [ 'buzzurl', 'http://buzzurl.jp/config/add/confirm?url=<!--url-->&title=<!--title-->&comment=<!--comment-->', '/img/icon_buzzurl.gif' ];
bookmark_list[ 2 ]	= [ 'del.icio.us', 'http://del.icio.us/post?url=<!--url-->&title=<!--title-->&notes=<!--comment-->', '/img/icon_delicious.gif' ];
bookmark_list[ 3 ]	= [ 'はてなブックマーク', 'http://b.hatena.ne.jp/append?<!--url-->', '/img/icon_hatena.gif' ];
bookmark_list[ 4 ]	= [ 'Google', 'http://www.google.com/bookmarks/mark?op=add&bkmk=<!--url-->&title=<!--title-->&annotation=<!--comment-->', '/img/icon_google.gif' ];
bookmark_list[ 5 ]	= [ 'livedoor', 'http://clip.livedoor.com/redirect?link=<!--url-->&title=<!--title-->&notes=<!--comment-->', '/img/icon_livedoor.gif' ];
bookmark_list[ 6 ]	= [ '@niftyクリップ', 'http://clip.nifty.com/create?url=<!--url-->&title=<!--title-->&comment=<!--comment-->', '/img/icon_nifty.gif' ];


/**
 *	CSSロード
 */
function LoadBookmarkCss() {
	var css = document.createElement( "link" );
	css.rel = 'stylesheet';
	css.type = 'text/css';
	css.href = link_url + '/css/bookmark.css';
	css.media = 'all';

	document.lastChild.firstChild.appendChild( css );
}

/**
 *	ドロップダウン部分生成
 */
function LoadDorpdownHTML() {
	var idx;
	var html = '';

	//ブックマーク対象一覧
	html += '<ul>';
	for( idx=0; idx<bookmark_list.length; idx++ ) {
		//表示アイコン
		icon = link_url + bookmark_list[ idx ][ 2 ];
		//表示名
		name = bookmark_list[ idx ][ 0 ];

		//表示
		html += '<li><img src="' + icon + '" width="16" height="16"><a href="javascript:add_bookmark(' + idx + ');">' + name + '</a></li>'
	}
	html += '</ul>';

	//閉じるボタン
	html += '<div id="bookmarkClose">';
	html += '<a href="javascript:hide_dropdown()">閉じる</a></div>';
	html += '</div>';

	//オブジェクト生成
	var dropdown = document.createElement( "div" );
	dropdown.id = dropdownid;
	dropdown.innerHTML = html;
	dropdown.onmouseout = hide_dropdown_start;
	dropdown.onmouseover = clear_hidetimer;

	document.body.insertBefore( dropdown, document.body.firstChild );
}

/**
 *	ブックマークボタンを描画
 */
function LoadBookmarkImage() {
	//ページ内表示
	var button_image = '';
	button_image += '<a href="javascript:;" onmouseover="show_dropdown( event, this );\" onmouseout="hide_dropdown_start();">';
	button_image += '<img class="' + imageid + '" src="' + image_url + '" width="151" height="19" border="0" alt="アキポ.jp（akipo.jp）をブックマークに登録" />';
	button_image += '</a>';

	document.write( button_image );
}

/**
 *	ドロップダウン表示
 */
function show_dropdown( ev, element ) {
	clear_hidetimer();

	//表示対象
	var dropdown = document.getElementById( dropdownid );

	//表示
	dropdown.style.display = "block";

	//位置基準取得
	var img_list = element.getElementsByTagName( 'img' );
	if( img_list ){
		element = img_list[ 0 ];
	}

	//表示位置計算
	var position = GetPositionSet( element );
	var windowsize = GetWindowSizeSet();
	var scroll = GetScrollSet();

	if( position[ 1 ] - scroll[ 0 ] + dropdown.clientWidth + 24 > windowsize[ 0 ] ) {
		position[ 1 ] = position[ 1 ] - 157;
	}
	if( position[ 0 ] - scroll[ 1 ] + dropdown.clientHeight + element.clientHeight + 24 > windowsize[ 1 ] ) {
		position[ 0 ] = position[ 0 ] - 195;
	}

	dropdown.style.left = position[ 1 ] + 'px';
	dropdown.style.top = ( position[ 0 ] + element.clientHeight ) + 'px';

	return false;
}

/**
 *	ドロップダウン非表示処理開始
 */
function hide_dropdown_start() {
	hide_timer = setTimeout( "hide_dropdown()",155 );
}

/**
 *	ドロップダウン非表示
 */
function hide_dropdown() {
	var dropdown = document.getElementById( dropdownid );
	dropdown.style.display = "none";
}

/**
 *	ドロップダウン非表示解除
 */
function clear_hidetimer() {
	if( hide_timer ) {
		clearTimeout( hide_timer );
		hide_timer = 0;
	}
}

/**
 *	ブックマーク追加
 */
function add_bookmark( index ) {
	hide_dropdown();

	//選択部分の取得
	var comment = "";
	if( document.selection && document.selection.type == 'Text' ) {
		comment = document.selection.createRange().text;
	}
	else if( window.getSelection ) {
		comment = window.getSelection();
	}
	else if( document.getSelection ) {
		comment = document.getSelection();
	}

	//エンコード処理
	//target_url = encodeURIComponent( target_url );
	var url = target_url;

	var title = encodeURIComponent( target_title );
	title = title.replace( /'/g, '\\\'' );

	comment = encodeURIComponent( comment );

	//URL作成
	var open = bookmark_list[ index ][ 1 ];

	open = open.replace( "<!--url-->", url );
	open = open.replace( "<!--title-->", title );
	open = open.replace( "<!--comment-->", comment );

	window.open( open );
}

/**
 *	位置情報取得
 */
function GetPositionSet( element ) {
	var top = 0;
	var left = 0;
	do {
		top += element.offsetTop || 0;
		left += element.offsetLeft || 0;

		element = element.offsetParent;
	} while ( element );

	return( [ top, left ] );
}

/**
 *	画面サイズ情報取得
 */
function GetWindowSizeSet() {
	var width = 0;
	var height = 0;

	if( typeof( window.innerWidth ) == 'number' ) {
		width = window.innerWidth;
		height = window.innerHeight;
	}
	else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		width = document.documentElement.clientWidth;
		height = document.documentElement.clientHeight;
	}
	else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		width = document.body.clientWidth;
		height = document.body.clientHeight;
	}
	return( [ width, height ] );
}

/**
 *	スクロール位置情報取得
 */
function GetScrollSet() {
	var width = 0;
	var height = 0;

	if( typeof( window.pageYOffset ) == 'number' ) {
		height = window.pageYOffset;
		width = window.pageXOffset;
	}
	else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
		height = document.body.scrollTop;
		width = document.body.scrollLeft;
	}
	else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
		height = document.documentElement.scrollTop;
		width = document.documentElement.scrollLeft;
	}
	return( [ width, height ] );
}

//ロード
LoadBookmarkCss();
LoadDorpdownHTML();
LoadBookmarkImage();

