if( !shareModule ) var shareModule = {
	
	sendLock : false,
	emailsDefaultMessage: "Enter emails separated by commas"
};


shareModule.friendfeed = function( context )
{
	shareModule.handleContext( context );
	var url = "http://friendfeed.com/?url=" + encodeURIComponent(shareModule.getSharedURL())+ "&title=Jinni: " + encodeURIComponent(document.title);
	window.open(url,'jinni');
}

shareModule.delicious = function( context )
{

	shareModule.handleContext( context );
	var url = "http://del.icio.us/post?url="+encodeURIComponent(shareModule.getSharedURL())+"&title=Jinni: " +encodeURIComponent( document.title );

	window.open(url,'jinni');
	
}
shareModule.myspace = function( context )
{
	shareModule.handleContext( context );
	
	var targetUrl = "http://www.myspace.com/Modules/PostTo/Pages/?t=Jinni: " + encodeURIComponent( document.title ) + "&u=" + encodeURIComponent(shareModule.getSharedURL());
	window.open(targetUrl,'jinni');
}
shareModule.facebook = function( context )
{
	shareModule.handleContext( context );
	
	var pageTitle = "Jinni";
	if( context == 'invite')
		window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent("http://www.jinni.com/"),'sharer','toolbar=0,status=0,width=626,height=436');
	else if( context == 'movie')
		window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent("http://www.youtube.com/watch%3Fv%3Dq1Wbw9sZIus&t=Jinni%20is%20%22Pandora%20for%20Movies%22"),'sharer','toolbar=0,status=0,width=626,height=436');
	else if( context == 'registrationActivation')
		window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent("http://www.jinni.com/"),'sharer','toolbar=0,status=0,width=626,height=436');
	else {
		var pageURL = shareModule.getSharedURL();
		window.open('http://www.facebook.com/sharer.php?u=' + encodeURIComponent(pageURL)+ '&t=' + encodeURIComponent(pageTitle),'sharer','toolbar=0,status=0,width=626,height=436');
	}
}

shareModule.twitter = function(  context )
{
	shareModule.handleContext( context );
	
	var pageURL = shareModule.getSharedURL();
	try{
	var twitterStatus = "Searching for '" + shareModule.getSearchDisplayString(true) + "' with @JinniDotCom " + pageURL;
	}catch(e){}
	var path = document.location.pathname;
	
	if( context == 'invite' )
	{
		twitterStatus = "I signed up free to @JinniDotCom. Click for a smart new way to get movies and TV shows http://www.jinni.com";
	}
	else if( context == "movie")
	{
		twitterStatus = "Watching a demo about finding movies & TV shows with @JinniDotCom http://www.jinni.com ";
	}
	else if( context == "registrationActivation")
	{
		twitterStatus = "I signed up free to @JinniDotCom. Click for a smart new way to get movies and TV shows http://www.jinni.com";
	}
	else if( context == "youtube_celeb")
	{
		twitterStatus = "bla bla bla";
	}
	else if(  path.startsWith("/movies/") || path.startsWith("/tv/") || path.startsWith("/shorts/") )
	{
		var reviewsPattern = /reviews$|reviews\/$/;
		var overviewPattern = / /;
		var castPattern = /cast-crew$|cast-crew\/$/;
		var photoPattern = /photos$|photos\/$/;
		var videosPattern = /videos$|videos\/$/;
		var watchPattern = /watch-it$|watch-it\/$/;
		
		if( reviewsPattern.test(path) )
			twitterStatus = "Reading reviews of '" + shareModule.getContentName() + "' with @JinniDotCom " + pageURL;
		
		else if( castPattern.test(path) )
			twitterStatus = "Reading about '" + shareModule.getContentName() + "' cast & crew with @JinniDotCom " + pageURL;
		
		else if( photoPattern.test(path) )
			twitterStatus = "Looking at '" + shareModule.getContentName() + "' photos with @JinniDotCom " + pageURL;
		
		else if( videosPattern.test(path) )
			twitterStatus = "Watching '" + shareModule.getContentName() + "' with @JinniDotCom " + pageURL;
		
		else if( watchPattern.test(path) )
			twitterStatus = "Watching '" + shareModule.getContentName() + "' with @JinniDotCom " + pageURL;
		
		else 
			twitterStatus = "Looking at '" + shareModule.getContentName() + "' with @JinniDotCom " + pageURL;	
			
	}
	else if( path.startsWith("/person/") )
	{
		var creditsPattern = /credits$|credits\/$/;
		var overviewPattern = / /;
		var photosPattern = /photos$|photos\/$/;
		var awardsPattern = /awards$|awards\/$/;
		var twitterStatus = "Looking at '" + shareModule.getName() + "' with @JinniDotCom " + pageURL;
		
		if( creditsPattern.test(path) )
			twitterStatus = "Looking at '" + shareModule.getName() + "'s credits with @JinniDotCom " + pageURL;
		
		else if( photosPattern.test(path) )
			twitterStatus = "Looking at photos of '" + shareModule.getName() + "' with @JinniDotCom " + pageURL;
		
		else if( awardsPattern.test(path) )
			twitterStatus = "Watching '" + shareModule.getName() + "'s awards with @JinniDotCom " + pageURL;
	}
	window.open('http://twitter.com/home?status=' + encodeURIComponent(twitterStatus),'jinni');
	
}

