
$(window).load(function() {
	/* ========================================================================================= */	
	/* ========================================================================================= */	
	/* fullscreen background */
	var theWindow		= $(window),
		$bg				= $("#background"),
		aspectRatio		= $bg.width() / $bg.height();

	function resizeBg() {
			if ( (theWindow.width() / theWindow.height()) < aspectRatio ) {
				$bg
					.removeClass()
					.addClass('bgheight');
			} else {
				$bg
					.removeClass()
					.addClass('bgwidth');
			}
	}
	theWindow.resize(function() {
		resizeBg();
	}).trigger("resize");
	
}); // end window load



$(document).ready(function() {
	/* ========================================================================================= */	
	/* ========================================================================================= */
	/* keep postcards center */			
	if ($("#wrapper").length) {			
		$('#wrapper').centerMe({
		  elementHeigh: 433, // this value should be total height of the choosen element, include paddings if any.
		  displayBlock: true // if true, sets display:block to the element. ( note css display:none )
		});
	};
		
	/* ========================================================================================= */
	/* ========================================================================================= */
	/* add class for each postcards */	
	var arr = [ "card-a", "card-b", "card-c", "card-d", "card-e", "card-f"]; // 6 total
	$('.card:not(".card-g")').each(function(){
		$(this).addClass(arr[$(this).index()]); // walk the array base on the index number
	});
	
	/* ========================================================================================= */
	/* ========================================================================================= */		
	/* fade in postcards and open navigation */	
	$('.card').fadeIn("slow");

	/* ========================================================================================= */
	/* ========================================================================================= */
	/* controlling postcards */	
	
	var k = $(".card").draggable({ /* swap card depth and drag */
		drag: function(){
			// $(this).addClass('dragging');
		}
	}).mousedown(function() {
		k.css({ 'z-index': 0 });
		$(this).css({ 'z-index': 10 });
		$(this).addClass('straight');
	});
	
	k.mousedown(function() {
		$(this).addClass('mousedown');
	}).mouseup(function() {
		$(this).removeClass('mousedown');
	});
	
	$('.card-a').find('.remove').remove();
	
	$('.remove').click(function(event){ 
		event.preventDefault();
		$(this).closest('.card').prev().addClass('straight');
		$(this).closest('.card').remove();

	});
		
	/*
	$('.rotate').click(function(event){ 
		event.preventDefault();
		$(this).closest('.card').addClass('straight');
		$(this).remove();
	}); */	

	if ( $("body.home").length ) {		
		// $('body').append('<div id="drag-to-move"><img src="http://dptr.co/wp-content/themes/departure/images/postcard-drag-to-move.png" /></div>');
		$('body').append('<div id="drag-to-move">Click to drag postcards</div>');
	};
	
	$(".card-g").mousemove(function(e){
    	$('#drag-to-move').css('top', e.clientY-25).css('left', e.clientX-0);
	}).mousedown(function(){
		$('#drag-to-move').remove();
	}).mouseenter(function(){
		$('#drag-to-move').show();
	}).mouseleave(function(e){
		$('#drag-to-move').hide();
	});

	/* ========================================================================================= */
	/* ========================================================================================= */
	/* read more fancybox */
	if ( $(".home").length ) {
			$(".more-link").fancybox({
				padding				: 0, 
				width 				: 670, 
				height 				: 700,
				autoScale			: false,
				transitionIn		: 'none',
				transitionOut		: 'none',
				overlayColor		: '#130502',
				overlayOpacity		: 0.7,					
				type				: 'iframe'
			});	
	}

	/* ========================================================================================= */				
	/* ========================================================================================= */
	/* single post */
	$('p span:empty').parent().remove();
	
	/* ========================================================================================= */	
	/* ========================================================================================= */
	/* fade in section ( work - projects )  */
	
	$('#projects').fadeIn(1700);

	$('.contanier a.thumbnail').hover(function() {
		$(this).stop().animate({'background-position': '0 0'});
			
	}, function() {
		$(this).stop().animate({'background-position': '0 -200px'});
	}); 
	
	$('.contanier a.thumbnail').click(function(event) {
		event.preventDefault();	
		$('.description').stop().animate({ height:0, width:0 });
		$('.inner').hide();
		
		$(this).prev().stop().animate({ height:'330px', width:'570px', opacity: 1 }, 600, 'easeOutBack', function() {
			$(this).children().fadeIn(); // fadein .inner
		});
	});

	$('.contanier').eq(2).children('.description').addClass('right');
	$('.contanier').eq(5).children('.description').addClass('right');	
	$('.contanier').eq(6).children('.description').addClass('bottom');
	$('.contanier').eq(7).children('.description').addClass('bottom');	
	$('.contanier').eq(8).children('.description').addClass('bottomright');		
		
	$('a.close').click(function(event) {
		event.preventDefault();
		$('.description').animate({ height:0, width:0 });
		$('.inner').hide();
	});

	/* ========================================================================================= */
	/* ========================================================================================= */
	// the work digital section
	
	$(".inner h4:contains('Web')").siblings('p').not('p:eq(0)').hide(); // hide all except 1st

	$("a.obox[rel='digital']").mouseenter(function(){
		$(".inner h4:contains('Web')").siblings('p').hide(); // hide all
		var n = parseInt($(this).text());
		$(this).closest('.inner').children('p:eq(' + n + ')').show();

		var k = $(this).closest('.inner').children('p:eq(' + n + ')').children('span.hide').text();
		$(this).closest('.inner').children('h2').html('DIGITAL | ' + k);
	});
	
	
	$(".inner h4:contains('Web')").siblings('.boxnumbers').mouseleave(function(){
		$(this).prevAll('p').hide();
		$(this).prevAll('p').last().show(); // reverted
	});
	
	/* ========================================================================================= */
	/* ========================================================================================= */
	/* the work - fancybox */
			
	if ($(".contanier").length) {		
		$("a.obox").fancybox({
					overlayColor	: '#130502',
					overlayOpacity	: 0.7,
					padding			: 0,
					transitionIn	: 'elastic'
		});

		$("#inline-crossfit-music").fancybox({
				type 				: 'inline',			
				overlayColor		: '#130502',
				overlayOpacity		: 0.7,
				padding				: 0,				
				transitionIn		: 'none',
				transitionOut		: 'none'
		});
		
		$("#inline-lifetech-gibco-freedom-video").fancybox({
				type 				: 'inline',			
				overlayColor		: '#130502',
				overlayOpacity		: 0.7,				
				padding				: 0,				
				transitionIn		: 'none',
				transitionOut		: 'none',
				autoDimensions		: false,
				width	 			: 496, 
				height		 		: 279
		});
		
		$("#inline-redbrick-video").fancybox({
				type 				: 'inline',			
				overlayColor		: '#130502',
				overlayOpacity		: 0.7,				
				padding				: 0,				
				transitionIn		: 'none',
				transitionOut		: 'none',
				autoDimensions		: false,
				width	 			: 720, 
				height		 		: 406
		});
				
		$(".inline-pick-up-stix-video").fancybox({
				type 				: 'inline',			
				overlayColor		: '#130502',
				overlayOpacity		: 0.7,				
				padding				: 0,				
				transitionIn		: 'none',
				transitionOut		: 'none',
				autoDimensions		: false,
				width	 			: 640, 
				height		 		: 480
		});		
	}
	
	/* ========================================================================================= */
	/* ========================================================================================= */
	/* ajax send email */
	
	$("form[name='contact']").submit(function(event) {
	event.preventDefault();
	$.post("http://dptr.co/wp-content/themes/departure/send-mail.php", {
							name: $("input[name='name']").val(),
							phone: $("input[name='phone']").val(),
							email: $("input[name='email']").val(),								
							company: $("input[name='company']").val(),
							message: $("textarea[name='message']").val()
							},
		function(data){
			if (data == 'Thanks for your message. We’ll get back to you shortly.' ) {
				$('form').fadeOut();
				$('.response').html(data);
			} else {
				$('.response').html(data);
			}
		});
	});
	
	
	/* ========================================================================================= */		
}); // end doc ready


