$().ready(function(){
    var isIE6 = $.browser.msie && parseFloat($.browser.version) < 7;

    $(".loginModal").fancybox({
        'titlePosition'		: 'inside',
        'transitionIn'		: 'none',
        'transitionOut'		: 'none',
        centerOnScroll:true,
        onClosed:function() {
            $('#msg').remove();
        }
    });

    $(".smsModal").fancybox({
        'titlePosition'		: 'inside',
        'transitionIn'		: 'none',
        'transitionOut'		: 'none',
        'centerOnScroll'        : 'true'
    });

    $('#submitSmsCode').click(function(){
        var smsCode = $('#smsCode').val();
        if(smsCode == '' || smsCode.length != 8) {
            $('#smsCode').attr('value', '');
            alert('Kod SMS składa się z 8 znaków');
            return false;
        }
        var host = location.host;
        var path = location.pathname;
        var postURL = "http://"+host+"/sms/";
        var loc = "http://"+host+path;
        $.ajax({
            cache:false,
            async:false,
            type: "POST",
            data: 'smsCode='+smsCode,
            url: postURL,
            success: function(msg) {
                if(msg == 1) {
                    location.href=loc;
                    return false;
                }else if(msg == 0) {
                    $('#smsCode').attr('value', '');
                    alert('Błędny kod SMS!');
                    return false;
                }else if(msg == 9) {
                    alert('Błąd bazy danych. Spróbuj pózniej!');
                    return false;
                }
                return false;
            }
        });

        return false;
    });

    $('#submitLogin').live('click',function(){
        var host = location.host;
        var postURL ="http://"+host+"/login/";
        var login = $('#username').val();
        var remember = $('#login input:checkbox:checked').val();
        var password = $('#password').val();
        $.ajax({
            cache:false,
            async:false,
            type: "POST",
            data: 'login='+login+'&password='+password+'&remember='+remember,
            url: postURL,
            success: function(msg) {
                if(msg == 'true') {
                    var postURL ="http://"+host+"/katalog/";
                    var url = "http://"+location.host+location.pathname;
                    document.location.href=url;
                } else if(msg == 'false') {
                    alert('Nieprawidłowy adres e-mail lub hasło.');
                }else {
                    alert('Wpisz swój adres e-mail i hasło.');
                }
            }
        });
        return false;
    });

    $('#register').live('click',function(){
        var host = location.host;
        var postURL ="http://"+host+"/rejestracja/";
        if(isIE6 == true) {
            return true;
        }
        $.ajax({
            cache:false,
            async:false,
            type: "POST",
            url: postURL,
            success: function(msg) {
                $('#logowanie #left').empty();
                $('#logowanie #left').append(msg);
            }
        });
        return false;
    });

    $('#submitRejestracja').live('click',function(){
        var email = $('#username').val();
        var password = $('#password').val();
        var password2 = $('#password2').val();
        var captcha = $('#captchaVal').val();
        var regulamin = $('input:checkbox:checked').val();

        if(captcha == '' || captcha.length != 5) {
            $('#captchaVal').attr('value', '');
            alert('Przepisz kod z obrazka!');
            return false;
        }

        if(regulamin != 'on') {
            alert('musisz zaakceptować regulamin');
            return false;
        }

        if(email == '' || password == '' || password2 == '') {
            alert('Wypełnij wszystkie pola');
            return false;
        }

        if(password.length < 4) {
            alert('Hasło musi się składać minimum z 4 znaków!');
            return false;
        }

        if(password != password2) {
            alert('Hasła muszą być identyczne');
            return false;
        }

        if(email_validate(email) == false) {
            alert('Wpisz poprawny adres e-mail!');
            return false;
        }

        var host = location.host;
        var postURL ="http://"+host+"/rejestracja/";

        $.ajax({
            cache:false,
            async:false,
            type: "POST",
            data: 'email='+email+'&password='+password+'&password2='+password2+'&captcha='+captcha,
            url: postURL,
            success: function(msg) {
                if(msg == 'Taki adres już istnieje w naszej bazie danych!') {
                    $('#username').attr('value', '');
                    alert(msg);
                }else if(msg == 'Konto zostało utworzone') {
                    alert(msg);
                    $('.loginModal').trigger('click');
                }else if(msg == 'Przepisz poprawnie kod z obrazka!') {
                    $('#captchaVal').attr('value', '');
                    alert(msg);
                }else {
                    alert(msg);
                }
            }
        });
        return false;
    });

   

    var animeInt;
    clearInterval(animeInt);
    
    if($('#time').html() != '') {
        animeInt = setInterval('liczCzas()', 1000);
    }

});
function email_validate(src)
{
    var regex = /^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+\.)+[a-zA-Z0-9.-]{2,4}$/;
    return regex.test(src);
}

function liczCzas(){
    var time = $('#time').html();
    if(time != null) {
        time = time.split(":");
        h = time[0];
        i = time[1];
        s = time[2];
        if(s != 0) {
            if(s < 11){
                s--;
                s = '0'+s;
            } else {
                s--;
            }
        } else {
            s = 59;
            if(i != 0) {
                if(i < 11) {
                    i--;
                    i = '0' +i;
                } else {
                    i--;
                }
            } else {
                h--;
                i = 59;
            }
        }
        $('#time').html(h+':'+i+':'+s);
        if(h == 0 && i == 0 && s == 0) {
            location.reload();
        }
    }
}
