﻿
var CarouselPosition = 1;
var CurrentScrollPosition = 0;
var Scrolling = 0;
var CarouselItemCount = 0;

$(document).ready(function() {
    
    // Contents of ready function moved to own function so it can be run from elsewhere... namely the search!
    startCarousel();

});

function startCarousel() {
    CarouselPosition = 1;
    CurrentScrollPosition = 0;
    Scrolling = 0;
    CarouselItemCount = 0;
    // Set the number of items in the carousel
    CarouselItemCount = $("#CarouselItems li").length;

    // Create the Carousel
    CreateCarousel();
    // Initiate the Carousel
    initCarousel();
}

function initCarousel(){

    //get window url
    var url = window.location.href;
    
    //if url contains #
    if (url.indexOf("#") > 0){
    
        //get itemid from url
        var itemId = url.substring(url.indexOf("#") + 1);
        
        //check item exists on page
        $("#Item" + itemId).each(function() {

            //get item index (convert to number)
            var itemIndex = Number($("#Item" + itemId + ":first").attr("class").substring(2));
           
            //get item X position
            var itemX = $("#Item" + itemId).parent().position().left;

            // This is a really horrible hack because for some reason IE6 is 13553 pixels out !!!!!!!!

            if (navigator.userAgent.indexOf("MSIE 6") != -1) {
                 itemX += 13553;
            }

            // And chrome too aparantly!?
            if (navigator.userAgent.indexOf("Chrome") != -1) {
                 //itemX += 1900;
            }

            itemX = (itemX * -1);
            if (itemIndex > 1) {
                itemX += 28;
            }

            //animate carousel
            Scrolling = 1;
            $("#CarouselItems").animate({ left: itemX }, 500, 'easeInSine', function() { Scrolling = 0; });

            CarouselPosition = itemIndex;
            CurrentScrollPosition = itemX;

        });
    }
        
}

function CreateCarousel(){

    $("#CarouselNext").click(function() {

        if (Scrolling == 0 && CarouselPosition <= CarouselItemCount-2) {
        
            Scrolling = 1;
            var CarouselItems = $("#CarouselItems");
            var CurrentItemsWidth = $("#CarouselItems .CP" + CarouselPosition).width() + 20;            
            CurrentItemsWidth += $("#CarouselItems .CP" + (CarouselPosition+1)).width() + 20;
            CurrentItemsWidth -= 8;

            CurrentScrollPosition = CurrentScrollPosition - CurrentItemsWidth;

            $("#CarouselItems").animate({left: CurrentScrollPosition}, 500,'easeInSine', function(){Scrolling=0;});
            
            CarouselPosition = CarouselPosition+2;
            
        }

    });
    
    
    $("#CarouselPrev").click(function() {

        if (Scrolling == 0 && CarouselPosition > 1) {
        
            Scrolling = 1;
            // Get items
            var CarouselItems = $("#CarouselItems");
            
            // If we are item 2, scroll to beginning, otherwise scroll a bit! 
            if (CarouselPosition == 2 || CarouselPosition == 3) {
                $("#CarouselItems").animate({left: 0}, 500,'easeInSine', function(){Scrolling=0;});
                CarouselPosition = 1;
                CurrentScrollPosition = 0;
            }
            else {
                var CurrentItemsWidth = $("#CarouselItems .CP" + (CarouselPosition-1)).width() + 20;
                CurrentItemsWidth += $("#CarouselItems .CP" + (CarouselPosition-2)).width() + 20;
                CurrentItemsWidth -= 8;
           
            
                CurrentScrollPosition = CurrentScrollPosition + CurrentItemsWidth;
                
                $("#CarouselItems").animate({left: CurrentScrollPosition}, 500,'easeInSine', function(){Scrolling=0;});
                
                CarouselPosition = CarouselPosition-2;
            }
        }

    });

}



//function CreateCarousel2(){

