﻿/// <reference path="jquery-1.3.2.min.js"/>

//number of ms between image rotations
var ROTATION_TIME = 10000;

//the number of seconds to perform fade animation.
var TRANSITION_TIME = 500;

//global variable holding the carousel
var carousel = new Carousel();

//the images to load, this will be dynamic eventually
var sections = ["flyawake", "sleepkit", "science", "survey", "ammo"];
var images = ["icn_flyawake.png", "icn_sleepkit.png", "icn_science.png", "icn_survey.png", "icn_ammo.png"];
var imagesActive = ["icn_flyawakeActive.png", "icn_sleepkitActive.png", "icn_scienceActive.png", "icn_surveyActive.png", "icn_ammoActive.png"];
var captions = [
"<h3>FlyAwake</h3>Originally developed under the Air National Guard SEF leadership team (ANG/SEF), the FlyAwake fatigue mitigation program is now sponsored by OSD’s DSOC (Defense Safety Oversight Council) in order to expand the benefits military-wide. The FlyAwake application is available for all Department of Defense agencies and military flying units in order to provide mitigation strategies in the air, on the ground or at sea. The critical objective of this program is to provide you with the ability to make the most informed decision regarding fatigue risks on a daily basis. Register today in order to mitigate fatigue risk in your missions.  Click <a href='/documents/DSOCFlyAwakeDemov2.wmv' target='_blank'>here</a> to see the demo!",
"<h3>Sleep Kit</h3>What’s sleep got to do with it? In short, everything. Don’t fear, kits will soon be here!  The Air National Guard, in conjunction with the Navy Medical Research Center and the University of Pittsburgh, is developing a pocket sized sleep kit for service members.   The kit will contain items to help with a good night’s sleep, along with education and proven mental techniques for those nights you just can’t turn your mind off.  Click <a href='mailto:sleepkits@flyawake.org' target='_blank'>here</a> to stay informed of the latest on Sleep Kits.<p>  <a href='http://www.facebook.com/pages/Combat-Fatigue/363301329676?ref=ts' title='Visit us on Facebook' target='_blank'>Combat Fatigue</a> is focused on providing simple, easy to use tools, to help military personnel get a good night sleep, in order to increase their performance and stay healthy and safe.</p>",
"<h3>Fatigue Physiology</h3>Sustained wakefulness within military combat operations has long been an issue directly related to safety and its impact on those who enter battle. Fatigue remains a formidable enemy in theater and shift work operations due to its pervasive impact on cognitive effectiveness – science shows fatigue directly correlates related to increased mishap risk.  Fatigue drives breakdowns in CRM, shortens attention spans, increases susceptibility to spatial disorientation, and causes deadly micro-sleep events in crews, even on final approach and landing. For more information about the science of fatigue and mitigating strategies for you and your personnel, click <a href='http://www.safetycenter.navy.mil/fatigue/index.asp' target='_blank'>here</a>. FlyAwake v2.0, funded through OSD’s Defense Safety Oversight Council, is a feature-rich new web application designed for ease-of-use on the ground, in the air or at sea, so you can identify exactly HOW at risk your crewmembers are of having a fatigue-related mishaps – and take appropriate mitigation strategies.  Sign up for FlyAwake today!",
"<h3>Fatigue Survey</h3>Would you like to become part of the solution?  Air Force Colonel (Dr.) Karen Klingenberger, of USAF School of Aerospace Medicine, is working hard to help aircrew suffering from mission-related fatigue.   Click <a href='survey.aspx?survey=fatigue' target='_blank'>here</a> to participate in a survey (rest assured, no personal information is collected) so that we can continue to make huge strides in the science of fatigue mitigation.",
"<h3>AMMO</h3>OSD’s Defense Safety Oversight Council has teamed with the Air National Guard and the Walter Reed Army Institute of Research to develop the ultimate tool for fatigue risk mitigation across Services.   This coming year,  Macrosystems, Inc. will be developing AMMO – Alertness Management in Military Operations -- next-gen software that fuses technological innovation with the daily needs of our Warfighters.   AMMO will have modules for aviation, shift work, and fatigue research.  It will be a one-stop-shop for reducing fatigue risk across the globe, and will offer real-time fatigue assessments using the latest technological advancements in wrist-worn actigraphy.   What do you want to see in AMMO?  What do you need it to do for you and your unit?  We need you to tell us.  Click <a href='mailto:admin@flyawake.org' >here</a> to join one of our user groups today!"
];
var titles = ["FlyAwake", "Sleep Kit", "Fatigue Physiology", "Fatigue Survey", "AMMO"];
var hrefs = ["/Default.aspx", "/Default.aspx", "/Default.aspx", "/Default.aspx", "/Default.aspx"];

var displaySurvey = window.location.search.substring(1).toLowerCase().match("survey") == "survey";

