$(function() {

    $("ul.menu > li > a").click(function() {
        $("ul.menu > li").removeClass("active");
        if($(this).parent().children("ul").size() > 0) {
            $("ul.menu > li > ul").slideUp("slow");
            $(this).parent().addClass("active");
            $(this).parent().children("ul").slideDown("slow");
            return(false);
        }
    });

    $(".thumbnails img").not('.active').fadeTo("slow", 0.4);

    $(".thumbnails a").click(function() {
        var thumbnail = $(this);
        $(".thumbnails img.active").each(function(i){
            $(this).removeClass("active").fadeTo("slow", 0.4);
        });
        $(thumbnail).children("img").addClass("active").fadeTo("slow", 1);
        $(".project-image img").fadeOut(800, function() {
            if($(thumbnail).attr("rel") != "") {
                $(".project-image > a").attr("href",$(thumbnail).attr("rel"));
            } else {
                $(".project-image > a").attr("href", "#");
            }
            $(".project-image img").attr("src",$(thumbnail).attr("href")).delay(400).fadeIn(800);
        });
        return(false);
    });

    $(".project-image > a").live("click", function() {
        if($(this).attr("href") != "#") {
             //$(".project-image > a").fancybox();
             //$(".project-image > a").trigger('click');
             $.fancybox(
             {'margin':10, 'type' : 'image', 'href' : $(this).attr("href")}
             );

        }
        return (false);
    });

    if($(".scroll-clip").height() < $(".scroll-item").height()) {
        $(".scroll-button-large, .scroll-button-thumbnail").show();
    }

    $(".description > h3 > a").click(function() {
        $(".description > h3").toggleClass("active");
        $("#description-box").slideToggle("slow");
        if($(".scroll-container-small").size() > 0) {
            var topOffset = $(".scroll-container-small").offset();
            var bottomOffset = $("#page-footer").offset();
            var adHeight = bottomOffset.top - topOffset.top - 14;
            $(".scroll-clip-small, .scroll-container-small").height(adHeight);
        }
        if($(".scroll-clip-small, .scroll-clip-medium").height() < $(".scroll-item-small, .scroll-item-medium").height()) {
            $(".scroll-button-small, .scroll-button-medium").show();
        }
        return(false);
    });

    $(".scroll-next").click(function(event){
        event.preventDefault();
        if($(this).hasClass('scroll-button-large')) {
            var position = $('.scroll-item-large').position();
            if($('.scroll-item-large').not(":animated") && (($('.scroll-item-large').height() - $('.scroll-clip-large').height() + position.top) > 0))
            {
                $('.scroll-item-large').animate( {top:'-=100px'}, 500 );
            }
        } else if($(this).hasClass('scroll-button-medium')) {
            var position = $('.scroll-item-medium').position();
            if($('.scroll-item-medium').not(":animated") && (($('.scroll-item-medium').height() - $('.scroll-clip-medium').height() + position.top) > 0))
            {
                $('.scroll-item-medium').animate( {top:'-=100px'}, 500 );
            }
        } else if($(this).hasClass('scroll-button-small')) {
            var position = $('.scroll-item-small').position();
            if($('.scroll-item-small').not(":animated") && (($('.scroll-item-small').height() - $('.scroll-clip-small').height() + position.top) > 0))
            {
                $('.scroll-item-small').animate( {top:'-=100px'}, 500 );
            }
        } else if($(this).hasClass('scroll-button-thumbnail')) {
            var position = $('.scroll-item-thumbnail').position();
            if($('.scroll-item-thumbnail').not(":animated") && (($('.scroll-item-thumbnail').height() - $('.scroll-clip-thumbnail').height() + position.top) > 0))
            {
                $('.scroll-item-thumbnail').animate( {top:'-=100px'}, 500 );
            }
        }

        return(false);
    });

    $(".scroll-prev").click(function(event){
        event.preventDefault();
        if($(this).hasClass('scroll-button-large')) {
            var position = $('.scroll-item-large').position();
            if($('.scroll-item-large').not(":animated") && position.top < 0) {
                $('.scroll-item-large').animate( {top:'+=100px'}, 500 );
            }
        } else if($(this).hasClass('scroll-button-medium')) {
            var position = $('.scroll-item-medium').position();
            if($('.scroll-item-medium').not(":animated") && position.top < 0) {
                $('.scroll-item-medium').animate( {top:'+=100px'}, 500 );
            }
        } else if($(this).hasClass('scroll-button-small')) {
            var position = $('.scroll-item-small').position();
            if($('.scroll-item-small').not(":animated") && position.top < 0) {
                $('.scroll-item-small').animate( {top:'+=100px'}, 500 );
            }
        } else if($(this).hasClass('scroll-button-thumbnail')) {
            var position = $('.scroll-item-thumbnail').position();
            if($('.scroll-item-thumbnail').not(":animated") && position.top < 0) {
                $('.scroll-item-thumbnail').animate( {top:'+=100px'}, 500 );
            }
        }
    });


    $("#subject-list a").live('click', function(){
        $("#subject-list input").attr("checked",false);
        $("#subject-list li").removeClass("active");
        $(this).parent().addClass("active");
        $(this).prev().attr("checked",true);
        return (false);
    });

    $("a.read").click(function(){
        $(this).parent().children(".answer").toggle();
        if($(".scroll-clip").height() < $(".scroll-item").height()) {
            $(".scroll-button-large, .scroll-button-thumbnail").show();
        }
        return (false);
    });
    
    $("#contact-form input.input-button").live('click', function(){
        $.ajax({
            url : "forum/",
            type: "POST",
            data: ({
                command : "sendQuestion",
                component : "consulting",
                name : $("input[name='name']").val(),
                email : $("input[name='email']").val(),
                subject_id : $("input[name='subject_id']:checked").val(),
                question : $("textarea[name='text']").val()
            }),
            dataType: "html",
            async: false,
            success: function(innerHTML){
               $("#contact-form").html(innerHTML); 
            }
       });
    });

});
