﻿
jQuery(document).ready(function ($) {

	// category page
	(function () {
		var hoverTimer = null,
			timeoutDelay = 300;


		function clearHoverTimer() {
			if (hoverTimer != null) {
				clearTimeout(hoverTimer);
				delete hoverTimer;
			}
		}

		function unselectBook() {
			$('table.category-list tr.selected').removeClass('selected');
			$('#book-details').hide();
		}

		$('table.category-list tbody tr').click(function () {
			window.location.href = $(this).find('span.title a').attr('href');
		});

		$('table.category-list tbody tr').mouseover(function () {
			clearHoverTimer();

			$('table.category-list tr.selected').removeClass('selected');
			var tr = $(this),
				trOff = tr.offset(),
				bookID = tr.data('bookid'),
				isbn10 = tr.data('isbn10'),
				isbn13 = tr.data('isbn13'),
				wtsUrl = tr.data('wtsurl'),
				logosUrl = tr.data('logosurl'),
				averagerating = parseInt(tr.data('averagerating'), 10),
				accordanceUrl = tr.data('accordanceurl');

			if (typeof logosUrl == 'undefined')
				logosUrl = '';
			if (typeof accordanceUrl == 'undefined')
				accordanceUrl = '';
			if (typeof wtsUrl == 'undefined')
				wtsUrl = '';

			tr.addClass('selected');

			$('#book-details')
				.css('top', trOff.top - 80)
				.css('left', trOff.left + tr.width()) // - 180)
				.show();

			$('#book-details-image').attr('src', tr.data('bookthumburl'));
			$('#book-details-text').html(
				tr.find('span.title').html() + ' (' + tr.find('span.year').html() + ')<br />'
				+ tr.find('span.authors').html() + '<br />'

				+ ((averagerating > 0) ?
					+'Rating: <span class="rating-outer-small"><span class="rating-inner" width="' + (averagerating * 70 / 5) + 'px"></span></span><br/>'
					: ''
				)
				+ 'Reviews: ' + tr.data('reviewscount') + '<br />'
				+ 'Libraries: ' + tr.data('librariescount') + '<br />'
				+ 'Tags: ' + tr.find('span.tags').html() + '<br />'
				+ '<br />Stores:'
				+ '<div class="details-link-list">'
					+ '<a class="amazon" href="/link/' + bookID + '/amazon">Amazon.com</a>'
					+ '<a class="christianbook" href="/link/' + bookID + '/cbd">Christian Book</a>'
					+ '<a class="barnesandnoble" href="/link/' + bookID + '/bn">Barnes &amp; Noble</a>'
					+ '<a class="dts" href="/link/' + bookID + '/dts">DTS Book Center</a>'
 					+ (wtsUrl != '' ? '<a class="wts" title="Westminster Bookstore" href="/link/' + bookID + '/wts">Westminster Bookstore</a>' : '')
				+ '</div>'
				+ 'Find:'
				+ '<div class="details-link-list">'
					+ '<a class="google" href="/link/' + bookID + '/google">Google Books</a>'
					+ '<a class="worldcat" href="/link/' + bookID + '/worldcat">World Cat</a>'
				+ '</div>'
				+ ((logosUrl != '' || accordanceUrl != '') ? 'Electronic:'
				+ '<div class="details-link-list">'
					+ (logosUrl != '' ? '<a class="logos" href="' + logosUrl + '" target="_blank">Available in Logos</a>' : '')
					+ (accordanceUrl != '' ? '<a class="accordance" href="' + accordanceUrl + '" target="_blank">Available in Accordance</a>' : '')
				+ '</div>' : '')
			);
		});


		$('#book-details').mouseover(function () {
			clearHoverTimer();
		});
		$('#book-details, table.category-list tbody tr').mouseout(function () {
			clearHoverTimer();
			hoverTimer = setTimeout(unselectBook, timeoutDelay);
		});



		// tags
		$('#tags input[type="checkbox"]').click(function () {

			var categoryRows = $('table.category-list tbody tr'),
				selectedTags = $('#tags input[type="checkbox"]:checked');

			if (selectedTags.length == 0) {

				categoryRows.removeClass('hidden');

			} else {
				categoryRows.addClass('hidden');

				// get selected ids
				selectedTags.each(function () {
					console.log('selected tag: ' + $(this).val());
					categoryRows.filter('.tag-' + $(this).val()).removeClass('hidden');
				});
			}
		});
	})();

});
