var $j = jQuery.noConflict();


$j(document).ready(function(){

  // email (quick contact form)
  $j('#telephone').click(function(){
	if($j(this).hasClass('open')){
	  $j('#topbar').animate({height:10},400,RevertTopbar);
	}else{
	  $j(this).addClass('open').css('background-image','url(/themes/aedis/images/email_bg.gif)').parent().css('background-image','url(/themes/aedis/images/phone_bg.gif)');
	  $j('#topbar').css('background','#222222').animate({height:$j('#callback').height()});
	}
	return false;
  });
  

  var RevertTopbar = function(){
    $j('#topbar').css('background','#8A3F9E');
    $j('#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){
  $j('#menu li:last-child').addClass('last');
  $j('#menu li').each(function(){
	$j(this).click(function(){
	  window.location = $j(this).find('a').attr('href');
	});
	var openHeight = $j(this).children('.hovermenu-wrap').height();
	$j(this).children('.hovermenu-wrap').height('0');
	$j(this).hover(function(){
	  $j(this).addClass('current').children('.hovermenu-wrap').stop().animate({height:openHeight},200);
	},function(){
	  $j(this).removeClass('current').children('.hovermenu-wrap').stop().animate({height:0},1);
	});
  });
  
  
  // slideshow


  $j('#slideshow .slideshow-image').css('opacity','0').filter(':first').css('opacity','1').addClass('current first');

  var Slideshow = function(){
    var current = $j('#slideshow .current');
    var target = current.next();
    if(!target.hasClass('slideshow-image')){
      target = $j('#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;
  $j('#quotes .quote').each(function(){
    QuoteCount = QuoteCount+1;
    $j(this).css('opacity','0');
    var quoteHeight = $j(this).height();
    if(quoteHeight > $j('#quotes').height()){
      $j('#quotes').height(quoteHeight);
    }
  }).filter(':first').css('opacity','1').addClass('current first');

  var Quotes = function(){
    var current = $j('#quotes .current');
    var target = current.next();
    if(!target.hasClass('quote')){
      target = $j('#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

  $j('.location').each(function(){
    var contentHeight = $j(this).find('.address').outerHeight();
    $j(this).children().css({height:'20px', width:'20px'}).find('.address-wrapper').css('display','none');
    $j(this).hover(function(){
      $j(this).css('zIndex','1001').children().stop().animate({height:contentHeight, width:'200px'},200).find('.address-wrapper').fadeIn();
    },function(){
      $j(this).children().stop().animate({height:'20px', width:'20px'},200, function(){
        $j(this).parent().css('zIndex','1000');
      }).find('.address-wrapper').fadeOut(100);
    })
  })

  /*
  $j('.address').each(function(){
    $j(this).css('height',$j(this).height());
  }).parent().css('display','none').parent().css({height:'20px',width:'20px'}).hover(function(){
    var newHeight = parseInt($j(this).find('.address-wrapper').find('.address').css('height').replace('px',''));
    $j(this).css('zIndex','2000').stop().animate({height: newHeight, width:'200px'}, 200, function(){
      $j(this).find('.address-wrapper').fadeIn(200);
    });
  },function(){
    $j(this).find('.address-wrapper').fadeOut(200,function(){
      $j(this).css('zIndex','1000').parent().stop().animate({height:'20px',width:'20px'}, 200);
    })
  });*/

  // services
  $j('.service').click(function(){
    window.location = $j(this).find('a').attr('href');
  }).hover(function(){
    $j(this).addClass('shadow');
  },function(){
    $j(this).removeClass('shadow');
  });

  $j('#layout-control a').click(function(){
    var services = $j('#services-layout');
    var newLayout = $j(this).attr('id');
    if(!services.hasClass(newLayout)){
      $j('#layout-control .current').removeClass('current');
      $j(this).addClass('current');
      services.hide().attr('class',newLayout).fadeIn(400);
    }
  });

  // forms
  $j('#contact-form .required, #surveyor-login-form .required').before('<span class="form-error">Field is required</span>');
  $j('#callback-form .required').after('<span class="form-error">Field is required</span>');
  
  $j('form input, form textarea').focus(function(){
    $j(this).addClass('shadow');
  }).focusout(function(){
    $j(this).removeClass('shadow');
  });
  
  $j('form').submit(function(){
    $j('.form-error').hide();
    var valid = true;
    $j('fieldset',this).each(function(){
      $j(this).find('.required').each(function(){
        if( $j(this).val() == '' ){
          $j(this).siblings('.form-error').fadeIn();
          valid = false;
        }
      });
    });
    return valid;
  });


  //PayPage
  var speed = 1200;
  var startHeight = $j('#readTerms').height()+5;
  var endHeight = $j('#payTerms').height();

  $j('#payTerms').height(startHeight);

	$j('#readTerms').click(function(){
		if($j('#payTerms').hasClass('open')){
			$j('#payTerms').stop().animate({height: startHeight}, speed).removeClass('open').addClass('closed');
		}
		else if($j('#payTerms').hasClass('closed')){
			$j('#payTerms').stop().animate({height: endHeight}, speed).removeClass('closed').addClass('open');
		}
	});


  // footer
  $j('#footer-content').css('opacity','0.5');
  $j('#footer').hover(function(){
    $j('#footer-content').stop().animate({opacity:1},200);
  },function(){
    $j('#footer-content').stop().animate({opacity:0.5},200);
  });

});
