// --------------------------------------------------------------------
// Author  : mashimonator
// Create  : 2009/01/19
// Update  : 2009/01/20
// Description : 各SBMサービスの登録ボタンを自動で配置する
//
// --------------------------------------------------------------------

var sbm = {

	//-----------------------------------------
	// 設定値
	//-----------------------------------------
	conf : {

		hatenaClsName : 'hatena',          // はてなブックマーク
		livedoorClsName : 'livedoor',      // livedoorクリップ
		yahooClsName : 'yahoo',            // Yahooブックマーク
		googleClsName : 'google',          // Googleブックマーク
		niftyClsName : 'nifty',            // @niftyクリップ
		deliciousClsName : 'delicious',    // del.icio.us
		technoratiClsName : 'technorati',  // テクノラティ
		fc2ClsName : 'fc2',                // fc2ブックマーク
		pookmarkClsName : 'pookmark',      // POOKMARK Airline
		choixClsName : 'choix',            // choix

		targetTag : 'ul',          // 挿し込み対象の要素名(※編集不可)
		targetCls : 'socialmedia_List'      // 挿し込み対象の要素のclass名

	},

	//-----------------------------------------
	// Main処理
	//-----------------------------------------
	main : function() {

		var url = encodeURIComponent(location.href);
		var title = encodeURIComponent(document.title);
		var elements = sbm.getTargetElements(sbm.conf.targetTag, sbm.conf.targetCls);
		var userSystem = new sbm.getBrowserInfo(navigator.userAgent);

		for (var i = 0; i < elements.length; i++) {

			var element = elements[i].childNodes;

			for (var x = 0; x < element.length; x++) {

				// for FireFox, Opera
				if (element[x].nodeType == 3) {
					continue;
				}

				if (element[x].className) {
					element[x].innerHTML = '';
				}

				if (element[x].className == sbm.conf.hatenaClsName) {
					
					var a = document.createElement('a');
					//a.setAttribute('href', 'http://b.hatena.ne.jp/append?' + url);
					//a.setAttribute('target', '_blank');
					a.setAttribute('href', 'javascript:(function(){var%20d=(new%20Date);var%20s=document.createElement(\'script\');s.charset=\'UTF-8\';s.src=\'http://b.hatena.ne.jp/js/Hatena/Bookmark/let.js?\'+d.getFullYear()+d.getMonth()+d.getDate();(document.getElementsByTagName(\'head\')[0]||document.body).appendChild(s);})();');
					
					var img = document.createElement('img');
					img.setAttribute('src', 'http://www.edwin.co.jp/common/images/smb_hatena_ico.gif');
					img.setAttribute('alt', 'この記事をはてなブックマークに追加');
					img.setAttribute('border', '0');
					
					a.appendChild(img);
					element[x].appendChild(a);
				}

				if (element[x].className == sbm.conf.livedoorClsName) {
					var a = document.createElement('a');
					a.setAttribute('href', 'http://clip.livedoor.com/clip/add?link=' + url + '&title=' + title + '&jump=ref');
					a.setAttribute('target', '_blank');
					
					var img = document.createElement('img');
					img.setAttribute('src', 'http://parts.blog.livedoor.jp/img/cmn/clip_16_16_w.gif');
					img.setAttribute('width', '16');
					img.setAttribute('height', '16');
					img.setAttribute('alt', 'この記事をクリップ！');
					img.setAttribute('border', '0');
					
					a.appendChild(img);
					element[x].appendChild(a);
				}

				if (element[x].className == sbm.conf.yahooClsName) {
					var a = document.createElement('a');
					a.setAttribute('href', 'http://bookmarks.yahoo.co.jp/action/bookmark?t=' + title + '&u=' + url);
					a.setAttribute('target', '_blank');
					
					var img = document.createElement('img');
					img.setAttribute('src', 'http://www.edwin.co.jp/common/images/smb_yahoo_ico.gif');
					img.setAttribute('width', '16');
					img.setAttribute('height', '16');
					img.setAttribute('alt', 'Yahoo!ブックマークに登録');
					img.setAttribute('style', 'border:none;');
					img.setAttribute('border', '0');
					
					a.appendChild(img);
					element[x].appendChild(a);
				}

				if (element[x].className == sbm.conf.googleClsName) {
					var a = document.createElement('a');
					a.setAttribute('href', 'http://www.google.com/bookmarks/mark?op=edit&bkmk=' + url + '&title=' + title);
					a.setAttribute('target', '_blank');
					
					var img = document.createElement('img');
					img.setAttribute('src', './common/images/google_ico.gif');
					img.setAttribute('width', '16');
					img.setAttribute('height', '16');
					img.setAttribute('alt', 'この記事をGoogleブックマークに追加');
					img.setAttribute('border', '0');
					
					a.appendChild(img);
					element[x].appendChild(a);
				}

				if (element[x].className == sbm.conf.niftyClsName) {
					var a = document.createElement('a');
					a.setAttribute('href', 'http://clip.nifty.com/create?url=' + url + '&title=' + title);
					a.setAttribute('target', '_blank');
					
					var img = document.createElement('img');
					img.setAttribute('src', 'http://clip.nifty.com/images/addclip_icn.gif');
					img.setAttribute('width', '16');
					img.setAttribute('height', '16');
					img.setAttribute('alt', '@niftyクリップに追加');
					img.setAttribute('border', '0');
					
					a.appendChild(img);
					element[x].appendChild(a);
				}

				if (element[x].className == sbm.conf.deliciousClsName) {
					var a = document.createElement('a');
					a.setAttribute('href', 'http://del.icio.us/post?url=' + url + '&title=' + title);
					a.setAttribute('target', '_blank');
					
					var img = document.createElement('img');
					img.setAttribute('src', 'http://www.edwin.co.jp/common/images/smb_delicious_ico.png');
					img.setAttribute('width', '16');
					img.setAttribute('height', '16');
					img.setAttribute('alt', 'del.icio.usに追加');
					img.setAttribute('border', '0');
					
					a.appendChild(img);
					element[x].appendChild(a);
				}

				if (element[x].className == sbm.conf.technoratiClsName) {
					var a = document.createElement('a');
					a.setAttribute('href', 'http://technorati.com/faves?add=' + url);
					a.setAttribute('target', '_blank');
					
					var img = document.createElement('img');
					img.setAttribute('src', './common/images/technorati_ico.gif');
					img.setAttribute('width', '16');
					img.setAttribute('height', '16');
					img.setAttribute('alt', 'テクノラティのお気に入りに追加');
					img.setAttribute('border', '0');
					
					a.appendChild(img);
					element[x].appendChild(a);
				}

				if (element[x].className == sbm.conf.fc2ClsName) {
					var a = document.createElement('a');
					a.setAttribute('href', 'http://bookmark.fc2.com/user/post?url=' + url + '&title=' + title);
					a.setAttribute('target', '_blank');
					
					var img = document.createElement('img');
					img.setAttribute('src', './common/images/fc2_ico.gif');
					img.setAttribute('width', '16');
					img.setAttribute('height', '16');
					img.setAttribute('alt', 'fc2ブックマークに追加');
					img.setAttribute('border', '0');
					
					a.appendChild(img);
					element[x].appendChild(a);
				}

				if (element[x].className == sbm.conf.pookmarkClsName) {
					var a = document.createElement('a');
					a.setAttribute('href', 'http://pookmark.jp/post?title=' + title + '&url=' + url);
					a.setAttribute('target', '_blank');
					
					var img = document.createElement('img');
					img.setAttribute('src', './common/images/pookmark_ico.gif');
					img.setAttribute('width', '16');
					img.setAttribute('height', '16');
					img.setAttribute('alt', 'POOKMARK Airlineに登録');
					img.setAttribute('border', '0');
					
					a.appendChild(img);
					element[x].appendChild(a);
				}

				if (element[x].className == sbm.conf.choixClsName) {
					var a = document.createElement('a');
					a.setAttribute('href', 'http://www.choix.jp/bloglink/' + title);
					a.setAttribute('target', '_blank');
					
					var img = document.createElement('img');
					img.setAttribute('src', './common/images/choix_ico.gif');
					img.setAttribute('width', '16');
					img.setAttribute('height', '16');
					img.setAttribute('alt', 'choixへ追加');
					img.setAttribute('border', '0');
					
					a.appendChild(img);
					element[x].appendChild(a);
				}
				
			}


		}

	},

	//-----------------------------------------
	// ターゲットタグを取得する
	//-----------------------------------------
	getTargetElements : function(tag, cls) {

		var elements = new Array();
		var targetElements = document.getElementsByTagName(tag);

		for (var i = 0; i < targetElements.length; i++) {
			if (targetElements[i].className.match(cls)) {
				elements[elements.length] = targetElements[i];
			}
		}

		return elements;

	},

	//-----------------------------------------
	// ブラウザ情報取得
	//-----------------------------------------
	getBrowserInfo : function() {

		var key, index, keyIndex, keyIndexEnd, versionKey, i, j;
		var uaString = navigator.userAgent.toUpperCase();

		this.browserLongName = "---";
		this.browserShortName = "---";
		this.browserVersion = "---";

		var BROWSERS = new Object();
		BROWSERS['MZ'] = new setBrowser('Mozilla','mz','GECKO');
		BROWSERS['IE'] = new setBrowser('Internet Explorer','ie','MSIE');
		BROWSERS['AO'] = new setBrowser('AOL','ao','AOL');
		BROWSERS['SF'] = new setBrowser('Safari','sf','SAFARI');
		BROWSERS['OP'] = new setBrowser('Opera','op','OPERA');
		BROWSERS['OW'] = new setBrowser('OmniWeb','ow','OMNIWEB');
		BROWSERS['IC'] = new setBrowser('iCab','ic','ICAB');
		BROWSERS['NS'] = new setBrowser('Netscape','ns','NETSCAPE,NETSCAPE6');
		BROWSERS['NN'] = new setBrowser('Netscape Navigator','nn','MOZILLA');
		BROWSERS['FF'] = new setBrowser('Firefox','ff','FIREFOX');


		var UNIXDETAIL = new Array("LNX","BSD");
		var checkVersionExp01 = new Array(' ', '/', '-', '');
		var checkVersionExp02 = new Array(';', ' ', '(', '[', ')', '+', '-', '/');

		uaString = " " + uaString + ";";
		

		index = 0;
		for (key in BROWSERS) {
			for (i=0; i<BROWSERS[key].keyword.length; i++) {
				keyIndex = uaString.indexOf(BROWSERS[key].keyword[i].toUpperCase());
				if (keyIndex > index) {
					this.browserLongName = BROWSERS[key].longName;
					this.browserShortName = BROWSERS[key].shortName;
					versionKey = BROWSERS[key].keyword[i].toUpperCase();
					index = keyIndex;
				}
			}
		}

		// Navigator is reary?
		if (this.browserShortName == "nn" && uaString.indexOf("COMPATIBLE")>0) {
			this.browserLongName = "---";
			this.browserShortName = "---";
		}

		// Version Check
		if (this.browserLongName != "---") {
			for (i=0; i<checkVersionExp01.length; i++) {
				key = versionKey + checkVersionExp01[i];
				if ( ( keyIndex = uaString.indexOf(key) ) > 0 ) break;
			}
			// Mozilla
			if ( key == 'GECKO/' ) {
				key = 'RV:';
				keyIndex = uaString.indexOf(key);
			}
			keyIndex = keyIndex + key.length;
			index = uaString.length;
			for (i=0; i<checkVersionExp02.length; i++) {
				if ((key = uaString.indexOf(checkVersionExp02[i], keyIndex)) > 0) {
					if (key < index) {
						keyIndexEnd = key;
						index = keyIndexEnd;
					}
				}
			}
			this.browserVersion = uaString.substring(keyIndex, keyIndexEnd);
		}

		function setBrowser(longName,shortName,keyWord) {
			this.longName = longName;
			this.shortName = shortName;
			this.keyword = keyWord.split(",");
		}

	},

	//-----------------------------------------
	// Loadイベントに追加
	//-----------------------------------------
	addLoadEvent : function() {
		try {
			window.addEventListener('load', this.main, false);
		} catch (e) {
			window.attachEvent('onload', this.main);
		}
	}

}

sbm.addLoadEvent();
