$(document).ready(function() {
	
	$('.hide').hide();
	
	$('a[rel="external"]').attr('target', '_blank');
	
	$('#contact-form').validate();
	
	Cufon.replace('*');
	
	// load tooltip data
	var words = new Array();
	var phrases = new Array();
	$.ajax({
		url : "tooltips.xml",
		dataType: "xml",
		success : function (xml) {
			$(xml).find('word').each(function(){
				words.push($(this).text());
				console.log($(this).text());
			});
			$(xml).find('phrase').each(function(){
				phrases.push($(this).text());
				console.log($(this).text());
			});
			
			$('a.fn').mouseover(function(){
				var numWords = words.length;
				var randWordNum = Math.floor(Math.random()*numWords);
				$('#tooltips .blue p').text(words[randWordNum]);
				$('#tooltips .blue').fadeIn();	
			}).mouseout(function(){
				$('#tooltips .blue').hide();
			});
			$('a.ln').mouseover(function(){	
				var numPhrases = phrases.length;
				var randPhraseNum = Math.floor(Math.random()*numPhrases);
				$('#tooltips .green p').text(phrases[randPhraseNum]);
				$('#tooltips .green').fadeIn();
			}).mouseout(function(){
				$('#tooltips .green').hide();
			});
			
			
			
		}
	});
	
	// tooltip events
	$('.blue, .green').hide();
	
	
	// drop opacity
	$('#projects img').css('opacity','.3');	
	
	// fade(target, time,multiplier)
	setTimeout(function(){fade("#projects", 70, .8);}, 750);
	
	// click and mouse over events
	var leftVal = $('#projects img').size()*25;
	//alert($('#projects').css('margin-left'));

	$('#projects a').mouseover(function(){
		if(modalOpen == false) $(this).find('img').stop().animate({'opacity':'1', 'height':'470px', 'margin-top':'29px'}, 100);
	}).mouseout(function(){
		if(modalOpen == false) $(this).find('img').stop().animate({'opacity':'.3', 'height':'499px', 'margin-top':'0px', 'margin-right':'0px'}, 100);
	});
	var modalOpen = false;
	$('#projects a.image').fancybox({
			'transitionIn'	:	'elastic',
			'transitionOut'	:	'elastic',
			'speedIn'		:	400, 
			'speedOut'		:	200,
			'padding'		: 	80,
			'overlayShow'	: 	false,
			'titleShow'		: 	false,
			'onStart'		: 	function(){
				if((parseInt($('#projects').css('margin-left'))-leftVal) >= -745){
					$('#projects').stop().animate({'opacity':'.2', 'margin-left':(parseInt($('#projects').css('margin-left'))-leftVal)+"px"}, 500);
					$('#projects img').each(function(){
						$(this).animate({'height':'400px', 'margin-top':'100px', 'margin-right':'50px'}, 500);
					});
					modalOpen = true;
				}		
			},
			'onClosed'		: 	function(){
				// alert((parseInt($('#projects').css('margin-left'))-leftVal)+"px");
				if((parseInt($('#projects').css('margin-left'))-leftVal) >= -1120){
					$('#projects').stop().animate({'opacity':'1', 'margin-left':(parseInt($('#projects').css('margin-left'))+leftVal)+"px"}, 100);
					$('#projects img').animate({'height':'499px', 'margin-top':'0px', 'opacity':'.3', 'margin-right':'0'},100);
					modalOpen = false;
				}
			}
		});
		$('a[href="#biography"]').fancybox({
				'padding'		:   0,
				'transitionIn'	:	'elastic',
				'transitionOut'	:	'elastic',
				'autoDimensions': 	false,
				'titleShow'		: 	false,
				'speedIn'		:	400, 
				'speedOut'		:	200,
				'width'			: 	900,
				'height'	 	: 	526,	
				'overlayShow'	:	false,
				'hideOnContentClick': false,
				'showCloseButton': 	true,
				'onStart'		: 	function(){
					$('#facebook-inner').css('background','rgba(0,0,0,.9)');
					modalOpen = true;
				},
				'onClosed'		: function(){
					modalOpen = false;
				}
  			});
	$('#projects a.modal').not('a[href="#biography"]').fancybox({
			'padding'		:   0,
			'transitionIn'	:	'elastic',
			'transitionOut'	:	'elastic',
			'autoDimensions': 	false,
			'titleShow'		: 	false,
			'speedIn'		:	400, 
			'speedOut'		:	200,
			'width'			: 	700,
			'height'	 	: 	540,	
			'overlayShow'	:	false,
			'hideOnContentClick': false,
			'showCloseButton': 	true,
			'onStart'		: 	function(){
				//alert(leftVal);
				$('#projects').stop().animate({'opacity':'.2', 'margin-left':(parseInt($('#projects').css('margin-left'))-leftVal)+"px"}, 500);
				$('#projects img').each(function(){
					$(this).stop().animate({'height':'400px', 'margin-top':'100px', 'margin-right':'50px'}, 500);
				});	
				modalOpen = true;
			},
			'onClosed'		: 	function(){
				// alert(leftVal);
				$('#projects').stop().animate({'opacity':'1', 'margin-left':(parseInt($('#projects').css('margin-left'))+leftVal)+"px"}, 100);
				$('#projects img').stop().animate({'height':'499px', 'margin-top':'0px', 'opacity':'.3', 'margin-right':'0'},100);
				modalOpen = false;
			}
		});
		
	// opacity effect
	$('#footer li').css('opacity','.7').mouseover(function(){
		$(this).stop().animate({'opacity':'1'}, 500);
	}).mouseout(function(){
		$(this).stop().animate({'opacity':'.7'}, 500);
	});
	
	
	
	
	if (location.href.indexOf("success") != -1) {
		alert('Thanks!');
	}
	
});



function fade(navigation_id, time, multiplier)
{
	// creates the target paths
	var list_elements = navigation_id + " li img";
	// initiates the timer used for the sliding animation
	var timer = 0;
	// creates the slide animation for all list elements 
	$(list_elements).each(function(i)
	{
		timer = (timer*multiplier + time);
		$(this).animate({ opacity: ".3" }, timer);
		$(this).animate({ opacity: "1" }, timer);
		$(this).animate({ opacity: ".3" }, timer);
	});
}

