/**
 * @section		: Unobtrusive JavaScript events and function calls, triggered on DOMContentLoaded.
 * @project		: XPUB product
 * @author		: Joris van Summeren <joris@e-sites.nl>
 * @since		: 28-04-2011
 */

var win = win || window,
	doc = doc || win.document;

jQuery(doc).ready(function ($) {

	/* Opens a new window for anchors with rel=external */
//	$('a[rel=external]').bind('click', setExtLinks);

	/* Init placeholder plugin */
	$('input, textarea').placeholder();

	/* Expand explorer tree */
	$('.collapsed, .expanded', '.tree').bind('click', function(e) {
		var self = $(e.currentTarget),
			correspondingList = self.parent().next('ul');

		e.preventDefault();

		if (self.hasClass('collapsed')) {
			self.removeClass('collapsed').addClass('expanded');
			correspondingList.show();
		} else {
			self.removeClass('expanded').addClass('collapsed');
			correspondingList.hide();
		}
	});

	/* Execute JS functions set as value for select options (Beeldbank) */
	$('.evalOnChange', '#filesOptionsFrm').bind('change keyup', function () {
		eval(this.value);
	});

	/* Attach hover functionality at publication preview page */
	$('.buttonsPageGroup, .pageGroup').bind({
		mouseover: function () {
			previewShowMouseOver( $(this).data('id') );
		},
		mouseout: function () {
			previewShowMouseOut( $(this).data('id') );
		}
	});

	/* Toggle divs at publication preview page */
	$('.buttonsPageGroup .toggle').bind('click', function (e) {
		var self = $(e.currentTarget),
			parent = self.parent(),
			children = parent.next(),
			id;

		e.preventDefault();

		if (parent.hasClass('open')) {
			id = self.data('id');
			xajax_veranderGroepnaam('groupTitel_' + id, readTextareaValue('textarea' + self.data('masterid'))); hide('pagegroup_' + id);
			children.hide();
			parent.removeClass('open');
			self.text('Openen');
		} else {
			children.show();
			parent.addClass('open');
			self.text('Sluiten');
		}
	});

	/* Prevent navigating away when busy with a publication */
	if (doc.getElementById('publicationPreviewWindow')) {
		var localPublicationChanges = false;

		$('input, select, textarea', '#frmPublicatie').bind('change keyup', function () {
			localPublicationChanges = true;
		});
		$('.arrow', '.pageGroup').bind('click', function () {
			localPublicationChanges = true;
		});

		$('input, a', '.buttonTab').bind('click', function () {
			localPublicationChanges = false;
		});

		win.onbeforeunload = function (e) {
			if (localPublicationChanges) {
				var e = e || window.event;

				//e.cancelBubble is supported by IE - this will kill the bubbling process.
				e.cancelBubble = true;
				e.returnValue = 'Er zijn momenteel niet opgeslagen wijzigingen.'; //This is displayed on the dialog

				//e.stopPropagation works in Firefox.
				if (e.stopPropagation) {
					e.stopPropagation();
					e.preventDefault();
				}
			}
		};
	}

	/* Prevent bubbling up, so the default onclick handler with window.location is not triggered */
	$('.knoppen a').bind('click', function (e) {
		if (e) e.stopPropagation();
	});

});
