﻿/// <reference path="jquery-1.2.6-vsdoc.js" />
var colors = ['#00fffc', '#ff00de', '#ff0000', '#b400ff', '#30ff00', '#fffc00', '#00ccff'];
var error = "Не звучит";
var send = "Мы вам обязательно что-нибудь напишем"
var i;
var prevC;
var errorVisible = false;
function GetColor() {
    var x = parseInt(Math.random() * 7);
    while (prevC == x) {
        x = parseInt(Math.random() * 7);
    }
    prevC = x;
    return x;
}
function SetColor() {
    var color = colors[GetColor()];
    $('#logo').animate({ backgroundColor: color }, 1500);
}
function processInput(event, obj) {
    if (event.which == 13) {
        AddEmail();
        return false;
    };
    if (errorVisible) {
        $('#message').animate({ marginTop: '-36px' });
        errorVisible = false;
    }
    return true;
}
function HideAll() {
    $('#message').animate({ marginTop: '-36px' }, function() {
        $('#message').text('');
        $('#emailInput').fadeOut(function() {
            $('#email').val('');
            $('#text').fadeIn();
            errorVisible = false;
            $('#email').css('color', 'Black')
        });
    });
}
function AddEmail() {
    var mail = $('#email').val();
    if (ValidEmail(mail)) {
        $.post("../handlers/email.ashx",
        {
            email: mail
        },
        function(data) {
            if (data == "OK") {
                $('#email').css('color','Gray').blur();
                $('#message').text(send).css('color', 'Green').animate({ marginTop: '10px' }, function() {
                    $('#message').animate({ marginTop: '2px' });
                    errorVisible = true;
                });
                setTimeout('HideAll()', 4000);
            }
        });

    } else {
        $('#message').text(error).css('color', 'Red').animate({ marginTop: '10px' }, function() {
            $('#message').animate({ marginTop: '2px' });
            errorVisible = true;
        });
    }

}
function ValidEmail(email) {
    var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
    return reg.test(email)
}
$(document).ready(function() {
    var color = colors[GetColor()];
    $('#logo').css({ backgroundColor: color });
    i = setInterval('SetColor()', 3000);
    $('#logo').click(function() {
        if ($('#text:visible').length) {
            $('#text').fadeOut('normal', function() {
                $('#emailInput').fadeIn();
                $('#email').select().focus();
            });
        }
        else {
            $('#emailInput').fadeOut('normal', function() {
                $('#text').fadeIn();
            });
        }
    });
    $('#email').alphanumeric({ allow: ".@" });
    $('#email').keypress(function(e) { return processInput(e, this); });
});


