$(document).ready(function(){


    $('.subnavigation').hide();


    $('.sub_collapse').hover(

        function () {
            $(this).toggleClass("active")

            $(this).find(".subnavigation").stop(true, true).fadeIn('fast');
        }, function() {
            $(this).find(".subnavigation").stop(true, true).fadeOut('fast');
        }
        );


    $.each($('.slide'), function(){

        if($(this).is(':visible') == true){

            var current = $(this).attr("id");
            var next    = $(this).next(".slide").attr("id");
            var prev    = $(this).prev(".slide").attr("id");

            if(prev == undefined){
                $("#arrow_left").hide();
            }
            else {
                $("#arrow_left").show();
            }
            if(next == undefined){
                $("#arrow_right").hide();
            }
            else {
                $("#arrow_right").show();
            }


        }

    });

    $("#arrow_right").click(function (){

        $.each($('.slide'), function(){

            if($(this).is(':visible') == true){

                var current = $(this).attr("id");
                var next    = $(this).next(".slide").attr("id");
                var prev    = $(this).prev(".slide").attr("id");

                var next2   = $(this).next(".slide").next(".slide").attr("id");
                var prev2   = $(this).next(".slide").prev(".slide").attr("id");

                if(prev2 == undefined){
                    $("#arrow_left").fadeOut('fast');
                }
                else {
                    $("#arrow_left").fadeIn('fast');
                }
                if(next2 == undefined){
                    $("#arrow_right").fadeOut('fast');
                }
                else {
                    $("#arrow_right").fadeIn('fast');
                }

                $('#'+current).fadeOut('slow', function() {
                    $('#'+next).fadeIn('fast');
                });



            }

        });
    });

    $("#arrow_left").click(function (){

        $.each($('.slide'), function(){

            if($(this).is(':visible') == true){

                var current = $(this).attr("id");
                var next    = $(this).next(".slide").attr("id");
                var prev    = $(this).prev(".slide").attr("id");

                var prev2   = $(this).prev(".slide").prev(".slide").attr("id");
                var next2   = $(this).prev(".slide").next(".slide").attr("id");

                if(prev2 == undefined){
                    $("#arrow_left").fadeOut('fast');
                }
                else {
                    $("#arrow_left").fadeIn('fast');
                }
                if(next2 == undefined){
                    $("#arrow_right").fadeOut('fast');
                }
                else {
                    $("#arrow_right").fadeIn('fast');
                }
                

                $('#'+current).fadeOut('slow', function() {
                    $('#'+prev).fadeIn('fast');
                });


            }

        });
    });




});
