﻿
function hcso_rotator(width, height, quality, scale, autoplay, loop, wmmode) {
    //Set Default State of each portfolio piece
    $("#rotator_paging").show();

    //Get size of images, how many there are, then determin the size of the image reel.
    var imageWidth = $("#rotator_window").width();
    //var imageSum = $(".image_reel a").size();
    var imageReelWidth = imageWidth; //imageWidth * imageSum;

    //Adjust the image reel to its new size
    $("#rotator_image_reel").css({ 'width': imageReelWidth, 'height': height });

    //Paging + Slider Function
    rotate = function () {        
        $("#rotator_paging a[rel]").removeClass('active'); //Remove all active class				
        $active.addClass('active'); //Add active class (the $active is declared in the rotateSwitch function)			

        $('#rotator_paging a img').removeClass('Selected');
        $('#rotator_paging a.active img').addClass('Selected');
        
        // switch the media content
        $('#rotator_image_reel').html('<div id="rotator_image_reel_media">&nbsp;</div>');

        $('#rotator_image_reel_media').flash({
            src: $active.attr('media_src'),
            width: width,
            height: height,
            bgcolor: '#ffffff',
            quality: quality,
            wmode: wmmode,
            allowfullscreen: 'false',
            allowscriptaccess: 'sameDomain',
            classid: 'clsid:d27cdb6e-ae6d-11cf-96b8-444553540000',
            codebase: 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=10,0,0,0'
        });


        // setup target href
        $('#rotator_target').unbind('click').click(function () {
            window.location = $active.attr('media_href');
        });        
    };

    //Rotation + Timing Event
    rotateSwitch = function () {
        if (!rotator_paused) {
            play = setInterval(function () { //Set timer - this will repeat itself every 3 seconds                                
                $active = $('#rotator_paging a[rel].active').parent('div.ControlButton').next('div.ControlButton').find('a[rel]');
                if ($active.length === 0) { //If paging reaches the end...
                    $active = $('#rotator_paging a[rel]:first'); //go back to first
                }
                rotate(); //Trigger the paging and slider function
            }, 7000); //Timer speed in milliseconds (3 seconds)
        };
    };

    $active = $('#rotator_paging a[rel]:first');
    rotator_paused = false;

    rotate();
    rotateSwitch(); // set timing interval on launch            

    //On Hover
    $("#rotator_target").hover(function () {	
        clearInterval(play); //Stop the rotation
    }, function () {
        rotateSwitch(); //Resume rotation
    });

    //On Click
    $("#rotator_paging a[rel]").click(function () {
        $active = $(this); //Activate the clicked paging
        //Reset Timer
        clearInterval(play); //Stop the rotation
        rotate(); //Trigger rotation immediately
        rotateSwitch(); // Resume rotation
        return false; //Prevent browser jump to link anchor
    });

    $("#rotator_paging_controls .PreviousButton").click(function () {
        // previous slide        	
        $active = $('#rotator_paging a[rel].active').parent('div.ControlButton').prev('div.ControlButton').find('a[rel]');
        if ($active.length === 0) { //If paging reaches the start...
            $active = $('#rotator_paging a[rel]:last'); //cycle to the end
        }
        // restart rotation from the new slide
        clearInterval(play);
        rotate();
        rotateSwitch();
    });
    $("#rotator_paging_controls .NextButton").click(function () {
        // previous slide        
        $active = $('#rotator_paging a[rel].active').parent('div.ControlButton').next('div.ControlButton').find('a[rel]');
        if ($active.length === 0) { //If paging reaches the end...
            $active = $('#rotator_paging a[rel]:first'); //cycle to the start
        }
        // restart rotation from the new slide
        clearInterval(play);
        rotate();
        rotateSwitch();
    });
    $("#rotator_paging_controls .PauseButton").click(function () {
        $this = $(this).find('img');
        $this.toggleClass("Selected");
        if ($this.hasClass("Selected")) {
            // user clicked pause       
            rotator_paused = true;

            // stop the rotation interval
            clearInterval(play);
        }
        else {
            // user clicked play            
            rotator_paused = false;

            // get the next slide
            $active = $('#rotator_paging a[rel].active').parent('div.ControlButton').next('div.ControlButton').find('a[rel]');
            if ($active.length === 0) { //If paging reaches the end...
                $active = $('#rotator_paging a[rel]:first'); //cycle to the start
            }

            // rotate to the new slide
            rotate();

            // restart the rotation interval                    
            rotateSwitch();
        }
    });
}
