$(document).ready(function() {

	/* autosize textareas */
	try { $('textarea').elastic(); } catch(err) {}
	
	/* Google Maps */
	try { initialize(); } catch(err) {}

	/* interior design projects lightbox overlay */
	$(".project-anchor").fancybox({
		'hideOnContentClick': false,
		'centerOnScroll': true,
		'overlayColor': '#000',
		'overlayOpacity': 0.6,
		'type': 'iframe',
		'scrolling': 'no',
		'padding': 0,
		'margin': 12,
		'width': 699,
		'height': 566
	});

	/* generic lightbox overlay */
	$("a.lightbox").fancybox({
		'hideOnContentClick': true,
		'centerOnScroll': true,
		'overlayColor': '#000',
		'overlayOpacity': 0.6,
		'scrolling': 'no',
		'padding': 0,
		'margin': 12,
		'titleShow': true
	});
	
	/* wee toggle for explanatory messages */
	$('.toggle-uitleg').toggle(function() {
		$(this).parent().next('.uitleg').slideDown();
	}, function() {
		$(this).parent().next('.uitleg').slideUp();
	});
	
	/* wee toggle for specs in the frontend */
	$('a.specs').toggle(function() {
		$(this).next().slideDown();
	}, function() {
		$(this).next().slideUp();
	});

	/* wee toggle for product variants in the backend */
	/*
	$('.toggle-admin-specs').toggle(function() {
		$(this).parent().next().slideDown();
	}, function() {
		$(this).parent().next().slideUp();
	});
	*/

	/* admin messages */
    if ($('#flashMessage')){
		$('#flashMessage').fancybox({
			'centerOnScroll': true,
			'type': 'inline',
			'padding': 0,
			'overlayColor': '#000',
			'overlayOpacity': 0.6,
			'showCloseButton': false,
			'hideOnContentClick': false
		}).trigger('click'); // Need this to show the dialog on page load
		/* FIXME */ // Bug: clicking the lightbox content causes it to trigger as well
		$('#dismiss').click(function() {
			$.fancybox.close();
		});
		$('.closeFlashMessage').click(function() {
			$.fancybox.close();
			window.location = $(this).attr('href');
		});
//		$('#flashMessage').remove();
	}
	
	/*
	 * Small jQuery confirmation box, mostly lifted from the jQuery Plugin jConfirmAction
	 * by Hidayat Sagita
	 * http://www.webstuffshare.com
	 * Licensed Under GPL version 2 license.
	 */
	$('.ask').bind('click', function(e) {
		e.preventDefault();
		thisHref	= $(this).attr('href');

		if($(this).next('.question').length <= 0)
			$(this).after('<div class="question">Zeker weten?<br/> <span class="button yes caution"><span>Ja</span></span><span class="button cancel safe"><span>Annuleer</span></span></div>');
			$(this).next('.question').fadeIn(300);

		$('.yes').bind('click', function(){
			window.location = thisHref;
		});

		$('.cancel').bind('click', function(){
			$(this).parents('question', 'alpha(opacity=99)');
			$(this).parents('.question').fadeOut(300, function() {
				$(this).remove();
			});
		});
	});
	
	/* Zebra stripes */
	$('.admin tr').hover(
	  function () {
	    $(this).addClass('stripe');
	  },
	  function () {
	    $(this).removeClass('stripe');
	  }
	);
	
	$('.admin tr').hover(function(){
		$(this).addClass('.italic');
	});
	$('.admin tr').click(function() {
		window.location = $(this).find('.view').attr('href');
	});
	
});
