jQuery(function ($) {
    contact = {
        message: null,
        init: function () {
                $.get("/user/show", function(data){
                    $(data).modal({
                        closeHTML: "<h3>Anmelden <a href='#' title='Close' class='modal-close ui-icon ui-icon-closethick'>x</a></h3>",
                        position: ["20%",],
                        overlayId: 'login-overlay',
                        containerId: 'loginForm',
                        onOpen: contact.open,
                        onShow: contact.show,
                        onClose: contact.close
                    });
                });
          
        },
       open: function (dialog) {
            var h = 550;
			
            dialog.overlay.fadeIn(200, function () {
                dialog.container.fadeIn(200, function () {
                    dialog.data.fadeIn(200, function () {
                        $('#loginBox .contact-content').animate({
                            height: h
                        }, function () {
                            $('#loginBox').fadeIn(200, function () {

                            });
                        });
                    });
                });
            });
        },
        show: function (dialog) {
            $('#loginForm .submitLogin').click(function (e) {
                e.preventDefault();
                // validate form
                if (contact.validate()) {
                    var password = $("input#password").val();
                    if (password == "") {
                        $("label#name_error").show();
                        $("input#name").focus();
                        return false;
                    }
                    var email = $("input#email").val();
                    var redirect = $("input#redirect").val();
                    if (email == "") {
                        $("label#email_error").show();
                        $("input#email").focus();
                        return false;
                    }
                    var dataString = 'password='+ password + '&email=' + email;
                    $('#loginForm .highlight').remove();
                    $.ajax({
                        type: "POST",
                        url: "/user/login",
                        data: dataString,
                        dataType: "json",
                        success: function(data, textStatus, XMLHttpRequest) {
                            if(data == true) {
                                $('#loginForm .contact-message').fadeOut();
                                $('.password').remove;
                                $('#loginForm form').fadeOut(200);
                                $('#loginForm .simplemodal-wrap').animate({
                                    height: 40
                                }, function () {
                                    dialog.data.fadeOut(200, function () {
                                            dialog.overlay.fadeOut(200, function () {
                                            	 dialog.container.remove();;
                                                $.modal.close();
                                            });
                                    });
                                });
                                $('#notAuthorized').remove();
                                
                                var authorized = '<span id="authorized"><span><a href="/user/account">Mein Konto</a></span>  | <span><a href="/user/logout">Abmelden</a></span></span>';
                                $('#topnav').prepend(authorized);
                                location.href=redirect;
                            }else{
                    	   		$('#error_wrap').remove();
                                $('#loginForm').slideDown("fast");
                                $("#signUpForm").prepend('<div id="error_wrap"><span class="highlight">&nbsp;'+data+'</span><br></div>')
                            }
                        }
                    });
                    return false;
                }
                else {
                    if ($('#loginForm .contact-message:visible').length > 0) {
                        var msg = $('#loginForm .contact-message div');
                        msg.fadeOut(200, function () {
                            msg.empty();
                            contact.showError();
                            msg.fadeIn(200);
                        });
                    }
                    else {
                        $('#loginForm .contact-message').animate({
                            height: '30px'
                        }, contact.showError);
                    }
                }
            });
        },
        close: function (dialog) {
        	 $('#loginForm .contact-message').fadeOut();
             $('.password').remove;
             $('#loginForm form').fadeOut(200);
             $('#loginForm .simplemodal-wrap').animate({
                 height: 40
             }, function () {
                         dialog.container.remove();
                         $.modal.close();
                 
             });
        },
        error: function (xhr) {
            alert(xhr.statusText);
        },
        validate: function () {
            return true;
        },
        showError: function () {
            $('#loginForm .contact-message')
            .html($('<div class="contact-error"></div>').append(contact.message))
            .fadeIn(200);
        }
    };
    $('#topnav input.contact, #topnav a.login').click(function (e) {
        contact.init();
    });
});
var ie6 = $.browser.msie && parseInt($.browser.version) == 6 && typeof window['XMLHttpRequest'] != "object";
if(ie6){
	$("#ie6").show();
}