﻿//
//This file contains all necessary functions for the top control bar with fullscreen and playback control buttons
//

//function for initial positioning of the control toolbar, including the buttons
function positionControls()
{
    var controlCanvas = host.findName("controlCanvas");
    controlCanvas.Width = host.ActualWidth;
    var controlCanvasTrigger = host.findName("controlCanvasTrigger");
    controlCanvasTrigger.Width = host.ActualWidth;
    var controlRect = host.findName("controlRect");
    controlRect.Width = host.ActualWidth;
    
    var play = host.findName("btnPlay");
    var pause = host.findName("btnPause");
    var prev = host.findName("btnPrevious");
    var next = host.findName("btnNext");
    
    play["Canvas.Left"] = (host.ActualWidth - play.Width)/2 - 26;
    pause["Canvas.Left"] = (host.ActualWidth - pause.Width)/2 - 19;
    prev["Canvas.Left"] = (host.ActualWidth - play.Width)/2 - 91;
    next["Canvas.Left"] = (host.ActualWidth - play.Width)/2 + 37;
}

//starts the animation to show the toolbar
function showControls()
{
    var opacity = host.findName("controlAnimationOpacity");
    opacity.To = 1.0;
    opacity.BeginTime = "0:0:0";
    opacity.Duration = fadeInDuration;
    var top = host.findName("controlAnimationTop");
    top.To = 0;
    top.BeginTime = "0:0:0";
    top.Duration = fadeInDuration;
    host.findName("controlAnimation").Begin();
}

//starts the animation to hide the toolbar
function hideControls()
{
    var opacity = host.findName("controlAnimationOpacity");
    opacity.To = 0.0;
    opacity.BeginTime = fadeOutDelay;
    opacity.Duration = fadeOutDuration;
    
    var top = host.findName("controlAnimationTop");
    top.To = -host.findName("controlCanvas").Height;
    top.BeginTime = fadeOutDelay;
    top.Duration = fadeOutDuration;
    
    host.findName("controlAnimation").Begin();
}

//click handler for the "next" button
function btnNextClick()
{
    //if the gallery is in slideshow mode and the user clicks the "next" button, the slideshow is paused
    if(slideshow)
        btnPauseClick();
    move(1);
}

//click handler for the "previous" button
function btnPreviousClick()
{
    //if the gallery is in slideshow mode and the user clicks the "previous" button, the slideshow is paused
    if(slideshow)
        btnPauseClick();
    move(-1);
}

//click handler for the "play" button
function btnPlayClick()
{
    if(!zoomedIn)
    {
        clickedImage = photoCanvas.Children.GetItem(0);
        currentImageNr = 0;
        zoomInStep1();
    }
    
    slideshow = true;
    slideshowTimeout = window.setTimeout("move(1)", slideshowSpeedSeconds * 1000);
    
    host.findName("btnPlay").Opacity = 0;
    host.findName("btnPause").Opacity = 1;
}

//click handler for the "pause" button
function btnPauseClick()
{
    slideshow = false;
    window.clearTimeout(slideshowTimeout);
    host.findName("btnPlay").Opacity = 1;
    host.findName("btnPause").Opacity = 0;
}

//moves to the next/previous image in detail view. the direction can either be 1 (forward) or -1 (back)
function move(direction)
{
    if(
        ((direction > 0) && ((currentImageNr + direction) >= photoCanvas.Children.Count))
        ||
        ((direction < 0) && ((currentImageNr + direction) < 0))
    )
    return;
    //
    currentImageNr += direction;
    checkPosition();
    
    var translateX = host.findName("zoomTranslateX");
    var zoomAnimation = host.findName("zoomAnimation");
    var zoomFactor = Math.min(host.ActualWidth, host.ActualHeight) / maxImageSize;
    translateX.To += -direction * (imageSpacing + maxImageSize) * zoomFactor;
    zoomAnimation.Begin();
    
    if(slideshow)
    {
        if(currentImageNr > photoCanvas.Children.Count - 1)
        {
            //when the end of the image-list is reached during slideshow mode, the slideshow is paused and a zoomout is issued
            btnPauseClick();
            zoomOutStep1();
        }
        else
        {
            //when in slideshow mode and there are more images available, a timeout-call to move(1) is performed 
            //(you can set the delay between two images in the variables at the top of this file)
            slideshowTimeout = window.setTimeout("move(1)", slideshowSpeedSeconds * 1000);
        }
        
        hideLegend();
    }
}

//in this function, the "previous" and "next" buttons are set visible/invisible according to the position in the list
function checkPosition()
{
    if(zoomedIn)
    {
        //if at the beginning of the imagelist, the "previous" button is hidden
        if(currentImageNr <= 0)
            host.findName("btnPrevious").Opacity = 0;
        else
            host.findName("btnPrevious").Opacity = 1;
        
        //if at the end of the imagelist, the "next" button is hidden    
        if(currentImageNr >= photoCanvas.Children.Count - 1)
            host.findName("btnNext").Opacity = 0;
        else
            host.findName("btnNext").Opacity = 1;
    }
    else
    {
        currentImageNr = 0;
    }
}

//animation-effect for the buttons (onMouseEnter)
function buttonHover(sender, eventArgs)
{
    var storyboard = sender.findName(sender.Name + "SB");
    var animation = sender.findName(sender.Name + "ANI");
    animation.From = "0";
    animation.To = "1";
    storyboard.BeginTime = "0:0:0";
    storyboard.Begin();
}

//animation-effect for the buttons (onMouseLeave)
function buttonOut(sender, eventArgs)
{
    var storyboard = sender.findName(sender.Name + "SB");
    var animation = sender.findName(sender.Name + "ANI");
    animation.From = "1";
    animation.To = "0";
    storyboard.BeginTime = "0:0:0";
    storyboard.Begin();
}
