if (jQuery) // Check if jQuery is loaded
{
  (function($) { // Making $ accessible even in noConflict
    
    jQuery.fn.fadingImages = function(){
      return this.each(function(){
        var _hold = $(this);
        var _el = $(this).find('li');
        var _count = _el.index(_el.filter(':last')) + 1;
        var _active = 0;
        var _timeout = 3500;
        var _speed = 750;
        
        _el.css('position', 'absolute').css('top', 0).css('left', 0);
        
        for (var i=0; i < _count; i++)
        {
          _el.eq(i).css('z-index', _count - i);
        }
        
        function changeImage()
        {
          _active++;
          if(_active == _count)
          {
            _active = 0;
            _el.eq(_active).fadeIn(_speed, function(){
              _el.not(':first').fadeIn(1);
            });
          }
          else
          {
            _el.eq(_active - 1).fadeOut(_speed);
          }
          setTimeout(changeImage, _timeout);
        }
        setTimeout(changeImage, _timeout);
      });
    }
    
    
    function initExpertForm ()
    {
      $('#expert-form').each(function(){
        var hold = $(this);
        var photo = hold.find('.photo');
        var image = photo.find('.image');
        hold.find('select').bind('change', function(){
          if($(this).val() != 'false')
          {
            photo.css('background-position', 'center center');
            image.css('background-image', 'url(/images/experts/form/' + $(this).val() + '.jpg)');
          }
          else
          {
            photo.css('background-position', '-100px -100px');
            image.css('background-image', 'none');
          }
        });
				
				hold.find('select').change();
        
        hold.bind('submit', function(){
          if(hold.find('#expert-form-expert').val() == 'false')
          {
            alert("You haven't choosen an expert"); 
            return false;
          }
          if(hold.find('#expert-form-name').val() == '')
          {
            alert("You must enter your name."); 
            return false;
          }
          if(hold.find('#expert-form-email').val() == '')
          {
            alert("You must enter your email address."); 
            return false;
          }
          if(hold.find('#expert-form-message').val().length < 10)
          {
            alert("Your question is too short, without facts that would help to answer the question"); 
            return false;
          }
          if(!hold.find('#expert-form-agree').get(0).checked)
          { 
            alert('You must agree to the terms in order to proceed.'); 
            return false;
          }
          return true;
        });
        
      });
    }
    
    
    $(document).ready(function(){
      
      // Adding placeholders for form fields
      // This MUST be first
      if($.fn.placeholder)
      {
        $(".placeholder").placeholder();
      }
      
      initExpertForm();
			
			$("#expert-form").attr('action', '/expert/index.php/frontend/ask');
      
      if($.fn.fadingImages)
      {      
        $('#expert-faces').fadingImages();
      }
      
      // Setting up action url for contact form
      var contactForm = $("#contact-form").attr('action', '/contact-form-mailer.php');
    
      // Adding basic validation to contact form
      if($.fn.checkRequired)
      {
        contactForm.checkRequired();
      }
    });
  })(jQuery);
}

