/*
AlignWinHorV.s
Align SecondImg based on FirstImg's position and size
w   ALT: Vertical   direction = 0
w/o ALT: horizontal direction = 1

I also wrote conventional/simple functions for window alignment: AlignWindowX() and AlignWindowY().

*/

// modern function
void AlignWindow( image FirstImg, image SecondImg, number direction )
{
    // Get the DocumentWindow objects associated with the images
    DocumentWindow win1 = FirstImg.ImageGetOrCreateImageDocument().ImageDocumentGetWindow() 
    DocumentWindow win2 = SecondImg.ImageGetOrCreateImageDocument().ImageDocumentGetWindow() 
    // Get the current frame boundaries of the first window 
    number t, l, b, r // top, left, bottom, right
    win1.WindowGetFrameBounds( t, l, b, r ) 
    // Calculate dimensions of the first window
    number w = r - l    // width
    number h = b - t    // height
    if ( direction == 0 )
    {
        // direction = 0: Position win2 aligned to the right of win1
        win2.WindowSetFrameBounds( t, r, b, r + w ) 
    }
    else if ( direction == 1 )
    {
        // direction = 1: Position win2 aligned below win1
        win2.WindowSetFrameBounds( b, l, b + h, r ) 
    }
    // Bring the win1 to frontmost
    win1.WindowSelect() 
}

// conventional functions (not used in main)
void AlignWindowY(image img1, image img2)
{
    number spaceY = 60 // > 55 recommended
	number winsizeX, winsizeY, winposX, winposY
	getwindowsize(img1,winsizeX,winsizeY)
	getwindowposition(img1, winposX, winposY)
	Setwindowsize(img2,winsizeX, winsizeY)
	Setwindowposition(img2, winposX, winposY+winsizeY+spaceY)
}
void AlignWindowX(image img1, image img2)
{
    number spaceX = 30 // > 25 recommended
	number winsizeX, winsizeY, winposX, winposY
	getwindowsize(img1,winsizeX,winsizeY)
	getwindowposition(img1, winposX, winposY)
	Setwindowsize(img2,winsizeX, winsizeY)
	Setwindowposition(img2, winposX+winsizeX+spaceX, winposY)
}

/// Main ///
// Main script logic to get front-most images and execute alignment
    image img1, img2
    if ( !GetFrontImage( img1 ) ) Throw( "No image displayed." ) 
    img2 := FindNextImage( img1 ) 
    if ( !img2.ImageIsValid() ) Throw( "Second image not found behind the front image." ) 

// Determine direction: 1 if ALT key is held, otherwise 0 (Horizontal)
    number dir = OptionDown() ? 1 : 0
     
// Call the defined function
    AlignWindow( img1, img2, dir )