﻿//Don't do anything until the page is ready

var carouselImgArr = [];
var hoverImgNumber = 0;
var initialPosition = true;
var LargestPic = 400;
var LargestPicWidth = 460;
var change = .15;
var spaceBetweenPics = 145;
var currentPic = 3;

$(document).ready(
    function () {
        //Call function that will set all carousel images into an array
        
        if (initialPosition) {
            LoadCarouselArray();
            shufflePics(3);
        }

        //Call the function that will listen for the rollover
        LoadCarousel();
    }
);

//Put all carousel images into an array
this.LoadCarouselArray = function () {
    for (var i = 0; i < $("img.CarouselImage").length; i++) {
        carouselImgArr.push($("img.CarouselImage")[i]);
    }
};


//Function called when an image in the carousel is hovered over
this.LoadCarousel = function () {
    $("img.CarouselImage").hover(
                function () {
                    hoverImgNumber = this.name;
                    // $(this).stop();
                    fadeCurrentPic();
                    shufflePics(hoverImgNumber);
                }, function () {
                    //TODO: shuffle
                    //$(this).stop().animate({ width: "110px" }, "slow");
                });

    $.each(carosulImgArr, function (i, v) {

    });

};

this.CalculateNewDimention = function (MousedPicNum, TargetPicNum) {
    var retWidth = Math.abs(((Math.abs(MousedPicNum - TargetPicNum)*change)*LargestPic)-LargestPic);
    return retWidth;
};

this.CalculateNewYPos = function (MousedPicNum, TargetPicNum) {
    var retYpos = Math.abs(((Math.abs(MousedPicNum - TargetPicNum) * change) * LargestPicWidth) / 2);
    return retYpos;
};

this.CalculateNewXPos = function (TargetPicNum) {
    var retXpos = (TargetPicNum - 1) * 145;
    return retXpos;
};

this.CalculateNewLayer = function (MousedPicNum, TargetPicNum) {
    var retYpos = Math.abs(MousedPicNum - TargetPicNum);
    retYpos = 105 - retYpos;
    return retYpos;
};


//function called to position the images on load
this.shufflePics = function (moused) {
    initialPosition = false;
    $.each(carouselImgArr, function (i, v) {
        if (v.name == 1) {
            $(v).stop().animate({ width: CalculateNewDimention(moused, v.name), left: CalculateNewXPos(v.name), top: CalculateNewYPos(moused, v.name), zIndex: CalculateNewLayer(moused, v.name) }, "slow");
            setCurrntPic(moused, v);
        }
        if (v.name == 2) {
            $(v).stop().animate({ width: CalculateNewDimention(moused, v.name), left: CalculateNewXPos(v.name), top: CalculateNewYPos(moused, v.name), zIndex: CalculateNewLayer(moused, v.name) }, "slow");
            setCurrntPic(moused, v);
        }
        if (v.name == 3) {
            $(v).stop().animate({ width: CalculateNewDimention(moused, v.name), left: CalculateNewXPos(v.name), top: CalculateNewYPos(moused, v.name), zIndex: CalculateNewLayer(moused, v.name) }, "slow");
            setCurrntPic(moused, v);
        }
        if (v.name == 4) {
            $(v).stop().animate({ width: CalculateNewDimention(moused, v.name), left: CalculateNewXPos(v.name), top: CalculateNewYPos(moused, v.name), zIndex: CalculateNewLayer(moused, v.name) }, "slow");
            setCurrntPic(moused, v);
        }
        if (v.name == 5) {
            $(v).stop().animate({ width: CalculateNewDimention(moused, v.name), left: CalculateNewXPos(v.name), top: CalculateNewYPos(moused, v.name), zIndex: CalculateNewLayer(moused, v.name) }, "slow");
            setCurrntPic(moused, v);
        }
    });
};

//Set current pic number
this.setCurrntPic = function (moused, CheckPic) {
    if ((moused - CheckPic.name) == 0) {
        currentPic = CheckPic
    }
};



//Fade the current pic out
this.fadeCurrentPic = function () {
    //alert(currentPic.name);
   // currentPic.stop().animate({ width: "1000") }, "slow");
};





