// initialise plugins
jQuery(function(){

	// jQuery Watermark Plugin
	jQuery('input[name="s"]').each(function() {
		jQuery(this).Watermark("Enter keywords");
	});
	
	// jQuery autoAlign
	jQuery("#fsb").autoColumn(32, ".widget-container");
	jQuery("#fsb").autoHeight(".widget-container");
	
	jQuery(".columns").autoColumn(33, ".column");
	jQuery(".columns").autoHeight(".column");
	
	jQuery(".columns2").autoColumn(33, ".column");
	jQuery(".columns2").autoHeight(".column");
	
	jQuery(".columns3").autoColumn(33, ".column");
	jQuery(".columns3").autoHeight(".column");
	
	jQuery(".columns4").autoColumn(33, ".column");
	jQuery(".columns4").autoHeight(".column");
	
	jQuery(".columns5").autoColumn(33, ".column");
	jQuery(".columns5").autoHeight(".column");
	
	jQuery(".columns6").autoColumn(33, ".column");
	jQuery(".columns6").autoHeight(".column");
	
	jQuery(".columns7").autoColumn(33, ".column");
	jQuery(".columns7").autoHeight(".column");
	
	jQuery(".columns8").autoColumn(33, ".column");
	jQuery(".columns8").autoHeight(".column");
	
	jQuery(".columns9").autoColumn(33, ".column");
	jQuery(".columns9").autoHeight(".column");
	
	jQuery(".columns10").autoColumn(33, ".column");
	jQuery(".columns10").autoHeight(".column");
	
	jQuery(".columns11").autoColumn(33, ".column");
	jQuery(".columns11").autoHeight(".column");
	
	jQuery(".columns12").autoColumn(33, ".column");
	jQuery(".columns12").autoHeight(".column");
	
	jQuery(".columns13").autoColumn(33, ".column");
	jQuery(".columns13").autoHeight(".column");
	
	jQuery(".columns14").autoColumn(33, ".column");
	jQuery(".columns14").autoHeight(".column");
	
	jQuery(".columns15").autoColumn(33, ".column");
	jQuery(".columns15").autoHeight(".column");
	
	jQuery(".portfolio-grid").autoHeight("li");

	// Jquery Fly Out Search box
	jQuery("#searchtext").hide();
	jQuery("#searchsubmit").click(function(){
		jQuery("#searchtext").animate({width: 'toggle'});
	});
	
	// Call jQuery Uniform for Select
	jQuery("select").uniform();
	
	// Call jQuery Scroll to Top
	if (jQuery(".gototop").length) {
		jQuery.localScroll();
	}

	// jQuery Navigation Sliding
	var offset = jQuery("#menu").offset();
	var topPadding = 50;
	jQuery(window).scroll(function() {
		if (jQuery(window).scrollTop() > offset.top) {
			jQuery("#menu").stop().animate({
				marginTop: jQuery(window).scrollTop() - offset.top + topPadding
			});
		} else {
			jQuery("#menu").stop().animate({
				marginTop: 0
			});
		};
	});

});

// Fade In/Out Flickr Images
	// On window load. This waits until images have loaded which is essential
	jQuery(window).load(function(){
		
		// Fade in images so there isn't a color "pop" document load and then on window load
		jQuery(".flickr img").fadeIn(500);
		
		// clone image
		jQuery('.flickr img').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":"0"}).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 
		jQuery('.flickr img').mouseover(function(){
			$(this).parent().find('img:first').stop().animate({opacity:1}, 1000);
		})
		jQuery('.img_grayscale').mouseout(function(){
			$(this).stop().animate({opacity:0}, 1000);
		});		
	});
	
	// 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();
    }
	
// jquery quicksand
	function reloadPrettyPhoto() {
		jQuery(".pp_pic_holder").remove();
		jQuery(".pp_overlay").remove();
		jQuery(".ppt").remove();
		// edit it with your initialization
		jQuery('a[rel^="prettyPhoto"]').prettyPhoto({
			animationSpeed: 'normal', /* fast/slow/normal */
			opacity: 0.70, /* Value between 0 and 1 */
			showTitle: false, /* true/false */
			allowresize: true, /* true/false */
			counter_separator_label: '/', /* The separator for the gallery counter 1 "of" 2 */
			theme: 'dark_rounded' /* light_rounded / dark_rounded / light_square / dark_square / facebook */
		});
	}
// Custom sorting plugin
jQuery(document).ready(function(jQuery) {

	// bind radiobuttons in the form
	var $filterType = $('#filter a');

	// get the first collection
	var $list = $('#portfolio-sorting');

	// clone applications to get a second collection
	var $data = $list.clone();

	$filterType.click(function(event) {

		if ($(this).attr('rel') == 'all') {
		  var $sortedData = $data.find('li');
		} else {
			var $sortedData = $data.find('.'+ $(this).attr('rel'));
		}

		jQuery('#filter a').removeClass('current_link');
		jQuery(this).addClass('current_link');
			
		$list.quicksand($sortedData, {
		  attribute: 'id',
		  duration: 800,
		  easing: 'easeInOutQuad',
		  adjustHeight: 'auto',
		  useScaling: 'false'
		}, function() {
			reloadPrettyPhoto();
			imagelink();
			videolink();
		});

		return false;
	});

});
