//Stuff that needs to happen before the DOM loads
Cufon.replace('.client h2', { fontFamily: 'dec' });
Cufon.replace('h2.usToggle', { fontFamily: 'let' });
Cufon.replace('h2.themToggle', { fontFamily: 'let' });
Cufon.replace('span.exclaim', { fontFamily: 'let' });
Cufon.replace('.title', { fontFamily: 'let' });
Cufon.replace('h3.manifesto', { hover: true, fontFamily: 'dec' });
Cufon.replace('.dec', { hover: true, fontFamily: 'dec' });
Cufon.replace('.homebox h5', { hover: true, fontFamily: 'old' });
Cufon.replace('.bar h5', { hover: true, fontFamily: 'old' });
Cufon.replace('.barbody h5', { hover: true, fontFamily: 'let' });
Cufon.replace('.contactlink', { hover: true, fontFamily: 'old' });

//Stuff that needs to happen after the DOM loads
$(document).ready(function() {

	//Logo Interaction
	//$('.logo').hover(function() {
	//	$(this).find('img').animate({'padding-right': '5px'}, 150);
	//}, function() {
	//	$(this).find('img').animate({'padding-right': '0px'}, 150);
	//});



	//Big pane slidey action
	var usWrapper      = $('#us');
	var themWrapper    = $('#them');
	var wrapper        = $('.innerContainer');
	var usButton       = $('.usToggle');
	var themButton     = $('.themToggle');
	var themLink       = $('.showThem');
	
	//US Button click
	usButton.click(function() {
        if ($projectDetail.height() > 1) {
            $projectDetail.slideUp();
        };
        wrapper.animate({
            left: 0,
            right: 'auto'   
        }, 1500, function() {
            usWrapper.addClass('active');
            themWrapper.removeClass('active');
        });
	});
	
	//THEM Button click
	themButton.click(function() {
        wrapper.animate({
            left: -(sectionW - 100)   
        }, 1000, function() {
            usWrapper.removeClass('active');
            themWrapper.addClass('active');
        });
	});
	
	//a.showThem link click (same function as 'themButton.click...' above
	themLink.click(function(e) {
        e.preventDefault();
        wrapper.animate({
            left: -(sectionW - 100)   
        }, 1000, function() {
            usWrapper.removeClass('active');
            themWrapper.addClass('active');
        });
	});

	//Image slider thingie
	$('.image_slider').mousemove(function(e) {
		var mouseX = e.clientX;
		$(this).css('background-position', Math.floor(mouseX * 2) + 'px 0');
	});
	
	//Project Thumbnail switching
	$('.project').hover(function(){
        $(this).find('.visible').fadeOut(400);
    },function(){
        $(this).find('.visible').fadeIn(400);
    });	
	
	//Opening a project
    var $el             = $(),
        $projectDetail  = $('#projectDetail'),
        $project        = $('.project');
        
    $project.click(function(e) {
        if ($projectDetail.size() > 1) {
            $projectDetail.fadeOut();
        } else {
            $projectDetail.hide();
        }
        
        $el = $(this);
        
        //Scroll people back up
        $('html,body').animate({scrollTop: 0}, 1000);
        
        var url = $el.find('a').attr('href');
        
        $projectDetail.load(url, function() {
            $(this).slideDown(1100);
			Cufon.replace('.client h2', { fontFamily: 'dec' });
			Cufon.replace('.clientright li', { fontFamily: 'let' });
			$('#slider').nivoSlider({effect: 'slideInLeft'});
        });
        
        //Project close button
        $('.detailClose').live('click', function(e) {
            e.preventDefault();
            $projectDetail.slideUp();
        });
        
        //Keep the href from taking someone to the html asset
        e.preventDefault();
    
    });
    
    //Contact form hide/show actions
    var contactButton   = $('.contactlink');
    var contactForm     = $('.contactform');
    
    contactForm.hide();
    
    contactButton.click(function(){
        contactForm.slideToggle();
    });

    //Running the inFieldLabels plugin    
    $("label").inFieldLabels();
    
    //Contact form magic
	$("#contact-form").submit(function(){
	
		var str = $(this).serialize();
		
		$.ajax({
			type: "POST",
			url: "contact.php",
			data: str,
			success: function(msg){
			    
				$(".form-success").ajaxComplete(function(event, request, settings){
				
					if(msg == 'OK') {
						//result = '<div class="notification_ok">Boo ya. Your message has been sent.</div>';
						//$("#fields").hide();
						$(this).fadeIn(1500).fadeOut(1500);
						$('#contact-form')[0].reset();
						$("label").inFieldLabels();
					} else {
						$('.form-error').fadeIn(1500).fadeOut(1500);
					}
				});
			}
		});
	return false;
	});

	//US info section hide/show actions
	$('.barbody').hide();
	$('.bar > .bar-status').addClass('closed');

	$('.bar').click(function() {
		var siblingInfo = $(this).next('.barbody');
		if ($(this).children('.bar-status').hasClass('closed')) {
			siblingInfo.slideDown();
			$(this).children('.bar-status').toggleClass('closed open');
		} else {
			siblingInfo.slideUp();
			$(this).children('.bar-status').toggleClass('open closed');
		}
	});
	
	//Opening About
	$('.movesusmore').click(function(e){
		e.preventDefault;
		$('#movesus').find('.barbody').slideDown();
		var barStatus = $('#movesus').find('.bar-status');
		if (barStatus.hasClass('closed')) {
			barStatus.toggleClass('closed open');
		}		
		var servicesY = $('#movesus').offset();
		$('html,body').animate({scrollTop: servicesY.top});
	});
	
	//Opening team content when clicking on a thumb
	$('.gut3 div').click(function(){
		$('#talent').find('.barbody').slideDown();
		var barStatus = $('#talent').find('.bar-status');
		if (barStatus.hasClass('closed')) {
			barStatus.toggleClass('closed open');
		}		
		var talentY = $('#talent').offset();
		$('html,body').animate({scrollTop: talentY.top});
	});

	//Opening Services
	$('.gutmid').find('a').click(function(e){
		e.preventDefault;
		$('#services').find('.barbody').slideDown();
		var barStatus = $('#services').find('.bar-status');
		if (barStatus.hasClass('closed')) {
			barStatus.toggleClass('closed open');
		}		
		var servicesY = $('#services').offset();
		$('html,body').animate({scrollTop: servicesY.top});
	});

    //Setting document element widths
    function setWidths() {

        var windowW;
        var windowH;
        
        // the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
        if (typeof window.innerWidth != 'undefined')
        {
            windowW = window.innerWidth,
            windowH = document.body.clientHeight
        }
        
        // IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
        else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0)
        {
           windowW = document.documentElement.clientWidth,
           windowH = document.documentElement.clientHeight
        }
        
        // older versions of IE
        else
        {
           windowW = document.getElementsByTagName('body')[0].clientWidth,
           windowH = document.getElementsByTagName('body')[0].clientHeight
        }
    
        sectionW = windowW - 115;
        
        $('#us, #them').css({
            width     : sectionW,
            minHeight : windowH
        });
		                
    }
    
	//Fire off the width/height function above
    setWidths();
    
    //Adjust stuff when the window is resized
	$(window).resize(function()
	{
		setWidths();
		
		if($('#them').hasClass('active')){
            $('.innerContainer').css('left', -(sectionW - 100));
		}
		
	});
	
	//When something changes, make sure the #us / #them heights are equal
	$(document).resize(function(){
	   var usH = $('#us').height();
	   $('#them').css({minHeight: usH});
	});

});
