jQuery(document).ready( function($) {

    if (jQuery('.leftcolumn').width() > 350) {
        jQuery('.centercolumn:first').remove();
    }

	positionFooter();
	
	
	$('.popup').click(function() {
		$(this).attr('target', '_blank');
	});
	
	$('form .submit a').click(function() {
        verifyForm( this );
	});
	
	if ($('.imagelist').length > 0) {
		window.setInterval(function() {
			$('.imagelist').each(function() {
				imgList = this;
				$('.imageitem:last', imgList).fadeOut('slow', function() {
					$(this).prependTo( $(this).parent() ).show();
				});
			});
			
		}, 5000);
	}
	
	$('a.slider').click(function() {
		$('#'+$(this).attr('rel')).slideToggle();
		return false;
	});
	
//	$('.superstaff_user_menu a').click(function() {
//		if ($(this).attr('class') != 'active') {
//			$('.superstaff_user_menu a').removeClass('active');
//			$(this).addClass('active');
//			lnk = this;
//			$('.pageitem').fadeOut('normal', function() {$('#view_user_pageitem_'+$(lnk).attr('rel')).fadeIn();});
//		}
//	});
	
	$('.superstaff_user_submenu a').click(function() {
		if ($(this).attr('class') != 'active') {
			$('.superstaff_user_submenu a').removeClass('active');
			$(this).addClass('active');
			lnk = this;
            $('.subpageitem').hide();
            $('#subpageitem_'+$(lnk).attr('rel')).show();
			//$('.subpageitem').fadeOut('normal', function() {$('#subpageitem_'+$(lnk).attr('rel')).fadeIn();});
		}
	});

    $('input.maxlength, textarea.maxlength').each(function() {
        if ($('div[rel='+$(this).attr('name')+']').length == 0) {
            if (!$(this).attr('maxlength')) $(this).attr('maxlength', 500);
            $(this).after('<div rel="'+$(this).attr('name')+'" class="formlimit"><span class="count">'+$(this).attr('maxlength')+'</span> karakters beschikbaar</div>');
        }
    });

    $('input.maxlength, textarea.maxlength').keyup(function() {
        charLeft = $(this).attr('maxlength')-$(this).val().length;
        if (charLeft < 1) {
            charLeft = 0;
            $(this).val( $(this).val().substr(0, $(this).attr('maxlength')) );
        }
        $('div[rel="'+$(this).attr('name')+'"] .count').html( charLeft );
    });

	$('textarea:not(.nocms)').tinymce({
		// Location of TinyMCE script
		script_url : '/wp-content/themes/superstaff_v1/tiny_mce/tiny_mce.js',

		// General options
		theme : "simple",
		width: '400px',
		height: "150px",
		plugins : "paste",
		paste_auto_cleanup_on_paste : true,
        paste_retain_style_properties: '',
        paste_strip_class_attributes: 'all',
        paste_remove_spans: true,
        paste_remove_styles: true,
        paste_remove_styles_if_webkit: true

	});
	
	$('.datepicker').datepicker({
		dateFormat: 'dd-mm-yy',
		dayNames: ['Zondag', 'Maandag', 'Dinsdag', 'Woensdag', 'Donderdag', 'Vrijdag', 'Zaterdag'],
		dayNamesMin: ['Zo', 'Ma', 'Di', 'Wo', 'Do', 'Vr', 'Za'],
		dayNamesShort: ['Zon', 'Maa', 'Din', 'Woe', 'Don', 'Vri', 'Zat'],
		monthNames: ['Januari', 'Februari', 'Maart', 'April', 'Mei', 'Juni', 'July', 'Augustus', 'September', 'Oktober', 'November', 'December'],
		monthNamesShort: ['Jan', 'Feb', 'Maa', 'Apr', 'Mei', 'Jun', 'Jul', 'Aug', 'Sep', 'Okt', 'Nov', 'Dec']
		
	});

});


function gotoTab(tab) {
	jQuery('.superstaff_user_menu a').removeClass('active');
	jQuery('.superstaff_user_menu a[rel='+tab+']').addClass('active');
	jQuery('.pageitem').fadeOut('normal', function() {jQuery('#view_user_pageitem_'+tab).fadeIn();});
	return false;
}

