// jQuery no conflict mode.
var $j = jQuery.noConflict();

function clickclear(thisfield, defaulttext)
{
    if (thisfield.value == defaulttext) {
        thisfield.value = "";
    }
}

function clickrecall(thisfield, defaulttext)
{
    if (thisfield.value == "") {
        thisfield.value = defaulttext;
    }
}

jQuery(document).ready(function(){
        
    // Validate the contact form.
    jQuery("#contact-form").validate();
    
    // Validate the blog comment form.
    jQuery("#commentform").validate();
        
    // Home page carousel.
    totalSlideNo    = jQuery('ul#carousel').children().length;
    currentSlideNo  = 0;
    prevSlideNo     = 2;
    nextSlideNo     = 1;
    
    var $carousel = jQuery('#carousel').cycle({ 
        fx:     'scrollHorz', 
        timeout: 7000,
        sync:    1,
        speed:   500,
        pause:   1,
        before:  onBefore,
        after:  onAfter,
        end: onEnd
    });
    
    // Dont autoplay?
    //jQuery('#carousel').cycle('pause'); 
    
    jQuery('.gotoprev > a').click(function() {  
        $carousel.cycle(prevSlideNo);
        return false;
    });
    jQuery('.gotonext > a').click(function() {  
        $carousel.cycle(nextSlideNo);
        return false;
    });
    
    function onEnd(curr,next,opts, fwd) {
        // Go back to slide 1.
        //...
    }
    function onBefore(curr,next,opts, fwd) {
        
        nextPosClass = jQuery(next).attr('class');
        
        nextPosNo = nextPosClass.replace('pos', '');
        
        parseInt(nextPosNo);
        
        if (1 == nextPosNo){
            jQuery('li.gotoprev').hide();
            jQuery('li.gotoprev-disabled').show();
        }else{
            jQuery('li.gotoprev').show();
            jQuery('li.gotoprev-disabled').hide();
        }
        
        if (totalSlideNo == nextPosNo){
            jQuery('li.gotonext').hide();
            jQuery('li.gotonext-disabled').show();
        }else{
            jQuery('li.gotonext').show();
            jQuery('li.gotonext-disabled').hide();
        }
        
    }
    function onAfter(curr,next,opts) {
        currentSlideNo  = (opts.currSlide);
        prevSlideNo     = (opts.currSlide - 1);
        nextSlideNo     = (opts.currSlide + 1);
        
        if (prevSlideNo < 0){
            prevSlideNo = 2;
        }
        
        // Debug:
        /*
        alert('Current: ' + currentSlideNo);
        alert('Prev: ' + prevSlideNo);
        alert('Next: ' + nextSlideNo);
        */
    }
    
    // About Us staff section.
    
    // Animation completed?
    var completed = true;
    jQuery('.staff-names li a').click(function(event){
            
        event.preventDefault();
    
        // If we're in the middle of an animation then dont allow the user to
        // start another.
        if (false == completed){
            return false;
        }
        
        // Set the "completed" flag to false.
        completed = false;
        
        var liClassName = jQuery(this).parent().attr('class');
        
        postID = liClassName.replace('staff-name-', '');
        postID = postID.replace('active', '');
        postID = postID.replace(/ /g, '');
        
        parseInt(postID);
        
        // Set the clicked staff name to active.
        jQuery('.staff-names li').removeClass('active');
        jQuery(this).parent().addClass('active');
        
        // Show the staff avatar & info.
        jQuery('div.staff-infos div.active').fadeOut(function(){
            jQuery(this).removeClass('active');
            jQuery('div.staff-info-' + postID).fadeIn(function(){
                jQuery('div.staff-info-' + postID).addClass('active');
                
                // Set the "completed" flag to true.
                completed = true;
            });
        });
        
        return false;
    });
    
    // Pseudo CSS support for old browsers.
    jQuery('.main-nav ul li:last-child a').parent().addClass('last-child');
    jQuery('.latest-news ul.lastest-new-list li:last-child').addClass('last-child');
    jQuery('ul.clients li:nth-child(3n+3)').addClass('right-most');

});
