$(function() {

	//////////////////////////////////////////////////////////////////
	/////////////////////////// MOBILE??? ////////////////////////////
	//////////////////////////////////////////////////////////////////

	var mobile = false;
	var tiny_screen = false;
	
	if (navigator.userAgent.match(/Android/i) ||
		navigator.userAgent.match(/webOS/i) ||
		navigator.userAgent.match(/iPhone/i) ||
		navigator.userAgent.match(/iPod/i) //||
		//navigator.userAgent.match(/iPad/i)
		){
		mobile = true;
	}	
	

	//////////////////////////////////////////////////////////////////
	//////////////////////////// FANCYBOX ////////////////////////////
	//////////////////////////////////////////////////////////////////
	
	var $project_title = $('#single_post .project_info');
	var $project_video = $('#single_post .video');
	
	// Don't run normal fancybox on Mobile platforms
	if (mobile == false) {
		$('.fancy_link').fancybox({
			'ajax'		:	{
				url : $(this).attr('href'),
				data :	{},
				dataType : 'html',
				dataFilter : function(data) {
					$project_title = $(data).find('#single_post .project_info');
					$project_video = $(data).find('#single_post .video');
					$project_video.find('iframe').attr('src', $project_video.find('iframe').attr('src') + '&autoplay=1');
					return $project_video;
				}
			},
			'padding'	: 7,
			'scrolling'	: 'no',
			'wrapCSS'	: 'fancybox-custom',
			'afterShow'	: function() {
				addInfo();
			}
		});
	} else { // On MOBILE just send user to vimeo video player
		$('.fancy_link').click(function(e) {
			e.preventDefault();
			
			$.ajax({
				url : $(this).attr('href'),
				data :	{},
				dataType : 'html',
				dataFilter : function(data) {
					var vimeo_link = $(data).find('#single_post iframe').attr('src');
					vimeo_link.replace('http://vimeo.com/', 'http://player.vimeo.com/video/');
					return vimeo_link;
				},
				success : function(data) {
					window.location = data;
				}
			});
			
			//window.location = vimeo_link;
			console.log(vimeo_link);
		});
	}
	
	
	// Fancybox for single project page
	$('.single_fancy').fancybox({
		'padding'	: 7,
		'scrolling'	: 'no',
		'wrapCSS'	: 'fancybox-custom',
		'afterShow'	: function() {
			addInfo();
		}
	});
	
	// Trigger Fancybox popup on single page load
	$('.single_fancy').trigger('click');
			
	function addInfo() {
		var project_url = $project_title.find('.project_info_link').text();
		$project_title.find('.project_info_link').contents().wrap('<a href="' + project_url + '"></a>');
		$('.fancybox-wrap').append($project_title);
	}

	//////////////////////////////////////////////////////////////////
	///////////////////////////// SLIDER /////////////////////////////
	//////////////////////////////////////////////////////////////////
	
	$('.featured_project').click(function() {
		var $activated_project = $(this);
		slideSlides($activated_project);
	});
	
	function slideSlides(p) {
		$('.active').animate({width: '150'}, {queue: false}).removeClass('active').find('.overlay').fadeIn();
		p.addClass('active').animate({width: '680'}, {queue: false});
		p.find('.overlay').fadeOut();
	}
	
	// Do same for SWIPE
	$('.featured_project').swipe({
		swipeLeft: function() {
			var $pl = $(this);
			slideSlides($pl);
		},
		swipeRight: function() {
			var $pr = $(this);
			slideSlides($pr);
		},
		threshold: 20,
		triggerOnEnd: false,
		allowPageScrol: 'vertical'
	});
	
	$('.featured_project').hover(function() {
		if ($(this).hasClass('active')) {
			return false;
		}
		$(this).find('.overlay').animate({opacity: 0}, 250);
	}, function() {
		$(this).find('.overlay').animate({opacity: 1}, 500);
	});


	//////////////////////////////////////////////////////////////////
	///////////////////////// WORK CATEGORIES ////////////////////////
	//////////////////////////////////////////////////////////////////
	
	var current_category = 'all';
	
	// Initialize displayed posts in case of a category page, only call if not main work page
	if ($('#project_filter').hasClass('category_page')) {
		if (!($('#reset').find('li').hasClass('active_category'))) {
			initProjects();
		}
	}
	
	// Reset displayed posts if "ALL"
	$('#reset').find('li').click(function() {
		if ($(this).hasClass('active_category')) {
			return false;
		}
		else {
			$('.active_category').removeClass('active_category');
			current_category = $(this).attr('class');
			$(this).addClass('active_category');
			$('.project').fadeIn(800);
		}
	});
	
	$('#categories').find('li').click(function() {
		if ($(this).hasClass('active_category')) {
			return false;
		}
		else {
			$('.active_category').removeClass('active_category');
			current_category = $(this).attr('class');
			$(this).addClass('active_category');
			sortProjects(current_category);
		}
	});
		
	function sortProjects(cat) {
		var fixed_class = '.' + cat;
		$('.project').hide();
		$(fixed_class).fadeIn(800);	
	}
	
	function initProjects() {
		var $init_class = $('#categories').find('.active_category');
		current_category = $init_class.attr('class').replace('active_category', '').replace(' ', '');
		var fixed_class = '.' + current_category;
		$('.project').hide();
		$(fixed_class).show();
	}





});
