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

	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_product_gallery( product_id, id, image_width, image_height, image_path, count, title, desc ) {
    
	index_id = id;
	
	var url = HTML_ROOT + '/includes/plugin_image_product_viewer.inc.php';
	$.ajax( {
       type: "GET",
       url: url,
       data: 'product_id=' + product_id + '&width=' + image_width + '&height=' + image_height + '&image=' + image_path + '&count=' + count + '&index=' + index_id + '&title=' + title + '&desc=' + desc,
       success: _showPopupWindow
    } );

	return false;
}

function open_send_to_friend() {
    
	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_send_to_friend( 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;
}