﻿/**
 * tld.home.js
 * Manages the styles and functionality of the home page.
 *
 * tld.home.js used for development only.  tld.home.min.js used for production.
 *
 * @author: Jeremy Burton (jermbo002@gmail.com)
 * @company: SohoPros Inc. (www.sohopros.com)
 * @date: 11 November 2009
 * @copyright TexasLegalDocs.com
 */
 
 var tld = window.tld || {};
 
 tld.home = (function() {
    var salutation, testimonialsContainer, testimonials, curTestimonial;
    
    function initVariables() {
        salutation = $( '#salutation' );
        testimonialsContainer = $( '#testimonials' );
        testimonials = [];
        curTestimonial = -1;
    }
    
    function setSalutation() {
        var currentTime = new Date()
        var hour = currentTime.getHours();
        if ( hour < 12 ) {
            salutation.text( 'Morning' );
        }
        else if ( hour >= 12 && hour <= 18 ) {
            salutation.text( 'Afternoon' );
        }
        else {
            salutation.text( 'Evening' );
        }
    }
    
    function getTestimonialsSuccess( result ) {
        if ( result ) {
            testimonials = $(result);
            
            // start with a random testimonial
            curTestimonial = Math.floor(Math.random() * testimonials.length)
            
            // display the current testimonial
            $( '<p></p>' )
                .css( 'margin', '0' )
                .text( testimonials[curTestimonial].testimonial )
                .appendTo( testimonialsContainer )
                .show();
            
            setTimeout( switchTestimonials, 5000 );
        }
    }
    
    function getTestimonialsFailure( error ) {
    
    }
    
    function switchTestimonials() {
        // update curTestimonial 
        (curTestimonial + 1) === testimonials.length ? curTestimonial = 0 : curTestimonial++;
        
        // remove the current testimonial displayed and then display the new one
        if ( !$.support.opacity ) {
            $( '#testimonials p:first' ).slideUp( 500, function() {
                $( this ).remove();
                $( '<p></p>' )
                    .css( 'margin', '0' )
                    .text( testimonials[curTestimonial].testimonial )
                    .appendTo( testimonialsContainer )
                    .hide()
                    .slideDown( 500, function() {
                        setTimeout( switchTestimonials, 5000 );
                    });
            });
        }
        else {
            $( '#testimonials p:first' )
                .animate( { 'height': 'toggle', 'opacity': 'toggle' }, 500, function() {
                    $( this ).remove();
                    $( '<p></p>' )
                        .css( 'margin', '0' )
                        .text( testimonials[curTestimonial].testimonial )
                        .appendTo( testimonialsContainer )
                        .hide()
                        .animate( { 'height': 'toggle', 'opacity': 'toggle' }, 500, function() {
                            setTimeout(switchTestimonials, 5000);
                        });
                });
        }
    }
    
    function initStepBoxes() {
        var h = 0;
        $( '#tbl-home-steps .style-box' ).each( function() {
            var height = $( this ).outerHeight();
            if ( height > h ) {
                h = height;
            }
        });
        $( '.style-box' ).height( h );
    }
    
    return {
        init: function() {  
            // initialize variables
            initVariables();
            
            // get all active testimonials
            // tld.ajax('WebServices/wsTestimonials.asmx/getActiveTestimonials', '{}', getTestimonialsSuccess, getTestimonialsFailure);
            
            // set salutation
            setSalutation();
            
            // init step boxes
            initStepBoxes();
        }
    };
 })();
 
 $(function(){tld.home.init();});