jQuery(document).ready(function () {
    cufon();
    navigation();
    searchDropdown();
    tabs();
    featuredPosts();
    masonry();
    minimizePosts();
    inputHovers();
    clearInputs();
    ulfixesPost();
    inputFocus();
    replyPopup();
    checkboxes();
    externalLinks();
});

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');

    Cufon.replace('.cufon_h', {
        hover: true
    });
    Cufon.replace('.cufon,#wp-calendar caption');

}

function navigation() {
    jQuery("ul#nav > li").each(function () {
        if (jQuery(this).children().size() == 1) {
            jQuery(this).addClass("lonely")
        } else {
            jQuery(this).addClass("popular").find("a:eq(0)").addClass('parent').find('span').append('<span class="menu-item-arrow">&nbsp;</span>');
        }

    });
    jQuery("ul#nav ul").each(function () {
        jQuery(this).wrap('<div class="menu-div outerbox"></div>').addClass('innerbox').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('#search select').selectmenu();
    var monk_box_height = jQuery('#box-outer ul').height() + 15;
    jQuery('#box-outer ul').css('top', -monk_box_height)
}



function tabs() {
    jQuery("#tabs ul li,.widget 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(:first)").hide();

    var tabhandler = function () {
        if (!jQuery(this).hasClass(".selected")) {
            var src_tab = jQuery(this);
            src_tab.siblings().unbind();
            var new_index = parseInt(jQuery('ul#tabs_nav li').index(jQuery(this)));

            jQuery('.selected').removeClass('selected');
            jQuery(this).addClass('selected');

            jQuery('#tab_top ul.active').fadeOut(200, function () {
                jQuery("#tab_top ul").eq(new_index).fadeIn(200);
            });

            jQuery('#tab_top #tab_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_nav li').bind('click', tabhandler);

}

function masonry() {
    jQuery('.home #posts').masonry({
        singleMode: true,
        columnWidth: 283,
        animate: true,
        animationOptions: {
            duration: 750,
            easing: 'linear',
            queue: false
        }
    });
}

function checkboxes() {
    jQuery(".notify input").filter(":checkbox").checkbox();
}

function ulfixesPost() {
      jQuery('#content #postcontainer ol li').each(function (i) {
        i = i+1;
        jQuery(this).addClass("item" + i);
    });
}

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;
        _popup.css({
            top: _btn.offset().top + _btn.outerHeight(),
            left: _btn.offset().left
        });
    }

    function hidePopup() {
        t_btn = -1;
        _popup.css({
            top: -9999
        });
        jQuery('a.comment-reply.active').removeClass('active');
    }
    jQuery(document).mousedown(function (e) {
        e = e || event;
        var t = e.target || e.srcElement;
        t = jQuery(t);
        if (t_btn != -1 && t.parents('#comment-popup').length == 0 && t.parents('a.comment-reply').length == 0 && t.attr('id') != 'comment-popup' && !t.hasClass('comment-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);
}

function externalLinks() {
    jQuery('a[rel~=external]').attr('target', 'blank');
}
