/* Fix for Facebook resizing windows */
var WindowObjectReferenceOfRequestedPopup, WindowObjectReferenceOfIntermediaryPopup;
function OpenRequestedPopup(strUrl, strTarget)
{
var windowWidth, windowHeight, windowLeft, windowTop;
if(typeof window.screenX == "number" && typeof window.innerWidth == "number")
{
windowWidth = window.innerWidth * .68;
windowHeight = window.innerHeight * .68;
windowLeft = window.screenX + window.innerWidth * .16;
windowTop = window.screenY + window.innerHeight * .16;
}
else if(typeof window.screenTop == "number" && typeof document.documentElement.offsetHeight == "number")
{
windowWidth = document.documentElement.offsetWidth * .68;
windowHeight = document.documentElement.offsetHeight * .68;
windowLeft = window.screenLeft + document.documentElement.offsetWidth * .16;
windowTop = window.screenTop - 50;
}
else
{
windowWidth = 500;
windowHeight = 250;
windowLeft = 60;
windowTop = 40;
};
if (WindowObjectReferenceOfRequestedPopup == null || WindowObjectReferenceOfRequestedPopup.closed)
{
WindowObjectReferenceOfRequestedPopup = window.open(strUrl, strTarget, "top=" + windowTop + ",left=" + windowLeft + ",width=" + windowWidth + ",height=" + windowHeight + ",menubar,toolbar,location,resizable,scrollbars,status");
}
else
{
if(WindowObjectReferenceOfRequestedPopup.focus)
  {
  WindowObjectReferenceOfRequestedPopup.focus();
  };
};
}
function OpenIntermediaryPopup(strUrl, strTarget)
{
var windowWidth, windowHeight, windowLeft, windowTop;
if(typeof window.screenX == "number" && typeof window.innerWidth == "number")
{
windowWidth = window.innerWidth * .68;
windowHeight = window.innerHeight * .68;
windowLeft = window.screenX + window.innerWidth * .16;
windowTop = window.screenY + window.innerHeight * .16;
}
else if(typeof window.screenTop == "number" && typeof document.documentElement.offsetHeight == "number")
{
windowWidth = document.documentElement.offsetWidth * .68;
windowHeight = document.documentElement.offsetHeight * .68;
windowLeft = window.screenLeft + document.documentElement.offsetWidth * .16;
windowTop = window.screenTop - 50;
}
else
{
windowWidth = 500;
windowHeight = 250;
windowLeft = 60;
windowTop = 40;
};
if (WindowObjectReferenceOfIntermediaryPopup == null || WindowObjectReferenceOfIntermediaryPopup.closed)
{
WindowObjectReferenceOfIntermediaryPopup = window.open(strUrl, strTarget, "top=" + windowTop + ",left=" + windowLeft + ",width=" + windowWidth + ",height=" + windowHeight + ",menubar,toolbar,location,resizable,scrollbars,status");
}
else
{
if(WindowObjectReferenceOfRequestedPopup.focus)
  {
  WindowObjectReferenceOfRequestedPopup.focus();
  };
};
}
function BringRequestedPopupOnTop()
{
if(WindowObjectReferenceOfRequestedPopup != null && !WindowObjectReferenceOfRequestedPopup.closed)
{
if(WindowObjectReferenceOfRequestedPopup.focus)
  {
  WindowObjectReferenceOfRequestedPopup.focus();
  };
};
}

