$(document).ready(function(){

    var Revealer = function(relativeParent,absoluteChild,speed) {
        var revealHeight = absoluteChild.height();
        relativeParent.height(revealHeight);
        relativeParent.addClass('hidden');
        this.toggleReveal = function() {
            if (!relativeParent.parent().find(':animated').length) {
                if (relativeParent.css('display')!='none' ) {
                    relativeParent.animate({height:0}, speed, function() {
                        relativeParent.hide();
                    });
                } else {
                    relativeParent.height(0).show().animate({height: revealHeight}, speed);
                }
            }
        };
    };
    
    $('#skip-link').after($('#contact_us'));
    
    var contactForm = new Revealer(
        $('#contact_us'),                     // the relatively positioned container
        $('#contact_us .page'),                // the absolutely positioned child
        300                                     // the speed of the animation
    );
    
    $('.contact-link').live('click',function(){
        $('html, body').animate({scrollTop:0}, 'fast');
        contactForm.toggleReveal();
        return false;
    });

	

    $('#contact_us form').live('submit',function(){
        var values = $(this).serialize();
        $.post('/ajax/contact.php',values,function(markup) {
			$('#contact_us .page').html(markup);
		},'html');
        return false;
    });

	//$('#contact_form').validate();

});
$(document).ready(function(){
    
	$('body').addClass('hasJS');
	
	$('.to-top').live('click', function() {
	   $('html, body').animate({scrollTop:0}, 'fast');
	   return false;
	});
	
	$('.active_project .project_items li').live('click', function() {
	    $('.active_project .active').removeClass('active');
	    $(this).addClass('active');
	    var newSrc = $(this).attr('rel')
	    $('.active_project img').fadeOut('fast', function(){
	        $('.active_project img').attr('src', '/images/portfolio/' + newSrc).fadeIn();
	    })
	   $('.active_project .project_title').html($(this).attr('title'));
	});
	$('.portfolio_thumbs article').live('click', function() {
	    $('.active_thumb').removeClass('active_thumb');
	    $(this).addClass('active_thumb');
	   var piece = $(this).attr('data-portfolio-piece');
	   var active_piece = $('.active_project');
	   active_piece.fadeOut('fast', function(){
	       active_piece.removeClass('active_project');
       	   $('#' + piece).fadeIn('fast', function(){
       	       $('#' + piece).addClass('active_project');
       	    });
	   });
	
	   var li_obj = $('.project_items li:first', active_piece);

		if( li_obj.attr('class') !== 'active') {
			$('.active', active_piece).removeClass('active');
	    	$(li_obj).addClass('active');
	    	var newSrc = $(li_obj).attr('rel');
	    	$('img', active_piece).fadeOut('fast', function(){
	        	$('img', active_piece).attr('src', '/images/portfolio/' + newSrc).fadeIn();
	    	})
	    	$('.project_title', active_piece).html($(li_obj).attr('title'));
		}
	
	
	});
	var counter = 1;
	
	$('#next').live('click', function() {
	    $('.portfolio_wrap').animate({
              left: parseInt($('.portfolio_wrap').css('left'),10) == 0 ?
                -1000*counter :
                -1000*counter
        });
		counter++;
		$('#prev').show();
		if(counter == 3) {
			$('#next').hide();
		}
	    return false;
	});
	$('#prev').live('click', function() {
		counter--;
		var newcounter = counter-1;
	    $('.portfolio_wrap').animate({
              left: parseInt($('.portfolio_wrap').css('left'),10) == 0 ?
                -1000*newcounter :
                -1000*newcounter
        });
		$('#next').show();
		if (counter == 1){
			
			$('#prev').hide();
		}
		
		return false;
	});
	$('.content_swap').live('click', function() {
	   var relevant = $(this).attr('data-relevant-content');
	   var active = $('.active_page');
	   var next = $('#' + relevant);
	   active.fadeOut('fast', function(){
	      active.removeClass('active_page');
	      next.addClass('active_page');
	      next.fadeIn(); 
	   });
	
		
	   	return false;
	});
});
