
/////////////////// display loading graphic while site loads ///////////////////

function switchdivs(){
$('#loading').fadeOut('slow',function(){
    });
}


/////////////////// catch all links clicked and log in google analytics //////////////////

 $('a').click(function () {
	var thisel = $(this);
	try
	{
		_gaq.push(['_trackEvent', thisel.parents().andSelf().filter('[id!=""]:first').get(0).id, 'clicked', (thisel.text() || thisel.children('img:first').attr('alt'))]);
	}
	catch (err) {}
});



// fix ie's terrible cleartype for fade functions

jQuery.fn.fixClearType = function(){
    return this.each(function(){
        if( !!(typeof this.style.filter  && this.style.removeAttribute))
            this.style.removeAttribute("filter");
    })

}


$(document).ready(function(){
  
/////////////////// set delay funtion so that bg fades dont interfere with animations ///////////////////
$.fn.delay = function( time, name ) {

    return this.queue( ( name || "fx" ), function() {
        var self = this;
        setTimeout(function() { $.dequeue(self); } , time );
    } );

};



/////////////////// button rollovers ///////////////////
 
    $(".button").hover(function(){
      $(this).addClass('over');
    }, function (){
      $(this).removeClass('over');
    });


/////////////////// main scrollable ///////////////////

  	$("#wrapper").scrollable({
  		easing: 'swing',
  		size: 1,
  		items: '#content',
  		loop: false,
  		virtical: false,
  		speed: 400,
  		clickable: false,
  		keyboard: true
  	}).navigator({ 
  		navi: '.nav',
  		naviItem: '.button',
  		activeClass:'selected' 
  	});

	var api = $("#wrapper").scrollable({api: true}); 
	
	
	$('.about .section-content').css('opacity','.2');
	
	
	
/////////////////// make stuff happen when you switch sections ///////////////////

var workdefaultbgcolor = '#5f6656';

	api.onStart(function() { 
		$('.button a').blur();
	  	$('.section-content').css('opacity','.2');
	  
	  if (this.getPageIndex() == 0) { 
	    $('.home .section-content').css('opacity','');
			$("body").delay(400).animate({ backgroundColor: "#057882" }, 1000);
			$('#arrow').animate({marginLeft:"0" }, 'normal', 'swing' );
			shortcut.remove("up");
			shortcut.remove("down");
		}
		else if (this.getPageIndex() == 1) { 
		  $('.about .section-content').css('opacity','');
			$("body").delay(400).animate({ backgroundColor: "#D69306" }, 1000);
			$('#arrow').animate({marginLeft:"108px" }, 'normal', 'swing' );
			shortcut.remove("up");
			shortcut.remove("down");
		}
		else if (this.getPageIndex() == 2) { 
			$('.services .section-content').css('opacity','');
			$("body").delay(400).animate({ backgroundColor: "#b92f28" }, 1000);
			$('#arrow').animate({marginLeft:"216px" }, 'normal', 'swing' );
			shortcut.remove("up");
			shortcut.remove("down");
		}
		else if (this.getPageIndex() == 3) { 
			$('.work .section-content').css('opacity','');
			$("body").delay(400).animate({ backgroundColor: workdefaultbgcolor }, 1000);
			$('#arrow').animate({marginLeft:"324px" }, 'normal', 'swing' );
			shortcut.add("up",function() {
				prevproject();
			});
			shortcut.add("down",function() {
				nextproject();
			});
		}
		else if (this.getPageIndex() == 4) { 
			$('.blog .section-content').css('opacity','');
			$("body").delay(400).animate({ backgroundColor: "#B40554" }, 1000);
			$('#arrow').animate({marginLeft:"432px" }, 'normal', 'swing' );
			shortcut.remove("up");
			shortcut.remove("down");
		}
		else if (this.getPageIndex() == 5) { 
		  $('.contact .section-content').css('opacity','');
			$("body").delay(400).animate({ backgroundColor: "#7F8902" }, 1000);
			$('#arrow').animate({marginLeft:"540px" }, 'normal', 'swing' );
			shortcut.remove("up");
			shortcut.remove("down");
		}
		
	}); 







/////////////////// link functions to jump to a section ///////////////////

$('.section a.serviceslink').click(function(){
  api.seekTo(2);
});

$('.section a.aboutlink').click(function(){
  api.seekTo(1);
});

$('.section a.worklink').click(function(){
  api.seekTo(3);
});

$('.section a.bloglink').click(function(){
  api.seekTo(4);
});

$('.section a.contactlink').click(function(){
  api.seekTo(5);
});



/////////////////// services functions ///////////////////////

var currentSvcMargin = 0; 


$('.services a.svc-button').mouseover(function(){
  $('.services a.svc-button').removeClass('over');
  $(this).addClass('over');
  if ($(this).hasClass('startups')) {
    $('.services .right-arrow').css('top','143px');
    $('.services .right #service-scroller').animate({marginTop:'0px' }, 'normal', 'swing' );
  }
  
  else if ($(this).hasClass('ucd')) {
    $('.services .right-arrow').css('top','214px');
    $('.services .right #service-scroller').animate({marginTop:'-320px' }, 'normal', 'swing' );
  }
  
  else if ($(this).hasClass('wp')) {
    $('.services .right-arrow').css('top','284px');
    $('.services .right #service-scroller').animate({marginTop:'-640px' }, 'normal', 'swing' );
  }
  
  else if ($(this).hasClass('alacarte')) {
    $('.services .right-arrow').css('top','355px');
    $('.services .right #service-scroller').animate({marginTop:'-960px' }, 'normal', 'swing' );
  }
  
});





/////////////////// portfolio functions ///////////////////////
var currentMargin = 0; 

$('.button-up').live("click", function(){
	prevproject();
});

// mousedown image
$(".button-up").live("mouseup", function(){
	$(this).removeAttr('style');
    }).live("mousedown", function(){
		$(this).css('backgroundPosition','0px -105px');
    });



$('.button-dn').live("click", function(){
	nextproject();
});

// mousedown image
$(".button-dn").live("mouseup", function(){
	$(this).removeAttr('style');
    }).live("mousedown", function(){
		$(this).css('backgroundPosition','0px -105px');
    });


function nextproject(){
	
	if (currentMargin != -4750){
	  currentMargin = currentMargin - 475;
		$('.button-up-disabled').addClass('button-up').removeClass('button-up-disabled');
		$('#projects').animate({marginTop:currentMargin+'px' }, 'normal', 'swing' );
  	changeBGcolor();
	}

	
	if (currentMargin == -4750){
	  $('.button-dn').removeClass('button-dn').addClass('button-dn-disabled');
	}
	
};


function prevproject(){
	if (currentMargin != 0){
		currentMargin = currentMargin + 475;
		$('#projects').animate({marginTop:currentMargin+'px' }, 'normal', 'swing' );
		$('.button-dn-disabled').addClass('button-dn').removeClass('button-dn-disabled');
		if (currentMargin == 0){
			$('.button-up').removeClass('button-up').addClass('button-up-disabled');
		}
	}
	changeBGcolor();
};

function changeBGcolor(){
	//crowdtubetv
  	if (currentMargin == 0) {
    	$("body").delay(400).animate({ backgroundColor: "#5f6656" }, 1000);
	  	workdefaultbgcolor = '#5f6656';
	}

	//appifywp
		else if (currentMargin == -475) {
		$("body").delay(400).animate({ backgroundColor: "#2f84c4" }, 1000);
	  	workdefaultbgcolor = '#2f84c4';
	}

	//pfchangs
  	else if (currentMargin == -950) {
    	$("body").delay(400).animate({ backgroundColor: "#650c0e" }, 1000);
	  	workdefaultbgcolor = '#650c0e';
	}

  //chimp
  	else if (currentMargin == -1425) {
  	  	$("body").delay(400).animate({ backgroundColor: "#a77112" }, 1000);
	    workdefaultbgcolor = '#a77112';
    }

	//ilp
	else if (currentMargin == -1900) {
	  	$("body").delay(400).animate({ backgroundColor: "#940200" }, 1000);
	  	workdefaultbgcolor = '#940200';
  }

	//kindfish
  	else if (currentMargin == -2375) {
  	  $("body").delay(400).animate({ backgroundColor: "#006892" }, 1000);
  	  workdefaultbgcolor = '#006892';
    }  
    
	//damian
    else if (currentMargin == -2850) {
    	$("body").delay(400).animate({ backgroundColor: "#0d5906" }, 1000);
    	workdefaultbgcolor = '#0d5906';
    }

	//ilp icon
  	else if (currentMargin == -3325) {
      $("body").delay(400).animate({ backgroundColor: "#bc0605" }, 1000);
      workdefaultbgcolor = '#bc0605';
    }
        
	//marley legend
    else if (currentMargin == -3800) {
      $("body").delay(400).animate({ backgroundColor: "#97804e" }, 1000);
      workdefaultbgcolor = '#97804e';
    }  

	//lodging.com
    else if (currentMargin == -4275) {
      $("body").delay(400).animate({ backgroundColor: "#2e63a7" }, 1000);
      workdefaultbgcolor = '#2e63a7';
    }

  //yahoo
    else if (currentMargin == -4750) {
      $("body").delay(400).animate({ backgroundColor: "#427db5" }, 1000);
      workdefaultbgcolor = '#427db5';
    }
 
  
};



$("#projects .project a.thumb").hover(function(){
  $('.zoom',this).css('backgroundPosition','0 0');
}, function (){
  $('.zoom',this).css('backgroundPosition','0 -115px');
});





/////////////////// contact form ///////////////////

$('.submit').click(function(){
  
  $('.error').hide();  
  $('input#name, input#email, textarea#message').css('border', '');
  
  var name = $('input#name').val();
  
    if (name == '') {
      $("label#name_error").show();
      $('input#name').css('border', '1px solid red');
      $("input#name").focus();
      return false;
    }

  var email = $('input#email').val();    
    if (email == '') {
      $("label#email_error").show();
      $('input#email').css('border', '1px solid red');
      $("input#email").focus();
      return false;
    }

    var message = $('textarea#message').val();    
      if (message == '') {
        $("label#message_error").show();
        $('textarea#message').css('border', '1px solid red');
        $("textarea#message").focus();
        return false;
      }   
    
       var dataString = 'name='+ name + '&email=' + email + '&message=' + message;  

       $.ajax({  
       type: "POST",  
       url: "email.php",  
       data: dataString,  
       success: function() {  
         $('#formage').fadeOut();
         $('#formage').html("<h2>Thanks for getting in touch!</h2>")  
         .append("<p>I'll respond as soon as possible (usually same day)</p>")  
         .fadeIn(function() {  
            $('#formage');  
          });  
            }  
          });  
        return false;
    
});



}); // end doc ready



