/**
* EAShare
*
* @param {String|HTMLElement} shareSiteCode			A token for the specific sharing site, or an HTML Element whose class name matches such a token.
* @param {String} [shareURL]						The URL for the item to share
* @param {String} [shareTitle]						The desired title for the item to share
* @param {String} [shareDesc]						The text description desired for the item to share
* @param {String} [shareMySpaceHTML]				HTML descrption for the item to share. Used only for MySpace.
*
* @example
* EAShare( "share-digg" );
*
* @example
* EAShare( "share-facebook" , "http://nfs09.ea.com/" , "Need For Speed Undercover" , "The latest Need For Speed game" , "The latest <strong>Need For Speed</strong> game" );
*/

function EAShare( shareSiteCode )
{
	var shareURL = "";
	var shareTitle = "";
	var shareDesc = "";
	var shareMySpaceHTML = "";
	var finalShareURL = "";
	var shareSiteString;
	
	// set info from global JS variables
	if( window.NFSDefaultSharingURL ) 				shareURL = window.NFSDefaultSharingURL;
	if( window.NFSDefaultSharingTitle ) 			shareTitle = window.NFSDefaultSharingTitle;
	if( window.NFSDefaultSharingDescription ) 		shareDesc = window.NFSDefaultSharingDescription;
	if( window.NFSDefaultSharingMySpaceHTML ) 		shareMySpaceHTML = window.NFSDefaultSharingMySpaceHTML;
	
	// override info from function params
	if( arguments[1] && ( arguments[1] != "" || arguments[1] != null ) )		shareURL = arguments[1];
	if( arguments[2] && ( arguments[2] != "" || arguments[2] != null ) )		shareTitle = arguments[2];
	if( arguments[3] && ( arguments[3] != "" || arguments[3] != null ) )		shareDesc = arguments[3];
	if( arguments[4] && ( arguments[4] != "" || arguments[4] != null ) )		shareMySpaceHTML = arguments[4];
	
	
	// set url from window.location if undefined
	if( !( window.NFSDefaultSharingURL ) && window.location )		shareURL = window.location;
	
	// fix lang code in URL, if missing
	new RegExp(/(\?|\&)(lang=[a-zA-Z\_]{2,5})?/ig).test( shareURL );
	
	if( RegExp.$2 == "" )
	{
		qPrepend = ( RegExp.$1 == "?" ) ? "&" : "?";
		shareURL = shareURL + qPrepend + "lang=" + window.NFSDefaultLocalizationLanguageCode + "&region=" + window.NFSDefaultLocalizationRegionCode;
	}
	
	
	
	// set title from <title> tag if undefined
	if( ( shareTitle == "" || shareTitle == null ) && document.title )		shareTitle = document.title;
	
	if( ( shareDesc == "" || shareDesc == null ) )
	{
		var metaDescriptionTags = document.getElementsByName( "Description" );
		
		if( metaDescriptionTags && metaDescriptionTags.item(0) && metaDescriptionTags.item(0).content )
		{
			shareDesc = metaDescriptionTags.item(0).content;
		}
	}
	
	if( shareMySpaceHTML == "" )	shareMySpaceHTML = shareDesc;
	
	// if called from an in-page element, use the class name of that element
	var shareSiteCode = ( shareSiteCode.className ) ? shareSiteCode.className : shareSiteCode;
	
	switch( shareSiteCode )
	{
		case "share-e-mail":
			shareSiteString = "mailto:?subject=%TOKEN_TITLE%&body=%TOKEN_URL%";
			break;
			
		case "share-facebook":
			shareSiteString = "http://www.facebook.com/sharer.php?u=%TOKEN_URL%&t=%TOKEN_TITLE%";
			break;
			
		case "share-digg":
			shareSiteString = "http://digg.com/remote-submit?phase=2&url=%TOKEN_URL%&title=%TOKEN_TITLE%&bodytext=%TOKEN_DESCRIPTION%";
			break;
		
		case "share-delicious":
			shareSiteString = "http://del.icio.us/post?v=4&noui&jump=close&url=%TOKEN_URL%&title=%TOKEN_TITLE%&bodytext=%TOKEN_DESCRIPTION%";
			break;
		
		case "share-myspace":
			// defaults to blog
			shareSiteString = "http://www.myspace.com/Modules/PostTo/Pages/?t=%TOKEN_TITLE%&c=%TOKEN_MYSPACE_HTML%&u=%TOKEN_URL%&l=1";
			break;
		
		case "share-n4g":
			shareSiteString = "http://www.n4g.com/tips.aspx?url=%TOKEN_URL%&title=%TOKEN_TITLE%";
			break;
	}
	
	if( shareSiteString != "" )
	{	
		shareSiteString = shareSiteString.replace( /\%TOKEN_URL\%/g , encodeURIComponent( shareURL) );
		shareSiteString = shareSiteString.replace( /\%TOKEN_TITLE\%/g , encodeURIComponent( shareTitle ) );
		shareSiteString = shareSiteString.replace( /\%TOKEN_DESCRIPTION\%/g , encodeURIComponent( shareDesc ) );
		shareSiteString = shareSiteString.replace( /\%TOKEN_MYSPACE_HTML\%/g , encodeURIComponent( shareMySpaceHTML ) );
	}
	
	if( shareSiteCode == "share-e-mail" )
	{
		window.location = shareSiteString;
	} else {
		window.open( shareSiteString );
	}

	
	
/*	//FIXXX: DEBUG
	alert( 
		"URL:\t" + shareURL +
		"\nTitle:\t" + shareTitle +
		"\nDescription:\t" + shareDesc +
		"\nHTML:\t" + shareMySpaceHTML
	);*/	
}

function EAShareFullParams( shareSiteCode , shareURL, shareTitle, shareDesc, shareMySpaceHTML )
{
	return EAShare( shareSiteCode , shareURL, shareTitle, shareDesc, shareMySpaceHTML );
}