// Ajax fix voor page reload
if (document.location.href.search(/#/) != -1) {
    var url = document.location.href.split("#");
    if (url[1]) document.location.href  =  url[1];
}

$(document).ready(function(){
    setBehaviour();
    initHistory();
});



/**
 * Zet de eigenschappen voor een aantal elementen op basis van classes etc
 *
 */
function setBehaviour() {
    // Ontkoppel eerst oude eigenschappen
    $('a').unbind();
    $('form').unbind();
    $('div').unbind();
    
    // Zet de ajax beviour (ajax.js)
    setAjaxBehaviour();
    initAutocomplete();
    
	// Initieer datepicker velden
    $('.datepicker').each(function(){
    	if (!$.isFunction($.datepicker)) {
    		includeScript("/scripts/jquery/ui/ui.datepicker.js", function(){
    			$('.datepicker').datepicker({dateFormat: 'dd-mm-yy'});
    		});
    		includeScript("/scripts/jquery/ui/i18n/ui.datepicker-nl.js");
    	} else {
    		$('.datepicker').datepicker({dateFormat: 'dd-mm-yy'});
    	}
	});
	
    $('.artistpic').mousemove(function(e){
    	var pos = $(this).position();
    	$('#artinfo').html($(this).attr('info'));
    	$('#artinfo').css('top', e.pageY);
    });

    $('.artistpic').mouseover(function(e){
    	$('#artinfo').show();
    });

    $('.artistpic').mouseout(function(e){
    	$('#artinfo').hide();
    });
    
    $.fck.path = '/scripts/fckeditor/'; 
    $('textarea.richtext').fck({toolbar: 'Basic', height: 200});
    
    
    $('.invoerveld').focus(function(){
        $(this).addClass('invoerveld2')
    });

    $('.invoerveld').blur(function(){
        $(this).removeClass('invoerveld2')
    });

    $('.bio_title').click(function(){
        if ($(this).siblings('.bio').css('display') == 'block') {
            $('.bio').hide();
        } else {
            $('.bio').hide();
            $(this).siblings('.bio').show();
        }
    });
}


function initAutocomplete(){
    $('input[autocompleteurl]').each(function(){
        if ($.isFunction($(this).autocomplete()) == false) {
            var url = $(this).attr('autocompleteurl');
            $(this).unbind();
            if ($(this).attr('autocompletematch')) {
                $(this).autocomplete(url,{mustMatch: true});   
            } else {
			    $(this).autocomplete(url);
            }
            $(this).result(function(event, data, formatted) {
            	$(this).parents('form').append('<input type=hidden value=1 name="autocompleted_'+$(this).attr('name')+' />');
            	$(this).parents('form').append('<input type=hidden value=1 name="autocompleted" />');
            });
        } 
    });
}



// Laad een javascript
function includeScript(uri, callback) {
    var script = document.createElement("script");
    script.src = uri;
    script.type = "text/javascript";
    script.language = "javascript";
    if (callback != null) {
        script.onreadystatechange = function() {
            if (script.readyState == "complete") callback();
        };
        script.onload = callback;
    }
    document.getElementsByTagName("head")[0].appendChild(script);
    return false;
}

// Laad een stylesheet
function includeStyle(uri) {
    $("head").append("<link rel='stylesheet' type='text/css' href='" + uri + "' />");
}


var notifier = 0;
function notify(message, type) {
    notifier++;
    if (!$('#notifier').attr('id')) {
        $('.content').append("<div id='notifier'></div>");
    }
    $('#notifier').append("<div id='not" + notifier + "' class='notification'>" + message + "</div>");
    $('#notifier').css('top', 0 + $(window).scrollTop());
    window.setTimeout("$('#not" + notifier + "').fadeOut()", 2000);
}



