// <![CDATA[
$(document).ready(function(){
    
    /*login*/
    $("a.login_a,.loginme").fancybox({
        'scrolling'		: 'no',
        'titleShow'		: false,
        'onClosed'		: function() {
            $("#login_error").hide();
           
        }
    });
    
    $("#login_form").bind("submit", function() {
    
        if ($("#login_name").val().length < 1 || $("#login_pass").val().length < 1) {
            $("#login_error").show();
            $.fancybox.resize();
            return false;
        }
        
        $.fancybox.showActivity();
        
        $.ajax({
            type		: "POST",
            cache		: false,
            url			: "../scripts/ajax/validate.login.php",
            data		: $(this).serializeArray(),
            success: function(xml) {

                $(xml).find('response').each(function(){
                    var type    = $(this).attr('type');
                    var message = $(this).find('message').text();
                    
                    if( type == 'error'){
                        $("#login_error").html(message);
                        $.fancybox.hideActivity(); 
                    }
                    else{
                        
                        var folder = $("#folder").val();
                        var stranica = $("#stranica").val();
                        
                        if( folder == 'main' && stranica == 'index'){
                            location.href = 'index.php';
                        }
                        else{
                            location.reload();
                        } 
                    }
                });
            }
        });
        
        return false;
    });
    /*logout*/
    $("#logout_a").fancybox({
                            'autoDimensions'	: false,
                            'width'         	: 430,
                            'height'        	: 'auto',
                            'onStart'           : function(){
                                $.ajax({
                                        type		: "POST",
                                        cache		: false,
                                        url			: "../scripts/ajax/logout.php",
                                        data		: $(this).serializeArray(),
                                        success: function(xml) {
                                
                                            $(xml).find('response').each(function(){
                                                var type    = $(this).attr('type');
                                                var message = $(this).find('message').text();
                                                
                                                if( type == 'error'){
                                                    $("#logout_div").html('<h1>' + message + '</h1>');
                                                }
                                            });
                                        }
                                    });
                            },
                            'onClosed'  : function(){
                                window.location = "http://www.otto.hr/hr";
                            }
                        });
    /*forgotpass*/
    $("a.forgotpass_a").fancybox({
        'scrolling'		: 'no',
        'titleShow'		: false,
        'onClosed'		: function() {
            $("#forgotpass_error").hide();
        }
    });
    
    $("#forgotpass_form").bind("submit", function() {
    
            if ($("#forgotpass_email").val().length < 1) {
                $("#forgotpass_error").show();
                $.fancybox.resize();
                return false;
            }
            
            $.fancybox.showActivity();
            
            $.ajax({
                type		: "POST",
                cache		: false,
                url			: "../scripts/ajax/forgotpass.php",
                data		: $(this).serializeArray(),
                success: function(xml) {

                    $(xml).find('response').each(function(){
                        var type    = $(this).attr('type');
                        var message = $(this).find('message').text();
                        
                        if( type == 'error'){
                            $("#forgotpass_error").html(message);
                            $.fancybox.hideActivity();
                        }
                        else{
                            $("#forgotpass_send").hide();
                            $("#forgotpass_info").hide();
                            $("#forgotpass_error").html(message);
                            $.fancybox.resize();
                            $.fancybox.hideActivity();
                        }
                    });
                }
        });
        
        return false;
    });
    
    /*registration*/
    $("a.registration_a, #registration_a2").fancybox({
        'autoScale'	:true,
        'onStart'	: function() {
         $("p.error").hide();
        }
    });
    
	

    $("form#registration_formtop").bind("submit", function() {
            
            $.fancybox.showActivity();
           
            $.ajax({
                type		: "POST",
                cache		: false,
                url			: "../scripts/ajax/registration.php",
                data		: $(this).serializeArray(),
                success: function(xml) {

                    $(xml).find('response').each(function(){
                        var type    = $(this).attr('type');
                        var message = $(this).find('message').text();
                        $(this).find('error_msg').each(function(){
                            message += '<br>- ' + $(this).text();
                        });
                        
                        if( type == 'error'){
                            $("p.error").html(message).show();
                     
                            $.fancybox.hideActivity();
                            $.fancybox.resize();
                        }
                        else{
                            $("p:first").hide();
                            $("p.error").hide();
                            $("#registration_data").hide();
                            
                            $("p.info").html(message).css("color", "black");                     
                            $.fancybox.resize();
                            $.fancybox.hideActivity();
                        }
                    });
                }
        });
        
        return false;
    });    
});
// ]]>
