jQuery(window).bind("load", function() {
	jQuery('.preload-image:hidden').fadeIn(800 , function(){ 
		jQuery('li:not(.portfolio-protected) a.portfolio-columns').css('background','none');
	});
	jQuery('.thumbnails-shortcode a.portfolio-columns').css('background','none');
	
	jQuery('.portfolio-ajax').css('opacity','1');
	jQuery('.portfolio-columns').css('opacity','1');
	jQuery('ul.portfolio-four li,ul.portfolio-three li,ul.portfolio-two li,ul.portfolio-one li').css('background','none');
});
jQuery(function($){
	//Detect Orientaiton change
	window.onload = orientationchange;
	window.onorientationchange = orientationchange;
	jQuery(window).bind("resize", orientationchange);
	function orientationchange() {
		jQuery('ul.portfolio-list').css('height','auto');
	}
	
	function QuickSandInit() {
		// Clone portfolio items to get a second collection for Quicksand plugin
		var $portfolioClone = jQuery(".portfolio-list").clone();
		
		// Display hovered item text
		jQuery(".portfolio-filter a").hover(
			function(e){
				
			Pfilterblock_height = jQuery('.portfolio-filter-wrap').height();
			jQuery('ul.portfolio-list').css('height',Pfilterblock_height + "px");
				
			var HoverFilterText = jQuery(this).text();
			var HoverFilterClass = jQuery(this).closest('li').attr("class");
			var HoverItemNumber = jQuery(this).data("title");
			
			var DisplayCurrentTitle=jQuery(".filter-heading-description").text();
			
			jQuery('#filter-heading').stop().animate({"opacity":"0.4","letter-spacing":"4px"}, 300);
			jQuery('#filter-heading').text(HoverFilterText);
			
			jQuery('.filter-heading-description').text(HoverItemNumber);

			},
			function(e){
			jQuery('#filter-heading').stop().animate({"opacity":"1","letter-spacing":"0"}, 300);
			var CurrentFilterText = jQuery(".portfolio-filter li.current a").text();
			var CurrentFilterItems = jQuery(".portfolio-filter li.current a").data("title");
			
			jQuery('#filter-heading').text(CurrentFilterText);
			jQuery('.filter-heading-description').text(CurrentFilterItems);
		});

		// Attempt to call Quicksand on every click event handler
		jQuery(".portfolio-filter a").click(function(e){
			// Change filtered text upon click. This is displayed on top of the filterable choices.
			var SelectedFilterText = jQuery(this).text();
			jQuery('#filter-heading').text(SelectedFilterText);
		
			// Set index to zero and disable prev
			jQuery('.ajax-gallery-navigation').attr('id', '-1');
			jQuery('.ajax-prev').css('cursor','default');
		
			jQuery(".portfolio-filter li").removeClass("current");	
		
			// Get the class attribute value of the clicked link
			var $filterClass = jQuery(this).parent().attr("class");

		
			if ( $filterClass == "all" ) {
				var $filteredPortfolio = $portfolioClone.find("li.portfolio-filterables");
			} else {
				var $filteredPortfolio = $portfolioClone.find("li[data-type~=" + $filterClass + "]");
			}

			// Call quicksand
			jQuery(".portfolio-list").quicksand( $filteredPortfolio, { 
				duration: 800,
				adjustHeight: 'dynamic',
          	    easing: 'easeInOutQuint',
				enhancement: function() {
					//get stored value to highlight
					jQuery('a.portfolio-columns').css('opacity','1');
					jQuery('.preload-image:hidden').show();
					PrettyPhotoLightbox();
				}
			}, function(){
				HoverEffect();
				jQuery(".flexslider-container-page").css("background","none");
				jQuery('.flexslider').flexslider({
					animation: "fade",
					slideshow: true,
					pauseOnAction: true,
					pauseOnHover: true,
					controlsContainer: "flexslider-container-page"
				});
			});
	

			jQuery(this).parent().addClass("current");

			// Prevent the browser jump to the link anchor
			e.preventDefault();
		});
	}
	
	
	
	function PrettyPhotoLightbox() {
		jQuery("a[rel^='prettyPhoto']").prettyPhoto({
			opacity: 0.9,
			theme: 'pp_default', /* light_rounded / dark_rounded / light_square / dark_square / facebook */
			default_width: 700,
			default_height: 444,
			overlay_gallery: false,
			show_title: false,
			social_tools: false
		});
	}
	
	function HoverEffect() {
		jQuery("a.portfolio-image-link").hover(
		function () {
			portfolio_height=jQuery(this).height();
			portfolio_width=jQuery(this).width();
			jQuery(this).find("span.column-portfolio-icon")
			.css({"top": "0", "left":"0", "height": ""+portfolio_height+"px", "width": ""+portfolio_width+"px"})
			.stop().animate({
				"opacity" : "1",
				}, "normal");
			jQuery(this).find("span.ajax-image-title").stop().animate({
				"bottom": "50px",
				"opacity" : "1"
				}, "slow");
		},
		function () {
			jQuery(this).find("span.column-portfolio-icon").stop().animate({
				"opacity" : "0"
				}, "fast");
			jQuery(this).find("span.ajax-image-title").stop().animate({
				"bottom": "50px",
				"opacity" : "0"
				}, "fast");
		});
	}
	
	QuickSandInit();
	HoverEffect();
	PrettyPhotoLightbox();
	
});