$(document).ready(function() {
    /*$("#fakeloginstr").bind('focus', function() {
		$("#fakelogin").hide("fast");
		$("#truelogin").show("fast");
		$("#loginstr").focus();
    }).bind('blur', function() {
        if ($("#loginstr").attr('value') == '') {
			$("#fakelogin").show("fast");
			$("#truelogin").hide("fast");
		}
    });

	$("#fakepasswordstr").bind('focus', function() {
		$("#fakepass").hide("fast");
		$("#truepass").show("fast");
		$("#passwordstr").focus();
    }).bind('blur', function() {
        if ($("#passwordstr").attr('value') == '') {
			$("#fakepass").show("fast");
			$("#truepass").hide("fast");
		}
    });*/

    $("#loginstr").bind('focus', function() {
        if ($("#loginstr").attr('value') == 'Логин') {
            $("#loginstr").attr('value', '');
        }
    }).bind('blur', function() {
        if ($("#loginstr").attr('value') == '') {
            $("#loginstr").attr('value', 'Логин');
        }
    });

    $("#passwordstr").bind('focus', function() {
        if ($("#passwordstr").attr('value') == 'Пароль') {
            $("#passwordstr").attr('value', '');
        }
    }).bind('blur', function() {
        if ($("#passwordstr").attr('value') == '') {
            $("#passwordstr").attr('value', 'Пароль');
        }
    });

    $("#loginblock_link").bind('click', function() {
        $("#login_block").toggle("slow");
        $("#loginerror").hide("slow");
        return false;
    });

    $("#memberlogout").bind('click', function() {
        var loginPage = BaseName + "login/";
        $.ajax({
            type: "GET",
            url: loginPage,
            timeout: 3000,
            dataType: "xml",
            data: "mode=logout",
            success: function(xml) {
                $("#loginprogress").hide('slow');

                $(xml).find('response').each(function() {
                    if ($(this).text() == 1) {
                        $(xml).find('item').each(function() {
                            var name = '';
                            var value = '';
                            var expire = '';
                            var domain = '';
                            var path = '';
                            $(this).find('name').each(function() {
                                name = $(this).text();
                            });
                            $(this).find('value').each(function() {
                                value = $(this).text();
                            });
                            $(this).find('expire').each(function() {
                                expire = $(this).text();
                            });
                            $(this).find('domain').each(function() {
                                domain = $(this).text();
                            });
                            $(this).find('path').each(function() {
                                path = $(this).text();
                            });                           
                            
                            $.cookies.del(name);
                        });

                        $("#notlogedin").show('slow');
                    }
                    else if ( $(this).text() == 3 ) {
                        $("#loginerror_msg").html('Неверный пароль или имя пользователя');
                        $("#loginerror").show("slow");
                        $("#logedin").show("slow");
                    } else {
                        $("#loginerror_msg").html('Произошла ошибка связи в процессе<br />обработки запроса');
                        $("#loginerror").show("slow");
                        $("#logedin").show("slow");
                    }
                });
            },
            error: function() {
                $("#loginerror_msg").html('Произошла ошибка связи в процессе<br />обработки запроса');
                $("#loginprogress").hide('slow');
                $("#loginerror").show("slow");
                $("#logedin").show("slow");
            },
            beforeSend: function() {
                $("#logedin").hide('slow');
                $("#loginerror").hide("slow");
                $("#loginprogress").show('slow');
            }
        });
        return false;
    });


    $("#sendlogin").bind('click', function() {
        var loginPage = BaseName + "login/";
        var loginStr = $("#loginstr").attr('value');
        var passwordStr = $("#passwordstr").attr('value');
        var sID = $.cookies.get('session_id');
        var persistent = $("#persistent").attr('checked') ? 1 : 0;

        $.ajax({
            type: "POST",
            url: loginPage,
            dataType: "xml",
            timeout: 3000,
            data: "login=" + loginStr + "&password=" + passwordStr + "&sID=" + sID + "&persistent=" + persistent,
            success: function(xml) {
                $("#loginprogress").hide('slow');

                $(xml).find('response').each(function() {
                    if ($(this).text() == 1) {
                        $("#loginprogress").hide('slow');
                        $("#logedin").show('slow');

                        $(xml).find('username').each(function() {
                            $("#membername").html($(this).text());
                        });

                        $(xml).find('item').each(function() {
                            var name = '';
                            var value = '';
                            var expire = 0;
                            var domain = '';
                            var path = '';
                            $(this).find('name').each(function() {
                                name = $(this).text();
                            });
                            $(this).find('value').each(function() {
                                value = $(this).text();
                            });
                            $(this).find('expire').each(function() {
                                expire = $(this).text();
                            });
                            $(this).find('domain').each(function() {
                                domain = $(this).text();
                            });
                            $(this).find('path').each(function() {
                                path = $(this).text();
                            });
                            var newOptions = {
                                domain: domain,
                                path: path,
                                expiresAt: new Date( (expire * 1000) ),
                                secure: false
                            }
                            
                            //alert(name +': ' + value + ' expire: '+expire);

                            $.cookies.set(name, value, newOptions);
                        });
                    }
                    else if ($(this).text() == '3') {
                        $("#loginerror_msg").html('Неправильный пароль или имя<br />пользователя');
                        $("#loginerror").show("slow");
                        $("#notlogedin").show('slow');
                    }
                    else {
                        
                        $("#loginerror_msg").html('Произошла ошибка связи в процессе<br />входа');
                        $("#loginerror").show("slow");
                        $("#notlogedin").show('slow');
                    }
                });

            //                if (msg.indexOf('Success', 0) !== -1) {
            //                    $("#loginprogress").hide('slow');
            //                    $("#logedin").show('slow');
            //                    //$("#membername").attr('value', msg);
            //                    var re = new RegExp("^Success\s*(.+?)$");
            //                    $("#membername").html(re.exec(msg)[1]);
            //                }
            //                else if (msg.indexOf('Failed', 0) !== -1) {
            //                    $("#loginerror_msg").html('Имя пользователя или пароль<br />не верны');
            //                    $("#loginerror").show("slow");
            //                    $("#notlogedin").show('slow');
            //                }
            //                else {
            //                    $("#loginerror_msg").html('Произошла ошибка связи в процессе<br />входа');
            //                    $("#loginerror").show("slow");
            //                    $("#notlogedin").show('slow');
            //                }
            },
            error: function() {
                $("#loginerror_msg").html('Произошла ошибка связи в процессе<br />обработки запроса');
                $("#loginprogress").hide('slow');
                $("#loginerror").show("slow");
                $("#notlogedin").show('slow');
            },
            beforeSend: function() {
                $("#login_block").hide('slow');
                $("#notlogedin").hide('slow');
                $("#loginerror").hide("slow");
                $("#loginprogress").show('slow');
            }
        });

        return false;
    });

    if ( $("#passwordstr").attr('value') != '' ) {
        $("#fakelogin").hide("fast");
        $("#truelogin").show("fast");
        $("#fakepass").hide("fast");
        $("#truepass").show("fast");
    }
});
