//image preloading jquery extension
j$.fn.preload = function () {
    this.each(function () {
        j$('<img/>')[0].src = this;
    });
};

// our-experts carousel
j$(window).load(function () {
    //hack: if the "visible" below is set to more than the number of thumbnails it breaks the carousel
    var numExperts = j$("section.authors .carousel ul li").length;
    j$("section.authors .carousel").jCarouselLite({
        btnNext: ".next",
        btnPrev: ".prev",
        visible: (numExperts) > 5 ? 5 : numExperts
    });
});


// topics padding
j$(document).ready(function() {
  j$('ul.topics li:odd').css("padding-right", "0");
});

// archive page accordion
j$(document).ready(function () {
    //archive collapsed init
    j$('ul.archive li div.hidden').hide();

    //expand and collapse
    j$('ul.archive li div.btn').click(function () {
        if (!j$(this).hasClass("btnActive")) {
            //j$('a.btnDown', this).hide();
            //j$('a.btnUp', this).show();
            j$('.btnActive').next(".hidden").slideUp(600);
            j$('.btnActive').removeClass('btnActive');
            j$(this).next(".hidden").slideDown(600);
            j$(this).addClass('btnActive');
        }
        else {
            //j$('a.btnDown', this).show();
            //j$('a.btnUp', this).hide();
            j$(this).next(".hidden").slideUp(600);
            j$(this).removeClass('btnActive');
        }
    });
});


// sidebar height
j$(window).load(function() {
	var biggestHeight = 0;
	j$('.same-height').each(function(){
		if(j$(this).height() > biggestHeight){
			biggestHeight = j$(this).height();
		}
	});
	j$('.same-height').height(biggestHeight);
});


// rounded corners
j$(window).load(function () {
    //curvycorners.js breaks in IE6 with curvyObject error popups (many, many of them) 
    if (j$(".roundedCorners").length > 0 &&
        !(j$.browser.msie == true && parseInt(j$.browser.version, 10) < 7)
    ) {
        var setting = {
            tl: { radius: 6 },
            tr: { radius: 6 },
            bl: { radius: 6 },
            br: { radius: 6 },
            antiAlias: true
        }
        curvyCorners(setting, ".roundedCorners");
    }
});

// input field clear
j$(document).ready(function() {
	j$('.clearField').clearField();
});

// print

j$(document).ready(function(){ 
    function print() { 
        window.print(); 
        return false;
    } 

    j$("a.print").click(function() { 
            print(); 
        });
});

j$(document).ready(function () {

    function searchPageForm() {
        var query = j$("#search-page-q").val();
        location.href = "/search-results/?q=" + encodeURIComponent(query) + "&pagenum=1";
    }
    j$("#search-page-q").keypress(function (e) {
        if (e.which == 13) {
            e.preventDefault();
            searchPageForm();
        }
    });
    j$("#search-page-button").click(function (e) {
        e.preventDefault();
        searchPageForm();
    });
});
