/**
 * 
 */
$(document).ready(function() {
        $("#tweets").tweet({
          avatar_size: 32,
          count: 4,
          username: "spovernl",
          template: "{text}"
        });

	// Tooltip
	xOffset = 10;
	yOffset = 20;
	$("a.questionmark").hover(function(e){
		this.t = this.title;
		this.title = "";
		$("body").append("<p id='tooltip'>"+ this.t +"</p>");
		$("#tooltip")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px")
			.fadeIn("fast");
    },
	function(){
		this.title = this.t;
		$("#tooltip").remove();
    });
	$("a.questionmark").mousemove(function(e){
		$("#tooltip")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px");
	});

	// end tooltip


	var default_values = new Array();
	$("input.default-value").focus(function() {
		if (!default_values[this.id]) {
			default_values[this.id] = this.value;
		}
		if (this.value == default_values[this.id]) {
			this.value = '';
		}
		$(this).blur(function() {
		if (this.value == '') {
			this.value = default_values[this.id];
		}
		});
	});

	$('#header_login_password').focus(function() {
	    $('#header_login_password').hide();
	    $('#header_login_credential_password').show();
	    $('#header_login_credential_password').focus();
	});
	$('#header_login_credential_password').focus(function() {
	    $('#login_password').hide();
	    /* $('#login_credential_password').show();
	    $('#login_credential_password').focus(); */
	});
	$('#header_login_credential_password').blur(function() {
	    if($('#header_login_credential_password').val() == '') {
	        $('#header_login_password').show();
	        $('#header_login_credential_password').hide();
	    }
	});

	$("#header_login_submit").click(function(event) {

		if($("#header_rememberme").attr("checked")==true) {
			remember = 1;
		} else {
			remember = 0;
		}

	    /* get some values from elements on the page: */
	    var $form = $("#header_loginform"),
	        url = $form.attr( 'action' );
	    
	    /* Send the data using post and put the results in a div */
	    $.post( url,
	    		{
	    			credential_email: $('#header_login_credential_email').val(),
	    			credential_password: $('#header_login_credential_password').val(),
	    			remember_me: remember
	    		},
			function ( data ) {
	    		if (data.result == -1) {
	    			if (!$('#dialog').length) {
	    				$('body').append('<div class="jqui"><div id="dialog" title="Error"><p><strong>Account has not been activated!</strong></p><p>Resend activation email?</p></div></div>');
	    			}
	    			
	    			$("#dialog").dialog({ modal: true, resizable: false, draggable: false, buttons: {
	    											"Yes": function() {
	    												var dialog = this;
	    												$.ajax({
	    													url: baseUrl + '/user/resend-activation/id/' + data.user_id,
	    													success: function(data) {
	    														if (data.status > 0) {
	    															alert('Email sent');
	    															
	    															$(dialog).dialog('close');
	    		    												$(dialog).dialog('destroy');
	    		    												$(dialog).remove();
	    															
	    														} else {
	    															alert(data.message);
	    														}
	    													},
	    													error: function(data) {
	    														alert('Error sending email');
	    														
	    													}
	    												});
	    											},
	    											"No": function() {
	    												$(this).dialog('close');
	    												$(this).dialog('destroy');
	    												$(this).remove();
	    											}
	    								},
	    								close: function(){
    										$(this).dialog('destroy');
											$(this).remove();
    									}
	    						});
	    			$('div.jqui:first').append($('div.ui-dialog:first'));
	    		}
	    		
	    		if (data.result==0) {
	    		
	    			$("body").append("<div id='dialog' class='dialog' title='Error'>" + data.description + "</div></div>");
	    			alert(data.description);

	    		} else if(data.result==1){
					if(data.password_update==0){
						$("#header form#header_loginform").remove();
						$("#userpannel").show();
						$("#userpannel").html('<a class="hbutton" href="' + baseUrl + '/space/index/key/' + data.userid + '" title="Ga naar je profiel"><span>Profiel</span> </a>');
						$("#userpannel").append('<a class="hbutton" href="' + baseUrl + '/user/logout" title="Log uit van spover.nl"><span>Uitloggen </span></a>');
						if (window.location.href.match(/club\/create/gi)) {
							window.location.href= baseUrl + "/club/create";
						} else {
							window.location.href= baseUrl + "/space/index/key/" + data.userid;
						}
					}else{
						window.location.href= baseUrl + "/user/chpassword";
					}
				}
			}
	    , "json");
	    
	    return false;
	  });

});
