/*
 * 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 )
{
	/* Inject the content into the litebox, with additional form tags which pipe the submit through ajax */
	$( '#modal_form' ).html( '<form onsubmit="return update_contact_form( this );">' + t + '</form>' );
	
	// eval in the context of the document.
	$( '#modal_form script' ).each( function( e ) {
		evalScript( e.html() );
	} );

    _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( '<form onsubmit="return update_contact_form( this );">' + t + '</form>' );
	
	// eval in the context of the document.
	$( '#modal_form script' ).each( function( e ) {
		evalScript( e.html() );
	} );

	return false;
}

function _showPopupSubscribe( t )
{
	/* Inject the content into the litebox, with additional form tags which pipe the submit through ajax */
	$( '#modal_form' ).html( '<form onsubmit="return update_subscribe_form( this );">' + t + '</form>' );
	
	// eval in the context of the document.
	$( '#modal_form script' ).each( function( e ) {
		evalScript( e.html() );
	} );

    _showLitebox();
	return false;
}

function modalOpen( dialog )
{
	dialog.overlay.show(); //fadeIn( 300, function () {
	    dialog.overlay.bind( 'click', function() { modalClose( dialog ); } );
		dialog.container.show();
		dialog.data.show();
	//});
}

function modalClose( dialog )
{
    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_contact_form( region ) {
    
	CONTACT_REGION = region;
	
	var url = HTML_ROOT + '/includes/plugin_contact_form.inc.php'
	
	$.ajax( {
        type: 'GET',
        url: url + '?region=' + CONTACT_REGION,
        cache: false,
        success: _showPopupForm
    } );
	
	return false;
}

function update_contact_form( form ) {
    
	var url = HTML_ROOT + '/includes/plugin_contact_form.inc.php';
	
	$.ajax( {
        type: 'POST',
        url: url + '?region=' + CONTACT_REGION,
        data: $( form ).serialize(),
        cache: false,
        success: _updatePopupForm
    } );

	return false;
}

function open_subscribe_form( ) {
	
	var url = HTML_ROOT + '/includes/plugin_subscribe_form.inc.php'
	
	$.ajax( {
        type: 'GET',
        url: url,
        cache: false,
        success: _showPopupSubscribe
    } );
	
	return false;
}

function update_subscribe_form( form ) {
    
	var url = HTML_ROOT + '/includes/plugin_subscribe_form.inc.php';
	
	$.ajax( {
        type: 'POST',
        url: url,
        data: $( form ).serialize(),
        cache: false,
        success: _updatePopupForm
    } );

	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;
}
