/* This function will perform an awe.sm-powered share to Twitter, 
 * Facebook, MySpace, or LinkedIn using values you specify.
 * 
 * Here's how to use it on your site:
 * 1) Specify your awe.sm API Key on the line below defining var awesm_api_key
 *
 * 2) Upload this file to a public directory on your server
 *
 * 3) Add the following code to any pages in which you want to use the 
 *    function (before the lines on which you want to use it):
 *    	<script src="{URL of this file on your server}"></script>
 *
 * 4) Call the function via the onclick tag, as such:
 *    	<a href="javascript:void(0);" onclick="awesm_share('twitter',
 *	'Page URL', 'Page Title', 'Tweet Content (<120 chars)');">Share 
 *    	on Twitter</a>  
 *
 * A recognized channel ('twitter', 'facebook','myspace', or 'linkedin') is
 * required, the other variables (url, title, content) are optional.
 * 
 * If you want to include single quotes (') or backslashes (\) in the title
 * or content fields, you must escape them with \ (e.g. 'Men\'s Pants'). [You
 * can use addslashes() in PHP - http://php.net/manual/en/function.addslashes.php]
 *
 * If you want to include double quotes (") in the title or content fields,
 * you must use &quot; instead (e.g. 'He said &quot;That/'s nuts!&quot;').
 * [If you use PHP addslashes(), be sure to escape the double quotes first]
 */

function awesm_share(channel, url, title, content) {

	/* You can optionally specify a 'Source' value for LinkedIn (e.g.
         * the name of your site). If left blank, it will be user-defined.
	 */
	var linkedin_source = '';
			
	/* This value corresponds to Tool/create_type on awe.sm. You can
	 * optionally change it to something more specific to your site.
	 * However, please use the recommended construction of {site}-{tool}.
	 */
	var awesm_tool = '02lPC6' // same as Awesm::MANUAL_SHARE
			
	/* This function is used to search for awe.sm Parent parameters in the
	 * current page URL. You must have this feature enabled in your awe.sm
	 * Account Settings for the awe.sm Parent parameters to be present.
	 */
	var getUrlVars = function() {
  		var vars = [], hash;
  		var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
 		for(var i = 0; i < hashes.length; i++) {
	    		hash = hashes[i].split('=');
	    		vars.push(hash[0]);
	    		vars[hash[0]] = hash[1];
	  	}
	  	return vars;
	}
			
	/* If no URL is specified, use the current page address */
	if (typeof(url) == 'undefined' || url == '') {
		url = encodeURIComponent(location.href);
	} else {
		url=encodeURIComponent(url);
	}
		
	/* If no title is specified, use the current page title */
	if (typeof(title) == 'undefined' || title == '') {
		title = encodeURIComponent(document.title);
	} else {
		title=encodeURIComponent(title);
	}
			
	/* If no content is specified, use title */
	if (typeof(content) == 'undefined' || content == '') {
		content = title;
	} else {
		content=encodeURIComponent(content);
	}			
		
	/* Configure the channel-specific information */
	switch (channel) {
		case 'twitter':
  			var awesm_channel = 'twitter-post';
			/* Make sure the content is <120 characters so there's room for the URL */
			if (decodeURIComponent(content).length > '120') {
				content = decodeURIComponent(content).substring(0,117) + '...'; 
				content = encodeURIComponent(content);
			}
			var awesm_destination = 'http://twitter.com/intent/tweet?text=' + content + '&url=AWESM_TARGET&via=Plancast';
			var window_specs = '';
  			break;
		case 'facebook':
  			var awesm_channel = 'facebook-share';
			var awesm_destination = 'http://www.facebook.com/sharer.php?u=AWESM_TARGET&t=' + title;
			var window_specs = 'toolbar=0, status=0, width=626, height=436';
  			break;
		case 'gbuzz':
			var awesm_channel = 'google-buzz-post';
			var awesm_destination = 'http://www.google.com/buzz/post?url=AWESM_TARGET&message=' + content;
			var window_specs = 'resizable=0, scrollbars=0, width=690, height=415';
  			break;
		case 'myspace':
			var awesm_channel = 'myspace';
			var awesm_destination = 'http://www.myspace.com/index.cfm?fuseaction=postto&u=AWESM_TARGET&t=' + title + '&c=' + content;
			var window_specs = 'toolbar=0, status=0, width=825, height=725';
  			break;
		case 'linkedin':
			var awesm_channel = 'linkedin';
			var awesm_destination = 'http://www.linkedin.com/shareArticle?mini=true&url=AWESM_TARGET&title=' + title + '&summary=' + content;
			if (linkedin_source != '') {
				awesm_destination += '&source=' + encodeURIComponent(linkedin_source);
			}
			var window_specs = 'toolbar=0, status=0, width=550, height=400';
  			break;
		case 'email':
			var awesm_channel = 'email';
			var awesm_destination = 'mailto:?subject=' + title + '&body=' + content + '%20AWESM_TARGET';
			var window_specs = 'toolbar=0, status=0, width=300, height=200';
  			break;
	}
			
	/* Construct the URL for the awe.sm Share API (see http://developers.awe.sm) */
	var awesm_shareit = 'http://api.awe.sm/url/share?v=2&key=' + awesm_api_key +
			    '&url=' + url + 
			    '&channel=' + awesm_channel + 
			    '&tool=' + awesm_tool + 
			    '&destination=' + encodeURIComponent(awesm_destination) +
			    '&notes=' + session_user_id; 
			
	/* Check for an awe.sm Parent parameter in the current page URL, and
	 * add it to the awe.sm Share API URL if present. You must have this
	 * feature enabled in your awe.sm Account Settings for the awe.sm
	 * Parent parameter to be present.
	 */
	var pageParams = getUrlVars();
	if ( pageParams['awesm'] ) {
		awesm_shareit += '&awesm_parent=' + pageParams['awesm'];
	}

	/* Launch the share UI in a new window of the correct size*/
	var awesm_popup = window.open(awesm_shareit, '_blank', window_specs);
	
	// Moves the popup to the center of the screen
  var x = Math.ceil((document.viewport.getWidth()/2)-350);
  var y = Math.ceil(200);
  awesm_popup.moveTo(x,y);
}
