(function(){
    // PRIVATE METHOD

    // Shifty handles moving the list items left or right
    // accepts a direction in which to move... either before or after
    var Shifty = function(direction){
        var dir = direction;
        var item_width = $('#carousel_ul li').outerWidth(); // measures item width for identation
        var indent = parseInt($('#carousel_ul').css('left'), 10); // sets item identation amount
        var left_indent;

        if(dir === 'after'){
            left_indent = indent - item_width;
        } else if (dir === 'before'){
            left_indent = indent + item_width;
        }else{
            return; // first list item will display but nothing moves
        }

        //make the sliding effect using jquery's animate function
        //prevent already animated elements from responding to clicks with not(:animated)
        $('#carousel_ul:not(:animated)').animate({
            'left' : left_indent
        },500,function(){

            if(dir === 'after'){
                $('#carousel_ul li:last').after($('#carousel_ul li:first'));
            } else if (dir === 'before'){
                $('#carousel_ul li:first').before($('#carousel_ul li:last'));
            } else {
                return; // first list item will display but nothing moves
            }

            //and get the left indent to the default -148px
            $('#carousel_ul').css({
                'left' : '-148px'
            });
        });

    };

    // Shuffle is a basic, but robust, random array shuffler
    var Shuffle = function(array) {
        var tmp, current, top = array.length;

        if(top) {while(--top) {
            current = Math.floor(Math.random() * (top + 1));
            tmp = array[current];
            array[current] = array[top];
            array[top] = tmp;
        }

        return array;
        }
    };

    // JQUERY DOCUMENT READY
    $(function(){
        var i;
        
        $.ajax({
            type: "GET",
            url: xmlSource, // set in the page that displays the carousel
            dataType: "text",
            success: function(results) {
                var counter = 0; // used to help determine when we have the needed three list items
                var total = 0; // used to get the total count of elements
                
                var xml = results;
                
                // Solely because IE stinks. What a lousy, lousy browser.
                // reference: http://docs.jquery.com/Specifying_the_Data_Type_for_AJAX_Requests
                if($.browser.msie){
                    xml = new ActiveXObject("Microsoft.XMLDOM");
                    xml.async = false;
                    xml.loadXML(results);
                }
                // count the elements available
                $(xml).find('speaker').each(function(){
                    total++;
                });

                var collectedData = new Array(total);
                for(i = 0; i < total; i++) {
                    collectedData[i] = new Array(4);
                }
                
                // start the first list item
                var output = '<li>';
                $(xml).find('speaker').each(function(){
                    

                    var speaker_image = $(this).find('speaker_image').text();
                    var speaker_name = $(this).find('speaker_name').text();
                    var speaker_title = $(this).find('speaker_title').text();
                    var speaker_company = $(this).find('speaker_company').text();
                    collectedData[counter][0] = speaker_image;
                    collectedData[counter][1] = speaker_name;
                    collectedData[counter][2] = speaker_title;
                    collectedData[counter][3] = speaker_company;

                    counter++;
                }); //close each

                // Randomly shuffle the order of items in the array of collected data
                Shuffle(collectedData);

                // Reset the counter to 0. Probably don't need this but it's working so...
                counter = 0;
                for(i=0;i<collectedData.length;i++){
                    output = output + '<div><a href="conference/'+ group +'.php#'+ collectedData[i][0] +'"><img src=assets/images/home/' + group + '/' + collectedData[i][0] + '.jpg />' + '<br /><label><span>' + collectedData[i][1] + '</span><br />' + collectedData[i][2] + '<br />' + collectedData[i][3] + '</label></a><br /></div>';
                    counter++;
                    // close the list item and start the next one if we have three elements UNLESS we are at the end of total elements
                    if(counter % 3 === 0 && counter !== total){
                        output = output + '</li><li>';
                    }
                }
                output = output + '</li>';

                $('#carousel_ul').html(output);

                // add and remove hover styles to image and text to match the flash version of this
                $('#carousel_ul div').hover(
                    function(){
                        $("img", this).css('border','2px solid #00a6ed');
                        $("a", this).css('color','#00a6ed');
                        $("img", this ).css('margin','0');
                    },
                    function(){
                        $("img", this).css('border','none');
                        $("a", this).css('color','#000');
                        $("img", this).css('margin','2px');
                    }
                    );
                // add and remove hover styles to image and text to match the flash version of this
                $('#carousel_ul a').hover(
                    function(){
                        $("img", this).css('border','2px solid #00a6ed');
                        $("img", this).css('margin','0');
                    },
                    function(){
                        $("img", this).css('border','none');
                        $("img", this).css('margin','2px');
                    }
                    );
                //move the last list item before the first item. The purpose of this is if the user clicks to slide left they will be able to see the last item.
                $('#carousel_ul li:first').before($('#carousel_ul li:last'));

                //when user clicks the image for sliding right
                $('#right_scroll').click(function(){
                    Shifty('after');
                });

                //when user clicks the image for sliding left
                $('#left_scroll').click(function(){
                    Shifty('before');
                });
            } // end success callback
            
        }); // end ajax call

    }); // end jquery document ready

})(); // trigger the closure (i.e. "make it so")