(function($) {
    $(function() {



        var sliderParent = $("#home-slider>div"),
            sliderWrap = sliderParent.children("div"),
            sliderItems = sliderWrap.find("img"),
            prevButton = sliderParent.parent().find("a").eq(0).css("z-index", 500),
            nextButton = sliderParent.parent().find("a").eq(1).css("z-index", 500);
        sliderItems.css({
            'width' : '134px',
            'height' : '356px'
        });
        var animateToFull = function(elem) {
            $(elem).animate({
                'margin-bottom' : '-12px',
                'width' : '175px',
                'height' : '465px',
                'margin-left' : '-20px'
            }, 250);
        }
        var animateToMedium = function(elem) {
            $(elem).animate({
                'margin-bottom' : '-12px',
                'width' : '150px',
                'height' : '414px',
                'margin-left' : '-20px'
            }, 250);
        }
        var animateToSmall = function(elem) {
            $(elem).animate({
                'margin-bottom' : 0,
                'width' : '134px',
                'height' : '356px',
                'margin-left' : 0
            }, 250);
        }
        var cancelAnimations = function() {
            sliderItems.stop(true, true);
        }
        var getState = function(elem) {
            var elemWidth = $(elem).css("width");
            if(elemWidth == "175px") return "full";
            if(elemWidth == "150px") return "medium";
            if(elemWidth == "134px") return "small";
        }
        sliderItems.click(function() {
            window.location = $(this).parent().attr("href");
        });
        sliderItems.hover(
            function() {
                cancelAnimations();
                var $t = $(this);
                var tState = getState(this);
                if(tState == "full") return;
                animateToFull(this);
                animateToMedium($t.parent().prev().find("img")[0]);

                animateToMedium($t.parent().next().find("img")[0]);

            }, function() {

            });
        $("#slider-content").hover(function() {
            clearTimeout(sliderAnim); sliderAnim = null;
        }, function() {
            cancelAnimations();
            sliderItems.each(function() {
                animateToSmall(this);
            });
            clearTimeout(sliderAnim);
            sliderAnim = setTimeout(function() {
                pullNegative();
            }, 8000);
        });
        var pullNegative = function() {
            //pull it left (- the margin)  
            sliderWrap.stop(true, true);
            clearTimeout(sliderAnim);
            sliderItems.css({
                'width' : "134px",
                'height' : "356px"
            });
            if(currentSet == totalSets-1) {
                sliderWrap.animate({
                    'margin-left' : '0px'
                }, 500, function() {
                    sliderAnim = setTimeout(function() {
                        pullNegative();
                    }, 8000);
                    currentSet = 0;
                });
            } else {
                sliderWrap.animate({
                    'margin-left' : '-=1042px'
                }, 500);
                currentSet++;
            }
            sliderAnim = setTimeout(function() {
                pullNegative();
            }, 8000);
        };
        var pullPositive = function() {
            sliderWrap.stop(true, true);
            clearTimeout(sliderAnim); 
            sliderItems.css({
                'width' : "134px"
            });
            if(currentSet == 0) {
                sliderWrap.animate({
                    'margin-left' : (totalSets-1)*-1042 + "px"
                }, 500, function() {
                    sliderAnim = setTimeout(function() {
                        pullNegative();
                    }, 8000);
                    currentSet = totalSets-1;
                });
            } else {
                sliderWrap.animate({
                    'margin-left' : "+=1042px"
                }, 500, function() {
                    sliderAnim = setTimeout(function() {
                        pullNegative();
                    }, 8000);
                    currentSet--;
                });
            }
                
        };
        clearTimeout(sliderAnim);
        var sliderAnim = setTimeout(function() {
            pullNegative();
        }, 8000);
        //setting up
        sliderItems.eq(0).addClass("first-viewable");
        currentSet = 0;
        totalSets = Math.floor((sliderItems.length/6)) + (sliderItems.length % 6 ? 1 : 0);
        sliderParent.parent().delegate("a", "click", function(e) {
            
            sliderWrap.stop(true, true);
            clearTimeout(sliderAnim);
            sliderAnim = null;
            var $t = $(this);
            if($t.text() == "Right") {
                pullNegative();
            } else {
                if($t.text() == "Left") {
        
                    pullPositive();
                }
            }
            return false;

        });

    });    



})(jQuery);