function gotoSubTab(tab) {
    jQuery('.superstaff_user_submenu a').removeClass('active');
    jQuery('.superstaff_user_submenu a[rel='+tab+']').addClass('active');

    jQuery('.subpageitem').hide();
    jQuery('#subpageitem_'+tab).show();
    return false;
}

function positionFooter() {
	
	
	offset = parseInt( parseInt(jQuery('body').height())-parseInt( jQuery('#overall').height()+jQuery('#footer').height() ));
	if( offset > 2 ) {
		jQuery('#footer').css({'margin-top':offset+'px'});
	}
}

function maxCheck(rel, num) {
	if( jQuery('input[rel='+rel+']:checked').length < num) {
		jQuery('input[rel='+rel+']').each(function() {jQuery( this ).removeAttr('disabled');});
	} else {
		jQuery('input[rel='+rel+']:not(:checked)').each(function() {jQuery( this ).attr('disabled', 'disabled');});
	}
}

function showStudent(num) {

    if (jQuery('#hiddencontent').length < 1) {
        jQuery('body').append('<div id="hiddencontent" style="display:none;"></div>');
        jQuery('#hiddencontent').html( jQuery('#pagecontent').html() );
    }
    jQuery('#pagecontent').html( '<div class="superstaff_studentlist_info">'+ jQuery('#studentinfo_'+num).html()+'</div>' );
    jQuery('#pagecontent .superstaff_studentlist_info').fadeIn();
}

function closeStudent() {
    jQuery('#pagecontent .superstaff_studentlist_info').fadeOut('normal', function() {
        jQuery('#pagecontent').html( jQuery('#hiddencontent').html() );
    });
}

function registerStep( step ) {

    if (verifyForm( jQuery('#superstaff_user_register_step'+step) )) {
        jQuery('#superstaff_user_register_step'+step).fadeOut('normal', function() {
            jQuery('#superstaff_user_register_step'+parseInt(step)+1).fadeIn();
        });
    }

    return false;
}

function verifyForm( el ) {

    jQuery('.error, .formerror', jQuery(el).parents('form')).fadeOut();

    response = true;

    jQuery('input,select,textarea', jQuery(el).parents('form')).each(function() {
        singleresponse = true;
        classes = jQuery(this).attr('class').split(' ');

        for(i = 0; i < classes.length; i++) {
            switch(classes[i]) {
            case 'req-string':
                    if (jQuery(this).val().length == 0) singleresponse = false;
                break;
            case 'req-email':
                    if (/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i.test(jQuery(this).val()) == false) singleresponse = false;
                break;
            case 'req-both':
                    bothres = false;
                    jQuery('[rel='+jQuery(this).attr('rel')+']').each(function() {if (jQuery(this).val() != '') {bothres = true;}});
                    if (bothres == false) {singleresponse = false;}
                break;
            case 'req-same':
                    sameres = true;
                    valfirst = '';
                    jQuery('[rel='+jQuery(this).attr('rel')+']').each(function() {
                        if (valfirst == '') {
                            valfirst = jQuery(this).val();
                        } else {
                            if (jQuery(this).val() != valfirst) {
                                sameres = false;
                            }
                        }
                    });
                    if (sameres == false) singleresponse = false;
                break;
            case 'req-number':
                    if (isNaN(jQuery(this).val()) || jQuery(this).val() == '' ) singleresponse = false;
                break;
            case 'req-checked':
                    if (jQuery(this).attr('checked') == false) singleresponse = false;
                break;
            case 'req-oneof-checked':
                    oneof = false;
                    jQuery('[rel='+jQuery(this).attr('rel')+']').each(function() {
                        if (jQuery(this).attr('checked') == true) {
                            oneof = true;
                        }
                    });
                    if (oneof != true) {
                        jQuery('.error', jQuery(this).parent().parent() ).fadeIn();
                        singleresponse = false;
                    }
                break;
            }
        //}
        }
        if (singleresponse == false) {
            jQuery('.error', jQuery(this).parent()).fadeIn();
            response = false;
        }
    });

    if (response != false) {
        jQuery(el).parents('form').submit();
    } else {
        jQuery('.formerror', jQuery(el).parents('form')).fadeIn();
    }

    return false;
}



