////////////////////////////////////////////////////////////////////////////
//
// home.js
//
// Description:
//  This file holds javascript used specifically for the home page
//
// Copyright (c) 2011 - The Marcus Buckingham Company, LLC,
// All rights reserved.
//
// Author: Bruce Hellstrom, Celebrity Computer Consulting
//         bruce@celebritycc.com
//
// $Revision$
// $HeadURL$
//
////////////////////////////////////////////////////////////////////////////

var current_profile = 0;
var current_gallery_img = 0;
var gallery_interval_id;
var controls_id = -1;
var gallery_state = 1;
var img_prefix = "/";

function feat_client( imgpath, text, link ) {
    this.imgpath = imgpath;
    this.text = text;
    this.link = link;
}

// anonymous function to preload the product images

( function( $ ) {
    var cache = [];
    // Arguments are image paths relative to the current page.
    $.preloadGallery = function() {
        var args_len = arguments.length;
    
        for ( var xctr = 0; xctr < args_len; xctr++ ) {
            var cacheImage = document.createElement( 'img' );
            cacheImage.src = arguments[xctr];
            cache.push( cacheImage );
        }
    }
})( jQuery )


/**
* This function runs when the DOM is ready. We initialize handlers and other
* various things here.
*/
$( document ).ready( function() {
	
    var bgImgNm = $("#gallery-image").attr("src");
    if (bgImgNm.indexOf("stop_ad") > 0)
	{
    	$("#gallery-image").click(jumpToStopPage);
    	$("#gallery-image").css("cursor", "pointer");
	}
	
    // Set the handlers for the client profile left/right arrows
    $( "#leftarrow" ).click( prevClientProfile ).show();
    $( "#rightarrow" ).click( nextClientProfile ).show();
    
    var bgpath = $( "#gal-pause-box" ).css( 'background-image' );
    bgpath = bgpath.substr( 5 );
    var imgind = bgpath.indexOf( "/images" );
    if ( imgind != -1 ) {
        bgpath = bgpath.substr( 0, imgind );
        img_prefix = bgpath;
    }
    
    
    var num_images = gallery_images.length;

    // Start the gallery rotating images on the interval specified
    if ( num_images > 1 ) {
        gallery_interval_id = setInterval( "rotate_gallery_image()", gallery_interval );
    
        // Show the gallery controls if the user mouses over the gallery image
        // set them to go away after 2 seconds
        $( "#gallery-image-box" ).mouseover( function( event ) {
            $( "#gallery-controls" ).fadeIn( "fast" );
            if ( controls_id == -1 ) {
                controls_id = setTimeout( "close_gallery_controls()", 2000 );
            }
        });

        // If the user is mousing over the controls, then cancel the closing of
        // the controls
        $( "#gallery-controls" ).mouseover( function( event ) {
            if ( controls_id != -1 ) {
                clearTimeout( controls_id );
                controls_id = -1;
            }
        });

        // If the user mouses out of the controls, set them to go off after 2 seconds
        $( "#gallery-controls" ).mouseout( function( event ) {
            if ( controls_id == -1 ) {
                controls_id = setTimeout( "close_gallery_controls()", 2000 );
            }
        });

        // Setup the numbered image boxes to do the proper thing
        for ( var xctr = 0; xctr < num_images; xctr++ ) {
            var img_ctr = xctr + 1;
            var cur_img_id = "#galctl-box-" + img_ctr;
            $( cur_img_id ).click( function( event ) {
                var imgpath = img_prefix + "/images/common/";
                var myid = $( this ).attr( 'id' );
                var lastdash = myid.lastIndexOf( "-" );
                if ( lastdash != -1 ) {
                    var idnum = myid.substr( lastdash + 1 );
                    idnum = ( typeof idnum == "number" ? idnum : parseInt( idnum ) );
                    
                    if ( idnum > 0 && idnum <= gallery_images.length ) {
                        $( "#galctl-box-" + ( current_gallery_img + 1 ) ).removeClass( 'gal-index-box-hot' );
                        $( "#galctl-box-" + ( current_gallery_img + 1 ) ).addClass( 'gal-index-box-cold' );
                        clearInterval( gallery_interval_id );
                        gallery_state = 0;
                        $( "#gal-pause-box" ).css( 'backgroundImage', 'url(' + imgpath + 'gallery_play.gif)' );
                        current_gallery_img = idnum -1;
                        $( "#gallery-image-box" ).fadeOut(  800, showNewGalleryImage );
                    }
                }
            });
        }
        
        $( "#galctl-box-1" ).removeClass( 'gal-index-box-cold' );
        $( "#galctl-box-1" ).addClass( 'gal-index-box-hot' );

        // Set the gallery controls pause button action
        $( "#gal-pause-box" ).click( function( event ) {
            var imgpath = img_prefix + "/images/common/";
            
            if ( gallery_state == 1 ) {
                clearInterval( gallery_interval_id );
                gallery_state = 0;
                $( this ).css( 'background-image', 'url(' + imgpath + 'gallery_play.gif)' );
            }
            else {
                gallery_interval_id = setInterval( "rotate_gallery_image()", gallery_interval );
                gallery_state = 1;
                $( this ).css( 'background-image', 'url(' + imgpath + 'gallery_pause.gif)' );
            }
        });
    }
    

    // For <IE6, apply the PNG transparency fix to the spotlight product
    if ( is_ie6 || islt_ie6 ) {
        $( "#sprod-1" ).supersleight( ss_setting );
    }
    
} );


