var sbm = {

    //-----------------------------------------
    // 設定値
    //-----------------------------------------
    conf : {

        hatenaFlg : 1,             // はてなブックマーク
        livedoorFlg : 1,           // livedoorクリップ
        yahooFlg : 1,              // Yahooブックマーク
        niftyFlg : 1,              // @niftyクリップ
        fc2Flg : 1,                // fc2ブックマーク
        flogFlg : 1,               // flogブックマーク
        buzzFlg : 1,               // BuzzUrlブックマーク
        deliciousFlg : 1,          // del.icio.us

        googleFlg : 0,             // Googleブックマーク
        technoratiFlg : 0,         // テクノラティ
        pookmarkFlg : 0,           // POOKMARK Airline
        choixFlg : 0,              // choix

        targetTag : 'p',           // 挿し込み対象の要素名
        targetCls : 'sbmArea'      // 挿し込み対象の要素のclass名

    },

    //-----------------------------------------
    // Main処理
    //-----------------------------------------
    main : function(imgdomain,url,title) {
        if(url == null || url == ''){
            url = encodeURIComponent(location.href);
        }else{
            url = encodeURIComponent(url);
        }

        if(title == null || title == ''){
            title = encodeURIComponent(document.title);
        }else{
            title = encodeURIComponent(title);
        }

        var elements = sbm.getTargetElements(sbm.conf.targetTag, sbm.conf.targetCls);

        for (var i = 0; i < elements.length; i++) {

            var element = elements[i];
            element.innerHTML = '';

            var dl = document.createElement('dl');
//            dl.setAttribute('className', 'sbm');
            dl.className = 'sbm';
            element.appendChild(dl);

            var dt = document.createElement('dt');
            dl.appendChild(dt);

            var dd = document.createElement('dd');
//            dl.appendChild(dd);
            var ul = document.createElement('ul');
//            dl.appendChild(ul);

            if (sbm.conf.hatenaFlg) {
                var li = document.createElement('li');

                var a = document.createElement('a');
                a.setAttribute('href', 'http://b.hatena.ne.jp/add?mode=confirm&title=' + title + '&url=' + url);
                a.setAttribute('target', '_blank');
                a.setAttribute('title', 'はてなブックマーク');

                var img = document.createElement('img');
                img.setAttribute('src', imgdomain + '/img2/common/icon_sbm_hb.gif');
                img.setAttribute('alt', 'はてなブックマーク');
//                img.setAttribute('border', '0');

                a.appendChild(img);
                li.appendChild(a);
                ul.appendChild(li);
            }

            if (sbm.conf.yahooFlg) {
                var li = document.createElement('li');

                var a = document.createElement('a');
//                title = encodeURIComponent(title);
//                url = encodeURIComponent(url);
                a.setAttribute('href', 'http://bookmarks.yahoo.co.jp/bookmarklet/showpopup?t=' + title +'&amp;u=' + url +'\'&amp;ei=UTF-8\'');
                a.setAttribute('target', '_blank');
                a.setAttribute('title', 'yahoo！ブックマーク');

                var img = document.createElement('img');
                img.setAttribute('src', imgdomain + '/img2/common/icon_sbm_yh.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);
                li.appendChild(a);
                ul.appendChild(li);
            }

            if (sbm.conf.livedoorFlg) {
                var li = document.createElement('li');

                var a = document.createElement('a');
//                url = encodeURIComponent(url);
//                title = encodeURIComponent(title);
                a.setAttribute('href', 'http://clip.livedoor.com/redirect?link=' + url + '&amp;title=' + title + '&amp;ie=UTF-8');
                a.setAttribute('target', '_blank');
                a.setAttribute('title', 'livedoorクリップ');

                var img = document.createElement('img');
                img.setAttribute('src', imgdomain + '/img2/common/icon_sbm_ld.gif');
//                img.setAttribute('width', '16');
//                img.setAttribute('height', '16');
                img.setAttribute('alt', 'livedoorクリップ');
//                img.setAttribute('border', '0');

                a.appendChild(img);
                li.appendChild(a);
                ul.appendChild(li);
            }

            if (sbm.conf.niftyFlg) {
                var li = document.createElement('li');

                var a = document.createElement('a');
                a.setAttribute('href', 'http://clip.nifty.com/create?url=' + url + '&title=' + title);
                a.setAttribute('target', '_blank');
                a.setAttribute('title', '@niftyクリップ');

                var img = document.createElement('img');
                img.setAttribute('src', imgdomain + '/img2/common/icon_sbm_nf.gif');
//                img.setAttribute('width', '16');
//                img.setAttribute('height', '16');
                img.setAttribute('alt', '@niftyクリップ');
//                img.setAttribute('border', '0');

                a.appendChild(img);
                li.appendChild(a);
                ul.appendChild(li);
            }

            if (sbm.conf.fc2Flg) {
                var li = document.createElement('li');

                var a = document.createElement('a');
                a.setAttribute('href', 'http://bookmark.fc2.com/user/post?url=' + url + '&title=' + title);
                a.setAttribute('target', '_blank');
                a.setAttribute('title', 'fc2');

                var img = document.createElement('img');
                img.setAttribute('src', imgdomain + '/img2/common/icon_sbm_fc2.gif');
//                img.setAttribute('width', '16');
//                img.setAttribute('height', '16');
                img.setAttribute('alt', 'fc2');
//                img.setAttribute('border', '0');

                a.appendChild(img);
                li.appendChild(a);
                ul.appendChild(li);
            }

            if (sbm.conf.flogFlg) {
                var li = document.createElement('li');

                var a = document.createElement('a');
                a.setAttribute('href', 'http://www.flog.jp/post.php?url_uri=\'' + url);
                a.setAttribute('target', '_blank');
                a.setAttribute('title', 'flog');

                var img = document.createElement('img');
                img.setAttribute('src', imgdomain + '/img2/common/icon_sbm_fl.gif');
//                img.setAttribute('width', '16');
//                img.setAttribute('height', '16');
                img.setAttribute('alt', 'fc2');
//                img.setAttribute('border', '0');

                a.appendChild(img);
                li.appendChild(a);
                ul.appendChild(li);
            }


            if (sbm.conf.buzzFlg) {
                var li = document.createElement('li');

                var a = document.createElement('a');
                url = decodeURIComponent(url);
                a.setAttribute('href', 'http://buzzurl.jp/entry/'+ url);
                a.setAttribute('target', '_blank');
                a.setAttribute('title', 'flog');

                var img = document.createElement('img');
                img.setAttribute('src', imgdomain + '/img2/common/icon_sbm_bz.gif');
//                img.setAttribute('width', '16');
//                img.setAttribute('height', '16');
                img.setAttribute('alt', 'buzzurl');
//                img.setAttribute('border', '0');

                a.appendChild(img);
                li.appendChild(a);
                ul.appendChild(li);
            }

            if (sbm.conf.deliciousFlg) {
                var li = document.createElement('li');

                var a = document.createElement('a');
                a.setAttribute('href', 'http://del.icio.us/post?url=' + url + '&title=' + title);
                a.setAttribute('target', '_blank');
                a.setAttribute('title', 'Delicious');

                var img = document.createElement('img');
                img.setAttribute('src', imgdomain + '/img2/common/icon_sbm_dc.gif');
//                img.setAttribute('width', '16');
//                img.setAttribute('height', '16');
                img.setAttribute('alt', 'Delicious');
//                img.setAttribute('border', '0');

                a.appendChild(img);
                li.appendChild(a);
                ul.appendChild(li);
            }

            if (sbm.conf.googleFlg) {
                var li = document.createElement('li');

                var a = document.createElement('a');
                //a.setAttribute('href', 'http://www.google.com/bookmarks/mark?op=edit&bkmk=' + url + '&title=' + title);
                //a.setAttribute('target', '_blank');
                a.setAttribute('href', 'javascript:void window.open(\'http://www.google.com/bookmarks/mark?op=edit&bkmk=\'+encodeURIComponent(location.href)+\'&title=\'+encodeURIComponent(document.title)+\'&hl=ja\',\'\',\'width=725,height=640,left=100,top=50,scrollbars=1,resizable=1\',0);');

                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);
                li.appendChild(a);
                ul.appendChild(li);
            }

            if (sbm.conf.technoratiFlg) {
                var li = document.createElement('li');

                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);
                li.appendChild(a);
                ul.appendChild(li);
            }

            if (sbm.conf.pookmarkFlg) {
                var li = document.createElement('li');

                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);
                li.appendChild(a);
                ul.appendChild(li);
            }

            if (sbm.conf.choixFlg) {
                var li = document.createElement('li');

                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);
                li.appendChild(a);
                ul.appendChild(li);
            }


            dd.appendChild(ul);
            dl.appendChild(dd);
            element.appendChild(dl);

        }

    },

    //-----------------------------------------
    // ターゲットタグを取得する
    //-----------------------------------------
    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;

    }

}