/* Share buttons */
$(function() {
    $('#share-tools-wrapper').fadeIn(500);
    var win_width = $(window).width();
    var shareTop = $('#share-tools-wrapper').offset();
    var shareTop_top = shareTop.top - 10;

    if (win_width < "1120") {
        $('#share-tools-wrapper').css({
            position: "relative",
            float: "left",
            marginLeft: "0"
        });
        $('#share-tools-wrapper').removeClass('vertical-share');
        $('#share-tools-wrapper').addClass('horizontal-share');

    }
    $(window).resize(function() {
        var win_width = $(window).width();
        var scroll = $(window).scrollTop();

        if (win_width <= "1120") {
            $('#share-tools-wrapper').css({
	            position: "relative",
	            float: "left",
	            marginLeft: "0",
							top: "0"
            });
            $('#share-tools-wrapper').removeClass('vertical-share');
            $('#share-tools-wrapper').addClass('horizontal-share');
        } else if (win_width > "1120" && scroll > shareTop_top) {
            $('#share-tools-wrapper').css({
                position: "fixed",
                top: "10px",
                marginLeft: "-75px"
            });
            $('#share-tools-wrapper').removeClass('horizontal-share');
            $('#share-tools-wrapper').addClass('vertical-share');
        } else if (win_width > "1120") {
            $('#share-tools-wrapper').css({
                position: "absolute",
                marginLeft: "-75px",
								top: "50px"
            });
            $('#share-tools-wrapper').removeClass('horizontal-share');
            $('#share-tools-wrapper').addClass('vertical-share');
        }

    });

    // Float menu
    $(window).scroll(function() {
        var win_width = $(window).width();
        var scroll = $(window).scrollTop();
        if (win_width > "1120") {
            if (scroll >= shareTop_top) {
                $('#share-tools-wrapper').css({
                    position: "fixed",
                    top: "10px"
                });
            } else {
							$('#share-tools-wrapper').css({
                  position: "absolute",
									top: "50px"
              });
            }

        }
    });

    var location = window.location.href;
    location = location.replace(/\#([A-Za-z0-9-]+)/, '');
    $.ajax({
        type: 'GET',
        url: "http://api.facebook.com/restserver.php?method=links.getStats&format=json&urls=" + location,
        dataType: 'jsonp',
        success: function(json) {
            $.each(json,
            function(i, items) {
                var count = items["share_count"];
                //$('<div class="count">Facebook (' + count + ')</div>').appendTo('#facebook-share');
								$('#facebook-share').html('<div class="count">Facebook (' + count + ')</div>');
            });
        }
    });
    $.ajax({
        type: 'GET',
        dataType: "jsonp",
        url: "http://api.tweetmeme.com/url_info.jsonc?url=" + location,
        success: function(json) {
            var status = json["status"];
            if (status == "failure") {
                //$('<div class="count">Retweet (0)</div>').appendTo('#tweetmeme-share');
								$('#tweetmeme-share').html('<div class="count">Retweet (0)</div>');
            } else {
                var json = json["story"];
                var count = json["url_count"];
                //$('<div class="count">Retweet (' + count + ')</div>').appendTo('#tweetmeme-share');
								$('#tweetmeme-share').html('<div class="count">Retweet (' + count + ')</div>');
            }
        }
    });
    $.ajax({
        type: 'GET',
        dataType: 'jsonp',
        url: "http://services.digg.com/1.0/endpoint?method=story.getAll&type=javascript&link=" + location,
        success: function(json) {
            var count = json["count"];
            if (count == 0 | count == undefined) {
                //$('<div class="count">Digg (0)</div>').appendTo('#digg-share');
								$('#digg-share').html('<div class="count">Digg (0)</div>');
            } else {
                var diggs = json["stories"][0]["diggs"];
                //$('<div class="count">Digg (' + diggs + ')</div>').appendTo('#digg-share');
								$('#digg-share').html('<div class="count">Digg (' + diggs + ')</div>');
            }
        }
    });
    $('#stumbleupon-share').html('<div class="count">Stumble</div>');

    var shortndUrl;

    function shortnUrl(location, btnType) {
        $.getJSON('http://su.pr/api/shorten?longUrl='+location+'&format=jsonp&login=chicagonow&apiKey=bc86d64f89a4ea8ad705e7a51e4b14b5&callback=?',
        function(data) {
            $.each(data.results,
            function(i, item) {
                shortndUrl = data.results[i].shortMsg;
                urlReady(btnType);
                return false;
            });
        });
    }

   function urlReady(btnType) {
				encoded_location = encodeURIComponent(location);
        if (btnType == 'twitter') {
            new_url = 'http://twitter.com/home/?status=RT @' + retweet_source + ': ' + post_title + ' ' + shortndUrl;
            window.location = new_url;
        } else if (btnType == 'digg') {
            new_url = 'http://digg.com/submit?url=' + encoded_location + '&title=' + post_title;
            $('.digg').attr('href', new_url);
        } else if (btnType == 'facebook') {
            new_url = 'http://www.facebook.com/sharer.php?u=' + encoded_location + '&t=' + post_title;
            $('.facebook').attr('href', new_url);
        } else if (btnType == 'stumbleupon') {
            new_url = 'http://www.stumbleupon.com/submit?url=' + encoded_location + '&title=' + post_title;
            $('.stumbleupon').attr('href', new_url);
        }
    }

    $('.twitter').click(function() {
        shortnUrl(location, 'twitter');
    });
    $('.digg').ready(function() {
        urlReady('digg');
    });
    $('.facebook').ready(function() {
        urlReady('facebook');
    });
    $('.stumbleupon').ready(function() {
        urlReady('stumbleupon');
    });

});
