﻿(function ($) {
	$.fn.scrollTo = function (hash) {
		$('html, body').animate({ scrollTop: $(this).position().top }, 500);
		if (hash != null)
			window.location.hash = hash;
	};
	$.fn.song = function (options) {
		var song = $(this);
		var defaults = { fontSize: 100, bold: false, center: false, sectionLabel: true, chords: true };

		var fontLarger = function () {
			if (options.fontSize < 180) {
				options.fontSize = Math.round(options.fontSize * 1.1);
				$(song).animate({ fontSize: options.fontSize + '%' }, 200);
			}
			saveCookie();
			return false;
		};
		var fontSmaller = function () {
			if (options.fontSize > 70) {
				options.fontSize = Math.round(options.fontSize * 0.9);
				$(song).animate({ fontSize: options.fontSize + '%' }, 200);
			}
			saveCookie();
			return false;
		};
		var boldToggle = function () {
			options.bold = !options.bold;
			bold(options.bold);
			saveCookie();
			return false;
		};
		var bold = function (bold) {
			$(song).css('fontWeight', bold ? 'bold' : '');
			$('.textLine pre, .chordLine pre').css('fontWeight', bold ? 'bold' : '');
			$('#bold').text(bold ? 'Unbold' : 'Bold');
		};
		var centerToggle = function (center) {
			$(song).css('textAlign', center ? 'left' : 'center');
			saveCookie();
			return false;
		};
		var sectionLabelToggle = function () {
			options.sectionLabel = ($('.sectionLbl').css('display') == 'none');
			$(this).text((options.sectionLabel ? 'Hide' : 'Show') + ' Section Labels');
			$('.sectionLbl').slideToggle(200);
			saveCookie();
			return false;
		};
		var chordsToggle = function () {
			options.chords = ($('.chordLine').css('display') == 'none');
			$(this).text((options.chords ? 'Hide' : 'Show') + ' Chords');
			$('.chordLine, #metadata').slideToggle(300);
			saveCookie();
			return false;
		};
		var resetOptions = function () {
			options = {};
			$.extend(options, defaults);
			$(song).animate({ fontSize: '100%' }, 300);
			bold(false);
			$(song).css('textAlign', 'left');
			$('.sectionLbl').slideDown(300);
			$('.lines, .chordLine, #songKey').slideDown(300);
			saveCookie();
			return false;
		};
		var redraw = function () {
			$(song).css('fontSize', options.fontSize + '%');
			bold(options.bold);
			$(song).css('textAlign', options.center ? 'center' : 'left');
			if (options.sectionLabel)
				$('.sectionLbl').show();
			else
				$('.sectionLbl').hide();

			if (options.chords)
				$('.lines, .chordLine, #songKey').show();
			else
				$('.lines, .chordLine, #songKey').hide();
		};
		var saveCookie = function () {
			$.cookie('pwarchive_song', JSON.stringify(options), { expires: 1000 });
		};
		var retrieveCookie = function () {
			var optionsCookie = {};
			$.extend(optionsCookie, defaults);
			$.extend(optionsCookie, JSON.parse($.cookie('pwarchive_song')));
			return optionsCookie;
		};

		var options = retrieveCookie();
		redraw();

		// hook up events
		$('#fontLarger').click(fontLarger);
		$('#fontSmaller').click(fontSmaller);
		$('#bold').click(boldToggle);
		$('#sectionLabels').click(sectionLabelToggle);
		$('#chords').click(chordsToggle);
		$('#reset').click(resetOptions);
		return this;
	};
})(jQuery);

// document.ready
$(document).ready(function () {
	var blockEnterKey = false;
	$('#search').keyup(function (e) {
		if (e.keyCode == 13 && !blockEnterKey) {
			document.location = '/search?q=' + encodeURI($('#search').val());
		}
	});
	$('#search').autocomplete({
		source: '/api/search',
		minLength: 3,
		position: { my: 'center top', at: 'center bottom', collision: 'none' },
		select: function (event, ui) {
			blockEnterKey = true;
			window.location = ui.item.id;
		}
	});
});
