(function ($) {
	$(document).ready(function(){

		// PROJECT SORT
		$('a.submit','.sort').live('click', function () {
			var data = $('form').serialize(); 
			$.ajax({
				url: '/beta/project-sort',
				type: 'GET',
				beforeSend: function () {
					$('.loading-content').show();
					$('.sorted-content').children(':not(.loading-content)').remove();
				},
				postType: 'JSON',
				data: data,
				success: function (data) {
					$('.sorted-content').html(data);
				}
			});
		});

		// FILTER BAR
		(function filterBar () {
			$('a.filter-tab', '#filter-search').live('click', function (event) {
				event.preventDefault();
				$('.filter-container', $(this).parent()).slideToggle('fast');
			});
		}());

		// SEARCH BAR

		(function searchHint () {
			$('input.search', '#filter-search').live('focus', function (event) {
				event.preventDefault();

				var label = $(this).val();
				$(this).val('');
	
				$(this).blur(function () {
					if($(this).val() === '') {
						$(this).val(label);
					}
				});
			});
		}());

		//CAPTION SLIDER
		(function captionSlider () {
			$('.slider-pane').hover(function(){

				$(".slider-caption", this).stop().animate({top:'100px'},{queue:false,duration:350,easing: 'easeOutBack'});
			}, function() {
				$(".slider-caption", this).stop().animate({top:'200px'},{queue:false,duration:200});

			});
		}());

		//GALLERY SLIDER
		(function gallerySlider () {

			var slideWidth = 321;

			$('.viewfinder').css('overflow','hidden');

			$('.slider').each(function () {
				var galleryID = $(this).parents('.gallery').attr('id');
				var currentPosition = 0;
				var slider = $('#' + galleryID + ' .slider');
				var slides = $(this).children('.slider-pane');
				var numberOfSlides = slides.length;
				var controls = $('#' + galleryID).children('.control');

				$(controls).click(function () {
					currentPosition = ($(this).hasClass('next-button')) ? currentPosition + 1 : currentPosition -1;
					if ( currentPosition >= 0 && currentPosition < 12) {
						$(slider).animate({
							'marginLeft' : slideWidth * (-currentPosition)
						}, {duration: 500, easing: 'easeOutBack'});
					} else if ( currentPosition === 12 )  {
						$(slider).animate({
							'marginLeft' : 0
						}, 1000);
						currentPosition = 0;
					} else {
						currentPosition = 0;
					}
				});
			});
		}());

		//FEATURE GALLERY CYCLE
		$('#featured-gallery').cycle({
			fx: 'fade',
			timeout: 4000,
			speed: 1000,
			speedIn: 1000,
			speedOut: 1500,
			pager: '.feature-nav',
			activePagerClass: 'current',
			after: function(currSlideElement, nextSlideElement, options, forwardFlag) {
				var link = $(nextSlideElement).find('a').attr('href');
				$('#view-project-link').attr('href', link);
			},
			pagerAnchorBuilder: function(idx, slide) {
				var link = $('.slide:visible h1 a').attr('href');
				$('#view-project-link').attr('href', link);
	            return '.feature-nav li:eq(' + (idx) + ') a';
			}
		});
		
		$('.feature-nav li a', '#featured-gallery').live('click', function (event) {
			event.preventDefault();
		});

		//VERTICAL SCROLL
	//	$('.news-content').mousemove(function(e) {
	//		var s_top = parseInt($('#news-menu').offset().top);
	//
	//		var s_bottom = parseInt($('.news-content').height() + s_top);
	//
	//		var mheight = parseInt($('#news-menu li').height() * $('#news-menu li').length);

	//		var top_value = Math.round(( (s_top - e.pageY) /100 * mheight / 2));

	//		$('#news-menu').animate({top: top_value}, { queue: false, duration: 500});
	//	}); 


	});


}(jQuery));

