$( document ).ready( function() {
    $( '#user_link_menucontent' ).hide();
    $( '#user_link' ).click( function() {
        $( '#user_link_menucontent' ).slideToggle( 200 );
        return false;
    });
    $( '#user_link' ).blur( function() {
        $( '#user_link_menucontent' ).fadeOut();
        return false;
    });


    if( $( '.accordion' ).length ) {
        $( '.accordion' ).accordion({collapsible: true, active: false});
    }

    if( $( '.datepicker' ).length ) {
        $( '.datepicker' ).each( function( index ) {
            // create Dutch view for users
            /*
            var dbInput = $( document.createElement( 'input' ) ).attr({ type: 'hidden', id: 'dbDate' + index, name: this.name, value: this.value });
            $( this ).val( this.value.substr( 8, 2 ) +  '-' + this.value.substr( 5, 2 ) + '-' + this.value.substr( 0, 4 ) );
            $( this ).siblings( '.fieldNote' ).text( function( index, text ) {
                return text.replace( 'jjjj-mm-dd', 'dd-mm-jjjj' );
            });
            $( this ).after( dbInput );
            $( this ).removeAttr( 'name' );
            */

    		    $( this ).datepicker({
                // place the format of the real value in a special input
                //altField: '#dbDate' + index,
                //altFormat: 'yy-mm-dd',

    				    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', 'Vrij', 'Zat'],
    						firstDay: 1,
    						monthNames: ['Januari', 'Februari', 'Maart', 'April', 'Mei', 'Juni', 'Juli', 'Augustus', 'September', 'Oktober', 'November', 'December'],
    						monthNamesShort: ['Jan', 'Feb', 'Mrt', 'Apr', 'Mei', 'Jun', 'Jul', 'Aug', 'Sep', 'Okt', 'Nov', 'Dec']
    				});
        });
		}

    if( $( '.multiselect' ).length ) {
        $( '.multiselect' ).multiselect({sortable: false, searchable: false});
    }

		$('.pfz-messages .close').click(function() {
			$(this).closest('li').hide();
			if ($(this).closest('ul').find('li:visible').length == 0) {
				$(this).closest('ul').hide();
			}
			$.ajax({
				url: $(this).attr('href'),
				success: function(data) {
				}
			});
			return false;
		});
		
	/* Placeholder fix */
	var input = document.createElement('input');
	if (!('placeholder' in input)) {
		$('input').each(function() {
			var $el = $(this);
			var ph = $el.attr('placeholder');
			if (ph) {
				$el.focus(function() {
					if ($el.attr('value') == ph) {
						$el.attr('value', '');
						$el.removeClass('default');
					}
				});
				$el.blur(function() {
					if ($el.attr('value') == '') {
						$el.attr('value', ph);
						$el.addClass('default');
					}
				});
				$el.trigger('blur');
			}
		});
	}
	
	$('.logs-events h3 + div').hide();
	$('.logs-events').click(function(e) {
		var $target = $(e.target);
		if ($target.is('h3')) {
			$target.next('div').slideToggle();
		}
	});
});


function setFieldBehaviour( fieldReference ) {
    $( fieldReference ).focus( function() {
        if ( $( this ).attr( 'value' ) == $( this ).attr( 'defaultValue' ) ) {
            $( this ).attr( 'value', '' );
        }
        return;
    });
    $( fieldReference ).blur( function() {
        if ( $( this ).attr( 'value' ) == '' ) {
            $( this ).attr( 'value', $( this ).attr( 'defaultValue' ) );
        }
        return;
    });
    return;
}

$( document ).ready( function() {
    //setFieldBehaviour( '#username' );
    //setFieldBehaviour( '#password' );
    return;
});

function adjustPageSize() {
    // create something to get the actual value from the stylesheet...?
    var wrapperWidth = 1250;
    var contentWidth = 855;
    if ( $( window ).width() < 1250 ) {
        wrapperWidth = 1000;
        contentWidth = 605;
    }
    // homepage has a different width for .content
    if( location.pathname == '/' ) {
        contentWidth -= 50;
    }
    $( '#wrapper' ).width( wrapperWidth );
    $( '.content' ).width( contentWidth );
    
    return;
}

$( document ).ready( function() {
    $( window ).resize( function() {
        adjustPageSize();
    });
    adjustPageSize();
    
    /* Give the recent blocks ellipsis */
    $( 'div.recent li' ).ellipsis();
    /* give the forum titles in the forum overview ellipsis */
    $( 'div.topics h3' ).ellipsis();
});

$( document ).ready( function() {
    $( 'textarea.pfz-editor' ).PFZEditor();
});


