jQuery(document).ready(function () {
    navigation();
    searchDropdown();
    cufon();
    tabs();
    minimizePosts();
    inputHovers();
    clearInputs();
    ulfixesPost();
    inputFocus();
    replyPopup();
    featuredPosts();
    checkboxes();
});

function navigation() {
    jQuery("ul#nav > li").each(function () {
        if (jQuery(this).children().size() == 1) {
            jQuery(this).addClass("lonely")
        } else {
            jQuery(this).addClass("popular")
            jQuery(this).find("a:eq(0)").addClass('parent');
            jQuery(this).find('a:eq(0) span').append('<span class="menu-item-arrow">&nbsp;</span>');
        }
    });
    jQuery("ul#nav ul").each(function () {
        jQuery(this).wrap('<div class="menu-div outerbox"></div>');
        jQuery(this).addClass('innerbox');
        jQuery(this).find("li:last-child").addClass("last");
    });

    jQuery("li.popular > div").addClass('children');
    jQuery(".children > ul > li > a").addClass('child');
    jQuery("li.popular div.children > div:first").addClass('grandchildren');

    jQuery(".children > ul > li > a.child").each(function () {
        if (jQuery(this).next().hasClass("outerbox")) {
            jQuery(this).append('<span class="menu-item-arrow">&nbsp;</span>');
        }
    });
    jQuery(".grandchildren a").addClass('grandchild');

    var configParents = {
        sensitivity: 1,
        interval: 150,
        over: function () {
            jQuery(this).addClass('active')
        },
        timeout: 300,
        out: function () {
            jQuery(this).removeClass('active')
        }
    };
    var configChildren = {
        sensitivity: 1,
        interval: 150,
        over: function () {
            jQuery(this).addClass('active')
        },
        timeout: 300,
        out: function () {
            jQuery(this).removeClass('active')
        }
    };

    jQuery("ul#nav > li").hoverIntent(configParents);
    jQuery(".children ul > li").hoverIntent(configChildren);

    jQuery("ul#nav ul li").hover(

        function () {
            jQuery(this).addClass("hover");
            jQuery(this).next().addClass("next");
        }, function () {
            jQuery(this).removeClass("hover");
            jQuery(this).next().removeClass("next");
        });
}

function searchDropdown() {
    jQuery('select').selectmenu();
    var monk_box_height = jQuery('#box-outer ul').height() + 15;
    jQuery('#box-outer ul').css('top', -monk_box_height)
    jQuery('#cat-menu').find('li:last').css({
        'padding-bottom': '4px'
    });
}

function cufon() {
    Cufon.replace('h1.title,#footer h3,.post .text h2 a,.scrollContainer h2', {
        hover: true
    });
    Cufon.replace('#content #postcontainer .post h1,#content #postcontainer.newsletter-page h1, #content #postcontainer .post h2, #content #postcontainer .post h3, #content #postcontainer .post h4, #content #postcontainer .post h5, #content #postcontainer .post h6,#content #postcontainer .page h1, #content #postcontainer .page h2, #content #postcontainer .page h3, #content #postcontainer .page h4, #content #postcontainer .page h5, #content #postcontainer .page h6,#heading h2,h2.date,.share h2,.comments h2,.comment h2,h2.results');

}

function tabs() {
    jQuery("#tabs ul li").hover(

        function () {
            jQuery(this).prev().toggleClass("prev");
            jQuery(this).toggleClass("hover");
            jQuery(this).next().toggleClass("next");

        }, function () {

            jQuery(this).prev().toggleClass("prev");
            jQuery(this).toggleClass("hover");
            jQuery(this).next().toggleClass("next");

        });
    jQuery("#tab_top ul:not('.active')").hide();

    var tabhandler = function () {
        if (!jQuery(this).hasClass(".ui-tabs-selected")) {
            var src_tab = jQuery(this);
            src_tab.siblings().unbind();
            var new_index = parseInt(jQuery('ul.tabs li').index(jQuery(this)));

            jQuery('.ui-tabs-selected').removeClass('ui-tabs-selected');
            jQuery(this).addClass('ui-tabs-selected');

            jQuery('#tab_top ul.active').fadeOut(200, function () {
                jQuery("#tab_top ul").eq(new_index).fadeIn(200);
            });

            jQuery('#tab_top .cont').animate({
                height: jQuery("#tab_top ul").eq(new_index).height()
            }, 300, function () {
                jQuery("#tab_top ul.active").removeClass("active");
                jQuery("#tab_top ul").eq(new_index).addClass("active");
                src_tab.siblings().bind('click', tabhandler);

            });
        }
    }
    jQuery('#tabs ul.tabs li').bind('click', tabhandler);
   
}

function checkboxes() {
    jQuery(".notify input").filter(":checkbox").checkbox();
}

