		var pageURL;
		var pageURLShort;
		var pageTitle;
		var domainName = 'www.stephenradl.com';
		var socialMediaTitle = "Stephen Radl Painting";
		var socialMediaExtraTitle = '';
		var socialMediaExtraDescription = '';
		var socialMediaDescription = ""; 
		var socialMediaImage = '';
		
		$(document).ready(function() {
/*
			$("#share-link").showPopup({
				'onStart':shareWindow,
				'onClose':popupClose
			});
			$("#follow-link").showPopup({
				'onStart':popupStart,
				'onClosed':popupClose
			});	
*/

			$("#share-link").click(function (e) {
				e.preventDefault();
				var element = $($(this).attr("href"));
				shareWindow(element)
			});

			$("#follow-link").click(function (e) {
				e.preventDefault();
				var element = $($(this).attr("href"));
				showPopup(element);
			});

			$("#overlay").click(function (e) {
				e.preventDefault();
				hidePopup();
			});

			$(".close").click(function (e) {
				e.preventDefault();
				hidePopup();
			});
	
		});
		
		function showPopup(elem) {
		
			$('#share-window').hide();
			$('#follow-window').hide();
			$('#overlay').show();
			if ($('#liveBooks') != undefined) $('#liveBooks').css({visibility: "hidden"});
			elem.show();
			var top = ($(window).height()-elem.height()) / 2;
			var left = ($(window).width()-elem.width()) / 2;
			elem.css("top",top + "px").css("left",left+"px");
		}
		
		function hidePopup() {
			$('#share-window').hide();
			$('#follow-window').hide();
			$('#overlay').hide();
			if ($('#liveBooks') != undefined) $('#liveBooks').css({visibility: "visible"});
		}

		function getShortURL(url) {
			if(url != null)
				return (url.indexOf('http://') != -1) ? url.substr(7) : url;
			else
				return '';
		}
		
		function shareWindow(element) {
			
			pageTitle = encodeURIComponent(document.title);
			pageURL = location.href;
			pageURLShort = pageURL;
			pageURLVeryShort = getShortURL(pageURLShort);

			getHtmlURL(pageURL);

			showPopup(element);
		}

		function getHtmlURL(longUrl) {
			var getHtmlThread = $.ajax({
				url:"http://" + domainName + "/sources/html/page_url.php",
				data:{u:longUrl},
				dataType:"json",
				success:function(data) {
					try {
						var response = eval(data);
						if(longUrl != response.url) {
							pageURL = response.url;
							pageURLShort = pageURL;
							pageURLVeryShort = getShortURL(pageURLShort);
							if(response.thumbnail != null) {
								socialMediaImage = response.thumbnail;
							}
							if(response.extratitle != null) {
								socialMediaExtraTitle = response.extratitle;	
							}
							if(response.extradescription != null) {
								socialMediaExtraDescription = response.extradescription;	
							}
						} else {
							socialMediaExtraTitle = '';
							socialMediaExtraDescription = '';
							socialMediaImage = '';
						}
						changeFacebookLink();
						changeLinks();
						bitly();
					} catch (err) {}
				},
				error:function(jqXHR, textStatus, errorThrown) {
					changeFacebookLink();
					changeLinks();
					bitly();
				}
			});
			
		}
		
		function bitly() 
		{ 
			var url=pageURL;
			var username="livebookscom";
			var key="R_50e71f32358aaa4d4f69346863b51639";
			var bitlyThread = $.ajax({
				url:"http://api.bit.ly/v3/shorten",
				data:{longUrl:url,apiKey:key,login:username},
				dataType:"jsonp",
				success:function(v) {
					if(v.status_code == 200) {
						pageURLShort = v.data.url;
						pageURLVeryShort = getShortURL(pageURLShort);
						changeLinks();
					}
				}
			});
		}		
		
		function changeFacebookLink() {
			var link;
			link = "http://www.facebook.com/plugins/like.php?href="+encodeURIComponent(pageURLShort)+"&layout=standard&show_faces=false&width=360&action=like&colorscheme=light&height=35";
			$('#facebook-link').attr("src",link);
		}
		
		function changeLinks() {
			var link;

			link = "http://twitter.com/intent/tweet?text=" + encodeURIComponent(socialMediaTitle + ((socialMediaExtraTitle != '') ? " - " + socialMediaExtraTitle : '') + " -") + "&url=" + pageURLShort;
			$('#twitter-link').attr("href",link);
			
			var linkedinDescription = socialMediaDescription;
			var imageFullDescription = '';
			if(socialMediaExtraTitle != '') {
				linkedinDescription = socialMediaExtraTitle;
				imageFullDescription = socialMediaExtraTitle;
			}
			if(socialMediaExtraDescription != '') {
				linkedinDescription = socialMediaExtraDescription;
				imageFullDescription = ((imageFullDescription != '') ? imageFullDescription + ' - ' : '') + socialMediaExtraDescription;
			}
				
			link = "http://www.linkedin.com/shareArticle?mini=true&url="+encodeURIComponent(pageURLShort)+"&title=" + encodeURIComponent(socialMediaTitle) + "&summary=" + encodeURIComponent(((imageFullDescription != '') ? imageFullDescription : linkedinDescription));
			$('#linkedin-link').attr("href",link);

			var tumblrTitle = socialMediaTitle;
			if(socialMediaExtraTitle != '') {
				tumblrTitle += ((tumblrTitle != '') ? ' - ' + socialMediaExtraTitle : socialMediaExtraTitle);
			}
			if(socialMediaExtraDescription != '') {
				var tumblrDescription = socialMediaExtraDescription;
			} else {
				var tumblrDescription = socialMediaDescription;
				if(socialMediaExtraTitle != '')
					tumblrDescription = '';
			}
			link = "http://www.tumblr.com/share?v=3&u="+encodeURIComponent(pageURLShort)+"&t="+cleanText(tumblrTitle, true)+"&s="+cleanText(encodeURIComponent(tumblrDescription), true);
			$('#tumblr-link').attr("href",link);

			link = "http://www.google.com/buzz/post?url="+encodeURIComponent(pageURLShort)+"&message="+encodeURIComponent(socialMediaTitle);
			if(socialMediaImage != '')
				link += '&imageurl='+socialMediaImage;
			$('#buzz-link').attr("href",link);

			link = "http://www.typepad.com/services/quickpost/post?v=2&qp_show=ac&qp_href="+encodeURIComponent(pageURLShort)+"&qp_title="+encodeURIComponent(socialMediaTitle)+"&qp_text=" + encodeURIComponent(((imageFullDescription != '') ? imageFullDescription : linkedinDescription));
			$('#typepad-link').attr("href",link);

			link = "http://www.blogger.com/blog_this.pyra?n="+encodeURIComponent(socialMediaTitle)+"&u="+encodeURIComponent(pageURLShort)+"&l&t=" + cleanText((imageFullDescription != '') ? imageFullDescription : linkedinDescription);
			$('#blogger-link').attr("href",link);

			var emailDescription = socialMediaTitle;
			if(socialMediaExtraDescription != '') {
				emailDescription += "\n\n" + socialMediaExtraDescription;
			} else if (socialMediaDescription != '') {
				emailDescription += "\n\n" + socialMediaDescription;
			}
			emailDescription += "\n\n" + pageURLShort;
			link = "mailto:?subject=" + cleanText(socialMediaTitle, true) + "&body="+encodeURIComponent(emailDescription);
			$('#email-link').attr("href",link);

		}

		function cleanText(value, encode) {
			if(encode == null) encode = true;		
			value = value.replace(/[%]+/g, ((encode) ? '%25' : ''));
			value = value.replace(/[&]+/g, ((encode) ? '%26' : ''));
			value = value.replace(/[#]+/g, ((encode) ? '%23' : ''));
			return value;
		}
