$( function( ) {

	/**
	  * jQuery WYSIWYG editor
	  */
	$('textarea.wysiwyg').wysiwyg({ css: '/css/wysiwyg.css' });

	/**
	  * Flashmessages
	  */
	$('.message').hide().append('<span class="close" title="Dismiss"></span>').fadeIn('slow');
	$('.message .close').hover( function() { $(this).addClass('hover'); }, function() { $(this).removeClass('hover'); } );
	$('.message .close').click(function() { $(this).parent().fadeOut('slow', function() { $(this).remove(); }); });

	/**
	  * Tabs
	  */
	$('#tabs').tabs();

	/**
	  * Datepicker
	  */
	$.extend(DateInput.DEFAULT_OPTS, {
		stringToDate: function(string) {
			var matches;
			if (matches = string.match(/^(\d{4,4})-(\d{2,2})-(\d{2,2})$/)) {
				return new Date(matches[1], matches[2] - 1, matches[3]);
			} else {
				return null;
			};
		},

		dateToString: function(date) {
			var month = (date.getMonth() + 1).toString();
			var dom = date.getDate().toString();
			if (month.length == 1) month = "0" + month;
			if (dom.length == 1) dom = "0" + dom;
			return date.getFullYear() + "-" + month + "-" + dom;
		}
	});
	$( 'input.date_picker' ).date_input( );

	/**
	  * CSS tweaks
	  */
	$('.textBox table tr:even').css('background-color', '#f2f2f2');

	/**
	  * Form styling
	  */
	$('form select.styled').select_skin();

	/**
	  * Fancybox
	  */
	$('a.conditions').fancybox({'type': 'iframe', 'autoDimensions': false, 'width': 725, 'height': 500});

	/**
	  * Update shopping-cart
	  */
	$('.cart-item').change(function(){
		$.getJSON('cart/update/product_id/' + $(this).attr('id') + '/quantity/' + $(this).val() + '/format/json', function(data){
			$('#cart-subtotal-' + data.item.product_id).html(data.item.subtotal);
			$('#cart_total').html(data.item.total);
		});
	});

	/**
	  * Products drag & drop order
	  */
    $('#products').tableDnD( {
    	onDrop: function(table, row) {
			$.get('/admin/products/position?' + $.tableDnD.serialize());
		}
	});

	/**
	  * Calculate pricings
	  */
	$('#price').change(function(){ $('#total').val(Math.round($(this).val()/100*119*10000)/10000); });
	$('#total').change(function(){ $('#price').val(Math.round($(this).val()/119*100*10000)/10000); });

} );

question = function( element ) {

	if( confirm( 'Weet je het zeker?' ) )
		window.location = element.href;

	return false;

}