//    var ItemCollectionWidth = 680;
//    var ItemWidth = 68;
//    var ItemsToShift = 3;
//    
//    /* Add rollovers to next and previous buttons */
//    $(".Carousel .ButtonPrev, .Carousel .ButtonNext").hover(
//      function () {
//        $(this).removeClass("Active").addClass("Hover");
//      }, 
//      function () {
//        $(this).removeClass("Hover").addClass("Active");
//      }
//    );
//    
//    /* Add mose down events to next and previous buttons */    
//    $(".Carousel .ButtonPrev, .Carousel .ButtonNext").mousedown(
//      function () {
//        $(this).removeClass("Hover").addClass("Click");
//      }
//    );
//    $(".Carousel .ButtonPrev, .Carousel .ButtonNext").mouseup(
//      function () {
//        $(this).removeClass("Click").addClass("Hover");
//      }
//    );
//    
//    $(".Carousel .ButtonPrev").click(function() {
//        
//        /* Grab the carousel that was clicked and some other variables */        
//        var Carousel = $(this).parent();
//        var CarouselItems = $(this).parent().find(".Items");
//        var CarouselItemCount = CarouselItems.children().size();
//        var CarouselItemsWidth = CarouselItemCount * ItemWidth;
//        var CurrentScrollPosition = parseInt(CarouselItems.css("left"));
//        
//        /* Set new scroll position */        
//        var NewScrollPosition = 0;
//        
//        if (CurrentScrollPosition > - (ItemWidth*ItemsToShift)){
//            NewScrollPosition = 0;
//        }
//        else {
//            NewScrollPosition = CurrentScrollPosition + (ItemWidth * ItemsToShift);
//        }
//        
//        /* Disable previous button if necessary */        
//        if (NewScrollPosition == 0) {
//            $(this).removeClass().addClass("ButtonPrev").addClass("Disabled");
//        }
//        
//        /* Enable Next Button */        
//        $(this).parent().find(".ButtonNext").removeClass().addClass("ButtonNext").addClass("Active");
//        
//        /* Do the animation */ 
//        $(this).parent().find(".Items").animate({left: NewScrollPosition}, {duration: 500, easing: 'easeInSine'});
//                
//    });  
//    
//    $(".Carousel .ButtonNext").click(function() {
//    
//        /* Grab the carousel that was clicked and some other variables */        
//        var Carousel = $(this).parent();
//        var CarouselItems = $(this).parent().find(".Items");
//        var CarouselItemCount = CarouselItems.children().size();
//        var CarouselItemsWidth = CarouselItemCount * ItemWidth;
//        var CurrentScrollPosition = parseInt(CarouselItems.css("left"));
//        
//        /* Set new scroll position */        
//        var NewScrollPosition = 0;
//        
//        if ((CarouselItemsWidth - Math.abs(CurrentScrollPosition) - ItemCollectionWidth) < (ItemWidth * ItemsToShift)){
//            NewScrollPosition = CurrentScrollPosition - (CarouselItemsWidth - Math.abs(CurrentScrollPosition) - ItemCollectionWidth);
//        }
//        else {
//            NewScrollPosition = CurrentScrollPosition -(ItemWidth * ItemsToShift);
//        }
//        
//        /* Disable next button if necessary */        
//        if ((CarouselItemsWidth - Math.abs(CurrentScrollPosition) - ItemCollectionWidth) < (ItemWidth * ItemsToShift + 1) ) {
//            $(this).removeClass().addClass("ButtonNext").addClass("Disabled");
//        }
//        
//        /* Enable Previous Button */        
//        $(this).parent().find(".ButtonPrev").removeClass().addClass("ButtonPrev").addClass("Active");
//        
//        /* Do the animation */ 
//        $(this).parent().find(".Items").animate({left: NewScrollPosition}, {duration: 500, easing: 'easeInSine'});
//        
//    });
//    
//    ResetCarousel()

//}

//function ResetCarousel(){

//    /* set previous buttons to disabled */    
//    $(".Carousel .ButtonPrev").removeClass("Active").addClass("Disabled");

//    /* set next button to enabled */    
//    $(".Carousel .ButtonNext").removeClass("Disabled").addClass("Active");

//    /* Add rollovers to all the phones 
//    $(".Carousel .Items li").hover(
//      function () {
//        $(this).animate({top: -5}, {duration: 200});
//      }, 
//      function () {
//        $(this).animate({top: 0}, {duration: 200});
//      }
//    );
//    */
//}

