$(function(){

    var commentsScrollSettings = {
        scrollbarWidth:9,
        dragMinHeight:40,
        dragMaxHeight:40
    };

    $('#s').labelify();

    $('#sneak-peek, .post .post-gallery').dxSlider();

    $('div.comments .list li span a').attr('rel', 'external');
    $("a[rel=external]").attr('target', '_blank');
    
    var validateOptions = {
        rules:{
            author:{
                required:true,
                minlength:2
            },
            email:{
                required:true,
                email:true
            },
            comment:{
                required:true
            }
        },
        ignoreTitle: true,
        onkeyup: false,
        onfocusout: false,
        showErrors:function(errorMap, errorList){
            $.each(errorList, function(key, elm){
                $(elm.element)
                   .animate( {opacity: 0.2}, 175 )
                   .animate( {opacity: 1}, 150 )
                   .animate( {opacity: 0.2}, 150 )
                   .animate( {opacity: 1}, 150 )
                   .animate( {opacity: 0.2}, 150 )
                   .animate( {opacity: 1}, 150 );
            })
        },
        submitHandler:function(_form){
            var form = $(_form);

            var post = form.parents('div.post');
            var comments = post.find('div.comments div.list ul');
            var messages = post.find('div.messages');
            var post_id = form.find('input[name=comment_post_ID]').val();
            var link = form.attr('action');
            var loadLink = link.replace('wp-comments-post.php', 'wp-content/themes/natalienorton/comments-ajax.php?id=' + post_id + '&type=comments')

            form.ajaxSubmit({
                beforeSubmit:function(){
                    form.find('div.say').hide();
                },
                success:function(){
                    if (!comments.length) {
                        messages.before('<div class="comments"><div class="list"><ul></ul></div></div>');
                        post.find('div.comments').show();
                        comments = post.find('div.comments div.list ul');
                    }
                    messages.hide().html('Thank you for commmenting!').slideDown('fast');

                    form.clearForm();
                    form.find('div.say').show();

                    comments.load(loadLink, {}, function(){
                        if (!comments.is(':visible')) {
                            comments.show();
                        }
                        var commentsList = comments.parent()
                        commentsList.jScrollPane(commentsScrollSettings);
                        comments.scrollTo();
                    });

                },
                error:function(request){
                    var error = request.responseText.match(/<p>([^<]+)<\/p>/)[1];
                    messages.hide().html(error).slideDown('fast');
                    form.find('div.say').show();
                }
            });
        }
    };
    
    $('form.comment-form').each(function(key, form){
        $(form).validate(validateOptions);
    });
    
    $('#nav ul li a').click(function(){
        var fn = $(this);
        var container = $('#nav-' + fn.parent().attr('class'));
        if (container.length) {
            openContainer(container, fn);
            return false;
        }
        return true;
    });

    var openContainer = function(container, fn)
    {
        fn.parent().siblings().find('a').removeClass('active');
        if (container.is(':visible')) {
            container.slideUp('fast', function(){
                $('#nav').css('border-color', '#fff');
                fn.removeClass('active');
            });
            
        } else {
            var sibl = container.siblings(':visible');
            if (sibl.length) {
                sibl.slideUp('fast', function(){
                    container.slideDown('fast');
                });
            } else {
                container.slideDown('fast');
            }
            $('#nav').css('border-color', '#c6d139');
            fn.addClass('active');
        }
    }

    

    


    $('div.post a.add-comment').click(function(){
        var link = $(this);
        var post = $(this).parents('div.post');
        var comments = post.find('div.comments');
        if (!comments.is(':visible')) {
            post.find('a.view-comments').trigger('click');
        }
        post.find('div.comment-n-share').slideToggle('fast', function(){
            var fn = $(this);
            if (fn.is(':visible')) {
                fn.scrollTo();
            }
        });
        

        return false;
    });


	/*

    $('div.post a.add-comment').click(function(){
        var link = $(this);
        var post = $(this).parents('div.post');

        post.find('div.comment-n-share').slideToggle('fast', function(){
            var fn = $(this);
            if (fn.is(':visible')) {
                link.scrollTo();
            }
        });

        return false;
    });
	
	*/

    $('div.post a.view-comments').click(function(){
        var link = $(this);
        var post = $(this).parents('div.post');

        post.find('div.comments').slideToggle('fast', function(){
            var fn = $(this);
            if (fn.is(':visible')) {
                $(this).find('.list').jScrollPane(commentsScrollSettings);
                link.scrollTo();
            }
        });
        return false;
    });

    $('div.post a.open-comments').click(function(){
        $(this).parents('div.post').find('a.view-comments').trigger('click');
        return false;
    })



    $('div.post a.toggle').click(function(){
        var fn = $(this);
        var post = fn.parents('div.post');
        var more = post.find('div.more');
        
        if (more.is(':visible')) {
            more.slideUp('fast', function(){
                fn.text('click here to open post');
            });
            more.find('div.comment-n-share, div.comments').hide();
        } else {
            var postSiblings = post.siblings('div.post').find('div.more:visible');
            postSiblings.hide()
                .find('div.comment-n-share, div.comments').hide();
            
            more.slideDown('fast', function(){
                fn.text('click here to close post');
                post.scrollTo();
            });
        }
        return false;
    });

    $('div.post .post-gallery').click(function(){
        $(this).parents('.post').find('a.toggle').trigger('click');
        return false;
    });


    var posts = $('div.post');

    if (posts.length < 2) {
        posts.find('a.toggle').trigger('click');
    }


    $('#footer-contact a.toggle').click(function(){
        var content = $(this).parent().find('div.content');

        content.slideToggle('fast', function(){
            var fn = $(this);
            if (fn.is(':visible')) {
                fn.scrollTo();
            }
        });

        return false;
    });


    setTimeout(function(){
        $('#header-welcome').fadeOut(2000, function(){
            $('#header-image').fadeIn(2000, function(){
                $('#header h1 a').fadeIn(2000);
            });
        });
    }, 3000);


    $('#header .line1, #header .line2').animate({
        width:'728px'
        }, 2000, function() {
            
    });

});
