/* 
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
 * Copyright 2009 Sun Microsystems, Inc. All rights reserved. Use is subject to license terms. 
 * 
 * This file is available and licensed under the following license:
 * 
 * Redistribution and use in source and binary forms, with or without 
 * modification, are permitted provided that the following conditions are met:
 *
 *   * Redistributions of source code must retain the above copyright notice, 
 *     this list of conditions and the following disclaimer.
 *
 *   * Redistributions in binary form must reproduce the above copyright notice,
 *     this list of conditions and the following disclaimer in the documentation
 *     and/or other materials provided with the distribution.
 *
 *   * Neither the name of Sun Microsystems nor the names of its contributors 
 *     may be used to endorse or promote products derived from this software 
 *     without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package carousel;

import carousel.Carousel;
import java.lang.Math;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.lang.Duration;
import javafx.lang.FX;
import javafx.scene.Cursor;
import javafx.scene.Group;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.*;
import javafx.scene.paint.*;
import javafx.scene.Scene;
import javafx.scene.shape.*;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

/**
 * @author Vaibhav Choudhary
 */

var carousel: Carousel[];
// list of images
var im:String[] = ["{__DIR__}im1.PNG", "{__DIR__}im2.PNG","{__DIR__}im3.PNG",
"{__DIR__}im4.PNG","{__DIR__}im5.PNG","{__DIR__}im6.PNG"];

var s:String;
// images for buttons for animation
var s_left:Image = Image {
    url: "{__DIR__}leftlight.png"};
var s_leftglow:Image = Image {
    url: "{__DIR__}left.png"};
var s_right:Image = Image {
    url: "{__DIR__}rightlight.png"};
var s_rightglow:Image = Image {
    url: "{__DIR__}right.png"};

var s_right_d = s_right;
var s_left_d = s_left;
var dur: Duration = 15ms;

// inserting the initial value
for(i in [0..5]) {
   insert Carousel {
        X: Math.cos(Carousel.angle + i  *  Carousel.image_gap +
        Carousel.shift) * Carousel.radius + Carousel.xcenter;
        Y: -Math .sin(Carousel.angle + i  *  Carousel.image_gap +
        Carousel.shift) * Carousel.radius_y + Carousel.ycenter ;
        Z: Math.sin(Carousel.angle + i  *  Carousel.image_gap +
        Carousel.shift) * Carousel.radius + Carousel.zcenter
        scale: Carousel.fl / (Carousel.fl + Math.sin(Carousel.angle +
        i  *  Carousel.image_gap + Carousel.shift) * Carousel.radius
                + Carousel.zcenter)
        image: bind Image {
            url: im[i]
        }

    } into carousel
}

// timeline for clockwise rotation
var timeline = Timeline {
    repeatCount: 20
    keyFrames: [
        KeyFrame {
            time: dur
            action: function() {
                Carousel.angle_rad = Carousel.angle_rad + Math.PI / 60;
                for(i in [0..5]) {
                    carousel[i].update(i,Carousel.angle_rad);
                }
            }
        }
    ]
}

// timeline for anti-clockwise rotation
var timeline_rev = Timeline {
    repeatCount: 20
    keyFrames: [
        KeyFrame {
            time: dur
            action: function() {
                Carousel.angle_rad = Carousel.angle_rad - Math.PI / 60;
                for(i in [0..5]) {
                    carousel[i].update(i,Carousel.angle_rad);


                }

            }
        }
    ]
}
var gp = Group {
    onKeyPressed:function(e:KeyEvent):Void {

        if(e.code == KeyCode.VK_LEFT) {
                    timeline_rev.play();
        }
        if(e.code == KeyCode.VK_RIGHT) {
                    timeline.play();
        }
        if(e.code == KeyCode.VK_Q) {
                    FX.exit();
        }
    }
    translateX: bind (width - 240) * 0.5;
    translateY: bind (height - 320) * 0.25;
    content: [
        carousel
                ,
    ]
        };

var scene:Scene = Scene {
    fill: Color.BLACK
    content: [
        Rectangle {
            x: 0,
            y: bind height - 30
            width: bind width,
            height: 25
            arcHeight:5
            arcWidth:5
            fill: LinearGradient {
                startX: 0.0
                startY: 0.0
                endX: 0.0
                endY: 1.0
                proportional: true
                stops: [
                    Stop {
                        offset: 0.0
                    color: Color.GRAY },
                    Stop {
                        offset: 0.5
                    color: Color.BLACK },
                    Stop {
                        offset: 1.0
                    color: Color.GRAY }
                ]
            }
            opacity:0.5
        },
        ImageView {
            x: 40
            y: bind height - 30
            image: bind s_left
            cursor: Cursor.HAND
            onMouseClicked: function( e: MouseEvent ):Void {
                s_left = s_leftglow;
                timeline_rev.play();
            }
            onMouseMoved: function( e: MouseEvent ):Void {
                s_left = s_leftglow;
            }
            onMouseExited: function( e: MouseEvent ):Void {
                s_left = s_left_d;
            }
        },
        ImageView {
            x: bind width - 70
            y: bind height - 30
            image: bind s_right
            cursor: Cursor.HAND
            onMouseClicked: function( e: MouseEvent ):Void {
                s_right = s_rightglow;
                timeline.play();
            }
            onMouseMoved: function( e: MouseEvent ):Void {
                s_right = s_rightglow;
            }
            onMouseExited: function( e: MouseEvent ):Void {
                s_right = s_right_d;
                carousel
            }
        },
    ]
              };

//capture the dynamic width and height of the scene
var width:Number = bind scene.width;
var height:Number = bind scene.height;
insert gp into scene.content;
var stage = Stage{
    title: "Carousel Application"
    width: 240
    height: 320
    style: StageStyle.TRANSPARENT
    scene: scene
}
gp.requestFocus();
stage;