// JavaScript Document

var shareThis;
var strLinks;
var contentID;
var contentURL;
var contentTitle;
var contentType;
var	recieverEmail = '';
var	senderName = '';
var	senderEmail = '';


function share(id, url, title, type)
{
	contentID = id;
	contentURL = url;
	contentTitle = title;
	contentType = type;
	
	shareThis = document.getElementById("share_this");
	shareLink = document.getElementById("share_link_" + contentID);

	if(shareThis.style.display != "block")
	{
		shareThis.className = "share_this";
		shareThis.style.top = getY(shareLink) + shareLink.offsetHeight + 5;
		shareThis.style.left = getX(shareLink);
		initTabs();
		changeTab('social');
		shareThis.style.display = "block";
	}
	else
	{
		shareThis.style.display = 'none';	
	}
}

function getX(oElement)
{
	var iReturnValue = 0;
	while( oElement != null )
	{
		iReturnValue += oElement.offsetLeft;
		oElement = oElement.offsetParent;
	}
	return iReturnValue;
}

function getY(oElement)
{
	var iReturnValue = 0;
	while( oElement != null )
	{
		iReturnValue += oElement.offsetTop;
		oElement = oElement.offsetParent;
	}
	return iReturnValue;
}

function initTabs()
{
	var strContent = '';
	
	strContent += '<div class="close_bar"><div class="close_link"><a href="" onClick="document.getElementById(\'share_this\').style.display = \'none\'; return false;">Close</a></div></div>';
	strContent += '<div class="menu">';
	strContent += '<ul>';
	strContent += '<li id="tab_button_social" class="active_tab"><a href="" onClick="changeTab(\'social\'); return false;">Social Web</a></li>';
	strContent += '<li id="tab_button_email" class="inactive_tab"><a href="" onClick="changeTab(\'email\'); return false;">Email</a></li>';
	strContent += '</ul>';
	strContent += '</div>';
	strContent += '<div id="tab_content" class="tab_content">';
	strContent += '<div id="tab_social" class="tab_social">';
	strContent += '</div>';
	strContent += '<div id="tab_email" class="tab_email">';
	strContent += '</div>';
	strContent += '</div>';
	
	shareThis.innerHTML = strContent;
}

function changeTab(tab)
{
	tabSocial = document.getElementById("tab_social");
	tabEmail = document.getElementById("tab_email");
	tabButtonSocial = document.getElementById("tab_button_social");
	tabButtonEmail = document.getElementById("tab_button_email");
	if(tab == "social")
	{
		updateSocialTab();
		// setInterval('updateSocialTab()', 10*1000); // enables real-time update of social tab
 		tabButtonEmail.className = "inactive_tab";
		tabButtonSocial.className = "active_tab";
		tabEmail.style.display = "none";
		tabSocial.style.display = "block";
	} 
	else if(tab == "email")
	{
		updateEmailTab();
		tabButtonSocial.className = "inactive_tab";
		tabButtonEmail.className = "active_tab";
		tabSocial.style.display = "none";
		tabEmail.style.display = "block";
	}
}

function updateSocialTab()
{
	var type = "article";
	var myAjax = new Ajax.Request
	(
		'/widgets/social_bookmark_plugin/load_links.php', 
		{
			method: 'get', 
			parameters: 'type=' + type, 
			onComplete: function(response)
			{
				strLinks = response.responseText;
				arrLinks = eval('(' + strLinks + ')');
				tabSocial = document.getElementById("tab_social");
				tabSocial.innerHTML = drawLinks(arrLinks);
			}
		}
	);
}

function updateEmailTab()
{
	var strContent = '';
	strContent += '<form id="send_email_form" action="/widgets/social_bookmark_plugin/send_email.php" method="post">';
	strContent += '<div class="email_form_row"><strong>Email this content:</strong></div>';
	strContent += '<br />';
	strContent += '<div class="email_form_row">Send to:</div>';
	strContent += '<div class="email_form_row"><input type="text" name="reciever_email" value="' + recieverEmail + '" class="email_form_textbox" /></div>';
	strContent += '<div class="email_form_row">Your name:</div>';
	strContent += '<div class="email_form_row"><input type="text" name="sender_name" value="' + senderName + '" class="email_form_textbox" /></div>';
	strContent += '<div class="email_form_row">Your email:</div>';
	strContent += '<div class="email_form_row"><input type="text" name="sender_email" value="' + senderEmail + '" class="email_form_textbox" /></div>';
	strContent += '<br />';
	strContent += '<div class="email_form_row"><input type="submit" name="submit" value="Send" class="submit_button" onClick="submitEmailForm(); return false;" /></div>';
	strContent += '<input type="hidden" name="title" value="' + contentTitle + '" />';
	strContent += '<input type="hidden" name="url" value="' + contentURL + '" />';
	strContent += '</form>';
	
	tabEmail = document.getElementById("tab_email");
	tabEmail.innerHTML = strContent;
}

function drawLinks(arrLinks)
{
	var strLinks = '';
	var linkURL = '';
	for(i = 0; i < arrLinks['links'].length; i++)
	{
		linkID = arrLinks['links'][i].id;
		linkURL = arrLinks['links'][i].link_url;
		linkURL = linkURL.replace("{url}", contentURL);
		linkURL = linkURL.replace("{title}", contentTitle);
		strLinks += '<div class="tab_social_row">';
		strLinks += '<a href="' + linkURL + '" onClick="followURL(\'' + linkURL + '\', ' + linkID + ');" target="_blank">';
		strLinks += '<img src="' + arrLinks['links'][i].link_icon + '" border="0" class="tab_social_icon" />';
		strLinks += arrLinks['links'][i].link_name;
		strLinks += '</a>';
		strLinks += '</div>';	
	}	
	return strLinks;
}

function submitEmailForm()
{
	objForm = document.getElementById('send_email_form');
	// objForm.submit.disabled = true;
	recieverEmail = objForm.reciever_email.value;
	senderName = objForm.sender_name.value;
	senderEmail = objForm.sender_email.value;
	submitButton = objForm.submit.value;
	var myAjax = new Ajax.Request
	(
		'/widgets/social_bookmark_plugin/send_email.php', 
		{
			method: 'post', 
			parameters: 'reciever_email=' + recieverEmail + '&sender_name=' + senderName + '&sender_email=' + senderEmail + '&submit=' + submitButton + '&content_id=' + contentID + '&url=' + contentURL + '&title=' + contentTitle, 
			onComplete: function(response)
			{
				strMessage = response.responseText;
				tabEmail = document.getElementById("tab_email");
				tabEmail.innerHTML  = '<div class="email_message">' + strMessage + '</div>';
				tabEmail.innerHTML += '<div class="email_ok_div"><input name="button" type="button" value="OK" onClick="updateEmailTab(); return false;" class="submit_button" /></div';
				setTimeout('updateEmailTab()', 5*1000);
			}
		}
	);
}

function followURL(url, link_id)
{
	var myAjax = new Ajax.Request
	(
		'save_stats.php', 
		{
			method: 'post', 
			parameters: 'link_id=' + link_id + '&content_id=' + contentID + '&type=social', 
			onComplete: function(response)
			{
				// N/A
			}
		}
	);
}

function emailStats()
{
	var myAjax = new Ajax.Request
	(
		'save_stats.php', 
		{
			method: 'post', 
			parameters: 'content_id=' + contentID + '&type=email', 
			onComplete: function(response)
			{
				return true;
			}
		}
	);
}
