$(document).ready(function(){
    //jQuery.easing.def="easeOutExpo";
    var top=0;
    var left=356;
    var zindex=6;
    var slideSpeed=2000;
    var layerNum=4;
    var currentDiv=0;
    var increase=true;
    var slideInterval=7000;
    var triggerSlidex=function(){
        //get move direction
        if(currentDiv <= 0){
            increase=true;
            currentDiv=0;
        }
        if(currentDiv >= (layerNum-1)){
            increase=false;
            currentDiv=layerNum-1;
        }
        //get div id
        if(increase==true) currentDiv+=1;
        else currentDiv-=1;
        //setup new slideInterval
        slideInterval=7000;
        //trigger sliding
        $("p#test").html(currentDiv);
        $(this).children("div:eq("+currentDiv+")").trigger("trySlide");
    };
    var slide=function(){ //set slide action
        var temp=$(this);
        temp.removeClass("right");
        //show this div if hidden
        if(!temp.hasClass("left")){
            temp.animate({"left": "-=496px"},slideSpeed,"easeOutExpo");
        }
        temp.addClass("left");
        //show all on the right side
        temp.nextUntil(".right").each(function(){
            $(this).animate({"left": "+=496px"},slideSpeed,"easeOutExpo");
            $(this).removeClass("left");
            $(this).addClass("right");
        });
        //hide all on the left side
        temp.prevUntil(".left").each(function(){
            $(this).animate({"left": "-=496px"},slideSpeed,"easeOutExpo");
            $(this).addClass("left");
            $(this).removeClass("right");
        });
        if (currentDiv==0){
            $(".feature-container").css({"border-left":"#cccccc 1px solid"});
        }
        else{
            $(".feature-container").css({"border-left":"#ffffff 1px solid"});
        }
        if(currentDiv==(layerNum-1)){
            $(".feature-container").css({"border-right":"#cccccc 1px solid"});
        }
        else{
            $(".feature-container").css({"border-right":"#cccccc 1px solid"});
        }
    };
    $(".feature-container .slice").addClass("right");
    $(".feature-container .slice").each(function(){
        //set initial position
        $(this).css({"top": top, "left": left, "z-index": zindex});
        left+=140;
        zindex+=2;
        //bind actions "trySlide" and "click"
        $(this).bind("trySlide",slide);
        $(this).click(function(){
            //stop current timer
            $("div.feature-container").stopTime("slide");
            
            //get the id of current div
            currentDiv=$(".feature-container .slice").index($(this));
            
            //remove all "mouseleave" action
            $(".feature-container .slice").each(function(){
                $(this).unbind("mouseleave");
            });
            
            //features keep on going
            //$("div.feature-container").everyTime(slideInterval,"slide",triggerSlidex,12);
            
            //clear slideshow timeout
            clearSlideshowTimeout();

            // clear slideshow timeout
            /*
            if (window.aSlideshowIntervalTimeouts !== undefined)
            {
                if (window.aSlideshowIntervalTimeouts['a-6-ucp-slideshow-1'])
                {
                    clearTimeout(window.aSlideshowIntervalTimeouts['a-6-ucp-slideshow-1']);
                }
            }
            */

            $('div.feature-container').stopTime();

            //set "mouseleave" action on current div
            $(this).mouseleave(function(){
                $("div.feature-container").everyTime(slideInterval,"slide",triggerSlidex,12);
                $(this).unbind("mouseleave");
                window.aSlideshowIntervalTimeouts['a-6-ucp-slideshow-1']=setTimeout(function(){
                    $('#a-slideshow-6-ucp-slideshow-1').find('.a-slideshow-image:eq(1)').click();
                },slideInterval);
                //setTimeout(clearSlideshowTimeout('a-6-ucp-slideshow-1'),8400);
                $("div.feature-container").everyTime(84000, 'slideshowStop', clearSlideshowTimeout);
            });
                        
            //slideshow keep on going
            //$('#a-slideshow-6-ucp-slideshow-1').find('.a-slideshow-image:eq(1)').click()
            
        });
        $(this).click(slide);
    });
    //
    $("div.slideClick").each(function(){
        var temp=$(this).closest("div.slice").prev();
        $(this).click(function(event){
            event.stopPropagation();
            temp.click();
        });
    });
    //init
    slideSpeed=0;
    $("div.slice:first").click();
    slideSpeed=2000;
    //set timer
    $("div.feature-container").everyTime(slideInterval,'slide',triggerSlidex,12);
    //setTimeout('clearSlideshowTimeout()',8400);
    $("div.feature-container").everyTime(84000,'slideshowStop',clearSlideshowTimeout);
});
    function clearSlideshowTimeout(){
    // clear slideshow timeout
        if (window.aSlideshowIntervalTimeouts !== undefined)
        {
            if (window.aSlideshowIntervalTimeouts['a-6-ucp-slideshow-1'])
            {
                clearTimeout(window.aSlideshowIntervalTimeouts['a-6-ucp-slideshow-1']);
            }
        }
    }

