﻿
jQuery.MicroBlogFW = function(me, option)
{
    var $me = jQuery(me); 
    buildFaceBookLink();
    buildPlurkLink();
    buildTwitterLink();
    $me.append('&ensp;&ensp;');
    $me.append('加入書籤:');
    buildGoogleLink();
    buildWindowsLink();
    buildFunPLink();
    buildHeMiLink();
    buildMyShareLink();
    
    function ToPlurk()
    {
        var plurk = 'http://www.plurk.com/?qualifier=shares&status=';            
        var u = encodeURI(plurk) + encodeURIComponent(option.url) + ' (' + option.title + ')';
        return u;
    };

    function ToTwitter()
    {
        var twitter = 'http://twitter.com/home/?status=';
        var u = encodeURI(twitter) + option.title + ' ' + encodeURIComponent(option.url);
        return u;
    };

    function fbs_click() 
    {
        var u = 'http://www.facebook.com/sharer.php?u='+encodeURIComponent(option.url)+'&t='+option.title;
        return u;
    };

    function ToYahoo()
    {
        var u = 'http://tw.myweb2.search.yahoo.com/myresults/bookmarklet?t='+ encodeURIComponent(option.url) + '&u=' + option.title;
        return u;
    };

    function ToWindowsLive()
    {
        var u = 'https://favorites.live.com/quickadd.aspx?marklet=0&mkt=zh-tw&top=1&url='+encodeURIComponent(option.url) + '&title=' + option.title;
        return u;
    };

    function ToGoogle()
    {
        var u = 'http://www.google.com/bookmarks/mark?op=add&bkmk='+ encodeURIComponent(option.url) + '&title=' + option.title;
        return u;
    };

    function ToFunp()
    {
        var u = 'http://funp.com/push/submit/add.php?url=' + encodeURIComponent(option.url) + '&s= ' + option.title + '&via=tool';
        return u;
    };

    function ToHeMi()
    {
        var u = 'http://www.hemidemi.com/user_bookmark/new?url=' + encodeURIComponent(option.url) + '&title=' + option.title;
        return u;
    };

    function ToMyShare()
    {
        var u = 'http://myshare.url.com.tw/index.php?func=newurl&from=mysharepop&NewsMaster=1&url='+ encodeURIComponent(option.url) + '(' + option.title + ')';
        return u;
    };
    
    function buildTwitterLink()
    {
        var u = ToTwitter();
        BuildLink('推到Twitter', option.imageBasePath + 'PushIcon-Twitter.png', u, 'twitterlink');
    };

    function buildPlurkLink()
    {
        var u = ToPlurk();
        BuildLink('推到Plurk去', option.imageBasePath +'PushIcon-Plurk.png', u, 'plurklink');
    };

    function buildFaceBookLink()
    {
        var u = fbs_click();
        BuildLink('在Facebook上分享', option.imageBasePath +'PushIcon-facebook.png', u, 'fblink');
    };

    function buildMyShareLink()
    {
        var u = ToMyShare();
        BuildLink('', option.imageBasePath +'PushIcon-MyShare.png', u, 'mysharelink');
    };

    function buildHeMiLink()
    {
        var u = ToHeMi();
        BuildLink('', option.imageBasePath +'PushIcon-HD.png', u, 'hemilink');
    };

    function buildFunPLink()
    {
        var u = ToFunp();
        BuildLink('', option.imageBasePath +'PushIcon-funP.png', u, 'funplink');
    };

    function buildGoogleLink()
    {
        var u = ToGoogle();
        BuildLink('', option.imageBasePath +'PushIcon-Google.png', u, 'googlelink');
    };

    function buildWindowsLink()
    {
        var u = ToWindowsLive();
        BuildLink('', option.imageBasePath +'PushIcon-MSN.png', u, 'winlink');
    };

    function BuildLink(LinkText, ImageSrc, LinkTarget, LinkID)
    {         
        var channelid = getid();
        if(LinkText != '')
        {
            var newImg = new Image();
            var newAnchor = document.createElement('a');
            var text = document.createTextNode(LinkText);                        
            newAnchor.appendChild(text);
            newAnchor.id = LinkID;            
            newAnchor.href = LinkTarget;
            newAnchor.target = option.linkTarget;
            jQuery(newAnchor).css({'color':'black', 'zoom':'1'});        
            newImg.src = ImageSrc;
            newImg.id = LinkID;
            newImg.hspace = 2;
            jQuery(newImg).css('vertical-align', 'middle');
            $me.append(newImg);
            $me.append(newAnchor);  
            $me.append('&nbsp');     
            function onDone()
            {}
            jQuery('a[id*='+LinkID+']').click(function(){
                jQuery.ajax({
                            url: "/AdWorker/Record.axd?id=" + channelid + "&keyid=" + option.keyid,
                            type: "POST", 
                            complete: onDone,
                            contentType: 'application/json',
                            data: LinkID
                            }); 
            }); 
        }
        else
        {
            var newImg = new Image();
            newImg.src = ImageSrc;
            newImg.hspace = 2;
            newImg.id = LinkID;
            jQuery(newImg).css('vertical-align','middle');
            var newAnchor = document.createElement('a');
            newAnchor.appendChild(newImg);            
            newAnchor.href = LinkTarget;
            newAnchor.target = option.linkTarget;   
            
            $me.append(newAnchor);    
            
            function onDone()
            {}
            jQuery('img[id*='+LinkID+']').click(function(){
                jQuery.ajax({
                            url: "/AdWorker/Record.axd?id=" + channelid + "&keyid=" + option.keyid,
                            type: "POST", 
                            complete: onDone,
                            contentType: 'application/json',
                            data: LinkID
                            }); 
            });            
        }
    };
    
    function getid(){
        var channelid = '';
        if(window.location.href.indexOf('Report') != -1) channelid = 'KMDJ-Report';
        else if(window.location.href.indexOf('News')!= -1) channelid = 'KMDJ-News';
        else if(window.location.href.indexOf('Forum')!= -1) channelid = 'KMDJ-Forum';
        else if(window.location.href.indexOf('Blog')!= -1) channelid = 'KMDJ-Blog';
        else if(window.location.href.indexOf('Wiki')!= -1) channelid = 'KMDJ-Wiki';
        else if(window.location.href.indexOf('Bookmark')!= -1) channelid = 'KMDJ-Bookmark';
        else if(window.location.href.indexOf('Opinion')!= -1) channelid = 'KMDJ-Opinion';
        
        return channelid;
    };
}

jQuery.fn.MicroBlogFW = function(option)
{
    option = option || {};
    option.title = option.title || 'default';
    option.url = option.url || 'www.funddj.com/kmdj';
    option.imageBasePath = option.imageBasePath || '/kmdj/Images/';
    option.linkTarget = option.linkTarget || '_blank';
    option.keyid = option.keyid || '  ';
    this.each(function(){
        var me = this;
        new jQuery.MicroBlogFW(me, option);
    });
    
    return this;
}