function ulfixesPost() {
    jQuery('#content #postcontainer ol').each(function () {
        var positie = 0;
        jQuery(this).find('li').each(function () {
            positie += 1;
            var oud = (jQuery(this).css('backgroundImage'));
            var arrOud1 = oud.split("_");
            var arrOud2 = arrOud1[1].split(".");
            var nieuw = arrOud1[0] + "_" + positie + "." + arrOud2[1];
            jQuery(this).css({
                "backgroundImage": nieuw
            });
        })
    });
}

function minimizePosts() {
    jQuery("#content #posts .post a.hide").each(

        function () {
            jQuery(this).click(function () {
                jQuery(this).parents(".post").find('.min').slideToggle('slow');
            }).mouseup(function () {
                jQuery(this).toggleClass("clicked")
            }).mousedown(function () {
                jQuery(this).toggleClass("clicked")
            });
        });
}

function replyPopup() {
    var monkreplyid;
    var _popup = jQuery('#comment-popup');
    if (_popup.length) {
        jQuery('a.comment-reply').click(function () {
            // child support
            monkreplyid = jQuery(this).attr('id').split("-")[1];
            _popup.find("input#comment_parent").val(monkreplyid);

            if (jQuery(this).hasClass('active')) {
                jQuery(this).removeClass('active');
                hidePopup(jQuery(this));
            }
            else {
                jQuery(this).addClass('active');
                showPopup(jQuery(this));
            }
            return false;
        });
        _popup.find('#cancel-comment-reply-link').click(function () {
            hidePopup();
            return false;
        });
    }
    var t_btn = -1;


    function showPopup(_btn) {
        t_btn = _btn;
        t_btn.parent().addClass('active');
        _popup.css({
            top: _btn.offset().top + _btn.outerHeight(),
            left: _btn.offset().left
        });
    }

    function hidePopup(_btn) {
        t_btn.parent().removeClass('active');
        t_btn = -1;
        _popup.css({
            top: -9999
        });
    }
    jQuery(document).mousedown(function (e) {
        e = e || event;
        var t = e.target || e.srcElement;
        t = jQuery(t);
        if (t_btn != -1 && t.parents('div.popup-comment').length == 0 && t.parents('a.btn-reply').length == 0 && t.attr('id') != 'comment-popup' && !t.hasClass('btn-reply')) {
            hidePopup();
        }
    });
    jQuery(window).resize(function () {
        if (t_btn != -1) _popup.css({
            top: t_btn.offset().top + t_btn.outerHeight(),
            left: t_btn.offset().left
        });
    });

}

function inputHovers() {
    jQuery('.searchBtn, .emailBtn').hover(

        function () {
            jQuery(this).toggleClass('hover');
        }, function () {
            jQuery(this).toggleClass('hover');
        });
}

function clearInputs() {
    var inputs = document.getElementsByTagName("input");
    for (var i = 0; i < inputs.length; i++) {
        if (inputs[i].type == "text") {
            inputs[i].valueHtml = inputs[i].value;
            inputs[i].onfocus = function () {
                this.value = "";
            }
            inputs[i].onblur = function () {
                this.value != "" ? this.value = this.value : this.value = this.valueHtml;
            }
        }
    }
}

function inputFocus() {
    jQuery('input[type="text"],#textarea').focus(function () {
        jQuery(this).parent().removeClass("idleField").addClass("focusField");
    });
    jQuery('input[type="text"],#textarea').blur(function () {
        jQuery(this).parent().removeClass("focusField").addClass("idleField");
    });
}




function featuredPosts() {
    if (jQuery('#slider .scrollContainer > div').length == 0) {
        return;
    }
    var monk_panels=jQuery('#slider .scrollContainer > div');
    var monk_cont=jQuery('#slider .scrollContainer');
    monk_panels.css({
        'float':'left',
        'position':'relative'
    });
    monk_cont.css('width',monk_panels[0].offsetWidth*monk_panels.length);
    var scroll=jQuery('#slider .scroll').css('overflow','hidden');
    function selectNav(){
        jQuery(this).parents('ul:first').find('a').removeClass('selected').end().end().addClass('selected')
    }
    jQuery('#slider .navigation').find('a').click(selectNav);
    function trigger(data){
        var el=jQuery('#slider .navigation').find('a[href$="'+data.id+'"]').get(0);
        selectNav.call(el)
    }
    if(window.location.hash){
        trigger({
            id:window.location.hash.substr(1)
        })
    }else{
        jQuery('ul.navigation a:first').click()
    }
    var offset=parseInt((true?monk_cont.css('paddingTop'):monk_cont.css('paddingLeft'))||0)*-1;
    var scrollOptions={
        target:scroll,
        items:monk_panels,
        navigation:'.navigation a',
        axis:'xy',
        onAfter:trigger,
        offset:offset,
        duration:500,
        force:true,
        interval:10000,
        easing:'swing'
    };

    jQuery('#slider').serialScroll(scrollOptions);
}