/*
 * t is a prototype transport
 * so this can be used as the onsuccess for a Ajax.Request(...)
 */
function _showPopupWindow( t )
{
	/*
	 * Inject the content into the litebox, with additional form tags
	 * which pipe the submit through ajax
	 */
	$( '#modal_form' ).html( t );

    _showLitebox();
	return false;
}

function _updatePopupForm( t )
{
	/*
	 * Inject the content into the litebox, with additional form tags
	 * which pipe the submit through ajax
	 */
	$( '#modal_form' ).html( t );

	return false;
}

function _showPopupForm( t, module, params )
{
	/* Inject the content into the litebox, with additional form tags which pipe the submit through ajax */
	$( '#modal_form' ).html( '<form id="'+module+'" name="'+module+'" onsubmit="return update_form( this, \''+module+'\', \''+params+'\' );" style="padding: 5px;">' + t + '</form>' );
	
	// eval in the context of the document.
	$( '#modal_form script' ).each( function( e ) {
		evalScript( e.html() );
	} );

    _showLitebox();
	return false;
}

function _updatePopupForm( t, module, params )
{
	/* Inject the content into the litebox, with additional form tags which pipe the submit through ajax */
	$( '#modal_form' ).html( '<form id="'+module+'" name="'+module+'" onsubmit="return update_form( this, \''+module+'\', \''+params+'\' );" style="padding: 5px;">' + t + '</form>' );
	
	// eval in the context of the document.
	$( '#modal_form script' ).each( function( e ) {
		evalScript( e.html() );
	} );

	return false;
}

function modalOpen( dialog )
{
	location.href="#top";
	$( 'body, html' ).css( 'overflow', 'hidden' );
	dialog.overlay.show(); //fadeIn( 300, function () {
	    dialog.overlay.bind( 'click', function() { modalClose( dialog ); } );
		dialog.container.show();
		dialog.data.show();
	//});
}

function modalClose( dialog )
{
	$( 'body, html' ).css( 'overflow', 'auto' );
    dialog.container.hide();
	dialog.data.hide();
	dialog.overlay.hide(); //fadeOut( 300, function () {
		$.modal.close();
	//});
}

function _showLitebox() {
	
    $( '#modal_form' ).modal( {
        onOpen: modalOpen,
        onClose: modalClose
    } );
    
	return false;
}

function open_news_gallery( news_id, image_width, image_height, image_path, count ) {
	
	var url = HTML_ROOT + '/includes/plugin_news_image_viewer.inc.php';
	$.ajax( {
       type: "GET",
       url: url,
       data: 'news_id=' + news_id + '&width=' + image_width + '&height=' + image_height + '&image=' + image_path + '&count=' + count,
       success: _showPopupWindow
    } );

	return false;
}

function open_friend_form()
{
	var url = HTML_ROOT + '/includes/plugin_send_friend.inc.php'
	
	$.ajax( {
        type: 'GET',
        url: url + '?url=' + SEND_PAGE_URL,
        cache: false,
        success: _showPopupForm
    } );
	
	return false;
}

function update_friend_form( form )
{
	var url = HTML_ROOT + '/includes/plugin_send_friend.inc.php';
	
	$.ajax( {
        type: 'POST',
        url: url + '?url=' + SEND_PAGE_URL,
        data: $( form ).serialize(),
        cache: false,
        success: _updatePopupForm
    } );

	return false;
}

function open_form( module, params ) {
	var url = HTML_ROOT + '/includes/plugin_' + module + '.inc.php';
	
	$.ajax( {
        type: 'GET',
        url: url + params,
        cache: false,
        success: function( t ) { _showPopupForm( t, module, params ); }
    } );
	
	return false;
}

function update_form( form, module, params ) {
    
	var url = HTML_ROOT + '/includes/plugin_' + module + '.inc.php';
	
	$.ajax( {
        type: 'POST',
        url: url + params,
        data: $( form ).serialize(),
        cache: false,
        success: function( t ) { _updatePopupForm( t, module, params ); }
    } );

	return false;
}

function toggle_menu( name, hide )
{
	if( $( '#sort_' + name ).css( 'display' ) == 'none' )
	{
		$( '#sort_' + name ).show();
		$( '#sort_' + hide ).hide();
	}
	else
	{
		$( '#sort_' + name ).hide();
	}
}

/*
 * 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;
}