$(document).ready( function (){ 
	
	// -----------------------------
	// Portfolio Category
	// -----------------------------
	
	// Portfolio Sorting Dropdowns
	var whichDropdown = '';
	
	$(".portfolio-sorting-block").hoverIntent(function(){
		whichDropdown = this.id;
		$('.'+whichDropdown).removeClass('temp-inactive');
		$(this).find('.heading').addClass('active').addClass('heading-active');
		$('.'+whichDropdown).slideDown('fast');
	}, function(){
		$('.'+whichDropdown).addClass('temp-inactive');
		$(this).find('.heading').removeClass('active').removeClass('heading-active');
		$('.'+whichDropdown).slideUp('fast');
	});
			
	// References
	var loading = $("#loading");
	var new_content = $('#sorted-area');
	var animationOn = true;
	
	// Show Loading Graphic
	function showLoading(){
		loading.show();
	}
	//hide loading bar
	function hideLoading(){
		loading.fadeOut("fast",function(){
			new_content.fadeIn("normal");
			animationOn = false;
		});
	}
	
	if (dontLoad != 1) {
		showLoading();
		new_content.hide();
		new_content.load("/wp-content/themes/altitude/sorting/"+sortingType+"-sort.php", sortData, hideLoading);
	}
	
	
	
	var currentHeading = '';
	
	// Sort by Client Name
	$('.client-name-sort').click(function(){
				 
		// Remove SELECTED Classes
		if (currentHeading != 'client') { $('.heading').removeClass('heading-selected'); }
		$('.sorting-dropdown > a').removeClass('selected');
		
		var client_name = this.id;
		sortData = "client_name="+client_name+"";
		
		$('.portfolio-title').fadeOut("fast",function(){
			$('.update-area').html("&raquo; Client &raquo; <strong>"+client_name+"</strong>");	
			$('.plain-text').hide();
			$('.featured-sort').show();
			$('.portfolio-title').fadeIn("normal");
		});
		
		if (animationOn == false || dontLoad == 1) {
			
			animationOn = true;
			
			$('#dropdown-client').find('.heading').removeClass('active');
			
			// Add SELECTED Classes
			$('#dropdown-client').find('.heading').addClass('heading-selected');
			currentHeading = 'client';
			$(this).addClass('selected');
			
			$('.dropdown-client').slideUp('fast',function(){
				whichDropdown = '';
			});
			
			// Load selected section				
			showLoading();
			new_content.hide();
			new_content.load("/wp-content/themes/altitude/sorting/client-sort.php", sortData, hideLoading);
			current_view = this.id;
				
		}
	});
	
	// Sort by Project Type
	$('.type-sort').click(function(){
						
		// Remove SELECTED Classes
		if (currentHeading != 'type') { $('.heading').removeClass('heading-selected'); }
		$('.sorting-dropdown > a').removeClass('selected');
		
		var category_name = $(this).attr('rel');
		var cat_data = this.id;
		cat = cat_data.split("-");
		category_id = cat[1];
		
		$('.portfolio-title').fadeOut("fast",function(){
			$('.update-area').html("&raquo; Type &raquo; <strong>"+category_name+"</strong>");		
			$('.plain-text').hide();
			$('.featured-sort').show();
			$('.portfolio-title').fadeIn("normal");
		});
		
		sortData = "cat_id="+category_id+"";				 
		
		if (animationOn == false || dontLoad == 1) {
			
			animationOn = true;
			
			$('#dropdown-type').find('.heading').removeClass('active');
			
			// Add SELECTED Classes
			$('#dropdown-type').find('.heading').addClass('heading-selected');
			currentHeading = 'type';
			$(this).addClass('selected');
			
			$('.dropdown-type').slideUp('fast',function(){
				whichDropdown = '';
			});
			
			// Load selected section				
			showLoading();
			new_content.hide();
			new_content.load("/wp-content/themes/altitude/sorting/type-sort.php", sortData, hideLoading);
			current_view = this.id;
				
		}
	});
	
	// Sort by Project Type
	$('.tag-sort').click(function(){
		
		// Remove SELECTED Classes
		if (currentHeading != 'tag') { $('.heading').removeClass('heading-selected'); }
		$('.sorting-dropdown > a').removeClass('selected');
		
		var tag_name = $(this).attr('rel');
		var tag = this.id;
		sortData = "tag="+tag+"";
		
		$('.portfolio-title').fadeOut("fast",function(){
			$('.update-area').html("&raquo; Industry &raquo; <strong>"+tag_name+"</strong>");	
			$('.plain-text').hide();
			$('.featured-sort').show();
			$('.portfolio-title').fadeIn("normal");
		});
		 
		if (animationOn == false || dontLoad == 1) {
			
			animationOn = true;
			
			$('#dropdown-tag').find('.heading').removeClass('active');
			
			// Add SELECTED Classes
			$('#dropdown-tag').find('.heading').addClass('heading-selected');
			currentHeading = 'tag';
			$(this).addClass('selected');
			
			$('.dropdown-tag').slideUp('fast',function(){
				whichDropdown = '';
			});
			
			// Load selected section				
			showLoading();
			new_content.hide();
			new_content.load("/wp-content/themes/altitude/sorting/tag-sort.php", sortData, hideLoading);
			current_view = this.id;
				
		}
	});
	
	
	// Sort by Featured
	$('.featured-sort').click(function(){
				
		// Remove SELECTED Classes
		$('.heading').removeClass('heading-selected');
		$('.sorting-dropdown > a').removeClass('selected');
		
		$('.portfolio-title').fadeOut("fast",function(){
			$('.update-area').html("&raquo; <strong>Featured</strong>");
			$('.plain-text').show();
			$('.featured-sort').hide();
			$('.portfolio-title').fadeIn("normal");
		});
		
		if (animationOn == false || dontLoad == 1) {
			
			var sortData = '';
			animationOn = true;
			
			// Load selected section				
			showLoading();
			new_content.hide();
			new_content.load("/wp-content/themes/altitude/sorting/featured-sort.php", sortData, hideLoading);
			current_view = '';
				
		}
	});
	
	
	// Portfolio Pagination
	$('.portfolio-pagination').click(function(){
												   
		var client_name = this.id;
		var page = $(this).attr('rel');
		sortData = "client_name="+client_name+"&page="+page+"";
						 
		if (animationOn == false || dontLoad == 1) {
			
			animationOn = true;
			
			// Load selected section				
			showLoading();
			new_content.hide();
			new_content.load("/wp-content/themes/altitude/sorting/client-sort.php", sortData, hideLoading);
				
		}
	});
	
});