/*
 * Social Network Sharing API Integration Kit
 * from Interfolio - www.interfolio.com
 * 
 * Special thanks to Dominic O'Connor who originally developed this for internal use
 * 
 * 
 */
function sharePage(destination,shareText,userId,overrideUrl){
	//api url
	var destinationurl = "";
	//url to share
	var theurl = window.location.href;
	//optionally override the detected url with the passed one
	if (overrideUrl) {
		theurl = overrideUrl;
	}
	//encoded version of url, page title and description
	var encodedurl = encodeURIComponent(theurl);
	var pagetitle = encodeURIComponent(document.title);
	var pagedescription = encodeURIComponent(document.title);
	//default window size and options
	var windowwidth = 0;
	var windowheight = 0;
	var windowoptions = "";
	
	//set default custom share text
	if (!shareText) {
		var shareText = 'Hi! I thought you might be interested in this';
	}
	//optionally log a user ID, ip address or other identifier
	if (!userId) {
		var userId = '';
	}
	
	switch(destination){
		case 'email':
			var daReferrer = document.referrer;
			var email = "";
			var subject = document.title;
			var body_message = shareText + "\n\n" + theurl;
			var mailto_link = 'mailto:'+email+'?subject='+encodeURIComponent(subject)+'&body='+encodeURIComponent(body_message);
			
			win = window.open(mailto_link,'emailWindow');
			if (win && win.open &&!win.closed) win.close();
			logShare('email', mailto_link, userId);
		break;
		case 'facebook':
			destinationurl='http://www.facebook.com/sharer.php?u=' + encodedurl + '&t=' + pagetitle;
			windowwidth = 626;
			windowheight = 436;
		break;
		case 'linkedin':
			destinationurl='http://www.linkedin.com/shareArticle?mini=true&url=' + encodedurl + '&title=' + pagetitle + '&summary=' + pagedescription;		
			windowwidth = 520;
			windowheight = 570;
		break;
		case 'delicious':
			destinationurl='http://delicious.com/save?v=5&noui&jump=close&url='+encodedurl+'&title='+pagetitle;
			windowwidth=550;
			windowheight=550;
		break;
		case 'digg':
			destinationurl='http://digg.com/submit?url='+encodedurl+'&title='+pagetitle+'&description='+Left(pagedescription,350);
			windowoptions=",scrollbars=1,resizable=1";
		break;
		case 'googlebookmarks':
			destinationurl='http://www.google.com/bookmarks/mark?op=edit&output=popup&bkmk="+'+encodedurl+'&title='+pagetitle;
			windowwidth=600;
			windowheight=500;
		break;
		case 'myaol':
			destinationurl='http://favorites.my.aol.com/ffclient/AddBookmark?url='+encodedurl+'&title='+pagetitle+'&favelet=true';
			windowwidth=770;
			windowheight=600;
		break;
		case 'twitter':
			destinationurl='http://twitter.com/home?status='+encodedurl;
			windowwidth=800;
			windowheight=400;
			windowoptions=",scrollbars=1,resizable=1";
		break;
		case 'yahoobookmarks':
			destinationurl='https://login.yahoo.com/config/login?.src=bmk2&.intl=us&.done=' + encodeURIComponent('http://bookmarks.yahoo.com/toolbar/savebm?opener=tb&') +encodedurl+'&t='+pagetitle;
			windowwidth=550;
			windowheight=400;
			windowoptions=",scrollbars=1,resizable=1";
		break;		
	}
	if (destinationurl.length){
		//open the third party login window
		window.open(destinationurl,'Publish','toolbar=0,status=0,width=' + windowwidth + ',height=' + windowheight + windowoptions);
		//log the share
		logShare(destination, window.location.pathname, userId);
	}
	return false;
}


//Log the share through cfajaxproxy
function logShare(method,pageUrl,userId){
	var sp = new shareProxy();
	sp.setCallbackHandler(onSuccessHandler);
	sp.setErrorHandler(onErrorHandler);
	sp.logShare(method,pageUrl,userId);
}

//Confirmation/success handler
function onSuccessHandler(result) {
	if (result) {
		alert('Log in and submit your entry to share this page.');
	}
}
//Error handler
function onErrorHandler(errCode,status) {
	alert('Sorry, there was a problem sharing the page.\n Error Code: ' + errCode + 'Message: ' + status);
}


//Utility
function Left(str, n)
{
   if (n <= 0)
         return "";
   else if (n > String(str).length)
         return str;
   else
         return String(str).substring(0,n);
}

