﻿function showHide(divName) { // hide or show an arbitrary container
    document.getElementById(divName).style.display = (document.getElementById(divName).style.display=='inline'?'none':'inline')
}

function valuetoggle(i) { // focus the "search" box
    i.value = (i.value == "search" ? "" : i.value);
}

function valueuntoggle(i) { // blur the "search" box
    i.value = (i.value == "" ? "search" : i.value);
}

function stripXML(str) {
    return str.replace(/(<([^>]+)>)/ig, "").replace(/\n|\r/g, "");
}

function toggleNew() {
    // toggles the "open in new window" functionality
    for (var x = 0; x < document.links.length; x++) {
        document.links[x].target = (document.links[x].target=="_self"?"_blank":"_self");
    }
}

function getShortUrl(strLongUrl, inputBox) {
    new Ajax.Request('/asmx/ShortUrl.asmx/IsGd', {
        method: 'post',
        onSuccess: function(trans) { document.forms[0].elements[inputBox].value = (document.forms[0].elements[inputBox].value + " " + stripXML(trans.responseText)); showHide(inputBox+"_warn") },
        onFailure: function(trans) { document.getElementById(inputBox+"_warn").innerHtml = "<i>failed to generate short URL</i>";},
        parameters: { longUrl: strLongUrl  }
    });
}

function populate(url, inputBox)  {
    getShortUrl(url, inputBox);
}

function sendTweet(strTweetBody, itemID)    {
    new Ajax.Request('/asmx/tweet.asmx/SendTweet', {
        method: 'post',
        onSuccess: tweetSuccess(itemID),
        onFailure: function(trans) { alert("twitter update failed. please alert @theskepticator and try again later");},
        parameters: { tweetBody: strTweetBody }
    });
}

function tweetSuccess(itemID)  {
    showHide('twitter_box_' + itemID);
    alert("Thank you. posted to twitter successfully");
}

function doSearch(e)    {
    self.location.href = "/search/" + escape(document.getElementById(e).value); return false;
}

function clickButton(e, buttonid){
      var evt = e ? e : window.event;
      var bt = document.getElementById(buttonid);
      if (bt){
          if (evt.keyCode == 13){
                //alert("you hit enter");
                bt.click();
                return false;
          }
      }
}

function showYouTube(ytID)    {
    // shows YouTube movies from yt channels.

    var embedCode = '<object width="480" height="385"><param name="movie" value="http://www.youtube.com/v/'+ytID+'&hl=en_US&fs=1&"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><param name="autoplay" value="1"></param><embed src="http://www.youtube.com/v/'+ytID+'&hl=en_US&fs=1&" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" autoplay="1" width="480" height="385"></embed></object><br /><a href="javascript:hideYouTube(\''+ytID+'\')">close X</a>';

    var dVideo = document.getElementById("div_" + ytID);
    dVideo.innerHTML = embedCode;

    /* embed code 
    '<object width="480" height="385"><param name="movie" value="http://www.youtube.com/v/'+ytID+'&hl=en_US&fs=1&"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/'+ytID+'&hl=en_US&fs=1&" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="480" height="385"></embed></object>'
    
    
    */

    /* thumbnail code 
    <a href=\"javascript:showYouTube(\'" + ytID + 
                    "\')\"><img src=\"http://i.ytimg.com/vi/" + ytID + "/1.jpg\" id=\"img_" + ytID + 
                    "\" />" + "</a>
    */
}

function hideYouTube(ytID)  {
    var embedCode = '<a href=\"javascript:showYouTube(\'' + ytID + 
                    '\')\"><img src=\"http://i.ytimg.com/vi/' + ytID + '/1.jpg\" id=\"img_' + ytID + 
                    '\" />' + '</a>';

    var dVideo = document.getElementById("div_" + ytID);
    dVideo.innerHTML = embedCode;
}

var shareWindowOptions = 'width=480,height=360';

function shareOnFacebook(title, url)  {
    window.open('http://www.facebook.com/sharer.php?u=' + encodeURIComponent(url) + '&t=' + encodeURIComponent(title) + '', 'fb_stuff', shareWindowOptions);
}

function shareOnStumbleUpon(url)   {
    window.open('http://www.stumbleupon.com/toolbar/badge_click.php?r=' + encodeURIComponent(url), 'su_stuff', shareWindowOptions);
}

function shareOnReddit(url)    {
    window.open('http://reddit.com/submit?url=' + encodeURIComponent(url), 're_stuff', shareWindowOptions);
}

function shareOnDigg(url)  {
    window.open('http://digg.com/submit?url='+encodeURIComponent(url), 'di_stuff', shareWindowOptions);
}

function shareOnDelicious(title, url) {
    window.open('http://delicious.com/save?v=5&noui&jump=close&url='+encodeURIComponent(url)+'&title='+encodeURIComponent(title), 'delicious','toolbar=no,width=550,height=550');
}

function shareOnGoogle(title, url)    {
    window.open('https://www.google.com/bookmarks/mark?op=edit&bkmk='+encodeURIComponent(url)+'&title=' + encodeURIComponent(title), 'google', shareWindowOptions);
}

function shareOnLive(title, url)  {
    window.open('https://skydrive.live.com/sharefavorite.aspx/.SharedFavorites??marklet=1&mkt=en-us&url='+encodeURIComponent(url)+'&title='+encodeURIComponent(title)+'&top=1&jump=1&wa=wsignin1.0', 'live', shareWindowOptions);
}

function  shareOnTwitter(title, url)  {
    // http://twitter.com/home?status=Currently reading http://www.test.com/post-url” title=”Click to share this post on Twitter
    window.open('http://twitter.com/home?status='+encodeURIComponent(title)+' '+encodeURIComponent(url)+'', 'live', shareWindowOptions);

}

//populateTweetBox("http://skepticator.com/");
