$(document).ready(function () {
	contactSubject();
	if($('body').width() > 320) {
		$('.piano-list').paginate(4);
	}
	if($('body').width() < 321) {
		smartPhone();
	}
	if($('#contact-us').length) {
		$('#contact-us').validate();
	}
});

function smartPhone() {
	
	$('#grand-pianos-title').remove();
	$('#upright-pianos-title h1').html('Upright &amp; Grand Pianos');
	
}

function contactSubject() {

	$('#contact-us').append('<input id="contact-subject" name="subject" value="'+$('#contact-recipient option:selected').text()+'" />');
	$('#contact-recipient').change(function() {
		$('#contact-subject').val($('#contact-recipient option:selected').text());
	});

}

// Plugins
$.fn.paginate = function (perPage) {
	// Funcs
	var getNext = function (num, total) { return (num < total) ? num + 1 : 1; };
	var getPrev = function (num, total) { return (num > 1) ? num - 1 : total; };

	this.each(function (index) {
		var that = this;
		var currentPage = 1;
		var items = $(this).find('ul li');
		var list = $(this).find('ul');
		var pgWidth = $(this).css({ overflow: 'hidden' }).width();
		
		if(items.length > perPage) {
		
			// Create pages
			list.wrap($('<div/>').css({ position: 'absolute', width: pgWidth * 2 + 'px' }));
			var container = list.parent('div');
			var shiftAndWrap = [];
			for (var slice = 1; (slice * perPage) < items.length; slice++)
				shiftAndWrap.push(items.slice(slice * perPage, (slice * perPage) + (perPage)));
			
			var totalPages = shiftAndWrap.length + 1;
			$.each(shiftAndWrap, function (index, value) {
				list.parent('div').append($('<ul/>').css({ position: 'absolute', float: 'left' }).append(value));
			});
			
			// Define function for changing page
			var changePage = function () {
				var destination = $(this).data('page');
				var existingSet = $(that).find('ul:nth-child(' + currentPage + ')');
				var newSet = $(that).find('ul:nth-child(' + destination + ')');
				var leftPos = existingSet.outerWidth(true);
				if ($(this).hasClass('next')) {
					newSet.css({ position: 'absolute', left: leftPos + 'px', top: '0px' }).show();
					newSet.animate({ left: '0px' }, { duration: 'slow' });
					existingSet.animate({ left: '-=' + leftPos }, { duration: 'slow', complete: function () { existingSet.hide(); } });
				} else {
					newSet.css({ position: 'absolute', left: -(leftPos) + 'px', top: '0px' }).show();
					newSet.animate({ left: '0px' }, { duration: 'slow' });
					existingSet.animate({ left: '+=' + leftPos }, { duration: 'slow', complete: function () { existingSet.hide(); } });
				}
				currentPage = destination;
				$(that).find('.prev').data('page', getPrev(currentPage, totalPages));
				$(that).find('.next').data('page', getNext(currentPage, totalPages));
			};
			
			// Create Next/Previous buttons
			$(this)
				.append('<nav class="pianos-nav" />');
			$(this).find('nav.pianos-nav')
				.append($('<a class="prev button" href="#" data-page="' + totalPages + '"><img src="http://www.cambridgemusic.net/assets/images/site/icon-arrow-left.png" /></a>').click(function (e) { changePage.apply(this); e.preventDefault(); }))
				.append($('<a class="next button" href="#" data-page="2"><img src="http://www.cambridgemusic.net/assets/images/site/icon-arrow-right.png" /></a>').click(function (e) { changePage.apply(this); e.preventDefault(); }))
			
			// Hide overflow items
			container.css({ position: 'relative', overflow: 'hidden' });
			$(this).find('ul').slice(1).hide()
			container.height($(this).height() - 30);
			$(this).find('ul').css({ position: 'absolute' });
			list.show();
		
		}
	});
};
