$(document).ready(function() {

	// Easing equation, borrowed from jQuery easing plugin
	// http://gsgd.co.uk/sandbox/jquery/easing/
	jQuery.easing.easeOutQuart = function (x, t, b, c, d) {
		return -c * ((t=t/d-1)*t*t*t - 1) + b;
	};

	/* pricing on single view pages */
	function getPrice() {
		var result = '';
		$('.opts input:checked').each(function() {
			result += '.' + $(this).val();
		});
		$('#price > span').stop(true, true).fadeOut('fast');
		$(result).stop(true, true).fadeIn('slow');
	}
	$('input').click(function() {
		getPrice();
	});
	$('.opt').click(function() {
		if ($(this).find('input:radio').attr('checked', true)) { $(this).addClass('dark-gray').siblings('.opt').removeClass('dark-gray'); };
		$(this).find('input:checkbox').attr('checked', !$(this).find('input:checkbox').attr('checked'));
		$.uniform.update();
		//$(this).addClass('dark-gray').siblings('.opt').removeClass('dark-gray');
		getPrice();
	});
	/* calculate price on page load */
	getPrice();
	
	/* row copy */
	$('a.clone').live('click', function() {
		/* variant count, sub 2 since Cake starts at 0 and we have a table head */
		var max = $('#page-edit-form tbody tr').length - 2;
		/* grab the row to copy */
		var row = $(this).closest('tr').clone();
		/* remove the hidden input containing the record id Cake inserts into our form */
		row.find('td input[type="hidden"]').remove();
		row.find('*').filter('[name]').each( function() {
			this.name = this.name.replace(/[0-9]/, function(n) { return max + 1; });
		});
		row.find('*').filter('[id]').each( function() {
			this.id = this.id.replace(/[0-9]/, function(n) { return max + 1; });
		});
		row.hide();
		$(this).closest('tbody').append(row);
		row.fadeIn();
		if ($('#page-edit-form tbody tr').length > 2) {
			$('#page-edit-form tbody .delete').addClass('caution').removeClass('disabled');
		}
	});
	$('a.delete').live('click', function(){
		if ($('#page-edit-form tbody tr').length > 2) {
			$(this).closest('tr').remove();
		}
		if ($('#page-edit-form tbody tr').length == 2) {
			$('#page-edit-form tbody .delete').addClass('disabled').removeClass('caution');
		}
	});

	/* uniform http://uniformjs.com/ */
	$("select, input:checkbox, input:radio, input:file").uniform();

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

	/* interior design projects lightbox overlay */
	$(".project-anchor-x").fancybox({
		'hideOnContentClick': false,
		'centerOnScroll': true,
		'overlayColor': '#000',
		'overlayOpacity': 0.6,
		'type': 'iframe',
		'scrolling': 'no',
		'padding': 0,
		'margin': 12,
		'width': 699,
		'height': 566
	});
	
	$(".project-anchor").fancybox({
		'hideOnContentClick': false,
		'centerOnScroll': true,
		'overlayColor': '#000',
		'overlayOpacity': 0.6,
		'type': 'iframe',
		'scrolling': 'no',
		'padding': 0,
		'margin': 12,
		'width': 703,
		'height': 569
	});
	
	/* image lightbox overlay */
	$("a.lightbox").fancybox({
		'hideOnContentClick': true,
		'centerOnScroll': true,
		'overlayColor': '#000',
		'overlayOpacity': 0.6,
		'scrolling': 'no',
		'padding': 0,
		'margin': 12,
		'titleShow': true
	});
	
	/* formbox overlay */
	$("a.formbox").fancybox({
		'hideOnContentClick': false,
		'centerOnScroll': true,
		'overlayColor': '#000',
		'overlayOpacity': 0.6,
		'scrolling': 'no',
		'padding': 0,
		'margin': 12,
		'titleShow': true
	});
	
	/* cart total */
	if ($('#jq-total').length) {
		var total = parseFloat($('#jq-total').text().replace(',','.'));
		var shipping = parseFloat($('#jq-shipping').text().replace(',','.'));
		$('#CartVerzenden-9').click(function() {
			if($(this).is(':checked')) {
				var result = total - shipping;
				$('#jq-total').text( result.toFixed(2).toString().replace('.',',') );
			}
		});
		$('#CartVerzenden-10').click(function() {
			if($(this).is(':checked')) {
				$('#jq-total').text(total.toFixed(2).toString().replace('.',','));
			}
		});
		$('#CartPayment-4').click(function() {
			if($(this).is(':checked')) {
				var result = total - shipping;
				$('#jq-total').text( result.toFixed(2).toString().replace('.',',') );
				$('#CartVerzenden-9').attr('checked', true);
			}
		});
		$('#CartPayment-5').click(function() {
			if($(this).is(':checked')) {
				$('#jq-total').text(total.toFixed(2).toString().replace('.',','));
				$('#CartVerzenden-10').attr('checked', 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();
		});
		$('#flashMessage div').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').click(function() {
		window.location = $(this).find('.view').attr('href');
	});
	
	/* debug */
	$('#debug-page').hide();
	$('#debug-page-close').click(function(e) {
		$('#debug-page').toggle('fast');
	});

});

