// resume.js Copyright (c) 2008 Julian M Bucknall
/*global $ window */

var JMBCV = {
    setShowImage: function() {
        this.src = "more.png";
        this.alt = "Show the description for " + $(this).parent().prev().prev().text();
    },

    setHideImage: function() {
        this.src = "less.png";
        this.alt = "Hide the description for " + $(this).parent().prev().prev().text();
    },

    hideAllJobs: function() {
        $("div.jobDescription").hide();
        $("#img_jobAll").attr("src", "moreAll.png").attr("alt", "Show all job descriptions");
        $("img.morelessJob").each(this.setShowImage);
    },

    showAllJobs: function() {
        $("div.jobDescription").show();
        $("#img_jobAll").attr("src", "lessAll.png").attr("alt", "Hide all job descriptions");
        $("img.morelessJob").each(this.setHideImage);
    },

    hideAllPubs: function() {
        $("div.pubDescription").hide();
        $("#img_pubAll").attr("src", "moreAll.png").attr("alt", "Show all publication descriptions");
        $("img.morelessPub").each(this.setShowImage);
    },

    showAllPubs: function() {
        $("div.pubDescription").show();
        $("#img_pubAll").attr("src", "lessAll.png").attr("alt", "Hide all publication descriptions");
        $("img.morelessPub").each(this.setHideImage);
    },

    handleSingleItemClick: function(e) {
        if (this.src.indexOf("more.png") >= 0) {
            $(this).each(JMBCV.setHideImage);
            $(this).parent().next().stop().slideDown(500);
        }
        else {
            $(this).each(JMBCV.setShowImage);
            $(this).parent().next().stop().slideUp(250);
        }
    },

    handleAllJobsClick: function(e) {
        if (this.src.indexOf("moreAll.png") >= 0) {
            JMBCV.showAllJobs();
        }
        else {
            JMBCV.hideAllJobs();
        }
    },

    handleAllPubsClick: function(e) {
        if (this.src.indexOf("moreAll.png") >= 0) {
            JMBCV.showAllPubs();
        }
        else {
            JMBCV.hideAllPubs();
        }
    },
    
    bindClickEvents: function() {
        $("img.morelessJob").click(this.handleSingleItemClick);
        $("img.morelessPub").click(this.handleSingleItemClick);
        $("#img_jobAll").click(this.handleAllJobsClick);
        $("#img_pubAll").click(this.handleAllPubsClick);
    },

    initialize: function() {
        this.hideAllJobs();
        this.hideAllPubs();
        this.bindClickEvents();
    },

    print : function() {
        window.print();
    }
};

$(function() {
    JMBCV.initialize();
});