shareModule.stumbleupon = function( context )
{
	shareModule.handleContext( context );
	
	var pageURL = shareModule.getSharedURL();
	             
	window.open('http://www.stumbleupon.com/submit?url=' + encodeURIComponent(pageURL) + '&title=Jinni: The Movie Genom','jinni');
	
}

shareModule.email = function( context )
{
	shareModule.handleContext( context );
	
	var signedIn = false;
	if( globalVariables.isUserSignIn )
	{
		signedIn = true;
		
		if( !generalFunctions.isSignedIn() )
		{
			signedIn = false;
		}
	}
	
	 utils.dimThePage();
	 document.getElementById('shareByEmail').style.zIndex = "200";
	 
	
	if( !signedIn )
	{
		document.getElementById('shareEmailDiv').style.display = "block";
		document.getElementById('shareNameDiv').style.display = "block";
	}
	else
	{
		document.getElementById('shareEmailDiv').style.display = "none";
		document.getElementById('shareNameDiv').style.display = "none";
	}
	
	var rawSearchString =  shareModule.getSearchDisplayString(false);
	
	searchString = rawSearchString.length > 40 ? (rawSearchString.substr(0,40) + "...") : rawSearchString;
	document.getElementById('shareURL').value = shareModule.getSharedURL(); 
	document.getElementById('shareSearchTerm').innerHTML = rawSearchString;
	
	var filter = shareModule.getGlobalFilter();
	
	var filterPhrase = "&nbsp;";
	if( filter == "onlineFilter") filterPhrase = "In: Online";
	else if( filter == "shortsFilter") filterPhrase = "In: Shorts";
	else if( filter == "tvFilter") filterPhrase = "In: TV Shows";
	else if( filter == "movieFilter") filterPhrase = "In: Movies";
	
	
	document.getElementById('shareFilters').innerHTML = filterPhrase;

	var defaultMessage = shareModule.getMessageText( rawSearchString );
	
	document.getElementById('shareMessage').value = defaultMessage.replace(/<br\/>/g,'\n');
	
	document.getElementById('shareByEmail').className = "shareFrame";
	document.getElementById('shareByEmail').style.display = "block";
}

shareModule.getMessageText = function( searchString )
{
	var message = "";
	var path = document.location.pathname;
	if( document.location.pathname.startsWith("/discovery") )
	{
		message = "Hi, <br/>" +
			"I just searched for \"" + trim(searchString) + "\" on Jinni. Check it out.<br/><br/> " + 
			document.getElementById('shareURL').value + 
			"<br/><br/>" +
			document.getElementById('shareName').value;
		
	}
	else if( document.location.pathname.startsWith("/movies/") || document.location.pathname.startsWith("/shorts/") || document.location.pathname.startsWith("/tv/") )
	{
		var contentName = shareModule.getContentName();
		
		message = "Hi, <br/>" +
			"I am looking at \"" + contentName + "\" on Jinni. Check it out.<br/><br/> " + 
			document.getElementById('shareURL').value + 
			"<br/><br/>" +
			document.getElementById('shareName').value;
	}
	
	else if( document.location.pathname.startsWith("/person/") )
	{
		var contentName = shareModule.getName();
		var message = "Hi, <br/>" +
					  "I am looking at \"" + contentName + "\" on Jinni. Check it out.<br/><br/> " + 
					  document.getElementById('shareURL').value + 
					  "<br/><br/>" +
					  document.getElementById('shareName').value;
		
		var overviewPattern = / /;
		var creditsPattern = /credits$|credits\/$/;
		var photosPattern = /photos$|photos\/$/;
		var awardsPattern = /awards$|awards\/$/;
		
		if( overviewPattern.test(path) )
			message = "Hi, <br/>" +
			"I am looking at \"" + contentName + "\" on Jinni. Check it out.<br/><br/> " + 
			document.getElementById('shareURL').value + 
			"<br/><br/>" +
			document.getElementById('shareName').value;
		
		else if( creditsPattern.test(path) )
			message = "Hi, <br/>" +
			"I am looking at \"" + contentName + "\"' credits on Jinni. Check it out.<br/><br/> " + 
			document.getElementById('shareURL').value + 
			"<br/><br/>" +
			document.getElementById('shareName').value;		
		
		else if( photosPattern.test(path) )
			message = "Hi, <br/>" +
			"I am looking at photos of \"" + contentName + "\" on Jinni. Check it out.<br/><br/> " + 
			document.getElementById('shareURL').value + 
			"<br/><br/>" +
			document.getElementById('shareName').value;
		
		else if( awardsPattern.test(path) )
			message = "Hi, <br/>" +
			"I am looking at  \"" + contentName + "\"' awards on Jinni. Check it out.<br/><br/> " + 
			document.getElementById('shareURL').value + 
			"<br/><br/>" +
			document.getElementById('shareName').value;
		
		
		
		
	}
	return message;
}

