var browseState;

$(document).ready(function() {

    // Build up the browseState object which allows paging through content on the home page  
    browseState = {
        case_content_switch: {
            index: 0,
            items: $('.highlightItem')
        },

        award_content_switch: {
            index: 0,
            items: $('.miniAdsItem')
        },
        
        featured_job_content_switch: {
            index: 0,
            items: $('.featured_job')
        },
        featured_employer_content_switch: {
            index: 0,
            items: $('.featured_employer')
        },
        featured_resource_content_switch: {
            index: 0,
            items: $('.resourceItem')
        },

        featured_conference_content_switch: {
            index: 0,
            items: $('.conferenceItem')
        },
        
        related_product_content_switch: {
            index: 0,
            items: $('.resourceItem')
        },
        
        related_resource_content_switch: {
            index: 0,
            items: $('.resourceItem')
        },

        related_conference_content_switch: {
            index: 0,
            items: $('.conferenceItem')
        },
        
        online_speaker_content_switch: {
            index: 0,
            items: $('.online_speakerItem')
        },

        activeSection: function(name) {

            var obj = this[name];
            return obj.items[obj.index];
        },

        next: function(name) {
            //debugger;
            var obj = this[name];
            obj.index++;

            if (obj.index >= obj.items.length) {
                obj.index = 0;
            }

            obj.items.hide().eq(obj.index).fadeIn();
        },

        back: function(name) {
            //debugger;
            var obj = this[name];
            obj.index--;

            if (obj.index < 0) {
                obj.index = obj.items.length - 1;
            }

            obj.items.hide().eq(obj.index).fadeIn();
        }
    };

    if ($('#dialog') != null) {
        $('#dialog').toggle(); //show the content
        $('#dialog').dialog(); //set it as a dialog
    }

    var containerIDs = ['case_content_switch', 'award_content_switch', 'featured_resource_content_switch', 'featured_employer_content_switch','featured_job_content_switch', 'featured_conference_content_switch', 'related_resource_content_switch', 'related_product_content_switch', 'related_conference_content_switch', 'online_speaker_content_switch'];

    // Loop over the containerIDs, find the first two anchor elements and build a javascript
    // function which looks allows paging through the related content items
    for (var v = 0; v < containerIDs.length; v++) {
        
        // Build a dynamic jQuery css selector based on the active containerID and finding the child 'a' elements
        var links = $('#' + containerIDs[v] + ' a');

        // Bind handlers for each of the links  (aka previous/next buttons)
        links.eq(0).click(function() {

            browseState.back(this.parentNode.id);
            this.blur();

            if (this.parentNode.id == 'award_content_switch') {
                ResetSectionImage(this)
            }
            else if (this.parentNode.id == 'case_content_switch') {
                ResetMainImage(this);
            }

            return false;

        });

        links.eq(1).click(function() {
            browseState.next(this.parentNode.id);
            this.blur();
            if (this.parentNode.id == 'award_content_switch') {
                ResetSectionImage(this)
            }
            else if (this.parentNode.id == 'case_content_switch') {
                ResetMainImage(this);
            }

            return false;
        });
    }

    function ResetSectionImage(elem) {
        var section = browseState.activeSection(elem.parentNode.id);
        var imageSrc = $('.section_image', section).attr('src');
        $('#home_award_top').css('background-image', 'url(' + imageSrc + ')');
    }
    
     function ResetMainImage(elem) {
        var section = browseState.activeSection(elem.parentNode.id);
        var imageSrc = $('.section_image', section).attr('src');
        var img = document.getElementById('highlight_image');
        if (imageSrc != null && imageSrc != '') {
            img.setAttribute("src", imageSrc);
            img.style.display = "";
        }
        else {
            img.style.display = "none";
        }
    }
});
        
