pantheon_baseDir = 'http://pantheon.nonoo.hu';

function pantheon_dbUpdate()
{
    $('pantheon-loading').style.visibility = 'visible';
    new Effect.Fade( $('pantheon-content'), { duration: 0.5 } );
    window.setTimeout(
	function()
	{
	    new Ajax.Request( '/wp-content/plugins/pantheon/pantheon-load-dir.php', {
		method: 'post',
		parameters: { l: pantheon_currentDir, dbupdate: '1' }
	    } );
	    
	}, 500 );
}

function pantheon_editCommentDir( url, currentcomment )
{
    newcomment = window.prompt( 'Editing comment for '+url, unescape( currentcomment ) );
    if( newcomment == null )
    {
	return;
    }

    $('pantheon-edit-comment-img').src = '/wp-content/plugins/pantheon/img/ajax-loader.gif';

    new Ajax.Request( '/wp-content/plugins/pantheon/pantheon-edit-comment-dir.php', {
	method: 'post',
	parameters: { l: url, n: escape( newcomment ) }
    } );
}

function pantheon_editCommentFile( url, currentcomment )
{
    pantheon_newComment = window.prompt( 'Editing comment for '+url, unescape( currentcomment ) );
    if( pantheon_newComment == null )
    {
	pantheon_editingCancelled = 1;
	return;
    }

    pantheon_editingOn = 1;

    new Ajax.Request( '/wp-content/plugins/pantheon/pantheon-edit-comment-file.php', {
	method: 'post',
	parameters: { l: url, n: escape( pantheon_newComment ) }
    } );
}

function pantheon_onmouseOverLib()
{
    return overlib( unescape(pantheon_overlib_text),WIDTH,'300',CAPTION,pantheon_overlib_caption,FGCOLOR,'',FGBACKGROUND,'/wp-content/plugins/pantheon/img/file-commentback.png',BGCOLOR,'#444444',TEXTCOLOR,'#dddddd',TEXTSIZE,'0.9em',CAPTIONSIZE,'0.7em',CAPCOLOR,'#aaaaaa',OFFSETX,'26',OFFSETY,'20');
}

function pantheon_prevImg()
{
    pantheon_Image = pantheon_prevImage;
    pantheon_loadFile( pantheon_prevURL );
}

function pantheon_nextImg()
{
    pantheon_Image = pantheon_nextImage;
    pantheon_loadFile( pantheon_nextURL );
}

function pantheon_imgClick()
{
    pantheon_changeImageCounter();
    window.open( $('pantheon-image-img-'+pantheon_imageCounter).src );
    pantheon_changeImageCounter();
}

function pantheon_navArrowHideIfNeeded()
{
    if( pantheon_nextURL == '' )
    {
	$('pantheon-next').style.visibility = 'hidden';
    }
    else
    {
	$('pantheon-next').style.visibility = 'visible';
    }
    if( pantheon_prevURL == '' )
    {
	$('pantheon-prev').style.visibility = 'hidden';
    }
    else
    {
	$('pantheon-prev').style.visibility = 'visible';
    }
}

function pantheon_changeImageCounter()
{
    if( ++pantheon_imageCounter > 2 )
	pantheon_imageCounter = 1;
}

function pantheon_showImage()
{
    $('pantheon-image-img-'+pantheon_imageCounter).src = pantheon_Image.src;
    $('pantheon-image-'+pantheon_imageCounter).style.zIndex = 500;
    $('pantheon-image-tn-'+pantheon_imageCounter).style.zIndex = 499;

    new Effect.Appear( $('pantheon-image-tn-img-'+pantheon_imageCounter), { duration: 0.5 } );
    new Effect.Appear( $('pantheon-image-img-'+pantheon_imageCounter), { duration: 0.5 } );
}

function pantheon_hideImage()
{
    new Effect.Fade( $('pantheon-image-tn-img-'+pantheon_imageCounter), { duration: 0 } );
    new Effect.Fade( $('pantheon-image-img-'+pantheon_imageCounter), { duration: 0 } );

    $('pantheon-image-'+pantheon_imageCounter).style.zIndex = -500;
    $('pantheon-image-tn-'+pantheon_imageCounter).style.zIndex = -499;
}

function pantheon_posResizeImage()
{
    if( pantheon_Image.height > 600 )
    {
        pantheon_Image.width = (600*pantheon_Image.width)/pantheon_Image.height;
        pantheon_Image.height = 600;
    }

    if( pantheon_Image.width > 800 )
    {
        pantheon_Image.height = (800*pantheon_Image.height)/pantheon_Image.width;
        pantheon_Image.width = 800;
    }

    // vertically centering image
    $('pantheon-image-'+pantheon_imageCounter).style.marginTop = -Math.round(pantheon_Image.height/2)-20+"px";
    $('pantheon-image-tn-'+pantheon_imageCounter).style.marginTop = -Math.round(pantheon_Image.height/2)-20+"px";

    $("pantheon-image-tn-img-"+pantheon_imageCounter).width = pantheon_Image.width;
    $("pantheon-image-tn-img-"+pantheon_imageCounter).height = pantheon_Image.height;
    $("pantheon-image-img-"+pantheon_imageCounter).width = pantheon_Image.width;
    $("pantheon-image-img-"+pantheon_imageCounter).height = pantheon_Image.height;

    $('pantheon-image-'+pantheon_imageCounter).width = pantheon_Image.width;
    $('pantheon-image-'+pantheon_imageCounter).height = pantheon_Image.height;
    $('pantheon-image-tn-'+pantheon_imageCounter).width = pantheon_Image.width;
    $('pantheon-image-tn-'+pantheon_imageCounter).height = pantheon_Image.height;
}

