$(document).ready(function() {
	$('body').append('<div id="dialog"></div>');
	$.ajaxSetup({cache:false});

	// Makes the login popup work
/*
	$('.loginPopup').click(function() {
		$('#dialog').load($(this).attr('href'));
		$('#dialog').dialog({
			title: 'Inloggen bij Gigakids',
			modal: true,
			width: 500,
			height: 330,
			resizable: false,
			buttons: {
				'Inloggen': function() {
					$.submitLogin()
				},
				'Annuleren': function() {
					$(this).dialog('close');
				}
			}

		});
		return false;
	});
*/

	// show user info on mouse over, welcome at #header
	$("#hearder-user-loged-in").mouseenter(function(){
		if(!$.browser.msie || ($.browser.version > 7 && $.browser.msie)) {
			$("#header-user-actions").slideDown();
		}
	});

	$("#hearder-user-loged-in").mouseleave(function(){
		$("#header-user-actions").css('display', 'none');
	});

	jQuery.submitLogin = function() {
		var form = $('#dialog form');
		$.post(form.attr('action'), form.serialize(), function(data){
			if(data.error) {
				$('#dialog').html(data.html);
			}
			else {
				window.location.reload();
			}
		}, 'json');
	}

	$('.hintBox').live("mousemove", function(event) {
		var hint = $('.hint', $(this).parent());
		if(hint.size() == 0) {
			var hint = $('<div></div>').addClass('hint');
			if($(this).attr('title') != '') {
				var text = $('<p></p>').html($(this).attr('title'));
				hint.fadeIn(200);
				hint.append(text);
			}

			$(this).attr('title', '');
			$(this).append(hint);
		}
		hint.css('top', (event.clientY - (hint.height()+16)));
		hint.css('left', (event.clientX - (hint.width()/2)-8));
		event.preventDefault();
		return;
	});

	$('.hintBox').live("mouseleave", function() {
		var hint = $('.hint', $(this));
		$(this).attr('title', $('p', hint).html());
		hint.remove();
	});


	$.extend($.ui.dialog.prototype, {
		'addButton': function(buttonName, func) {
			var buttons = this.element.dialog('option', 'buttons');
			buttons[buttonName] = func;
			this.element.dialog('option', 'buttons', buttons);
		},
		'removeButton': function(buttonName) {
			var buttons = this.element.dialog('option', 'buttons');
			delete buttons[buttonName];
			this.element.dialog('option', 'buttons', buttons);
		}
	});

	$(".sendButton").button();
	$("#loginForm .submit input").button();
	$('A[rel="external"]').each(function(){
		$(this).attr('target', '_blank');
	});

	$('.popup').click(function() {
		$('#dialog').dialog({ title: 'Jouw voortgang' });
		$.get($(this).attr('href'), function(data) { $('#dialog').html(data) });
		$('#dialog').dialog("open");
		return false;
	});
	$('#dialog').dialog({autoOpen: false, modal: true, width: 800, height: 500 });
});
