// only run when the substr function is broken which is in IE
if ('ab'.substr(-1) != 'b') {
  /**
   *  Get the substring of a string
   *  @param  {integer}  start   where to start the substring
   *  @param  {integer}  length  how many characters to return
   *  @return {string}
   */
    String.prototype.substr = function(substr) {
        return function(start, length) {
        if (start < 0) start = this.length + start;
            return substr.call(this, start, length);
        }
    }(String.prototype.substr);
}

window.wrslider = window.wrslider||{};
(function($) {
    if ($("#slider").length === 0) { return; }
    
    //  list to even wrslider.config and modify wrslider.config object.
    $("#slider").trigger("wrslider.config");
    
    var config = $.extend({
            items: 0,
            width: 930,
            height: 300,
            duration: 1000,
            autoPlay: true,
            delay: 5000,
            transition: "slide",
            contentHidden: true,
            renderContent: true
        }, wrslider.config),
    elements = {
        slider: $("#slider"),
        sliderContent: $("#slider-content"),
        lastItem: $("#slider .last-item"),
        firstItem: $("#slider .first-item"),
        sliderNav: $(".slider-nav"),
        sliderCounter: $("#slider-counter")
    },
    shiftFunction = (function() {
        if (config.transition === "fade") {
            $(".slider-item").css({ "float":"none", "position":"absolute", "opacity": 0, "z-index": 0 });
            $(".first-item").css("opacity", 1);
            return function(index) {
                elements.sliderContent.find(".current-item").removeClass("current-item").stop().css("z-index", 1).animate({"opacity": 0}, config.duration);
                elements.sliderContent.find("#slider-item-" + index).addClass("current-item").stop().css({"opacity": 1, "z-index": 0});
            };
        }
        return function(index) {
            elements.sliderContent.find(".slider-item").removeClass("current-item");
            elements.sliderContent.find("#slider-item-" + index).addClass("current-item");
            elements.sliderContent.stop().animate({left: -(index * config.width)}, config.duration);
        };
    }());
    getItemIndex = function(item){
        return parseInt(item.prop("id").substr(-1), 10);
    },
    getCurrentIndex = function(){
        return getItemIndex(elements.sliderContent.find(".current-item"));
    },
    getCurrentItem = function(){
        return elements.sliderContent.find(".current-item");
    },
    shiftLeft = function(e) {
        var index = getCurrentIndex();
        if (!elements.sliderContent.is(":animated") && index > 0) {
            shiftToIndex(index-1);
        }
        if (index === 0) {
            shiftToIndex(config.items-1);
        }
    },
    shiftRight = function(e) {
        var index = getCurrentIndex();
        if (!elements.sliderContent.is(":animated") && index < config.items-1) {
            shiftToIndex(index + 1);
        }
        if (index === config.items-1) {
            shiftToIndex(0);
        }
    },
    shiftToIndex = function(index) {
        updateCounter(index);
        shiftFunction(index);
        !stopped && config.autoPlay && slideStart();
    },
    updateCounter = function(index) {
        $(".selected", elements.sliderCounter).removeClass("selected");
        $("#select-item-" + index).addClass("selected");
    },
    slideStart = function() {
        stopped = false;
        clearTimeout(slideTimer);
        slideTimer = setTimeout(shiftRight, config.delay);
    },
    slideStop = function() {
        stopped = true;
        clearTimeout(slideTimer);
    },
    
    contentOffset = config.renderContent && $(".slider-item-content:first").length === 1 && $(".slider-item-content:first").position().top
    slideTimer = 0, 
    stopped = false; // End var statement
    
    console.log(config);
    
    $(window).load(function(e){ });
    
    $(function(){
        var c = config;
        c.items = $(".slider-item").length;
        elements.sliderContent.width(c.items * c.width);
        
        $("#slider").bind({
            "mouseenter": function(e) { 
                config.autoPlay && slideStop();
                config.items > 1 && elements.sliderNav.show();
            },
            "mouseleave": function(e) { 
                config.autoPlay && slideStart();
                config.items > 1 && elements.sliderNav.hide();
            }
        });
        
        elements.sliderNav.bind({
            "click": function(e){
                e.preventDefault();
                var dir = $(this).prop("rel");
                if(dir === "next"){
                    shiftRight();
                }
                if (dir === "previous") {
                    shiftLeft();
                }
            },
            "mouseenter": function(e) {
                $(this).find("img").stop().fadeTo(275, 1);
            },
            "mouseleave": function(e) {
                $(this).find("img").stop().fadeTo(275, 0.66);
            }
        });
        
        $("a", elements.sliderCounter).bind("click", function(e) {
            e.preventDefault();
            var index = getItemIndex($(this));
            shiftToIndex(index);
        });
        
        $(window).bind("keydown", function(e) {
           switch(e.which){
               case 39: //-->
               shiftRight();
               break;
               case 37: //<--
               shiftLeft();
               break;
               default:
               break;
           }
        });
        
        $(".slider-item").bind({
           "mouseenter": function(e) {
                var that = $(this).find(".slider-item-content").length === 1 ? $(this).find(".slider-item-content") : $(this),
                    height = that.innerHeight(),
                    pos = that.position().top,
                    titleHeight = elements.slider.height() - pos,
                    newPos = (pos - height) + titleHeight;
                
                if (config.contentHidden && that.find(".slider-entry").length === 1) {
                    that.stop().animate({"top": newPos}, 700);
                }
           },
           "mouseleave": function(e) {
               if ($(this).hasClass("readMore")) {
                   return;
               }
               $(this).find(".slider-item-content").stop().animate({"top": contentOffset}, 700);
           }
        });
        
        var startX,
            endX;
        $("#slider").bind({
            "touchstart": function(e) {
                startX = event.changedTouches[0].screenX;
            },
            "touchend": function(e) {
                endX = event.changedTouches[0].screenX;
                if (startX > endX) {
                    shiftLeft();
                }
                if (startX < endX) {
                    shiftRight();
                }
                startX = endX = null;
            }
        });
        
        config.autoPlay && slideStart();
        
    }); // End onload
}(jQuery));
