/*
 * * SLIDE SHOW SCRIPT
 *
 * * 1) Refference Following CSS Files In Your Page's Head
 *
 *      <link href="lib/css/yui/3.1.1/cssfonts/fonts-min.css" rel="stylesheet" type="text/css" />
 *      <link href="lib/css/yui/3.1.1/cssreset/reset-min.css" rel="stylesheet" type="text/css" />
 *      
 *      <link href="lib/css/hyattBuilt.HRDLP.css" rel="stylesheet" type="text/css" />
 *
 * * 2) Place Following In Your Page's Head, and then define refference to images:
 *
 *      <script language="javascript" type="text/javascript">
 *          //<![CDATA[
 *              // START - List of images for slideshow
 *              var hbImagesListToPlay = new Array();
 *                  hbImagesListToPlay[0] = "lib/img/slideshow/aruba.jpg";
 *                  hbImagesListToPlay[1] = "lib/img/slideshow/boat.jpg";
 *                  hbImagesListToPlay[2] = "lib/img/slideshow/hole.jpg";
 *                  hbImagesListToPlay[3] = "lib/img/slideshow/shipwreck.jpg";
 *                  hbImagesListToPlay[4] = "lib/img/slideshow/tamaya.jpg";
 *              // END - List of images for slideshow
 *          //]]>
 *      </script>
 *
 * * 3) Refference Following JS Files In Your Page's Head
 *
 *      <script src="lib/js/jquery/jquery-1.4.2.min.js" language="javascript" type="text/javascript"></script>
 *      <script src="lib/js/jquery/jquery-ui-1.8.2.custom.min.js" language="javascript" type="text/javascript"></script>
 *      <script src="lib/js/hyattBuilt.HRDLP.js" language="javascript" type="text/javascript"></script>
 *
 */
    
    function hbSlideShowHrdl() {
        if (typeof hbImagesListToPlay == 'undefined') {
            jQuery('#hbGlobalContentAndSlideShow').append('<div id="hbErrorMessage"></div>');
            jQuery('#hbErrorMessage').text('Image List Is Not Defined');
        } else {
            if (hbImagesListToPlay.length) {
                jQuery('#hbGlobalContentAndSlideShow').append('<div id="hbErrorMessage"></div>');
                //jQuery('#hbErrorMessage').text('Loading Slide Show...');
                jQuery('#hbErrorMessage').hide();
                hbSlideShowHrdlLoad();
            } else {
                jQuery('#hbGlobalContentAndSlideShow').append('<div id="hbErrorMessage"></div>');
                jQuery('#hbErrorMessage').text('No Images Defined In List');
            }
        }
    }
    
    // Data Holder
    var hbImageNameReferece = new Array();
    var hbImageLoadStatus = new Array();
    
    // Intervals Holder
    var hbIntervalsStorage = new Array();
    
    function hbSlideShowHrdlLoad() {
        //preload image, if success then identify that it has been loaded
        function hbCheckIfSlideIsLoaded(hbCurrentCountForImageSlide) {
            jQuery(hbImageNameReferece[hbCurrentCountForImageSlide])
                .load(function(){
                    hbImageLoadStatus[hbCurrentCountForImageSlide] = 'loaded';
                })
                .attr('src', hbImagesListToPlay[hbCurrentCountForImageSlide]);
        }
        
        //loop through images reference list and create images and execute preload
        for (var hbImageItemFromImagesListToPlay = 0; hbImageItemFromImagesListToPlay < hbImagesListToPlay.length; hbImageItemFromImagesListToPlay++){
            // fix for IE, by Krishnapriya, changed to regular for loop, following is old condition - var hbImageItemFromImagesListToPlay in hbImagesListToPlay
            
            hbImageNameReferece[hbImageItemFromImagesListToPlay] = new Image();
            hbCheckIfSlideIsLoaded(hbImageItemFromImagesListToPlay);
        }

        hbIntervalsStorage[0] = setInterval("hbLoadingCheck()", 5);

    }
    
    var hbSlideShowHrdlExecuteCountVariable = 0;
    var hbSlideShowTransitionOn = 1; // Make First Slide Appear With Transition
    
    function hbSlideShowHrdlExecute() {
        /* Display Slide Number * // *
        jQuery("#hbErrorMessage")
            .text(hbSlideShowHrdlExecuteCountVariable+1)
            .show();
        */
        if(hbImageLoadStatus[hbSlideShowHrdlExecuteCountVariable] !== 'loaded') {
            hbSlideShowHrdlExecuteCountVariable = 0;
        }
        jQuery('#hbGlobalContentAndSlideShow')
            .append(
                jQuery(hbImageNameReferece[hbSlideShowHrdlExecuteCountVariable])
                    .addClass('hbSlideImage')
                    .attr('id', 'hbImageId' + hbSlideShowHrdlExecuteCountVariable)
            );
        
        if (hbSlideShowTransitionOn == 1){
            jQuery('#hbImageId' + hbSlideShowHrdlExecuteCountVariable)
                .css('opacity', '0')
                .animate({
                    opacity: 1
                }, 2000);
        } else {
            jQuery('#hbImageId' + hbSlideShowHrdlExecuteCountVariable)
                .css('opacity', '1');
        }
        
        hbSlideShowHrdlExecuteCountVariable+=1;
        
        if (hbSlideShowHrdlExecuteCountVariable == hbImagesListToPlay.length){
            hbSlideShowHrdlExecuteCountVariable = 0;
        }
        
        hbSlideShowTransitionOn = 1;

    }
    
    
    function hbLoadingCheck(){
        /*
        jQuery("#hbErrorMessage")
            .text('Currently loading image ' + hbImageLoadStatus.length + ' out of ' + hbImagesListToPlay.length)
            .show();
        */
        
        if (hbImageLoadStatus[0] == 'loaded'){
            clearInterval(hbIntervalsStorage[0]);
            hbSlideShowHrdlExecute();
            hbIntervalsStorage[1] = setInterval("hbSlideShowHrdlExecute()", 5000);
        }
    }
    
    function hbExploreMoreTab(){
        jQuery('#hbButtonExploreNow').click(function() {
            $('#hbExploreNowInfoBlock').toggleClass('hbHiddenNow', 600);
            return false;
        });
        jQuery('#hbExploreNowInfoBlockCloseButton').click(function() {
            $('#hbExploreNowInfoBlock').toggleClass('hbHiddenNow', 600);
            return false;
        });

    }
    
    jQuery(document).ready(function () {
        hbSlideShowHrdl();
        hbExploreMoreTab();
    });
    

