$(document).ready(function() {

	$('.blockOverlay').attr('title','Click to unblock').click($.unblockUI);
	
    if (document.images)
    {
      preload_image = new Image(138,14);
      preload_image.src="images/default/top_rzecz_bg_2.gif";

      preload_image2 = new Image(138,1);
      preload_image2.src="images/default/top_rzecz_bg_2a.gif";
    }

    $("#my_stuff_trigger").mousedown(function(){
        $("#my_stuff").toggle();

    });

    $("#my_stuff_trigger").parent().hover(
      function () {

      },
      function () {
        $("#my_stuff").hide();
      }
    );

    $(".close").mousedown(function () {
        $.unblockUI({});
    });

    $(".close_2").mousedown(function () {
        $.unblockUI({});
    });

    $("span.passwd").mousedown(function () {
         $.blockUI({
            message:  $("#passwd_form"),
            css: {
            },

            baseZ: 1000,
            centerX: true, // <-- only effects element blocking (page block controlled via css above)
            centerY: true,
            allowBodyStretch: true,
            constrainTabKey: true,
            fadeOut:  400,
            applyPlatformOpacityRules: true
        });
    });
    
    $("#remindpasswd_form").validate( {
		rules : {
			email : {
				email : true,
				required : true
				}
			},
			messages : {
				email : "Wpisz poprawny adres e-mail"
				}
	});

	$("#remindpasswd_form").submit(function(){
    	$("#remindpasswd_form").attr('action', Service.getSecureBaseUrl() + 'default/auth/passwd-remind-popover');
    	if(!$("#remindpasswd_form-currenturl").length){
			$('<input type="hidden" id="remindpasswd_form-currenturl" name="currenturl" />').appendTo("#remindpasswd_form");
		}
		$("#remindpasswd_form-currenturl").val(document.location.href);
		if($("#remindpasswd_form").valid()){
			return true;
		}
    	return false;
    });

    
	if (!_ni_cart) {

    	$(".top-secret").click(function() {
         $.blockUI({
            message:  $("#login_form"),
            css: {
            },

            baseZ: 1000,
            centerX: true, // <-- only effects element blocking (page block controlled via css above)
            centerY: true,
            allowBodyStretch: true,
            constrainTabKey: true,
            fadeOut:  400,
            applyPlatformOpacityRules: true
        });

        var url = $(this).attr("href");

    	$("#login-form").validate({
    		rules: {
    			email: {
    				email: true,
    				required: true
    				}
    		},
    		messages: {
    			email: "Wpisz poprawny adres e-mail"
    		}
    	});
    	
    	if ($.cookie('loginModalRegError')){
			$('#login-email').val($.cookie('loginModalRegError'));
			$('#action-new').attr('checked', 'checked');
			$('<label class="error" for="login-email">Rekord o podanej nazwie już istnieje w systemie</label>').insertAfter('#login-email');
			$('#login-email').click(
				function() {$(this).next().remove()}
			);
			$.cookie('loginModalRegError', '', {path: '/'});
		}
        
        function togglePasswd(){
        	var action = $("#action-login").attr('checked');
        	if (action == true){
        		$('.passwd_input').show();
        		$('#create-account-passwd').hide();
        		
    			$('#client-passwd_new_1').rules('remove');
    			$('#client-passwd_new_2').rules('remove');
    			$('#login-passwd').rules('add', {
    				required: true,
    				messages: {required: 'Pole wymagane'}
    			});
        		
        	} else {
        		$('.passwd_input').hide();
        		$('#create-account-passwd').show();
        		
        		$('#login-passwd').rules('remove');
        		$('#client-passwd_new_1').rules('add', {
    				required: true,
    				messages: {required: 'Proszę wpisać hasło.'}
    			});
        		$('#client-passwd_new_2').rules('add', {
        			equalTo: "#client-passwd_new_1",
    				messages: {equalTo: 'Hasło nie jest identyczne.'}
    			});
        		
        	}
        }
        
        togglePasswd();
        
        $('#login_form :radio').click(function(){
        	togglePasswd();
        });
        
        
        $("#login-form").submit(function() {
        		
        		if(!$("#login-form-targeturl").length){
					$('<input type="hidden" id="login-form-targeturl" name="targeturl" />').appendTo("#login-form");
				}
				$("#login-form-targeturl").val(url);
				
				if(!$("#login-form-currenturl").length){
					$('<input type="hidden" id="login-form-currenturl" name="currenturl" />').appendTo("#login-form");
				}
				$("#login-form-currenturl").val(document.location.href
				
				if($("#login-form").valid()){
					var login = $("#action-login").attr('checked');
					if(login){
						$("#login-form").attr('action', Service.getSecureBaseUrl() + 'default/auth/login-popover');
					} else {
						$("#login-form").attr('action', Service.getSecureBaseUrl() + 'default/auth/register-popover');
					}
					return true;
				} else {
					return false;	
				}
				
        });
		
    });
}

});


