/* CIS Search JS */

    function insertIntoSkillArea(JSONObj) {
        // empty the existing options
        var selectLen = $('#a_course_category').length;
        for (x=0; x<selectLen; x++) {
            $('#a_course_category option').remove(x);
        }
        // add in the 'all' option
        $('#a_course_category').append('<option label="ALL" value="">ALL</option>');
        // pull out the bits
        $(JSONObj).each(function(index,value) {
            var match = /(\d+)\s*\((.+)\)/.exec(value.text);
            $('#a_course_category').append('<option label="' + match[2] + '" value="' + match[1] + '">' + match[2] + '</option>');
        });
    }


$(document).ready(function() {
    
    // basic search button group
    if ($('.radiogroup').length) {
        // radio grouping of non-related radio buttons
        $('.radiogroup').click(function() {
                var notthis = $(this);
                $('.radiogroup').each(function (i) {
                        $(this).not(notthis).attr('checked', false);
                });
        });
    }

    // homepage serach box tabs
    if ($('#searchbox').length) {
        $('#searchtabs').detatabs({ width: 477 });
    }
    
        
    // advanced search ajax
    if ($('#advancedsearch').length) {
        $('#a_area_of_study').change(function() {
            var areaOfStudy = $(this).attr('value');
            $.ajax({
                type: "GET",
                url: 'index.php?script_name=ajax',
                data: 'category=skillarea&id_area_of_study=' + areaOfStudy + '',
                dataType: "JSON",
                success: function(data) {
                    //for IE browser
                    if (window.JSON) {
                        insertIntoSkillArea(JSON.parse(data));
                    } else {
                        insertIntoSkillArea(json_parse(data));
                    }
                }
            });
        });
    }
    
    // faceted search remove item
    if ($('#filterlist').length) {
        $('#filterlist a').bind('click', function() {
            var removeItem = $(this).attr('href');
            $(removeItem).val('');
            $(this).parent().hide();
            $('.searchbutton').trigger('click');
        });
    }
    
    // course details page
    if ($('#course_details_content').length) {
        $('#courseDetails').detatabs({ width: 955 });
    }
    
    // course details - units table
    if ($('#modules_table').length) {
        $('#modules_table').tablesorter({ sortList: [[0,0]], headers: { 2:{sorter: false}} });
    }
    
    // course details - other institute offerings table
    if ($('#institute_table').length) {
        $('#institute_table').tablesorter({ sortList: [[0,0]], headers: { 1:{sorter: false}, 2:{sorter: false}, 3:{sorter: false}} });
    }


    // setup unit module modal window
    if ($('#course_modules').length) {
        
        $("a[rel]").overlay({
            top: '20%',
            onBeforeLoad: function() {
                // grab wrapper element inside content 
                var wrap = this.getContent().find(".overlayContent");
                // load the page specified in the trigger                
                wrap.load(this.getTrigger().attr("href"));
            } 
        });         
    }
     
    
    // faceted serach hide options
    if ($('.selectionMade').length) {
        $('.selectionMade').each(function(){
            $(this).next().hide();
        });
        $('.selectionMade a').bind('click',function(){
            $(this).parent().parent().hide();
            $(this).parent().parent().next().slideDown();
        });
    }
    

    // initiate the table sorter for the search results
    if ( $('#sortTable').length)  {
        if ($('.noSort').length === 0) {
            $('#sortTable').tablesorter({sortList: [[1,0]]}).tablesorterPager({size: 25,container: $('#pager')});
        }
        else
        {
            $('#sortTable').tablesorter().tablesorterPager({size: 25, container: $('#pager')});
        }
    }
    
    // alpha indexing
    if ($('#alphaIndex').length) {
        var selectedAnchor;
        var selectedDiv;
        // hide all the divs on load except the first
        $.each($('#alphaIndex a'), function (key, value) {
            // Hide all the options, except for the first one
            var alphaDiv = $(value).attr('href');
            if (key !== 0) {
                $(alphaDiv).hide();
            }
            if (key === 0) {
                $(this).addClass('selectedIndex');
                selectedAnchor = $(this);
                selectedDiv = $(this).attr('href');
            }
            // Bind the click event to showing the new options, hiding the old
            $(this).bind('click', function(event) {
                event.preventDefault();
                
                // hide the previous selection
                if (selectedDiv =='#ALL') {
                    $('.courseCategory').hide();
                    $(selectedAnchor).removeClass('selectedIndex');
                } else {
                    $(selectedAnchor).removeClass('selectedIndex');
                    $(selectedDiv).hide();
                }
                
                // assign the new div & anchor
                selectedAnchor = $(this);
                selectedDiv = $(this).attr('href');
                
                if (selectedDiv =='#ALL') {
                    $('.courseCategory').show();
                    $(selectedAnchor).addClass('selectedIndex');
                } else {
                    // show the new selection
                    $(selectedAnchor).addClass('selectedIndex');
                    $(selectedDiv).show();
                }
            });
        });
    }
});
