fmn.slides = {};

fmn.slides.duration = 10000;

fmn.slides.settimer = function(){
    if(typeof(fmn.slides.timer) == "number"){
        clearInterval(fmn.slides.timer);
    }
    fmn.slides.timer = window.setInterval(function() {
        fmn.slides.next();
    }, fmn.slides.duration);
};
fmn.slides.next = function(){
    var from = $("#slideshow .slides a.active");
    var toIndex = 0;
    if(parseInt(from.attr("pos"),10)+1 < $("#slideshow .slides a").length){
        toIndex = parseInt(from.attr("pos"),10)+1;
    }
    var to = $("#slideshow .slides a:eq("+toIndex+")");

    to.css("left","100%").show().animate({left: "0%"}, 450, "linear", function(){
        $(this).addClass("active");
    });
    from.animate({left:"-100%"}, 450, "linear", function(){
        $(this).hide().removeClass("active");
    });
};
fmn.slides.back = function(){
    var from = $("#slideshow .slides a.active");
    var toIndex = parseInt(from.attr("pos"),10)-1;
    if(toIndex == -1){
        toIndex = $("#slideshow .slides a").length-1;
    }
    var to = $("#slideshow .slides a:eq("+toIndex+")");

    to.css("left","-100%").show().animate({left: "0%"}, 450, "linear", function(){
        $(this).addClass("active");
    });
    from.animate({left:"100%"}, 450, "linear", function(){
        $(this).hide().removeClass("active");
    });
};
fmn.slides.watch = function(){
    window.location.href = $("#slideshow .slides a.active").attr("href");
};
fmn.slides.init = function(){
    /******************
        FMA Sliders
    *******************/
    $("#slideshow .slides a")
        .each(function(i){
            $(this).attr("pos",i);
            if(i===0){
                $(this).show().addClass("active");
            } else {
                $(this).hide();
            }
        });

    $("#slideshow .nav a[href=#next]").click(function(){ fmn.slides.next(); fmn.slides.settimer(); return false;});
    $("#slideshow .nav a[href=#back]").click(function(){ fmn.slides.back(); fmn.slides.settimer(); return false; });
    $("#slideshow .nav a[href=#watch]").click(function(){ fmn.slides.watch(); return false; });

    /* Auto Advance FMA */
    fmn.slides.settimer();    
};

