/**
 * GLOBAL JAVASCRIPT FUNCTIONS
 * DIGITRONIX
 *
 * @author Neal Hulme <nealhulme@gmail.com>
 * @version 1.0
 */

$(document).ready(function() {
	initEndlessScroll();
	initLandingPage();
	initProjects();
	initUniform();
	// initUpdateMe();
});

function initEndlessScroll () {
	if ($('body').hasClass('home')) {
		$(document).endlessScroll({
			callback: function(i) {
				$.post('/projects', {offset: i + 2}, function(data, textStatus, xhr) {
					if (data != 'No results') {
						$(data).insertAfter($('.project:last'));
						setupProject($('.project:last'));
					};
				});
			}
		});
	};
}

function initLandingPage () {
	if ($('body').hasClass('landing-page')) {
		$('.landing-page #intro a').removeClass('default');
		$('#agency').css({
		  width: 370,
		  height: 286
		});
		$('#print').mouseenter(function() {
			$('#print').stop().animate({width: 370, height: 286}, 200);
			$('#agency').stop().animate({width: 296, height: 229}, 200);
		});
		$('#agency').mouseenter(function() {
			$('#agency').stop().animate({width: 370, height: 286}, 200);
			$('#print').stop().animate({width: 296, height: 229}, 200);
		});
	};
}

function initProjects () {
	$('.project').each(function(index) {
		setupProject($(this));
	});
}

function initUniform () {
	$('input:text, input:checkbox, input:file, textarea, select, button').uniform();
}

function initUpdateMe () {
	$('#update-me').click(function(event) {
		$(this).animate({right: 0}, 100, 'linear');
		$('#update-me img')
			.show()
			.animate({opacity: 1}, 100);
			event.stopPropagation();
	})
	$('#update-me img').click(function(event) {
		$('#update-me').animate({right: '-385px'}, 100, 'linear');
		$(this)
			.hide()
			.css('opacity', 0)
		event.stopPropagation();
	});
	
	$('#update-me').validate();
}

function setupProject ($project) {
	var prefix = 'project-' + $project.index('.project');
	if (!$project.hasClass('setup')) {
		imageTitle = $project.find('.project-gallery img:first').attr('alt');
		$project
			.addClass('setup')
			.append('<nav id="' + prefix + '-pager" class="pager" />')
			.append('<p class="title">' + imageTitle + '</p>')
			.append('<div class="next-previous"><a href="" class="previous">Previous</a><a href="" class="next">Next</a></div>')
			.find('.title')
			.click(function() {
				$(this).animate({left: 912}, 500);
			})
			.end()
			.append('<a href="" class="info">i</a>')
			.find('.info')
			.click(function() {
				$title = $(this).parent().find('.title');
				w = $title.outerWidth();
				$title.animate({left: 912 - w}, 500);
				return false;
			})
			.end()
			.find('header')
			.css('cursor', 'pointer')
			.click(function(){
				$(this)
					.toggleClass('close')
					.parent()
					.find('.project-details')
					.slideToggle();
			})
			.end()
			.find('.project-gallery')
			.cycle({
				activePagerClass: 'current',
				before: function(currSlideElement, nextSlideElement, options, forwardFlag) {
					$project
						.find('.title')
						.animate({left: 912}, 500, function(){
							$project
								.find('.title')
								.html($(nextSlideElement).attr('alt'));
						});
				},
				cleartypeNoBg: true,
				delay: 0,
				fx: 'scrollHorz',
				next: $project.find('.next'),
				pager: '#' + prefix + '-pager',
				pause: true,
				prev: $project.find('.previous'),
				speed: 1200,
				timeout: 8000
			})
			.cycle('pause');
	};
}