// called after ajax load file request
function pantheon_loadFileSuccess()
{
    pantheon_navArrowHideIfNeeded();

    pantheon_posResizeImage();

    if( pantheon_isURLDisplayed )
    {
	$('pantheon-url').style.display = 'none';
	pantheon_isURLDisplayed = 0;
    }

    pantheon_showImage();
    pantheon_changeImageCounter();
    pantheon_hideImage();

    // preloading
    pantheon_nextImage.src = pantheon_baseDir + pantheon_nextURL;
    pantheon_prevImage.src = pantheon_baseDir + pantheon_prevURL;
}

// called after ajax load file request
function pantheon_loadURLSuccess()
{
    pantheon_navArrowHideIfNeeded();

    pantheon_changeImageCounter(); // changing back the image counter
    pantheon_hideImage(); // hiding currently displayed image
    pantheon_changeImageCounter();

    $('pantheon-url').style.display = 'block';

    pantheon_isURLDisplayed = 1;

    // preloading
    pantheon_nextImage.src = pantheon_baseDir + pantheon_nextURL;
    pantheon_prevImage.src = pantheon_baseDir + pantheon_prevURL;
}

function pantheon_showViewer( url )
{
    new Effect.Appear( $('pantheon-image-layer'), { duration: 0.7 } );
    new Effect.Fade( $('main'), { to: 0.3, duration: 0.7/*, queue: 'front'*/ } );

    pantheon_imageCounter = 1;

    new Effect.Fade( $('pantheon-image-tn-img-1'), { duration: 0 } );
    new Effect.Fade( $('pantheon-image-tn-img-2'), { duration: 0 } );
    new Effect.Fade( $('pantheon-image-img-1'), { duration: 0 } );
    new Effect.Fade( $('pantheon-image-img-2'), { duration: 0 } );

    pantheon_Image.src = pantheon_baseDir+url;
}

function pantheon_hideViewer()
{
    new Effect.Fade( $('pantheon-image-layer'), { duration: 0.7 } );
    new Effect.Fade( $('main'), { to: 1, duration: 0.7/*, queue: 'front'*/ } );

    new Effect.Fade( $('pantheon-image-tn-img-1'), { duration: 0.7 } );
    new Effect.Fade( $('pantheon-image-tn-img-2'), { duration: 0.7 } );
    new Effect.Fade( $('pantheon-image-img-1'), { duration: 0.7 } );
    new Effect.Fade( $('pantheon-image-img-2'), { duration: 0.7 } );
}

function pantheon_loadFile( url )
{
    if( pantheon_editingOn )
	return;

    if( pantheon_editingCancelled )
    {
	pantheon_editingCancelled = 0;
	return;
    }

    $('pantheon-image-tn-img-'+pantheon_imageCounter).src = 'http://pantheon.nonoo.hu/.thumbnails'+url;

    // is the viewer visible?
    if( $('pantheon-image-layer').style.display != 'block' )
	pantheon_showViewer( url );

    new Ajax.Request( '/wp-content/plugins/pantheon/pantheon-load-file.php', {
	method: 'post',
	parameters: { l: url }
    } );
}




function pantheon_loadDir( url )
{
    $('pantheon-loading').style.visibility = 'visible';
    new Effect.Fade( $('pantheon-content'), { duration: 0.5 } );
    window.setTimeout(
	function()
	{
	    new Ajax.Request( '/wp-content/plugins/pantheon/pantheon-load-dir.php', {
		method: 'post',
		parameters: { l: url }
	    } );
	    
	}, 500 );
}

function pantheon_onLoad( url )
{
    new Effect.Fade( $('pantheon-image-tn-img-1'), { duration: 0 } );
    new Effect.Fade( $('pantheon-image-tn-img-2'), { duration: 0 } );
    new Effect.Fade( $('pantheon-image-img-1'), { duration: 0 } );
    new Effect.Fade( $('pantheon-image-img-2'), { duration: 0 } );
    pantheon_loadDir( url );
}

pantheon_Image = new Image;
pantheon_prevImage = new Image;
pantheon_nextImage = new Image;
pantheon_prevURL = '';
pantheon_nextURL = '';
pantheon_isURLDisplayed = 0; // true when an url file is currently displayed
pantheon_imageCounter = 1;
pantheon_overlib_text = '';
pantheon_overlib_caption = '';
pantheon_currentDir = '';

// workaround: when the user clicks on a file edit comment pencil image, the viewer is opened after editing the comment
pantheon_editingOn = 0; // true when a file's comment is currently under editing
pantheon_editingCancelled = 0; // true when the user clicks on cancel on the file comment editing dialog
