/**
 * Base class for Joomla! Module 'Onze catalogus', extended by specialised JS objects that may or may not be included
 * 
 * Namespace "Catalogus"
 * @class Catalogus
 * @desc Catalogus class of this project
 */
var BaseCatalogus = (function($) {
	var Base = function() {
		// VARIABLES DEFINED HERE ARE PRIVATE
		var arr_init = []; // Array containing all registered init-functions of subclasses
		
		// FUNCTIONS DEFINED HERE ARE PRIVATE
		
		/**
		 * Check and execute registered subclass functions
		 */
		function check_register()
		{
			for (var i = 0; i < arr_init.length; i++)
			{
				arr_init[i]();
			}
		}

		return {
			// FUNCTIONS DEFINED HERE ARE PUBLIC
			
			/**
			* Register (initialization) calls from subclasses
			* @param (obj_function) Function to initialize subclass
			*/
			register: function(obj_function) {
				arr_init.push(obj_function);
			},

			/**
			* Initialize this class
			*/
			init: function() {
				check_register();
			}
		};
	} ();
	
	$(document).ready(function() {
		Base.init();
	});
	
	return Base;
	
}) (jQuery);

/**
 * Namespace "Common"
 * @class Common
 * @desc Common of Base
 */

BaseCatalogus.Common = (function($) {
	Common = function() {
		// Variables & functions defined here are private
	
		/**
		 * Triggers checkboxes on click
		 */
		function searchFilterType(li)
		{
			var li = $(li);
			if (li.hasClass('checked'))
			{
				li.children('input').attr('checked', false);
				li.removeClass('checked');
			}
			else
			{
				li.children('input').attr('checked', true);
				li.addClass('checked');
			}
		}

		/**
		 * Toggles the item infobox when clicking cover
		 * 
		 * DEV: The href on a.buttons is set by this function. Make sure the 
		 * attributes are named correctly.
		 * 
		 * @param {Object} src Div containing data to populate infobox
		 */
		function toggleCoverBox(src)
		{
			var box = $('#search-cover-tooltip');

			if (!src)
			{
				// No src, close box
//BaseTemplate.Common.logToConsole("No SRC!");
				box.hide();
				Common.coverBoxOpen = '';
				return;
			}

			var type = src.attr('title'), 
				author = src.children('span.ci-author').text(),
				authorFirst = src.children('span.ci-author-first').text(),
				authorLast = src.children('span.ci-author-last').text(),
				isbn = src.children('span.ci-isbn').text(),
				bnr = src.children('span.ci-bnr').text(),
				ppn = src.children('span.ci-ppn').text(),
				t = src.children('span.ci-title'),
				id = t.attr('title'),
				useZoekEnBoek = Boolean(t.attr('useZenB')),
				zenbExtraParams = t.attr('zenbExtraParams'),
				title = t.text(), 
				urls = src.children('span.ci-urls'),
				leesMeerURL = urls.attr('leesMeerURL'),
				reserveerURL = urls.attr('reserveerURL'),
				zoekEnBoekURL = urls.attr('zoekEnBoekURL'),
				aquaBrowserURL = urls.attr('aquaBrowserURL'),
				availability_txt = src.hasClass('in') ? Common.available_txt : Common.lend_txt;

			if (Common.coverBoxOpen)
			{
				if (Common.coverBoxOpen == id)
				{
					// If the same cover was clicked, close and exit
//					toggleCoverBox();
					return;
				}
				box.hide();
			}
			Common.coverBoxOpen = id;

			// Insert data
			$('.item-type', box).removeClass().addClass('item-type ' + type);
//			$('.left span', box).removeClass().addClass(src.attr('class')).text(availability_txt);
			$('h3', box).text(title);
			$('.right > p > span', box).text(author);
			if (id != "")
			{
				$('a.dark-large', box).attr('href', leesMeerURL);
				$('a.plain', box).attr('href', reserveerURL).removeAttr("target");

				$('.dark-large', box).show();
				$('.plain', box).show();
			}
			else if (useZoekEnBoek)
			{
				$('a.plain', box).attr('href', zoekEnBoekURL +
					zenbExtraParams +
					'&isbn=' + isbn +
					'&bnr=' + bnr +
					'&ppn=' + ppn +
					'&title=' + title +
					'&aufirst=' + authorFirst +
					'&aulast=' + (authorLast != "" ? authorLast : author)).attr("target", "_blank");

				$('.dark-large', box).hide();
				$('.plain', box).show();
			}
			else
			{
				$('a.dark-large', box).attr('href', aquaBrowserURL);

				$('.dark-large', box).show();
				$('.plain', box).hide();
			}

			// Positioning
			var base_offset = src.closest('div.widget-body').position();
			var li_offset   = src.closest('li').position(); // offset to .widget-body
			var img_offset  = { left:-64, top:110 } // box will be placed with this offset to image (or actually the li)

			box.css({
				top : base_offset.top + li_offset.top + img_offset.top,
				left: base_offset.left + li_offset.left + img_offset.left
			}).show();
		} // END toggleCoverBox()

		function processTabClick(src)
		{
			// Remove the 'checked' attribute from all input belonging to the tabs
			$(src).parent('ul').children('input').removeAttr('checked');
			$(src).parent('ul').children('li').removeClass('active');
			// Set the 'checked' attribute on the one that was clicked
			$(src).children('input').eq(0).attr('checked', 'checked');
			$(src).addClass('active');
			//$(src).closest('form').get(0).submit();//dit niet meer doen, we gebruiken ajax.
			getCatalogusSearchResults();
		}

/*
		function replaceEmptyImage(image)
		{
// This causes the 'width' to be set to 1, making the 'No cover' invisible
//			var curWidth = image.width;
//			$(image).css("width", "auto");

			if (image.naturalWidth &&
				image.naturalWidth <= 1 ||
				image.naturalWidth <= 1 ||
				image.src.length < 12 ||
				image.width == undefined ||
				image.height == undefined ||
				(image.width <= 1 &&
				image.height <= 1))
			{
				// Load a 'no cover supplied' image
				image.src = "static/images/icons/noCover.png";
			}
//			$(image).css("width", curWidth);
		}
*/

		function installCoverTooltips()
		{
			$('#search-covers a').hover(
				function() {
					if (Common.timerID != -1)
					{
						clearTimeout(Common.timerID);
						Common.timerID = -1;
					}
					toggleCoverBox($(this).next('div'));
				},
				function() {
					//	if ($("#search-cover-tooltip.hovering").length == 0)
					//	{
					if (Common.timerID == -1)
					{
						Common.timerID = setTimeout(toggleCoverBox, 10);
					}
					//	}
				}
			);
						
			$('#search-cover-tooltip').hover(
				 function() {
					if (Common.timerID != -1)
					{
						clearTimeout(Common.timerID);
						Common.timerID = -1;
					}
				 //console.log("Begin hover over tooltip");
					$(this).addClass("hovering");
				 },
				 function() {
				 //console.log("End hover over tooltip");
					$(this).removeClass("hovering");
					if (Common.timerID == -1)
					{
						Common.timerID = setTimeout(toggleCoverBox, 10);
					}
				}
			);
		}
					
		
		return {
			available_txt: 'Beschikbaar',
			lend_txt: 'Uitgeleend',
			timerID: -1,

/*
			replaceEmptyImageWithPlaceholder: function(image) {
				replaceEmptyImage(image);
			},
*/
			installCoverTooltips: function() {
				installCoverTooltips();
			},
			// Initialize this Class
			init: function() {
				// Search only
				$('#search-filter-type li').bind('click', function() {
					searchFilterType(this);
				});
				$('#search-filter-type li').bind('mousedown', function() {
					$(this).addClass('down');
				});
				// Should add 'mouseout' here (i.e. replace by 'mouseup, mouseout'), but turns out that makes it unreliable
				$('#search-filter-type li').bind('mouseup', function() {
					$(this).removeClass('down');
				});
				$('#search-display-type li').bind('click', function() {
					processTabClick(this);
					return false;// return false anders wordt Ajax 2x aangeroepen
				});
				// Search widget - toggle cover info
/*				if($('#search-covers')[0]) {
					$('#search-covers a').bind('click.coverBox', function() {
						toggleCoverBox($(this).next('div'));
						return false;
					});
					$('#search-cover-tooltip .close').click(function() {
						toggleCoverBox();
					});
				}*/
				if ($('#search-covers')[0])
				{
					$(".removeToolTip .pager").mouseover(
						function() {
//console.log("Mouse over...");
							toggleCoverBox(NULL);
						}
					);
					Common.installCoverTooltips();
/*					$('#search-cover-tooltip .close').click(function() {
						toggleCoverBox();
					});*/
				}
			}
		};
	} ();

	// Register the initializer for this class
	BaseCatalogus.register(Common.init);

	return Common;

}) (jQuery);
