$(document).ready(function() {
	initDisplay();	
});

function initDisplay() {
	var nHeight = parseInt($(window).height());
	var nSiteHeight = parseInt($('#site').height());
	
	if(nHeight < nSiteHeight) {
		$('#site').css({
			'margin-top': '10px'
		});
	}
	
}
function initClick() {
	$('.meerinfo').click(function() {
		if(!$(this).parents('.row').next().is(':visible')) {	
			$('.info').slideUp(500);
			$(this).parents('.row').next().slideDown(500);
		} else {
			$(this).parents('.row').next().slideUp(500);
		}
	});
}


function checkBox() {
	$('.checkbox .input').click(function() {
		if(!$(this).parents('.checkbox').find('input').attr('checked')) {
			$(this).parents('.checkbox').find('input').attr('checked', true);
			$(this).addClass('checked');
		} else {
			$(this).parents('.checkbox').find('input').attr('checked', false);
			$(this).removeClass('checked');
		}
	});
}

function radioButtons() {
	$('.radio .input').click(function() {
	  	$(this).parents('.radio').find('input').attr('checked', false);
		$(this).parents('.radio').find('.input').removeClass('checked');
		$(this).prev().attr('checked', true);
		$(this).addClass('checked');
	});
}


function makeSortable() {
	$('.item').draggable({
		revert: 'invalid',
		snap: '.drop li',
		snapMode: 'inner',
		opacity: 0.5,
		containment: '.left'
	});
	$('.drop li').droppable({
		accept: '.item',
		tolerance: 'fit',
		drop: function(event, ui) {
			setItem(ui.draggable, this);
		}
    });
}
function setItem(p_oDraggable, p_oDroppable) {
	
	// Gegevens ophalen
	var sId = $(p_oDraggable).attr('id');
	var sTitle = $(p_oDraggable).attr('title');
	var aNumber = sId.split('-');
	var sNewId = 'target-'+aNumber[1];
	
	// Gegevens zetten
	$(p_oDroppable).attr('id', sNewId);
	$(p_oDroppable).attr('title', sTitle);
	$(p_oDroppable).addClass('dropped');
	
	// Overige acties
	$(p_oDraggable).hide();
	$(p_oDroppable).droppable({ disabled: true });
	
	$('.drop').sortable({
		items: '.ui-droppable',
		opacity: 0.5
    });
}

function initSubmit() {
	$('.submit').click(function() {
		var bAjax = true;
		var aVolgorde = new Array();
		var nI = 0;
		
		$('.drop li').each(function(el,val) {
			if(!$(this).hasClass('dropped')) {
				bAjax = false;	
			}
			var aId = $(this).attr('id').split('-');
			aVolgorde[nI] = aId[1];
			nI++;
		});
		
		if(!bAjax) {
			$('.error').fadeIn();	
		} else {
			$('.error').hide();
			
			$.ajax({
				type: 'POST',
				data: 'aVolgorde='+aVolgorde,
				url: 'inc/request.inc.php',
				success: function(result){
					window.location = 'formulier.php';
				}
			});
		}
	});
}
