$(document).ready(function(){

  // email (quick contact form)
  $('#telephone').click(function(){
    if($(this).hasClass('open')){
      $('#topbar').animate({height:10},400,RevertTopbar)
    }else{
      $(this).addClass('open').css('background-image','url(/themes/aedis/images/email_bg.gif)').parent().css('background-image','url(/themes/aedis/images/phone_bg.gif)')
      $('#topbar').css('background','#222222').animate({height:$('#callback').height()})
    }
    return false;
  })

  var RevertTopbar = function(){
    $('#topbar').css('background','#8A3F9E')
    $('#telephone').removeClass('open').css('background-image','url(/themes/aedis/images/phone_bg.gif)').parent().css('background-image','url(/themes/aedis/images/email_bg.gif)')
  }

  // menu (standard)
  $('#menu li:last-child').addClass('last')
  $('#menu li').each(function(){
    $(this).click(function(){
      window.location = $(this).find('a').attr('href')
    })
    var openHeight = $(this).children('.hovermenu-wrap').height()
    $(this).children('.hovermenu-wrap').height('0')
    $(this).hover(function(){
      $(this).addClass('current').children('.hovermenu-wrap').stop().animate({height:openHeight},200)
    },function(){
      $(this).removeClass('current').children('.hovermenu-wrap').stop().animate({height:0},1)
    })
  })
  
  // slideshow
  $('#slideshow .slideshow-image').css('opacity','0').filter(':first').css('opacity','1').addClass('current first')

  var Slideshow = function(){
    var current = $('#slideshow .current');
    var target = current.next();
    if(!target.hasClass('slideshow-image')){
      target = $('#slideshow .first');
    }
    current.animate({opacity:0}, 1000).removeClass('current')
    target.animate({opacity:1}, 1000).addClass('current')
  }

  var AutoSlideshow = setInterval( Slideshow, '4000' )

  // quotes
  var QuoteCount = 0;
  $('#quotes .quote').each(function(){
    QuoteCount = QuoteCount+1;
    $(this).css('opacity','0')
    var quoteHeight = $(this).height();
    if(quoteHeight > $('#quotes').height()){
      $('#quotes').height(quoteHeight)
    }
  }).filter(':first').css('opacity','1').addClass('current first')

  var Quotes = function(){
    var current = $('#quotes .current');
    var target = current.next();
    if(!target.hasClass('quote')){
      target = $('#quotes .first');
    }
    if( current != target ){
      current.animate({opacity:0}, 1000).removeClass('current')
      target.animate({opacity:1}, 1000).addClass('current')
    }
  }

  if(QuoteCount>1){
    var AutoQuotes = setInterval( Quotes, '6000' )
  }

  // map

  $('.location').each(function(){
    var contentHeight = $(this).find('.address').outerHeight()
    $(this).children().css({ height:'20px', width:'20px' }).find('.address-wrapper').css('display','none')
    $(this).hover(function(){
      $(this).css('zIndex','1001').children().stop().animate({ height:contentHeight, width:'200px' },200).find('.address-wrapper').fadeIn()
    },function(){
      $(this).children().stop().animate({ height:'20px', width:'20px' },200, function(){ 
        $(this).parent().css('zIndex','1000')
      }).find('.address-wrapper').fadeOut(100)
    })
  })

  /*
  $('.address').each(function(){
    $(this).css('height',$(this).height());
  }).parent().css('display','none').parent().css({height:'20px',width:'20px'}).hover(function(){
    var newHeight = parseInt($(this).find('.address-wrapper').find('.address').css('height').replace('px',''));
    $(this).css('zIndex','2000').stop().animate({height: newHeight, width:'200px'}, 200, function(){
      $(this).find('.address-wrapper').fadeIn(200);
    });
  },function(){
    $(this).find('.address-wrapper').fadeOut(200,function(){
      $(this).css('zIndex','1000').parent().stop().animate({height:'20px',width:'20px'}, 200);
    })
  });*/

  // services
  $('.service').click(function(){
    window.location = $(this).find('a').attr('href');
  }).hover(function(){
    $(this).addClass('shadow')
  },function(){
    $(this).removeClass('shadow')
  });

  $('#layout-control a').click(function(){
    var services = $('#services-layout');
    var newLayout = $(this).attr('id');
    if(!services.hasClass(newLayout)){
      $('#layout-control .current').removeClass('current');
      $(this).addClass('current');
      services.hide().attr('class',newLayout).fadeIn(400);
    }
  })

  // forms
  $('#contact-form .required, #surveyor-login-form .required').before('<span class="form-error">Field is required</span>')
  $('#callback-form .required').after('<span class="form-error">Field is required</span>')
  
  $('form input, form textarea').focus(function(){
    $(this).addClass('shadow')
  }).focusout(function(){
    $(this).removeClass('shadow')
  })
  
  $('form').submit(function(){
    $('.form-error').hide();
    var valid = true;
    $('fieldset',this).each(function(){
      $(this).find('.required').each(function(){
        if( $(this).val() == '' ){
          $(this).siblings('.form-error').fadeIn();
          valid = false;
        }
      })
    })
    return valid;
  })

  // footer
  $('#footer-content').css('opacity','0.5')
  $('#footer').hover(function(){
    $('#footer-content').stop().animate({opacity:1},200)
  },function(){
    $('#footer-content').stop().animate({opacity:0.5},200)
  })

})