﻿// page.load event
addLoadEvent(writeDotNetKicksHtml);
addLoadEvent(writeDotNetShoutoutHtml);
addLoadEvent(writeDZoneHtml);
addLoadEvent(writeFacebookHref);

var windowID = 'newwindow'; // id of new window opened via 'window.open()'
var documentUrl = encodeURIComponent(location.href);
var documentTitle = encodeURIComponent(document.title);

// ********************************************************************************************************************
// ********************************************************************************************************************
// ********************************************************************************************************************

function addLoadEvent(fn) 
{
    // do this only for web pages, not in WLW...
    if (document.location.protocol == 'http:') 
    {
        if (window.addEventListener) {
            window.addEventListener('load', fn, false)
        } else if (window.attachEvent) {
            window.attachEvent('onload', fn);
        }
    }
}

// ********************************************************************************************************************
//
// Looks for a (invisible) div with id 'summary'.

function getDescription() 
{
    var summaryInput = document.getElementById("summary");
    if (summaryInput) {
        var text = summaryInput.value;
        if (text.length > 100) {
            text = text.substring(0, 97) + '...';
        }
        return text;
    }
    return "";
}

// ********************************************************************************************************************

function writeDotNetKicksHtml() 
{
    var insertLocation = document.getElementById('kickit');
    if (insertLocation) 
    {
        var newHtml;

        newHtml = '<a href="http://www.dotnetkicks.com/kick/?url=' + documentUrl + '">';
        newHtml += '<img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=' + documentUrl + '" ';
        newHtml += 'style="padding-top: 6px; position: relative; top: 4px; display: inline; cursor: pointer" ';
        newHtml += 'border="0" ';
        newHtml += 'width="88" height="22" ';
        newHtml += 'title="Kick it!" ';
        newHtml += 'alt="kickit" ';
        newHtml += 'id="kick" ';
        newHtml += '/>';
        newHtml += '</a>';

        insertLocation.innerHTML = newHtml;
    }
}
// ********************************************************************************************************************

function writeDotNetShoutoutHtml()
 {
    var insertLocation = document.getElementById('shoutit');
    if (insertLocation) 
    {
        var newHtml;

        newHtml = '<a href="http://dotnetshoutout.com/Submit?url=' + documentUrl + '">';
        newHtml += '<img src="http://dotnetshoutout.com/image.axd?url=' + documentUrl + '" ';
        newHtml += 'style="padding-top: 6px; position: relative; top: 4px; display: inline; cursor: pointer" ';
        newHtml += 'border="0" ';
        newHtml += 'title="Shout it!" ';
        newHtml += 'alt="shoutit" ';
        newHtml += 'id="shout" ';
        newHtml += '/>';
        newHtml += '</a>';

        insertLocation.innerHTML = newHtml;
    }
}

// ********************************************************************************************************************
// hard-coded dzone variables!
function writeDZoneHtml()
 {
    var insertLocation = document.getElementById('zoneit');
    if (insertLocation) 
    {
        var newHtml;

        newHtml  = '<iframe src="http://widgets.dzone.com/links/widgets/zoneit.html?t=2';
        newHtml += '&url=' + documentUrl;
        newHtml += '&title=' + documentTitle;
        newHtml += '&description=' + getDescription();
        newHtml += '"style="position: relative; top: 5px;" height="20" width="150" ';
        newHtml += 'scrolling="no" frameborder="0" title="Zone it!"></iframe>';
        
        insertLocation.innerHTML = newHtml;
    }
}

// ********************************************************************************************************************

function writeFacebookHref() 
{
    var insertLocation = document.getElementById('fbshare');
    if (insertLocation) 
    {
        insertLocation.href = getUrl('facebook');
    }
}

// ********************************************************************************************************************

