/********************************/
/* www.eskowo.pl                */
/* author: Tomasz Nabrzeski     */
/* e-mail: tnabrzeski@gmail.com */
/* year:   2009                 */
/********************************/
function sendMessage()
{
    $('#submit').blur();
    $('#submit').attr('disabled', 'disabled');
    $('#submit').addClass('disabled');
    $('#top').append('<div id="waiting">Wysyłanie wiadomości...</div>');
    $('#waiting').css('opacity', 0);
    $('#waiting').animate({'opacity':0.7});

    $.ajax({
        type: "POST",
        url: "sms.php?task=send",
        data: $('#sms').serialize(),
        dataType: "json",
        success: function(response){
            if (response.error_msg) {
                $('#waiting').html(response.error_msg);
                setTimeout(hideMessage, 2000);
                text = $('#captcha').html();
                $('#captcha').html('');
                $('#captcha').html(text);
            } else {
                if (response.result == 1) {
                    $('#phone').val('');
                    $('#message').val('');
                    $('#captcha').html('');
                    $('#phone').css('background-image', 'none');
                    
                    $('#waiting').html('Wiadomość została wysłana.');
                    setTimeout(hideMessage, 2000);
                }
            }
        },
        error: function(data){ alert(data.responseText); }
    });
    
    return false;
}

function hideMessage()
{
    $('#waiting').animate({'opacity':0}, 500, "swing", function(){ $('#waiting').remove(); $('#submit').attr('disabled', ''); $('#submit').removeClass('disabled'); });
}

function getOpsos()
{
    phone = $('#phone').val();
    
    $('#captcha').html('');
    $('#phone').css('background-image', 'none');
    $('#opsos').val('');
    
    if (phone.length < 9) return;
    
    $('#phone').css('background', 'transparent url(images/loading.gif) no-repeat 127px center');
    $('#submit').attr('disabled', 'disabled');
    $('#submit').addClass('disabled');

    $.ajax({
        type: "POST",
        url: "sms.php?task=operator",
        data: "phone=+" + phone,
        dataType: "json",
        success: function(response){
            $('#submit').attr('disabled','');
            $('#submit').removeClass('disabled');
            $('#opsos').val(response.operator);
            $('#phone').css('background-image', 'none');

            if (response.operator) {
                $('#phone').css('background', 'transparent url(images/gsm-'+response.operator+'.gif) no-repeat 127px center');

                if (response.captcha) {
                    $('#captcha').html('<img src="captcha.php?s='+response.captcha+'" /><input type="hidden" name="session_id" value="'+response.captcha+'" /> <input type="text" name="captcha" />');
                }
            }
        },
        error: function(data){ alert(data.responseText); }
    });
}

function getStats()
{
    $.ajax({
        type: "POST",
        url: "stats.php",
        data: "eskowo=1",
        dataType: "json",
        success: function(response){
            $('#online').html(response.online);
            $('#today').html(response.today);
        }
    });
    
    setTimeout(getStats, 60000);
}

function insertMessage(sms)
{
    $('#message').val($(sms).next().text());
    $('html,body').animate({'scrollTop':0}); 
}

$(document).ready(function(){
    $('#phone').keyup(getOpsos);
    $('#sms').submit(sendMessage);
    $('#content p').before('<a href="javascript:void(0)" onclick="insertMessage(this)"><img src="images/bg-sms-btn.jpg" alt="" /></a>');
    $('a[rel*=ext]').attr('target', '_blank');
    getStats();
});