/**
* This is called every gallery_interval to change to the next image
*/
function rotate_gallery_image() {
    if ( typeof( gallery_images ) == 'undefined' ) {
        return;
    }
    nextGalleryImage();
}

/**
* This is called to hide the gallery controls
*/
function close_gallery_controls() {
    $( "#gallery-controls" ).fadeOut( "fast" );
    controls_id = -1;
}
    
/**
* This function will set the next gallery image to be displayed and
* fade out the current image
*/
function nextGalleryImage() {
    var num_images = gallery_images.length;
    if ( num_images == 0 ) {
        return;
    }
    
    $( "#galctl-box-" + ( current_gallery_img + 1 ) ).removeClass( 'gal-index-box-hot' );
    $( "#galctl-box-" + ( current_gallery_img + 1 ) ).addClass( 'gal-index-box-cold' );
    
    current_gallery_img = ( current_gallery_img + 1 ) % num_images;
    $( "#gallery-image-box" ).fadeOut( "slow", showNewGalleryImage );
}

/**
* This function will set the previous gallery image to be displayed
* and fade out the current image.
*/
function prevGalleryImage() {
    var num_images = gallery_images.length;
    if ( num_images == 0 ) {
        return;
    }
    
    $( "#galctl-box-" + ( current_gallery_img + 1 ) ).removeClass( 'gal-index-box-hot' );
    $( "#galctl-box-" + ( current_gallery_img + 1 ) ).addClass( 'gal-index-box-cold' );

    current_gallery_img--;
    if ( current_gallery_img < 0 ) {
        current_gallery_img = num_images - 1;
    }
    $( "#gallery-image-box" ).fadeOut(  800, showNewGalleryImage );
}

/**
* This function is called at the end of fading out the current image to change
* the image out to the new src and fade in on the new image.
*/
function showNewGalleryImage() {
    $( "#gallery-image" ).attr( "src", gallery_images[current_gallery_img] );
    $( "#galctl-box-" + ( current_gallery_img + 1 ) ).addClass( 'gal-index-box-hot' );
    $( "#gallery-image-box" ).fadeIn( 800 );

// Total Hack for the Strong Manager training seminar image which needs a link.   DWW 8/19/11 for Alfonso and Tiffany  
    var bgImgNm = $("#gallery-image").attr("src");
    if (bgImgNm.indexOf("stop_ad") > 0)
    {
    	$("#gallery-image").click(jumpToStopPage);
    	$("#gallery-image").css("cursor", "pointer");
    }
    else if (bgImgNm.indexOf("standout_gallery") > 0)
	{
    	$("#gallery-image").click(jumpToStandOutPage);
    	$("#gallery-image").css("cursor", "pointer");
	}
    else
    {
    	$("#gallery-image").css("cursor", "default");
    	$("#gallery-image").unbind("click");
    }
}

function jumpToStopPage()
{
	window.location = "http://www.tmbc.com/tools/stop-workshop";
}


function jumpToStrongManagerPage()
{
	window.location = "http://www.tmbc.com/training/strong-manager";
}

function jumpToStandOutPage()
{
	window.location = "http://www.tmbc.com/about-marcus/books";
}

/**
* This function sets up for the previous client profile and fades out the current
* client profile.
*/
function prevClientProfile() {
    var num_profiles = fc_info.length;
    
    current_profile--;
    if ( current_profile < 0 ) {
        if ( num_profiles > 0 ) {
            current_profile = num_profiles - 1;
        }
        else {
            current_profile = 0;;
        }
    }
    
    $( "#cprof-1" ).fadeOut( "slow", showNewProfile );
}


/**
 * This function sets up for the next client profile and fades out the current
 * client profile.
 */
function nextClientProfile() {
    var num_profiles = fc_info.length;
    
    current_profile++;
    if ( current_profile >= num_profiles ) {
        current_profile = 0;;
    }
    
    $( "#cprof-1" ).fadeOut( "slow", showNewProfile );
}


/**
 * This function is called at the end of fading out the current client profile to change
 * the display the new client profile.
 */
function showNewProfile() {
    $( ".cprof-1-link" ).attr( 'href', fc_info[current_profile].link );
    $( "#cprof-1-logo" ).attr( 'src', fc_info[current_profile].imgpath );
    $( "#cprof-1-text" ).html( fc_info[current_profile].text );
    
    $( "#cprof-1" ).fadeIn();
}


