$(document).ready(function() {

	$("#request").fancybox({					//Request form
				'width'			: 500,
				'height'		: 600,
				//'scrolling'		: 'yes',
				'transitionIn'	: 'elastic',
				'transitionOut'	: 'elastic',
				'type'			: 'iframe'				
			});


	$("#req2").fancybox({					//Request form footer
				'width'			: 500,
				'height'		: 600,
				//'scrolling'		: 'yes',
				'transitionIn'	: 'elastic',
				'transitionOut'	: 'elastic',
				'type'			: 'iframe'				
			});


	$("#tweet").fancybox({					//twitter feed
		'width'				: 599,
		'height'			: 499,
		'transitionIn'		: 'elastic',
		'transitionOut'		: 'elastic',
			'type'			: 'iframe'
		
	});
    

/* ---------------------------------------------------- */
/*		Input Placeholders
/* ---------------------------------------------------- */

$(function() {
	$('[placeholder]').focus(function() {
		var input = $(this);
		if (input.val() == input.attr('placeholder')) {
			input.val('');
			input.removeClass('placeholder');
		}
	}).blur(function() {
		var input = $(this);
		if (input.val() == '' || input.val() == input.attr('placeholder')) {
			input.addClass('placeholder');
			input.val(input.attr('placeholder'));
		}
	}).blur().parents('form').submit(function() {
		$(this).find('[placeholder]').each(function() {
			var input = $(this);
			if (input.val() == input.attr('placeholder')) {
				input.val('');
			}
		})
	});
})

/* end Input Placeholders */

/* ---------------------------------------------------- */
/*		Homepage Project Grid
/* ---------------------------------------------------- */

$(function(){
	$('#projects-slider').gridnav({
		rows	: 2,
		type	: {
			mode		: 'sequpdown',
			speed		: 350,
			easing		: 'easeOutCubic',
			factor		: 50,
			reverse		: false,
			timeout		: 3000
		}
	});
});

/* end Homepage Project Grid */

/* ---------------------------------------------------- */
/*		Blog Post Carousel
/* ---------------------------------------------------- */

$(function() {
	$('.carousel').jcarousel({
		animation: 600,
		easing: 'easeOutCubic'
	});
});

/* end Blog Post Carousel */

/* ---------------------------------------------------- */
/*		Navigation Dropdowns
/* ---------------------------------------------------- */


			$(function() {

	$('#nav ul').css('width', 'auto');
	$('#header').after('<div class="subnav-background"></div>');

	$('#nav li').hover(function() {
		$(this).children('ul').hide().stop(true, true).slideDown(200);
	}, function() {
		$(this).children('ul').stop(true, true).fadeOut(0, function() {
			$('.subnav-background').slideUp(200);
			$('#header').removeClass('active');
		});
	});

	$('#nav li').hover(function() {
		if( $(this).children('ul').length > 0 ) {
			var containerWidth = $('.container').width(),
				subWidth = $(this).children('ul').width(),
				pos = $(this).position(),
				left = containerWidth - subWidth - ( pos.left + ( $(this).width() / 2 )),
				margin = ( $(this).children('ul').children('li').size() - 1 ) * 30;
				
			$(this).children('ul').css('right', left+margin);
			$(this).addClass('hover');
			$('.subnav-background').stop(true, true).slideDown(200);
			$('#header').addClass('active');
		}
	}, function() {
		$(this).removeClass('hover');
		
	});
});

/* end Navigation Dropdowns */

/* ---------------------------------------------------- */
/*		Grayscale Image Hover Effect
/* ---------------------------------------------------- */

$(function() {
	
	// clone image
	$('.single_image img, .multi_images img, .blog-posts .post-image').each(function(){
		var el = $(this);
		el.css({"position":"absolute"}).wrap("<div class='img-wrapper' style='display: inline-block;'>").clone().addClass('img-grayscale').css({"position":"absolute","z-index":"998","opacity":"1"}).insertBefore(el).queue(function(){
			var el = $(this);
			el.parent().css({"width":this.width,"height":this.height});
			el.dequeue();
		});
		this.src = grayscale(this.src);
	});
	
	// Fade image 
	$('.single_image img, .multi_images img, .blog-posts .post-image').mouseover(function(){
		$(this).parent().find('img:first').stop().animate({opacity:0.3}, 400);
	})
	$('.img-grayscale').mouseout(function(){
		$(this).stop().animate({opacity:1}, 400);
	});		
	
	// Grayscale w canvas method
	function grayscale(src){
		var canvas = document.createElement('canvas');
		var ctx = canvas.getContext('2d');
		var imgObj = new Image();
		imgObj.src = src;
		canvas.width = imgObj.width;
		canvas.height = imgObj.height; 
		ctx.drawImage(imgObj, 0, 0); 
		var imgPixels = ctx.getImageData(0, 0, canvas.width, canvas.height);
		for(var y = 0; y < imgPixels.height; y++){
			for(var x = 0; x < imgPixels.width; x++){
				var i = (y * 4) * imgPixels.width + x * 4;
				var avg = (imgPixels.data[i] + imgPixels.data[i + 1] + imgPixels.data[i + 2]) / 3;
				imgPixels.data[i] = avg; 
				imgPixels.data[i + 1] = avg; 
				imgPixels.data[i + 2] = avg;
			}
		}
		ctx.putImageData(imgPixels, 0, 0, 0, 0, imgPixels.width, imgPixels.height);
		return canvas.toDataURL();
    }

});

/* end Blog Post Carousel */

/* ---------------------------------------------------- */
/*		Single Work Slider(s)
/* ---------------------------------------------------- */

$(function() {
	$('#single-project .slider')
		.after('<div class="single-project-slider-nav">')
		.each(function(){
			var p = this.parentNode;
			$(this).cycle({
				fx: 'fade',
				pager:  $('.single-project-slider-nav', p),
				pause: true,
				speed: 600
			});    
	});
});

/* end Single Work Slider */

});
