function _sendToFriendSubmit(form) {
	var url = HTML_ROOT + '/includes/plugin_send_friend.inc.php?url=' + SEND_PAGE_URL;

	$.ajax( {
        type: "POST",
        url: url,
        data: $(form).serialize(),
        cache: false,
        success: function( msg ) {
            _showResponseInLitebox( msg );
        }
    } );
	
	return false;
}

// showFeedbackAgency
function _sendFeedbackAgencySubmit(form, agency_id) {
	var url = HTML_ROOT + '/includes/plugin_agency_feedback.inc.php?_id' + agency_id;

	$.ajax( {
        type: "POST",
        url: url,
        data: $(form).serialize(),
        cache: false,
        success: function( msg ) {
			_showAgencyFeedbackInLitebox( msg );
        }
    } );
	
	return false;
}

/*
 * t is a prototype transport
 * so this can be used as the onsuccess for a Ajax.Request(...)
 */
function _showResponseInLitebox(t, agency_id) {
	/*
	 * Inject the content into the litebox, with additional form tags
	 * which pipe the submit through ajax
	 */
	$('#overlay_box').html( '<form onsubmit="return _sendToFriendSubmit(this, ' + agency_id + ');">' + 
								 t + 
							'</form>' );
							
	$('#overlay_box script').each(function(e) {
		// eval in the context of the document.
		evalScript(e.html());
	});

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

	_showLitebox();
	return false;
}

/*
 * t is a prototype transport
 * so this can be used as the onsuccess for a Ajax.Request(...)
 */
function _showAgencyFeedbackInLitebox(t) {
	/*
	 * Inject the content into the litebox, with additional form tags
	 * which pipe the submit through ajax
	 */
	$('#overlay_box').html( '<form onsubmit="return _sendFeedbackAgencySubmit(this);">' + 
								 t + 
							'</form>' );
							
	$('#overlay_box script').each(function(e) {
		// eval in the context of the document.
		evalScript(e.html());
	});

	// 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').html( '<form onsubmit="return false;">' + 
								 t + 
							'</form>' );

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

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

	_showLitebox();
	return false;
}

function _showLitebox() {
	$('#overlay').show();
	_liteBoxCenter('#overlay_box');
	return false;
}

function _hideLitebox() {
	$('#overlay_box').hide();
	$('#overlay').hide();
}

function _liteBoxCenter(element){
	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).css('position', 'absolute');
	$(element).css('z-index', 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 setX = ( my_width  - $(element).width()  ) / 2;
	var setY = ( my_height - $(element).height() ) / 2 + scrollY;

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

	$(element).css( 'left', setX + 'px' );
	$(element).css( 'top', setY + 'px' );

	$(element).css( 'display', 'block' );
	$(element).show();
}

function showSendToFriend() {
    $( '#overlay_box' ).css( 'width', '500px' );
    var url = HTML_ROOT + '/includes/plugin_send_friend.inc.php'
	
	$.ajax( {
        type: "GET",
        url: url + '?url=' + SEND_PAGE_URL,
        cache: false,
        success: function( msg ) {
            _showResponseInLitebox( msg );
        }
    } );
	
	return false;
}

function showShareThis() {
    $( '#overlay_box' ).css( 'width', '500px' );
	var url = HTML_ROOT + '/includes/plugin_share_this.inc.php'
	
	$.ajax( {
        type: "GET",
        url: url + '?url=' + SHARE_THIS_URL,
        cache: true,
        success: function( msg ) {
            _showShareResponseInLitebox( msg );
        }
    } );
	
	return false;
}

function showFeedbackAgency( agency_id )
{
    $( '#overlay_box' ).css( 'width', '500px' );
    var url = HTML_ROOT + '/includes/plugin_agency_feedback.inc.php'
	
	$.ajax( {
        type: "GET",
        url: url + '?_id=' + agency_id,
        cache: false,
        success: function( msg ) {
			_showAgencyFeedbackInLitebox( msg, agency_id );
        }
    } );
	
	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';
	$.ajax( {
        type: "GET",
        url: 'acid='+value,
        cache: false
    } );
	return false;
}
