      function user_update_box() {
            var sURL = unescape(window.location.pathname);
            window.location.replace( sURL );
            /*$.ajax({
                 type: "GET",
                 url: "http://www.honden.be/ajax/Profile/subnav",
                 success: function(html) {
                        $('#subNav').html(html);
                 }
            });
            $.ajax({
                  type: "GET",
                  url: "http://www.honden.be/ajax/userProfileSmall/profile",
                  success: function(html) {
                        $('#usp_profile').replaceWith(html);
                  }
            });
            FB.XFBML.Host.parseDomTree();*/
      }

      
    function update_fields(msg) {
	var z = msg.split("^_^_^");
        if (z.length > 1) {
            for (var i in z) {
                update_fields(z[i]);
            }
        } else {
            var x = msg.split(" ");
            y = msg.replace(x[0], "");
            if (x[0] == "ok") {
                $("#messages").addClass("ui-state-highlight");
                $("#messages").html("Opgeslagen!");
            } else if (x[0] == "nok") {
                $("#messages").addClass("ui-state-error");
                $("#messages").html(y);
            } else if (x[0] == "#messages") {
                $("#messages").html(y);
                $("#messages").show();
                $("#messages").fadeOut(3000);
            } else if (x[0] == "location"){
                location.href=y;
            } else if (x[0] == ".formMessages") {
                $(".formMessages").html(y);
                $(".formMessages").show();
                $(".formMessages").fadeOut(10000);                  
            } else {
                $(x[0]).next('.clearfloat').remove();
                $(x[0]).replaceWith(y);
            }
        }
    }
    
$(document).ready(function() {
      
      var xx = 0;   
      function addMega(){
        $(this).addClass("hovering");
      }
      function removeMega(){
        $(this).removeClass("hovering");
      }
      function user_logout() {
                  var api = FB.Facebook.apiClient;
                  if (api.get_session()) {
                        FB.Connect.logout(function () {
                            $.ajax({
                               type: "GET",
                               url: "http://www.honden.be/logout",
                               success: function(html) {
                                    var sURL = unescape(window.location.pathname);
                                    window.location.replace( sURL );
                                }
                            });
                        }); 
                  } else {
                            $.ajax({
                               type: "GET",
                               url: "http://www.honden.be/logout",
                               success: function(html) {
                                    var sURL = unescape(window.location.pathname);
                                    window.location.replace( sURL );                                 
                                }
                            });
                  }
      }
    var megaConfig = {
         interval: 200,
         sensitivity: 4,
         over: addMega,
         timeout: 500,
         out: removeMega
    };

    /*$("li.mega").hoverIntent(megaConfig);*/
    
    $(".showComment").click(function () {
        var x = $(this).parents(".activity, .blogpost, #photos").find(".giveComment").css("display");
        if (x == "none") {
            $(this).parents(".activity, .blogpost, #photos").find(".giveComment").css({'display' : 'block'});
        } else {
            $(this).parents(".activity, .blogpost, #photos").find(".giveComment").css({'display' : 'none'});
        }
        return false;
    });
    
    
    $(".usp_showLoginScreen").click(function (){
            $(".usp_loginScreen").css({'display': 'block'});
            return false;
     });
    
     $(".logout").livequery('click', function () {
            user_logout();
            return false;
      });
     
      $(".loginForm").submit(function() {
            $.ajax({
                  type: "post",
                  url: "",
                  data: "action=login&email="+$("#email").val()+"&password="+$("#password").val()+"&firstname="+$("#firstname").val()+"&lastname="+$("#lastname").val(),
                  success: function (response) {
                        if (!response) {
                              user_update_box();
                              return false;
                        } else {
                              $(".loginErrors").html(response);
                              return false;
                        }
                  }
            });
            return false; 
      });
    
    $(".clearFocus").focus(function() {
        $(this).val('');
    });
    
    $("#tabs").tabs({ cookie: { expires: 30 } });
    $(".wysiwyg").livequery("focus", function() {
        if (xx == 0) {
            $(this).wysiwyg();
            xx = 1;
        }

    });
    $("textarea").growfield({});
    $("textarea").livequery("focus", function () {
        $(this).growfield({});
    });
    
    $("#dialog").dialog({
	  bgiframe: true
	, autoOpen: false
        , height: 570
        , width: 600
        , modal: true
	, buttons: {
	    'Verzenden': function() {
                $(this).find(".submitForm").submit();
		$(this).dialog('close');
	    }
            ,'Annuleren': function() {
		$(this).dialog('close');
	    }
	}
    });

    
    $(".beFriend").livequery("click", function() {
	var x = $(this).find("input[name^='userid']").val();
        $.ajax({
              type: "GET"
            , url: "ajax/Friends/beFriend/"+x
            , success: function(msg) {
                $("#dialog").html(msg);
		$("#dialog").css("display","block");
		$('#dialog').dialog('option', 'width', 500);
		$('#dialog').dialog('option', 'height', 220);	
		$("#dialog").dialog('option', 'title', 'Vriend worden.');		
		$("#dialog").dialog('open');
            }
        });
	return false;	
    });
    $("input[name='wallmessage'], textarea[name='wallmessage']").livequery("focus", function () {
        $(this).val('');
    });
    
    $("input[type='text'], textarea").livequery("blur", function () {
        if ($(this).val() == '') {
            title=$(this).attr("title");
            $(this).val(title);
        }
    });
    
    $(".submitForm").livequery('submit', function () {
	$("#ajax").val("1");
	$(this).ajaxSubmit({
                resetForm: true,
		success: function(msg) {
		    update_fields(msg);
		}
	});
	return false;
    });
    
    $(".wallmsg").livequery('submit', function () {
            title=$("textarea[name='wallmessage']").attr("title");
            $("textarea[name='wallmessage']").val(title);
    });
    $("#messages").livequery('change', function() {
        alert("test");
        $(this).hide(5000);
    });
    
    $(".vote").livequery("click" ,function() {
            $.ajax({
                  type: "GET",
                  url: "http://www.honden.be/ajax/Dogs/vote/"+$("input[name='dogid']").val(),
                  success: function(html) {
                       update_fields(html);
                  }
            });        
    });   

    function AfterUpload() {
	$(".dialog2").dialog("close");
	    $.ajax({
		  type: "POST"
		, data: "action=updatePhotos&widget=Photos&ajax=1"
		, success: function(msg) {
		    update_fields(msg);
		}
	    });
    }      

});

