function PWUser () {
    this.firends = false;
    this.users = new Object();
}
PWUser.prototype.loginRegUser = function(redirectUrl) {
    var ref = this;
	
	if(!redirectUrl) {
		redirectUrl = "http://www.polskawita.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/img/ajax-loader.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() {
    var ref = this;
    
    $.ajax({
        url: "/ajax.php",
        type: "POST",
        data: $("#form_login_user").serialize() + "&plugin_name=user_profile",
        beforeSend: function() {
            $('<div style="text-align: right;"><img src="/_items/img/ajax-loader.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();

                var loc = window.location.toString();

                if(loc.indexOf("51087") != -1) {
                    //window.location.href = "http://www.polskawita.pl/strona-glowna.html";
                }
                else {
                    //window.location.reload();
                }
            }
            else {
                $("#login_form_error").remove();
                $("#form_login_user").before('<span id="login_form_error" style="color: red;">Niepoprawny login lub hasło</span>');
            }
        }
    });
    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=top_bar",
        success: function(html) {
            $("#top").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();




$(document).ready(function() {
    $("#user_panels .row1 .cell2 .links a").click(function() {
        $("#user_panels .row1 .cell2 .links a").removeClass("active");
        $(this).addClass("active");
        
        var stype = "";
        
        if($(this).hasClass("last")) {
            stype = "last";
        }
        else if($(this).hasClass("popular")) {
            stype = "popular";
        }

        user.getUsers(stype);
        return false;
    });
	
    $("#favorites_add a img").click(function() {
        $("#user_panels .row1 .cell2 .links a").removeClass("active");
		user.favoriteAdd();
        return false;
    });	
	
});