shareModule.getContentName = function(  )
{
	return globalVariables.content.displayTitle;
}
shareModule.getName = function(  )
{
	return globalVariables.person.displayName;
}

shareModule.handleContext = function( context )
{
	if( context && context == "emailFrame")
	{
		shareModule.closeShareFrame();
	}
}
shareModule.handleClick = function()
{
	if( document.getElementById('shareTo').value == shareModule.emailsDefaultMessage )
	{
		document.getElementById('shareTo').value = "";
	}
}

shareModule.shareLink = function()
{
	shareModule.email();
	document.getElementById('shareByEmail').className = document.getElementById('shareByEmail').className + " shareLinkFrame";
	document.getElementById('shareByEmail').style.display = "block";
	
}

shareModule.shareByEmail = function()
{
	if( shareModule.sendLock ) return;
	
	var emailBox = document.getElementById('shareEmail');	
	var nameBox = document.getElementById('shareName');
	
	var message = document.getElementById('shareMessage').value;
	var email = emailBox.value;
	var to = document.getElementById('shareTo').value;
	var userName = nameBox.value;
	
	var url = document.getElementById('shareURL').value;	
	var searchOrContentPage = true;

	
	//validates emails
	function CheckMail1(shareTo){  
		var reg = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;  
		var address = to;
			if (address.search(reg) == -1 ){
				return true;  
			}
	}
	function CheckMail2(shareEmail){  
		var reg = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;  
		var address = email;
			if (address.search(reg) == -1 ){
				return true;  
			}
	}
	
	//validates username is of alpha numeric value
	function CheckAlphaNum(Str){
		var AlphaNum = Str;
		for(var j=0; j<AlphaNum.length; j++){
			var Char1 = AlphaNum.charAt(j);
			if( !( (Char1 >='0'&& Char1<='9') || (Char1 >= 'a' && Char1 <= 'z') || (Char1 >= 'A' &&	Char1<='Z') ) ){
				return false;
			}
		}
		return true;
	}
	function CheckUserName(shareName){
		if ( !CheckAlphaNum(document.getElementById('shareName').value) ){
			return true;
		}		
	}	
	
//	function CheckMessage(shareMessage){
//		if ( !CheckAlphaNum(document.getElementById('shareMessage').value) ){
//			return true;
//		}else ( document.getElementById('shareMessage').value == shareModule.emailsDefaultMessage ){
//		}
//	}
	
	if( document.location.pathname.startsWith("/movies/") || document.location.pathname.startsWith("/tv/") || document.location.pathname.startsWith("/shorts/"))
		searchOrContentPage = false;
	
	message = message.replace(/\n/g,'<br/>');
	
	if( trim(to) == "" || trim(email) == "" || trim(userName) == "" ){
		document.getElementById('errorMessage').innerHTML = "One field or more is empty";
		return;
	}
	
	if( trim(message) == "") return;
	
	if( trim(userName) == "" &&  document.getElementById('shareNameDiv').style.display != "none" ) return;
	
	if( trim(email) == "" &&  document.getElementById('shareEmailDiv').style.display != "none" ) return;
	
	if( trim(to) == "" ) return;
	
	else if (CheckMail1()){
		document.getElementById('errorMessage').innerHTML = "None of the recipients' emails are valid";
		return false;
	}
	else if (CheckMail2()){
		document.getElementById('errorMessage').innerHTML = "Your email address is not valid";
		return false;
	}
//	else if (CheckUserName()){
//		document.getElementById('errorMessage').innerHTML = "Please enter your name. Use letters and numbers only";
//		return false;
//	}
//	else if (CheckMessage()){
//		document.getElementById('errorMessage').innerHTML = "Please enter your message, Use letters and numbers only";
//		return false;
//	}
	shareModule.sendLock = true;
	
	document.getElementById('shareButton').className = "commandButton ajaxTransCommandButton";
	document.getElementById('shareButton').value = "Sending..."
	document.getElementById('shareByEmailLoading').className = "invitationLoading visible";
	
	var contentName = null;
	if( !searchOrContentPage )
		contentName = shareModule.getContentName();
	
	AjaxController.shareByEmail(message, email, to, userName, contentName, {
		
		callback: function( success ){ 
			
			document.getElementById('shareButton').className = "commandButton";
			document.getElementById('shareButton').value = "Send"
			document.getElementById('shareByEmailLoading').className = "invitationLoading invisible";
			document.getElementById('shareTo').value = shareModule.emailsDefaultMessage;
			shareModule.sendLock = false;
			
			document.getElementById('shareByEmail').style.display = "none";
			
			shareModule.openThanks('Success! Message sent.');
			utils.dimThePage();
			
			window.setTimeout(function(){
				shareModule.closeThanks();
				utils.unDimThePage();
			},5000);
			
			
			
		},
			
		errorHandler: function( error ){
			
			document.getElementById('shareButton').className = "commandButton";
			document.getElementById('shareButton').value = "Send"
			document.getElementById('shareByEmailLoading').className = "invitationLoading invisible";
			
			shareModule.sendLock = false;
			}
		
	});
}