function getUrl(where) 
{
    var ret;
    var documentDescription = getDescription();

    switch (where)
    {
        case 'delicious':
            ret  = 'http://del.icio.us/post?url=' +documentUrl;
            ret += '&title=' + documentTitle;
            ret += '&notes=' + documentDescription;
            break;
        case 'digg':
            ret  = 'http://digg.com/submit?phase=2&url=' + documentUrl; 
            ret += '&title=' + documentTitle;
            break;
        case 'reddit':
            ret  = 'http://reddit.com/submit?url=' + documentUrl; 
            ret += '&title=' + documentTitle;
            break;
        case 'googlebkm':
            ret  = 'http://www.google.com/bookmarks/mark?op=add&bkmk=' + documentUrl; 
            ret += '&title=' + documentTitle;
            ret += '&annotation=' + documentDescription;
            break;
        case 'linkedin':
            ret = 'http://www.linkedin.com/shareArticle?mini=true&url=' + documentUrl;
            ret += '&title=' + documentTitle;
            ret += '&summary=' + documentDescription;
            ret += '&source=' + window.location.hostname;
            break;
        case 'technorati':
            ret = 'http://technorati.com/faves?add=' + documentUrl; 
            break;
        case 'stumbleupon':
            ret  = 'http://www.stumbleupon.com/submit?url=' + documentUrl;
            ret += '&title=' + documentTitle;
            break;
        case 'mrwong':
            ret  = 'http://www.mister-wong.com/index.php?action=addurl&bm_url=' + documentUrl;
            ret += '&bm_description=' + documentTitle;
            ret += '&bm_notice=' + documentDescription;
            break;
        case 'yahoobkm':
            ret  = 'http://myweb2.search.yahoo.com/myresults/bookmarklet?Webmasteru=' + documentUrl;
            ret += '&t=' + documentTitle.replace(/%20/g, '+');
            ret += '&d=' + documentDescription.replace(/\s/g, '+');
            break;
        case 'facebook':
            ret = 'http://www.facebook.com/sharer.php?u=' + documentUrl;
            ret += '&t=' + documentTitle;
            break;
        case 'dzone':
            ret = ' http://www.dzone.com/links/add.html?url=' + documentUrl;
            ret += '&title=' + documentTitle;
            ret += '&description=' + documentDescription;
            break;
    }

    return ret;
}

// ********************************************************************************************************************

function setBookmark(where) 
{
    var url = getUrl(where);
    var newWindow = window.open(url, windowID);
    newWindow.focus();
    return false;
}

// ********************************************************************************************************************

BitlyCB.shortenResponse = function(data) {
    var s = '';
    var first_result;
    // Results are keyed by longUrl, so we need to grab the first one.
    for (var r in data.results) {
        first_result = data.results[r]; break;
    }

    // Now get the shortURL
    shortURL = first_result["shortUrl"];
    windowOpenUrl = "http://www.twitter.com/home/?status=" + document.title.replace(/ /g, "+") + "+-+" + shortURL + "+";
    window.open(windowOpenUrl, windowID);
}

// ********************************************************************************************************************

function mailpage() {
    mail_str = 'mailto:?subject=Check out this blog post: ' + document.title;
    mail_str += '&body=I thought you might be interested in this: "' + document.title;
    mail_str += '". You can view it at here: ' + location.href + '.';
    location.href = mail_str;
}

// ********************************************************************************************************************

function bookmarkInBrowser() {

    var errorText = "Sorry, could not recognize your browser!\nYou must add the bookmark manually...";
    
    try {
        if (navigator.userAgent.toLowerCase().indexOf('chrome') > -1) { // Google Chrome
            setBookmark('googlebkm');
        }
        else if (window.sidebar) { // Mozilla Firefox Bookmark
            window.sidebar.addPanel(document.title, location.href, "");
        }
        else if (window.external) { // IE Favorite
            window.external.AddFavorite(location.href, document.title);
        }
        else if (window.opera && window.print) { // Opera Hotlist
            return true;
        }
        else {
            alert(errorText);
        }
    }
    catch (e) {
        alert(errorText);
    }
}

// ********************************************************************************************************************



// ********************************************************************************************************************
//        
// Other URLs
// **********        
//        
//        case 'Folkd': window.open('http://www.folkd.com/submit/' + burl); break;
//        case 'Furl': window.open('http://www.furl.net/storeIt.jsp?u=' + burl + '&t=' + btitle); break;
//        case 'Icio': window.open('http://www.icio.de/add.php?url=' + burl); break;
//        case 'Linkarena': window.open('http://linkarena.com/bookmarks/addlink/?url=' + burl + '&title=' + btitle); break;
//        case 'Magnolia': window.open('http://ma.gnolia.com/bookmarklet/add?url=' + burl + '&title=' + btitle); break;
//        case 'Newsvine': window.open('http://www.newsvine.com/_tools/seed&save?u=' + burl + '&h=' + btitle); break;
//        case 'Slashdot': window.open('http://slashdot.org/bookmark.pl?url=' + burl + '&title=' + btitle); break;
//        case 'Smarking': window.open('http://smarking.com/editbookmark/?url=' + burl + '&description=' + btitle); break;
//        case 'Spurl': window.open('http://www.spurl.net/spurl.php?title=' + btitle + '&url=' + burl); break;
//        case 'Webnews': window.open('http://www.webnews.de/einstellen?url=' + burl + '&title=' + btitle); break;
//        case 'Yigg': window.open('http://yigg.de/neu?exturl=' + burl); break;
//        case 'Infopirat': window.open('http://infopirat.com/node/add/userlink?edit[url]=' + burl + '&edit[title]=' + btitle); break;
//        case 'Oneview': window.open('http://www.oneview.de/quickadd/neu/addBookmark.jsf?URL=' + burl + '&title=' + btitle); break;
//        
// ********************************************************************************************************************

