$(document).ready( function (){ 

	$("#content a.fancybox").fancybox();

	// Open external links in new window (without target='_blank')
	$(function() {
		$("a[href*='http://']:not([href*='"+location.hostname+"'],[href*='mailto:'])").click( function() {
			window.open(this.href);
			return false;
		});
	});

	// Contact Form
	var options = { 
		target:'#formData'
	}; 
	$('#contactForm').ajaxForm(options);
	$('#contactForm :input').removeAttr('disabled');
	$('.contactSubmit').click(function(event){
		$(".contactLoader").show();
	});
	
	var fieldID = '';
	
	$('#contactForm .field').each(function(){
		if ($(this).val() != ''){
			$(this).removeClass('inactive');
		} else {
			$(this).addClass('inactive');
		}
	});
	
	$('#contactForm .field').bind("keyup",function(){
		if ($(this).val() != ''){
			$(this).removeClass('inactive');
		} else {
			$(this).addClass('inactive');
		}
	});
	
	// -----------------------------
	// Top pull-down contact area
	// -----------------------------
	
	$('.click-off').animate({'opacity': 0}, 0);
	
	$('#contact-block .puller').live('click', function() {
		var content = $('#contact-block .content');
		if (content.is(':visible')) {
			$('.click-off').animate({'opacity': 0, 'duration':200, 'easing':'easOutQuad'}, function(){
				$('.click-off').hide();
			});
			$('.blue').removeClass('active');
			$('.contact-block-fader').fadeOut(600,"easeInOutExpo");
			$(this).html('Contact / Search');
			$(this).removeClass('active');
			content.slideUp(600,"easeInOutExpo");
		} else {
			$('.blue').addClass('active');
			$('.click-off').show();
			$('.click-off').animate({'opacity': 0.6}, 400,'easeOutQuad');
			$(this).html('Close');
			$(this).addClass('active');
			$('.contact-block-fader').fadeIn(600,"easeInOutExpo");
			content.slideDown(500,"easeInOutExpo");
		};
		return false;
	});
	
	$('.contact-link').live('click', function() {
		var content = $('#contact-block .content');
		if (content.is(':visible')) {
			// Do nothing
		} else {	
			$('.blue').addClass('active');
			$('.click-off').show();
			$('.click-off').animate({'opacity': 0.6}, 400,'easeOutQuad');
			$('#contact-block .puller').html('Close');
			$('#contact-block .puller').addClass('active');
			$('.contact-block-fader').fadeIn(600,"easeInOutExpo");
			content.slideDown({duration:500,easing:"easeInOutExpo",complete:function(){
				$('input#name').focus();
			}});
		}
	});
	
	$('.click-off').click(function() {
		var content = $('#contact-block .content');
		if (content.is(':visible')) {
			$('.click-off').animate({'opacity': 0, 'duration':200, 'easing':'easOutQuad'}, function(){
				$('.click-off').hide();
			});
			$('.blue').removeClass('active');
			$('.contact-block-fader').fadeOut(600,"easeInOutExpo");
			$('#contact-block .puller').html('Contact / Search');
			$('#contact-block .puller').removeClass('active');
			content.slideUp(600,"easeInOutExpo");
		}
		return false;
	});
	
	var scrolling = 0;
	
	$(window).scroll(function () { 
      	var content = $('#contact-block .content');
      	if (scrolling) {
      		// Do nothing
      	} else {
			if (content.is(':visible')) {
				scrolling = 1;
				$('.blue').removeClass('active');
				$('.click-off').animate({'opacity': 0, 'duration':200},function(){ scrolling = 0; $('.click-off').hide(); });
				$('#contact-block .puller').html('Contact / Search');
				$('#contact-block .puller').removeClass('active');
				$('.contact-block-fader').fadeOut(600,"easeInOutExpo");
				content.slideUp(600,"easeInOutExpo");
			}
		}
		return false;
    });
    
    
    
    $('.sorting-dropdown a').hover(
    	function(){ $(this).addClass('a-sort-hover'); },
    	function(){ $(this).removeClass('a-sort-hover'); }
    );

	// -----------------------------
	// Back Home Animation
	// -----------------------------
	
	$('h1#logo a').hover(function() {  
		$('a.back-home').stop().animate({'width': 124}, 300,'easeInOutExpo', function(){ $(this).show(); });
	}, function() {  
		$('a.back-home').stop().animate({'width': 1}, 300, 'easeInOutExpo', function(){ $(this).hide(); });
	});
	
	

	// -----------------------------
	// Slider nav blocks
	// -----------------------------
	$('ul.nav-box li a span').animate({'opacity': 0}, 0);
	$('ul.nav-box li a').hover(function() {  
		$('span', this).stop().animate({'opacity': 1}, 200,'easeOutSine');
	}, function() {  
		$('span', this).stop().animate({'opacity': 0}, 300, 'easeOutQuad');
	});
	
	$('ul.nav-box li a').click(function(){
		var slideID = this.id;
		slideID = slideID.split("-");
		slideID = slideID[2];
		moveWhere = slideID * 270;
		moveWhere = moveWhere - 270;
		$('.slide-link').removeClass("active");
		$(this).addClass("active");
		$('.copy-blocks').stop().animate({'top': '-'+moveWhere},800,'easeOutExpo');
		
		$('.slide-image').stop().animate({'opacity': 0}, 700, 'easeOutQuad');
		$('#slide-image-'+slideID).stop().animate({'opacity': 1}, 500, 'easeOutQuad');
	});
	
	
	// -----------------------------
	// Box Hovers
	// -----------------------------
	$('.box-holder a.hover').stop().animate({'opacity': 0}, 0);
	$('.box-row').hover(function() {
		$(this).find('.box-holder a.hover').stop().animate({'opacity': 1}, 200,'easeOutSine');
	}, function() {
		$(this).find('.box-holder a.hover').stop().animate({'opacity': 0}, 300, 'easeOutQuad');
	});
	
	
	// -----------------------------
	// Our Work Hovers
	// -----------------------------
	$('.white-box').hover(function() {
		$(this).find('.work-slider').stop().animate({'top': '0'}, 400,'easeOutExpo');
	}, function() {
		$(this).find('.work-slider').stop().animate({'top': '218px'}, 400, 'easeInOutExpo');
	});


	// -----------------------------
	// Blue Box Hovers
	// -----------------------------
	$('span.blue-hover').stop().animate({'opacity': 0}, 0);
	$('.silver-box').hover(function() {
		$(this).find('span.blue-hover').stop().animate({'opacity': 1}, 200,'easeOutSine');
	}, function() {
		$(this).find('span.blue-hover').stop().animate({'opacity': 0}, 300, 'easeOutQuad');
	});
	
	
	// -----------------------------
	// Click the Box
	// -----------------------------
	$('.silver-box').click(function(){
		var linkHREF = $(this).find('h3 a').attr('href');
		location.href = linkHREF;
	});
	
});