﻿
    var timer = null;
    var delay = 8000;
    var speed = 950;
    
    var activeItem;
    var downcount;
    var sliders;
    var itemWidth;
    var offset;
    var margin;
    var autoSlide;
    
    (function( $ ){
        $.fn.gtslider = function() {
            
            return this.each(function(){
            
                activeItem = 1;
                downcount;
                sliders;
                itemWidth = 0;
                offset = 0;
                margin = 10;
                autoSlide = true;
                clearInterval(timer);
                timer = null;
                
                $(".carousel-feature").removeClass("active").removeClass("viewing").removeClass("left").removeClass("next").removeClass("first").css("left", "-9999px");
                
                itemWidth = $(".carousel-feature").width();
                offset = ($("#body").width() - itemWidth) / 2;
  
                sliders = $(".carousel-feature").length;
                
                var first = getItem(1);
                var next = getItem(2);
                var last = getItem(sliders);
                
                $(first).css("left", offset + "px").addClass("active").addClass("viewing").addClass("live");   
                $(first).hover( 
                  function () { 
                    clearTimeout(timer);
                    timer = null;
                  },  
                  function () { 
                    if (timer != null) return false;
                    timer = setTimeout(function() { move("-=") },delay);
                  } 
                ); 
   
                $(next).css("left", offset + itemWidth + margin + "px").addClass("active").addClass("next");
                $(last).css("left", offset - itemWidth - margin + "px").addClass("active").addClass("next");

                $('.nav-left').bind('click', function() {
                  clearInterval(timer);
                  timer = setTimeout(function() { move("+=") }, 100);
                });
                
                $('.nav-right').bind('click', function() {
                  clearInterval(timer);
                  timer = setTimeout(function() { move("-=") }, 100);
                });
                
                scaleImages();

                if (autoSlide) {
                    timer = setTimeout(function() { move("-=") },delay);
                }
                               
            });   
            
            var stop = false;
            
            function scaleImages() {
                
                var myImg = $(".carousel-feature img")[0];
                 
                var imgWidth = $(myImg).width();
                var imgHeight = $(myImg).height();

                var width = $(".carousel-feature").width();
                var height = $(".carousel-feature").height();
                
                var scaler = width / imgWidth;
                
                var newHeight = scaler * imgHeight;
                
                if (newHeight < height) {
                    var scaler = height / imgHeight;
                    var newWidth = scaler * imgWidth;
                    $(".carousel-feature img").width(newWidth);
                    $(".carousel-feature img").height(height);
                }
                else {
                    $(".carousel-feature img").width(width);
                    $(".carousel-feature img").height(newHeight);
                }
                 
            }
            
            function setNext() {
                if (activeItem == sliders) {
                    activeItem = 0;
                }
                activeItem = activeItem + 1;
                var item = getItem(activeItem + 1);
                if (item == null) {
                    item = getItem(1);
                    activeItem = 6;
                }
                $(item).css("left", (offset + (itemWidth + margin) * 2) + "px").addClass("active").addClass("next");
            }

            function setPrev() {
                if (activeItem == 1) {
                    activeItem = sliders;
                }
                else {
                    activeItem = activeItem - 1;
                }
                var item = getItem(activeItem - 1);
                if (item == null) {
                    item = getItem(sliders - 1);
                    activeItem = 1;
                }
                $(item).css("left", (offset - (itemWidth + margin) * 2) + "px").addClass("active").addClass("prev");
            }
           
            function move(operator) {
            
                $(".live").unbind();
                $(".live").removeClass("live");
                
                if (operator == "-=")   {
                    $(".nav-right").hide();
                }
                else {
                    $(".nav-left").hide();
                }
                 switch(activeItem) {
                    case 1:
                    if (operator == "-=")   {
                        $("#id_" + (activeItem + 1)).addClass("viewing");
                    }
                    else {
                        $("#id_" + (sliders)).addClass("viewing");
                    }
                    break;
                    case sliders:
                    if (operator == "+=")   {
                        $("#id_" + (activeItem - 1)).addClass("viewing");
                    }
                    else {
                        $("#id_1").addClass("viewing");
                    }
                    break;
                    default :
                    if (operator == "-=")   {
                        $("#id_" + (activeItem + 1)).addClass("viewing");
                    }
                    else {
                        $("#id_" + (activeItem - 1)).addClass("viewing");
                    }
                    break;
                }
    
                if (operator == "-=")   {
                    setNext();
                }
               
                if (operator == "+=")   {
                    setPrev();
                }

                $(".carousel-feature.active").each(function() {
                    var item = $(this);
                    item.animate(
                        { left: operator + (itemWidth + margin) }, // what we are animating
                        {
                        duration: speed, // how fast we are animating
                        easing: 'easeInOutCirc', // the type of easing
                        complete: function() { // the callback
                            locked = false;
                            if (item.css("left") == "160px") {
                                item.addClass("live");
                                item.hover( 
                                  function () { 
                                    clearTimeout(timer);
                                    timer = null;
                                  },  
                                  function () { 
                                    if (timer != null) return false;
                                    timer = setTimeout(function() { move("-=") },delay);
                                  } 
                                );
                            }
                            else {
                                item.removeClass("viewing");
                            }
                            $(".nav-right").show();
                            $(".nav-left").show();
                        }
                    });
                });
                
                if (autoSlide) {
                    timer = setTimeout(function() { move(operator) },delay); 
                }
            
            }
            
            function getItem(index) {
                return $(".carousel-feature")[index - 1];
            }
            
        };
    })( jQuery );
