function _sendToFriendSubmit(form) {
	var url = HTML_ROOT + '/includes/plugin_send_friend.inc.php?url=' + SEND_PAGE_URL;
	new Ajax.Request(url, {
		method: 'post',
		postBody: $(form).serialize(),
		onSuccess: _showResponseInLitebox,
		onFailure: function(t) {
			/* Change location to the sendpage page */
		}
	});
	return false;
}

/*
 * t is a prototype transport
 * so this can be used as the onsuccess for a Ajax.Request(...)
 */
function _showResponseInLitebox(t) {
	/*
	 * Inject the content into the litebox, with additional form tags
	 * which pipe the submit through ajax
	 */
	$('overlay_box').innerHTML = '<form onsubmit="return _sendToFriendSubmit(this);">' + 
								 t.responseText + 
								 '</form>';


	$$('#overlay_box script').each(function(e) {
		// eval in the context of the document.
		evalScript(e.innerHTML);
	});

	// If this is the form completion
	if($$('#overlay_box #completed').size() == 1) {
		setTimeout(_hideLitebox, 3000);
	}

	_showLitebox();
	return false;
}

function _showShareResponseInLitebox(t) {
	/*
	 * Inject the content into the litebox, with additional form tags
	 * which pipe the submit through ajax
	 */
	$('overlay_box').innerHTML = '<form onsubmit="return _sendToFriendSubmit(this);">' + 
								 t.responseText + 
								 '</form>';


	$$('#overlay_box script').each(function(e) {
		// eval in the context of the document.
		evalScript(e.innerHTML);
	});

	// If this is the form completion
	if($$('#overlay_box #completed').size() == 1) {
		setTimeout(_hideLitebox, 3000);
	}

	_showLitebox();
	return false;
}

function _showLitebox() {
    $j("#news_video").css( 'visibility', 'hidden');
	$('overlay').show();
	_liteBoxCenter('overlay_box');
	return false;
}
function _hideLitebox() {
	$('overlay_box').hide();
	$('overlay').hide();
	$j("#news_video").css( 'visibility', 'visible');
}

function _liteBoxCenter(element){
	try{
		element = $(element);
	}catch(e){
		return;
	}

	var my_width  = 0;
	var my_height = 0;

	if ( typeof( window.innerWidth ) == 'number' ){
		my_width  = window.innerWidth;
		my_height = window.innerHeight;
	}else if ( document.documentElement &&
			( document.documentElement.clientWidth ||
			  document.documentElement.clientHeight ) ){
		my_width  = document.documentElement.clientWidth;
		my_height = document.documentElement.clientHeight;
	}
	else if ( document.body && 
			( document.body.clientWidth || document.body.clientHeight ) ){
		my_width  = document.body.clientWidth;
		my_height = document.body.clientHeight;
	}

	element.style.position = 'absolute';
	element.style.zIndex   = 99;

	var scrollY = 0;

	if ( document.documentElement && document.documentElement.scrollTop ){
		scrollY = document.documentElement.scrollTop;
	}else if ( document.body && document.body.scrollTop ){
		scrollY = document.body.scrollTop;
	}else if ( window.pageYOffset ){
		scrollY = window.pageYOffset;
	}else if ( window.scrollY ){
		scrollY = window.scrollY;
	}

	var elementDimensions = Element.getDimensions(element);

	var setX = ( my_width  - elementDimensions.width  ) / 2;
	var setY = ( my_height - elementDimensions.height ) / 2 + scrollY;

	setX = ( setX < 0 ) ? 0 : setX;
	setY = ( setY < 0 ) ? 0 : setY;

	element.style.left = setX + "px";
	element.style.top  = setY + "px";

	element.style.display  = 'block';
}

function showSendToFriend() {
    $j("#overlay_box").css( 'width', '325px' );
	var url = HTML_ROOT + '/includes/plugin_send_friend.inc.php'
	new Ajax.Request(url, {
		method: 'get',
		parameters: 'url='+SEND_PAGE_URL,
		onSuccess: _showResponseInLitebox,
		onFailure: function(t) {
			/* Change location to the sendpage page */
		}
	});
	return false;
}

function showShareThis() {
    $j("#overlay_box").css( 'width', '500px' );
	var url = HTML_ROOT + '/includes/plugin_share_this.inc.php'
	new Ajax.Request(url, {
		method: 'get',
		parameters: 'url='+SHARE_THIS_URL,
		onSuccess: _showShareResponseInLitebox,
		onFailure: function(t) {
			/* Change location to the sendpage page */
		}
	});
	return false;
}

/*
 * Globally Scoped JS Evaluation
 */
(evalScript = function(e){
 var h = evalScript.node,
 s = document.createElement("script");
 s.type = "text/javascript";
 s.text = e;
 h.appendChild(s);
 h.removeChild(s);
 }).node = document.getElementsByTagName("head")[0] || document.getElementsByTagName("*")[0];

 function addsessiondata( value ) {
	var url = HTML_ROOT + '/includes/sessiondata.php';
	new Ajax.Request(url, {
		method: 'get',
		parameters: 'acid='+value
	});
	return false;
}


