$(function() {
	$("input[type=submit]").click(function(e) {
		e.preventDefault();
	});
	
	jQuery.fn.setDefaultValue = function(value) {
		return this.each(function() {
			if(this.type == 'text') {
				value = value || $(this).attr('data-defvalue');
				
				if($.trim(this.value) == '') this.value = value;
				$(this).focus(function() {
					if(this.value == value || $.trim(this.value) == '') this.value = '';
				}).blur(function() {
					if(this.value == value || $.trim(this.value) == '') this.value = value;
				});
			}
		});
	};
	
	jQuery.ajaxSetup({
		scriptCharset: "UTF-8",
		contentType: "application/x-www-form-urlencoded; charset=UTF-8"
	});

	jQuery.fn.customMessages = function(options, title, content)
	{
		if($('.custom_message_container').length == 0) {
			o = jQuery.fn.extend({
				draggable		: true,
				modal			: true,
				okButtonText	: 'Ok',
				cancelButtonText: 'Annuleren',
				callbackCancel	: $.noop,
				callbackOk		: $.noop
			}, options);
			
			var cmc = '.custom_message_container', _coW = $(cmc).outerWidth(), buttons = ['<input id="cmessage_ok_button" value="'+o.okButtonText+'" type="button" />','<input id="cmessage_cancel_button" value="'+o.cancelButtonText+'" type="button" />'];
			b = (o.useFunctions == 'alert') ? buttons[0] : (o.useFunctions == 'confirm' ? buttons[0] + ' ' + buttons[1] : '');
			$('body').append('<div style="z-index:100;position:fixed;top:0;left:0;width:100%;height:100%;'+(o.modal ? 'background:#000;opacity:0.6;filter:alpha(opacity=60)' : '')+'" class="custom_mess_overlay"></div>');
			$('.container').append('<div class="custom_message_container" style="display:none;"><h1 class="custom_message_title">'+title+'</h1><div class="custom_message_content"><div class="custom_mtext">'+content+'</div></div><div style="text-align:center">'+b+'</div></div>');
			$('.custom_message_container').fadeIn(2500);
			
			$('#cmessage_ok_button').click(function() {
				o.callbackOk();
				$(".custom_message_container,.custom_mess_overlay").remove();
			});
			
			$('#cmessage_cancel_button').click(function() {
				o.callbackCancel();
				$(".custom_message_container,.custom_mess_overlay").remove();
			});
			
			if(o.draggable) $('.custom_message_container').draggable({cursor: "move",cancel: "div.custom_message_content",containment: "div.custom_message_title",scroll: false,zIndex: 15000});
		}
		return false;
	};
	
	jQuery.fn.createToolTip = function(ctitle, content)
	{
		var title = '', titleh = '';
		return this.each(function() {
			var _p = this;
			
			$(_p).mouseover(function(e) {
				titleh = ctitle || $(this).attr('data-titleh'); 
				title = content || $(this).attr('title');
				$(this).attr({ title: '', ntitle: _p.title });
				$('<div id="tooltip"><div class="tooltip-head">'+titleh+'</div><div class="tooltip-body">'+title+'</div></div>').css({ top: e.pageY+10, left: e.pageX+20 }).appendTo('body').fadeTo(100, 0.8);
			}).mousemove(function(e) {
				var data = [15,'div#tooltip'];
				l = (e.clientX + $(data[1]).width() + data[0] > $(window).width()) ? e.pageX - ($(data[1]).width()*1.1) : e.pageX + data[0];
				t = (e.clientY + $(data[1]).height() + data[0] > $(window).height()) ? e.pageY - ($(data[1]).height()*2) + data[0] : e.pageY + data[0];
				
				$("div#tooltip").css({
					top: t,
					left: l
				});
			}).mouseout(function() {
				$("div#tooltip").remove();
				$(_p).attr('title', title);
			});
		});
	};
	
	$('input:text[name=tab-login-u]').setDefaultValue('Gebruikersnaam');
	$('div.footer-logo').createToolTip('Alien Conquest Forum', 'Klik hier om door te worden gestuurd naar het Alien Conquest forum.');
	
	$("div.createac").hover(function() {
		$(this).animate({opacity: 0.9}, 750);
	}, function() {
		$(this).animate({opacity: 0.4}, 750);
	});
	
	$(".createac").live('click', function() {
		$(this).customMessages({
			useFunctions: 'confirm',
			okButtonText: 'Aanmelden',
			cancelButtonText: 'Annuleren',
			callbackOk: function() {
				if($.trim($('#username').val()).match(/[\w\s]{5,20}/) === true) {
					if($('#password_one').val() === $('#password_two').val()) {
						$.post('/api/register/signup.json', {d:[$('#username').val(),$('#password_one').val(),$('#email').val()]}, function(e) {
							var xe = [1];
						}, "json");
					} else {
						var xe = [0, 'De ingevoerde wachtwoorden komen niet overeen of je wachtwoord is korter dan 6 tekens.'];
					}
				} else {
					var xe = [0, 'Je gebruikersnaam moet minstens 5 tekens lang zijn en kleiner zijn dan 20 tekens.'];
				}
				
				var xz = [$('#username').val(), $('#email').val()];
				setTimeout(function() {
					$('#sign-message p').text(xe[1]);
					$(this).customMessages({
						useFunctions:'alert',
						callbackOk: function() { setTimeout(function() { 
							$('.createac').click();
							$('#username').val(xz[0]).end().find('#email').val(xz[1]);
						}, 25); }
					}, (xe[0] == 0 ? 'Er is een fout opgetreden' : 'Succes'), $('#sign-message').html());
				}, 50);
			}
		}, $('#sign-dialog').attr('title'), '</span>'+$('#sign-dialog').html());
	});
	
	$(".tab-login-submit").click(function() {
		if($.trim($(".tab-login-username").val()) === "" || $.trim($(".tab-login-password").val()) === "") {
			alert('Gebruikersnaam of wachtwoord is opgegeven.');
		} else {
			$.post("/api/login/user.json", { "tab-login-u": $(".tab-login-username").val(), "tab-login-p": $(".tab-login-password").val(), "tab-login-submit": $(".tab-login-submit").val() }, function(d) {
				(d.success === 0) ? alert(d.details) : window.location = d.url;
			}, "json");
		}
	});
});