$(document).ready(function() {

    initializeItems();

    initializeSectionDivs();

    carousel.start();

    carousel.selectItem(0);
});

function initializeItems() {
    var itemIndex = 0;
    for (var i = 0; i < sections.length; i++) {
        if (i == 3)
            continue;

        var item = new CarouselItem();
        item.image = $("<img id='img_" + sections[i] + "' src='images/carousel/" + images[i] + "' alt='" + titles[i] + "' idx='" + itemIndex + "' />");
        item.section = sections[i];
        item.caption = captions[i];
        item.index = itemIndex;
        item.title = titles[i];
        item.href = hrefs[i];
        carousel.addItem(item);

        itemIndex++;
    }
}

function initializeSectionDivs() {
    var sectionContainer = $("#carouselSelectors");
    var containerWidth = sectionContainer.width();

    for (var i = 0; i < carousel.items.length; i++) {
        var divScroll = $("<div class='carouselIcon'></div>");
        divScroll.width(containerWidth / carousel.items.length);
        var image = carousel.items[i].image;
        var title = $("<div class='title'>" + carousel.items[i].title + "</div>");
        var anchor = $("<a href='" + carousel.items[i].href + "'/>");
        if (document.all) $(anchor).css("filter", "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='images/carousel/" + images[i] + "')");
        else $(anchor).css("background", "url(images/carousel/" + images[i] + ")");

        sectionContainer.append(divScroll);
        divScroll.append(image);
        divScroll.append(title);
        divScroll.append(anchor);
    }
}

function Carousel(element) {
    var selectedIndex = 0;
    var previousIndex = 3;
    this.items = [];
    var timeoutID = null;

    this.addItem = function(item) {
        this.items.index = this.items.length;
        this.items[this.items.length] = item;
    }

    this.activateItem = function(index, time, highLight) {
        if (highLight) {
            $("#carouselCaption").fadeOut(TRANSITION_TIME, function() {
                $("#caption").html(carousel.items[index].caption);
                //                $("#captionHref").attr("href", carousel.items[index].href);

                //and fade it back in
                $("#carouselCaption").fadeIn(2 * TRANSITION_TIME);
            });
        }

        $(carousel.items[index].image).stop().animate({
            height: "150px",
            paddingTop: "0px"
        },
				time, 'swing',
				function() {
				    if (highLight) {
				        $(carousel.items[index].image).attr("src", "images/carousel/icn_" + carousel.items[index].section + "Active.png");
				    }
				});
    }

    this.deActivateItem = function(index, time) {
        $(carousel.items[index].image).stop().animate({
            height: "100px",
            paddingTop: "20px"
        },
				time, 'swing',
				function() {
				    $(carousel.items[index].image).attr("src", "images/carousel/icn_" + carousel.items[index].section + ".png");
				});
    }

    this.selectItem = function(index) {

        this.stopAnimations();

        previousIndex = selectedIndex;

        carousel.deActivateItem(previousIndex, 100);

        carousel.activateItem(index, TRANSITION_TIME, true);

        //set the new selected index
        selectedIndex = index;

        //set the next rotation period
        timeoutID = setTimeout("carousel.selectItem(" + carousel.getNextIndex(index) + ")", ROTATION_TIME);
    };

    this.stopAnimations = function() {
        $("#carouselCaption").stop(true, true);
        for (var i = 0; i < this.items.length; i++) {
            $(this.items[i].image).stop(true, true);
        }
    }

    this.getNextIndex = function(index) {
        index = index * 1;
        var newIndex = 0;
        if (index == null || index == this.items.length - 1) {
            newIndex = 0;
        }
        else {
            newIndex = index + 1;
        }

        return newIndex;
    }

    this.start = function() {
        $("#carouselSelectors img").hover(
        //set the mouse enter function
        //expand the  when the mouse is over
			function(e) {
			    var itemIndex = $(this).attr("idx");
			    if (itemIndex != selectedIndex)
			        carousel.activateItem(itemIndex, 100, false);
			},
        //and the mouse leave function
        //collapse the  unless it is currently selected
			function(e) {
			    var itemIndex = $(this).attr("idx");
			    if (itemIndex != selectedIndex)
			        carousel.deActivateItem(itemIndex, 400);
			}
		);

        //when the mouse is up on an 
        //select that
        $("#carouselSelectors img").click(function() {
            clearTimeout(timeoutID);
            var itemIndex = $(this).attr("idx");
            if (itemIndex != selectedIndex)
                carousel.selectItem(itemIndex);
        });
    };

    this.stop = function() {
        if (timeoutID)
            clearTimeout(timeoutID);
    };
}

function CarouselItem() {
    this.image = null;
    this.section = null;
    this.icon = null;
    this.caption = null;
    this.title = null;
    this.index = null;
    this.subCaption = null;
    this.href = null;
}