$(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');
		}
	});
	
	$('#subscribeForm .field').each(function(){
		if ($(this).val() != ''){
			$(this).removeClass('inactive');
		} else {
			$(this).addClass('inactive');
		}
	});
	
	$('#mc-embedded-subscribe-form .field').bind("keyup",function(){
		if ($(this).val() != ''){
			$(this).removeClass('inactive');
		} else {
			$(this).addClass('inactive');
		}
	});
	
	if ($('.testimonial-block').has('.testimonial-slide')){
		var animating = false;
		$('.testimonial-slide').eq(0).show();
		var currentTestimonial = 0;
		var totalTestimonials = $('.testimonial-slide').size();
		totalTestimonials = totalTestimonials - 1;
	}
	
	$('.testimonial-block .next-arrow').click(function(){
		if (animating == false) {
			animating = true;
			var slideHeight = $('.testimonial-slide').eq(currentTestimonial).find('.testimonial-slide-content').height();
			$('.testimonial-slide').css({'height':slideHeight+'px','overflow-y':'hidden'});
			var newTestimonial = parseInt(currentTestimonial) + 1;
			if (newTestimonial > totalTestimonials) { newTestimonial = 0; }
			$('.testimonial-slide').eq(currentTestimonial).hide();
			$('.testimonial-slide').eq(newTestimonial).fadeIn('fast');
			var slideHeight = $('.testimonial-slide').eq(newTestimonial).find('.testimonial-slide-content').height();
			$('.testimonial-slide').eq(newTestimonial).animate({'height':slideHeight+'px'},400,'easeOutQuad',function(){
				$('.testimonial-slide').animate({'height':slideHeight+'px'},0);
				animating = false;
			});
			
			
			currentTestimonial = newTestimonial;
		}
		return false;
	});
	
	// -----------------------------
	// 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 / SUBSCRIBE / 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 Window');
			$(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 / Subscribe / 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 / Subscribe / 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
	// -----------------------------
	
	var currentSlideID = 1;
	
	$('ul.slider-buttons li:last-child').addClass('no-border');
	$('ul.slider-buttons li a').click(function(){
		var slideID = this.id;
		slideID = slideID.split("-");
		slideID = slideID[2];
		currentSlideID = slideID;
		slideNewID = parseInt(slideID) - 1;
		moveWhere = slideNewID * 417;
		$('.slider-buttons li').removeClass("active");
		$(this).parent().addClass("active");
		$('.slider-text').stop().animate({'top': '-'+moveWhere+'px'},1000,'easeInOutExpo');
		
		$('.slider-image').stop().animate({'opacity': 0}, 1000, 'easeOutQuad');
		$('#slider-image-'+slideID).stop().animate({'opacity': 1}, 1000, 'easeOutQuad');
	});
	
	
	function move_to_next(){
		
		slideID = parseInt(currentSlideID) + 1;
		if (slideID == 4) { slideID = 1; }
		if (slideID != currentSlideID) {
			
			animationOn = 1;
		
			$('.slider-buttons li').removeClass("active");
			$('.slider-buttons li a#slider-button-'+slideID).parent().addClass("active");
			
			currentSlideID = slideID;
			slideNewID = parseInt(slideID) - 1;
			moveWhere = slideNewID * 417;

			$('.slider-text').stop().animate({'top': '-'+moveWhere+'px'},1000,'easeInOutExpo');
			
			$('.slider-image').stop().animate({'opacity': 0}, 1000, 'easeOutQuad');
			$('#slider-image-'+slideID).stop().animate({'opacity': 1}, 1000, 'easeOutQuad');
		
		}
		
	}
	
	// START AUTO-ROTATE CODE
	var timer;
  	var donext = function (x){ move_to_next(); }
  
  	var dotimer = function (){
    	if(timer != null) {
      		clearInterval(timer);
    	}
  
    	timer = setInterval(function() {
      		donext();
    	}, 9000); // Change the time in between rotations here (in milliseconds)   
  	}
  	
  	
  
  	dotimer();
  	
  	$('.slider-buttons').hover(function(){
  		clearInterval(timer);
  	},function(){
  		dotimer();
  	});
  	// END AUTO-ROTATE CODE
	
	
	// -----------------------------
	// 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;
	});
	
	/*var keys 	= [];
	var konami  = '38,38,40,40,37,39,37,39';
	var konamiActive = false;
	
	$(document).keydown(
		function(e) {
			keys.push( e.keyCode );
			if ( keys.toString().indexOf( konami ) >= 0 ){
				konamiCode();
				konamiActive = true;
				keys = [];
			}
		}
	);
	
	function konamiCode(){
		if (konamiActive == false){
			//var i,s,ss=['http://kathack.com/js/kh.js','http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js'];
			//for(i=0;i!=ss.length;i++){s=document.createElement('script');s.src=ss[i];document.body.appendChild(s);}void(0);
			var s = document.createElement('script');
			s.type='text/javascript';
			document.body.appendChild(s);s.src='http://erkie.github.com/asteroids.min.js';
		}
	}*/
	
});


