/**
* Returns the content value of a META tag for a given name.
*/

function getmetaContents(mn){ 
  var m = document.getElementsByTagName('meta'); 
  for(var i in m){ 
   if(m[i].name == mn){ 
     return m[i].content; 
   } 
  } 
}



/**
* Provides an easy method for sharing the current page over social networks extracting from META tags
*
* @param {String} socialSite Name of the target sharing site - currently facebook, twitter, myspace, digg, delicious, stumbleupon or reddit. Will use e-mail if none is defined.
* @param {String} urlAppend Optional. Any additional parameters you need to append to the URL string (useful for lightboxes).
* @param {Boolean} newWindow Optional. Determines whether the link opens in a new window. Default value is true.
*
*/

function shareLink(socialSite, urlAppend, newWindow, baseUrl, baseTitle, baseDesc) {
	
	if (newWindow === undefined) {newWindow = true;}
	if (urlAppend === undefined) {urlAppend = '';} else {urlAppend = "&"+urlAppend;}
	pageTitle = (baseTitle === undefined) ? encodeURIComponent(getmetaContents('title')) : encodeURIComponent(baseTitle);
	pageDesc = (baseDesc === undefined) ? encodeURIComponent(getmetaContents('description')) : encodeURIComponent(baseDesc);
	pageUrl = (baseUrl === undefined) ?  encodeURIComponent (document.location.href) : baseUrl;

	switch (socialSite.toLowerCase()) {
		case "facebook": link = 'http://www.facebook.com/sharer.php?u='+pageUrl+'&t='+pageTitle+urlAppend; break;
		case "twitter": link = 'http://twitter.com/home?status='+pageTitle+': '+pageUrl; break;
		case "myspace": link = 'http://www.myspace.com/Modules/PostTo/Pages/?l=3&u='+pageUrl+'&t='+pageTitle+'&c='+pageDesc+urlAppend; break;
		case "digg": link = 'http://digg.com/submit?phase=2&url='+pageUrl+'&title='+pageTitle+urlAppend; break;
		case "delicious": link = 'http://delicious.com/save?url='+pageUrl+'&title='+pageTitle+urlAppend; break;
		case "stumbleupon": link = 'http://www.stumbleupon.com/submit?url='+pageUrl+'&title='+pageTitle+urlAppend; break;
		case "reddit": link = 'http://reddit.com/submit?url='+pageUrl+'&title='+pageTitle+urlAppend; break;
		default: link = 'mailto:?subject='+pageTitle+'&body='+pageDesc+'%0A%0A'+pageUrl;
	}

	//add tracking pixels to page, delete this when reusing;
	trackShare();
	
	//alert(link);
	if (newWindow) {
		window.open(link, '_blank');
	} else {
		window.location.href = link;
	}
}


/**
* Function for displaying tracking pixels on screen
*/
function trackShare() {
	$("#trackingpixels").html('<span class="trtags"><img src="http://ad.afy11.net/ad?mode=2&mgId=1000000323015&ac=3&av=0&cMode=0&rand='+getR()+'" width="1" height="1" /><img src="http://ad.afy11.net/ad?mode=2&mgId=1000000344115&ac=3&av=0&cMode=0&rand='+getR()+'" width ="1" height="1" /><img src="http://ad.afy11.net/ad?mode=2&mgId=1000000344015&ac=3&av=0&cMode=0&rand='+getR()+'" width ="1" height="1" /><img src="http://ad.afy11.net/ad?mode=2&mgId=1000000416215&ac=1&av=0&cMode=0&rand='+getR()+'" width ="1" height="1" /></span>');
}

/* Function to return timestamp as a number
*/
function getR() {
	return new Date().getTime();
}
