function PWUser () {
    this.firends = false;
    this.users = new Object();
}
PWUser.prototype.loginRegUser = function(redirectUrl) {
    var ref = this;
	
	if(!redirectUrl) {
		redirectUrl = "http://www.witajwpodrozy.pl/konto-uzytkownika.html";
	}
    
    $.ajax({
        url: "/ajax.php",
        type: "POST",
        data: $("#form_register_login").serialize() + "&plugin_name=user_profile",
        beforeSend: function() {
            $('<div style="text-align: right;"><img src="/_items/_new/img/medium-loading-green.gif" style="display: inline;" id="login_loader" alt="" /></div>').insertAfter("#btn_login_user");
            $("#btn_login_user").hide();
        },
        complete: function() {
            $("#login_loader").remove();
            $("#btn_login_user").show();
        },
        success: function(html) {
            if(html != 0) {
                $(".box_user .user_profile, #login_form_error").remove();
                $(".box_user").prepend(html);

                var content = $(html).find(".user_profile_wrapper").html();
                $("#user_panels .user_profile .welcome").remove();
                 $("#user_panels .user_profile .youcan").remove();
                $("#user_panels .user_profile").append(content);
                ref.getFriends();
                ref.changeTopbar();
                window.location.href = redirectUrl;
            }
            
        }
    });
    return false;
}
PWUser.prototype.loginUser = function(form_selector) {
    var ref = this;
    login_result = false;
    if($(form_selector).length){
    	if($(form_selector)[0].tagName=='form' || $(form_selector)[0].tagName=='FORM'){
		    $.ajax({
		        url: "/ajax.php",
		        type: "POST",
		        async: false,
		        data: $(form_selector).serialize() + "&plugin_name=user_profile&get=loggedinbox",
		        beforeSend: function() {
		            //$('<div style="text-align: right;"><img src="/_items/_new/img/medium-loading-green.gif" style="display: inline;" id="login_loader" alt="" /></div>').insertAfter("#btn_login_user");
		            //$("#btn_login_user").hide();
		        },
		        complete: function() {
		            //$("#login_loader").remove();
		            //$("#btn_login_user").show();
		        },
		        success: function(html) {
		            if(html != 0) {
		                //$("#user-area").html($(html).html());
		                login_result = html;
		            } else {
		            	login_result = false;
		                //$("#login_form_error").remove();
		                //$("#form_login_user").before('<span id="login_form_error" style="color: red;">Niepoprawny login lub hasło</span>');
		            }
		        }
		    });
		    return login_result;
	    }else{
    		return false;
		}
    }else{
		return false;
	}
}

PWUser.prototype.getFriends = function() {
    var ref = this;
    if(!this.friends) {
        $.ajax({
            url: "/ajax.php",
            type: "POST",
            data: "plugin_name=friend_list&position=main",
            success: function(html) {
                if(html != 0) {
                    $("#mini_friends_list").html(html);
                    ref.friends = html;
                }
            }
        });
    }
    else {
        $("#mini_friends_list").html(this.friends);
    }
}

PWUser.prototype.getUsers = function(stype) {
    var ref = this;
    if(!this.users[stype]) {
        $.ajax({
            url: "/ajax.php",
            type: "POST",
            data: "plugin_name=user_list&stype=" + stype,
            success: function(html) {
                $("#user_panels .user_list").html(html);
                ref.users[stype] = html;
            }
        });
    }
    else {
        $("#user_panels .user_list").html(this.users[stype]);
    }
}


PWUser.prototype.changeTopbar = function() {
    $.ajax({
        url: "/ajax.php",
        type: "POST",
        data: "plugin_name=user_profile&get=loggedinbox",
        success: function(html) {
            alert(html);
            $("#user-area").html($(html).html());
        }
    });
}

PWUser.prototype.favoriteAdd = function() {
    $.ajax({
        url: "/ajax.php",
        type: "POST",
        data: "plugin_name=favorites_add&url=" + document.location + "&title=" + document.title,
        success: function(html) {
			switch(html){
				case "-1":
					alert("Musisz być zalogowany aby dodać do ulubionych");
					break;
				case "0":
					alert("Dodane do ulubionych");
					break;
				case "-2":
					alert("Wybrany element znajduje się już na liście ulubionych");
					break;
				case "-3":
					alert("Wystąpił nieoczekiwany błąd prosimy spróbować później");
					break;	
			}            
        }
    });
}

var user = new PWUser();

