$(document).ready(function() {

	// wat for safari to have all load
  if (jQuery.browser.safari && document.readyState != "complete"){
    setTimeout( arguments.callee, 100 );
    return;
  }
	
	// rotated flash cover
	imageSrc=base_url + $('#rotatedCover img').attr('src');
	swfobject.embedSWF("site/assets/img/cover.swf", "rotatedCover", "380", "470", "9.0.0", '', {image:imageSrc}, {wmode:'transparent'});

	// img popup
	$("img.popup").fullsize({forceTitleBar:false,triggerIcon:false});

	// form justify
	$('form input.str').add('form input.int').add('form input.url').add('form input.email').add('form texarea').each(function(){
		p=$(this).parent('div:first');
		pwidth=$(p).innerWidth();
		lwidth=$(p).children('label:first').outerWidth();
		thisWidth=pwidth-lwidth-20;
		if (thisWidth>0) $(this).css({'width':thisWidth+'px'});
	});
	
	// form changes
	// when abo form is send and payment by acceptgiro, don't aks for giro number (fill it with something)
	$('form.abo').submit(function(e){
		var accept=$('form.abo #b_betaalwijze__acceptgiro').attr('checked');
		if (accept) {
			$('#int_rekeningnummer').attr('value','0');
		}
	});
	
	// label after checkbox/radio
	$('form label[for~=b_notify]').add('form label[for~=b_kado]').add('form .b_betaalwijze span.optionLabel').each(function(){
		tWidth=$(this).parent('div:first').innerWidth() - $(this).outerWidth() - 47;
		$(this).css({'float':'right','margin-right':tWidth});
	});
	
	
	
	// checkbox styling
	$('form input:checkbox').each(function(){
		$(this).hide();
		// replace with <span id="jCheck_##" class="jCheckbox ##">
		$(this).after('<span id="jCheck_'+$(this).attr('id')+'" class="jCheckbox '+$(this).attr('class')+'" style="display:block;width:'+($(this).outerWidth()+2)+'px;height:'+($(this).outerHeight()+2)+'px;"></span>');
		// check for what is/isn't already checked and match it
		(this.checked) ? $("#jCheck_"+this.id).addClass('checked') : $("#jCheck_"+this.id).removeClass('checked');
	});
	// 'check' the fake ones and their matching checkboxes
	$(".jCheckbox").click(function(){
		($(this).hasClass('checked')) ? $(this).removeClass('checked') : $(this).addClass('checked');
		$('form input:checkbox#'+( $(this).attr('id').replace('jCheck_','')) ).trigger("click");
	});

	// radiobutton styling
	$('form input:radio').each(function(){
		$(this).hide();
		// replace with <span id="jRadio_##" class="jRadio ##">
		$(this).after('<span name="'+$(this).attr('name')+'" id="jRadio_'+$(this).attr('id')+'" class="jRadio '+$(this).attr('class')+'" style="display:block;width:'+($(this).outerWidth()+2)+'px;height:'+($(this).outerHeight()+2)+'px;"></span>');
		// check for what is/isn't already checked and match it
		(this.checked) ? $("#jRadio_"+this.id).addClass('checked') : $("#jRadio_"+this.id).removeClass('checked');
	});
	// 'check' the fake ones and their matching checkboxes
	$(".jRadio").click(function(){
		name=$(this).attr('name');
		id=$(this).attr('id').replace('jRadio_','');
		// remove all checked
		$('form span[name~='+name+']').removeClass('checked');
		$('form input:radio[name='+name+']').removeAttr('checked');
		// check this one
		$(this).addClass('checked');
		$('form input:radio#'+id).attr('checked','checked');
	});	

});
