;(function($) {

$.fn.bookmark = function(opts) {
    genBookmark(this, opts);
};
$.bookmark = new Array();
$.bookmark.defaults = {
    url: location.href,
    title: document.title,
    css: {
        textAlign: 'right',
        width: '100%',
        margin: '10px 0',
        padding: 0,
        border: 'none',
        backgroundColor: '#FFF'
    },
    linkCSS: {
        margin: '0 5px'
    },
    imageCSS: {
        border: 'none'
    },
    position: 'before',
    sites: ['twitter', 'buzzurl', 'hatena', 'livedoor', 'yahoo', 'delicious'],
    twitter_hash: '#zigsow',
    twitter_icon: '',
    twitter_icon_layout: '',
    printable: false
};
function genBookmark(el, opts) {
    opts = $.extend({}, $.bookmark.defaults, opts || {});
    var css = $.extend({}, $.bookmark.defaults.css, opts.css || {});
    opts.linkCSS = $.extend({}, $.bookmark.defaults.linkCSS, opts.linkCSS || {});
    opts.imageCSS = $.extend({}, $.bookmark.defaults.imageCSS, opts.imageCSS || {});
    var container = $('<div class="jquery-bookmark-container"></div>');
    container.css(css);
    $.each(opts.sites, function(i, val) {
        switch (val) {
            case 'twitter':
                container.append(genTwitter(opts));
                break;
            case 'buzzurl':
                container.append(genBuzzurl(opts));
                break;
            case 'hatena':
                container.append(genHatena(opts));
                break;
            case 'livedoor':
                container.append(genLivedoor(opts));
                break;
            case 'yahoo':
                container.append(genYahoo(opts));
                break;
            case 'delicious':
                container.append(genDelicious(opts));
                break;
        }
    });
    if (opts.position == 'after') {
        el.after(container);
    } else if (opts.position == 'append') {
        el.append(container);
    } else if (opts.position == 'prepend') {
        el.prepend(container);
	} else {
        el.before(container);
    }
    if (!opts.printable) {
        var printStyle = $('<style type="text/css" media="print">div.jquery-bookmark-container { display:none; }</style>');
        $('head').append(printStyle);
    }
};
function genTwitter(opts) {
    var link = $('<a href="http://twitter.com/home?status=' + encodeURIComponent(opts.title) + ' ' + encodeURIComponent(opts.url) + ' ' + encodeURIComponent(opts.twitter_hash) + '" title="Twitterに投稿" class="jquery-bookmark-twitter-link" target="_blank"></a>');
    link.css(opts.linkCSS);
    var image;
    if (opts.twitter_icon) {
        if (opts.twitter_icon_layout == 'newline') {
            image = $('<img src="' + opts.twitter_icon + '" alt="Twitterに投稿"/><br/>');
        } else {
            image = $('<img src="' + opts.twitter_icon + '" alt="Twitterに投稿"/>');
        }
    } else {
        image = $('<img src="http://twitter-badges.s3.amazonaws.com/t_mini-b.png" alt="Twitterに投稿"/>');
    }
    image.css(opts.imageCSS);
    if (typeof(BitlyCB) != 'undefined') {
        BitlyCB.shortenResponse = function(data) {
            var first_result;
            for (var r in data.results) {
                first_result = data.results[r];
                break;
            }
            var short_link = 'http://twitter.com/home?status=' + encodeURIComponent(opts.title) + ' ' + encodeURIComponent(first_result['shortUrl']) + ' ' + encodeURIComponent(opts.twitter_hash);
            $('a.jquery-bookmark-twitter-link').attr('href', short_link);
        }
        BitlyClient.shorten(opts.url, 'BitlyCB.shortenResponse');
    }
    return link.append(image);
};
function genBuzzurl(opts) {
    var link = $('<a href="http://buzzurl.jp/config/add/confirm?url=' + encodeURIComponent(opts.url) + '&title=' + encodeURIComponent(opts.title) + '" title="Buzzurlにブックマーク" target="_blank"></a>');
    link.css(opts.linkCSS);
    var image = $('<img src="http://buzzurl.jp/static/image/api/icon/add_icon_mini_01.gif" alt="Buzzurlにブックマーク"/>');
    image.css(opts.imageCSS);
    return link.append(image);
};
function genHatena(opts) {
    var url = 'http://b.hatena.ne.jp/add?mode=confirm&is_bm=1&title=' + encodeURIComponent(opts.title) + '&url=' + encodeURIComponent(opts.url);
    var link = $('<a href="' + url + '" target="_blank" title="はてなブックマーク"></a>');
    link.css(opts.linkCSS);
    link.bind('click', function() {
        window.open(url, 'はてなブックマーク', 'width=550,height=600,resizable=1,scrollbars=1');
        return false;
    });
    var image = $('<img src="http://b.hatena.ne.jp/images/append.gif" alt="はてなブックマーク"/>');
    image.css(opts.imageCSS);
    return link.append(image);
};
function genLivedoor(opts) {
    var link = $('<a href="http://clip.livedoor.com/redirect?link=' + encodeURIComponent(opts.url) + '&title=' + encodeURIComponent(opts.title) + '&ie=utf-8" title="この記事をクリップ！" target="_blank"></a>');
    link.css(opts.linkCSS);
    var image = $('<img src="http://parts.blog.livedoor.jp/img/cmn/clip_16_16_w.gif" alt="この記事をクリップ！"/>');
    image.css(opts.imageCSS);
    return link.append(image);
};
function genYahoo(opts) {
    var url = 'http://bookmarks.yahoo.co.jp/bookmarklet/showpopup?t=' + encodeURIComponent(opts.title) + '&u='+encodeURIComponent(opts.url)+'&ei=UTF-8';
    var link = $('<a href="' + url + '" target="_blank" title="Yahoo!ブックマークに登録"></a>');
    link.css(opts.linkCSS);
    link.bind('click', function() {
        window.open(url, 'Yahoo!ブックマークに登録', 'width=550,height=480,resizable=1,scrollbars=1');
        return false;
    });
    var image = $('<img src="http://i.yimg.jp/images/sicons/ybm16.gif" alt="Yahoo!ブックマークに登録"/>');
    image.css(opts.imageCSS);
    return link.append(image);
};
function genDelicious(opts) {
    var url = 'http://delicious.com/save?v=5&noui&jump=close&url=' + encodeURIComponent(opts.url) + '&title=' + encodeURIComponent(opts.title);
    var link = $('<a href="' + url + '" target="_blank" title="Delicious"></a>');
    link.css(opts.linkCSS);
    link.bind('click', function() {
        window.open(url, 'Delicious', 'width=550,height=550,resizable=1,scrollbars=1');
        return false;
    });
    var image = $('<img src="http://static.delicious.com/img/delicious.small.gif" alt="Delicious"/>');
    image.css(opts.imageCSS);
    return link.append(image);
};

})(jQuery);