shareModule.closeThanks = function() {
	document.getElementById('inlineInvitationDiv').className = document.getElementById('inlineInvitationDiv').className.replace(/visible/,'invisible');
	document.getElementById('thanksBox').className = document.getElementById('thanksBox').className.replace(/visible/,'invisible');
}

shareModule.openThanks = function(msg) {
	document.getElementById('inlineInvitationDiv').className = document.getElementById('inlineInvitationDiv').className.replace(/invisible/,'visible');
	document.getElementById('anonymousInviteStep').className = document.getElementById('anonymousInviteStep').className.replace(/\bvisible/,'invisible');
	document.getElementById('invitationMessageStep').className = document.getElementById('invitationMessageStep').className.replace(/\bvisible/,'invisible');
	
	document.getElementById('thanksBox').innerHTML = msg;
	document.getElementById('thanksBox').className = document.getElementById('thanksBox').className.replace(/\binvisible/,'visible');
}

shareModule.closeShareFrame = function()
{
	document.getElementById('shareByEmail').style.display = "none";
	utils.unDimThePage();
}

shareModule.getSharedURL = function()
{
	//TODO	condition for discover,content,cast pages.
	var url = "";
	var tinyURL = "";
	
	if((typeof currentSearchShortURL != 'undefined') && currentSearchShortURL != "")
	{
		tinyURL = currentSearchShortURL;
	}
	else
	{
		tinyURL = document.location.pathname + document.location.search;
	}
	return "http://www.jinni.com" + tinyURL;
}

shareModule.getTinyURI = function( url )
{
	var tinyURI = url;
	
	AjaxController.convertToTinyURL(url, {
					   
			async: 			false,
			callback: 		function( data ){ tinyURI = data;},
			errorHandler: 	function(){ tinyURI = url;}
			
		});
	
	return tinyURI;
}

shareModule.getSearchFilters = function()
{
	var params = "";
	/* story tuner */
	if( SearchFilter.tasteFilter )
	{
		if( SearchFilter.tasteFilter.obscure != -1 )
		{
			params += "&obscure=" + SearchFilter.tasteFilter.obscure;
		}
		if( SearchFilter.tasteFilter.pace != -1 )
		{
			params += "&pace=" + SearchFilter.tasteFilter.pace;
		}
		if( SearchFilter.tasteFilter.realistic != -1 )
		{
			params += "&realistic=" + SearchFilter.tasteFilter.realistic;
		}
		if( SearchFilter.tasteFilter.serious != -1 )
		{
			params += "&serious=" + SearchFilter.tasteFilter.serious;
		}
	}
//	/* global filters */
//	if( suggestFilter.contentTypeFilter != null )
//	{
//		params += "&contentType=" + suggestFilter.contentTypeFilter;
//	}
//	if( suggestFilter.onlineContentFilter == true )
//	{
//		params += "&online=true";
//	}
//	
	return encodeURI(params);
}
shareModule.getGlobalFilter = function()
{
	var filter = document.getElementById('contentTypeField').value;
	if (filter == "FreeOnline") {
		filter = "onlineFilter";
	} else if (filter == "Shorts") {
		filter = "shortsFilter";
	} else if (filter == "TV") {
		filter = "tvFilter";
	} else if (filter == "Movies") {
		filter = "movieFilter";
	}
	return filter;
}
shareModule.getSearchDisplayString = function( truncate )
{
	var search = "";
	try{
		search = document.getElementById("searchInput").value;
	
		if( truncate == true && search.length > 40)
		{
			search = search.substr(0,40) + "...";	
		}
	}catch(e){}
	return search;
	
}
shareModule.getSearchCode = function()
{
	return "&searchCode=" + getSearchCode( shareModule.getSearchDisplayString() );
}
