var trans = new Array();
trans['pl'] = 'Wysy\u0142anie...';
trans['pz'] = 'Loading...';

function poll_send(button) {
    var id_tab = button.id.toString().split("_");
    var id = id_tab[1];

    var j = 'pz'
    if($('#lang').val() == 'pl') {
        j = 'pl'
    }

    $('#polls_content #info_' + id).removeClass().addClass('waiting').html('<p>' + trans[j] + '</p>').show();

    $.ajax({
        type : 'POST',
        url : 'modules/polls/ajax.php',
        dataType : 'html',
        data: {
                poll_id : $('#poll_id_' + id).val(),
                vote : $('input[name=vote_' + id + ']:checked').val(),
                lang : $('#poll_lang_' + id).val()
        },
        success: function(data){
            $('#polls_content').html(data);
            $('#polls_content #info_' + id).removeClass();
            $('.polls_send').click(function() {
               poll_send(this);
               return false;
            });
        },
        error : function(XMLHttpRequest, textStatus, errorThrown) {
            alert('Error with Ajax.');
        }
    });
    
}

$(document).ready(function(){
$('#skip_top').click(function() {
 $( 'html, body' ).animate( { scrollTop: 0 }, 'slow' );
 return false;
});
    $('.add_icon').click(function() {
       var icon = this.rel;
       $('#guestbook_comment').val($('#guestbook_comment').val() + icon);
       $('#guestbook_comment').focus();
       return false;
    });
    $('#go').click(function() {
        var j = 'pz'
        if($('#lang').val() == 'pl') {
            j = 'pl'
        }

        $('.newsletterBox #info').removeClass().addClass('waiting').html('<p>' + trans[j] + '</p>').show();

        $('.input input, .input textarea').blur(function(){
            $(this).removeClass();
        });

        $.ajax({
            type : 'POST',
            url : 'modules/newsletter/ajax.php',
            dataType : 'json',
            data: {
                    newsletter : $('#newsletter').val(),
                    lang: $('#lang').val()
            },
            success: function(data){
                var msg = '';
                var l = data.msg.length;
                var j = 0;
                for(var i = 0; i < l; i++) {
                    msg += '<p>' + data.msg[i].text + '</p>';
                    if(data.msg[i].pole !== false) {
                        $('.newsletterBox #' + data.msg[i].pole).addClass('input_error');
                        if(j == 0) {
                            $('.newsletterBox #' + data.msg[i].pole).focus();
                        }
                        j++;
                    }
                }
                $('.newsletterBox #info').removeClass().addClass((data.status === true) ? 'successA' : 'errorA').html(msg).show();
                if(data.status === true) {
                    $('#newsletter').val("");
                }

            },
            error : function(XMLHttpRequest, textStatus, errorThrown) {
                alert('Error with Ajax.');
            }
        });
        return false;
    });
    $('#contact_send').click(function() {
        var j = 'pz'
        if($('#lang').val() == 'pl') {
            j = 'pl'
        }

        $('.contactBox #info').removeClass().addClass('waiting').html('<p>' + trans[j] + '</p>').show();

        $('.input input, .input textarea').blur(function(){
            $(this).removeClass();
        });

        $.ajax({
            type : 'POST',
            url : 'modules/contact/ajax.php',
            dataType : 'json',
            data: {
                    contact_name : $('#contact_name').val(),
                    contact_title : $('#contact_title').val(),
                    contact_mail : $('#contact_mail').val(),
                    contact_text : $('#contact_text').val(),
                    contact_company : $('#contact_company').val(),
                    contact_boss : $('#contact_boss').val(),
                    contact_phone : $('#contact_phone').val(),
                    contact_city : $('#contact_city').val(),
                    lang: $('#contact_lang').val()
            },
            success: function(data){
                var msg = '';
                var l = data.msg.length;
                var j = 0;
                for(var i = 0; i < l; i++) {
                    msg += '<p>' + data.msg[i].text + '</p>';
                    if(data.msg[i].pole !== false) {
                        $('.contactBox #' + data.msg[i].pole).addClass('input_error');
                        if(j == 0) {
                            $('.contactBox #' + data.msg[i].pole).focus();
                        }
                        j++;
                    }
                }
                $('.contactBox #info').removeClass().addClass((data.status === true) ? 'successA' : 'errorA').html(msg).show();
                if(data.status === true) {
                    $('#contact_name').val("");
                    $('#contact_title').val("");
                    $('#contact_mail').val("");
                    $('#contact_text').val("");
                    $('#contact_company').val("");
                    $('#contact_boss').val("");
                    $('#contact_phone').val("");
                    $('#contact_city').val("");
                }

            },
            error : function(XMLHttpRequest, textStatus, errorThrown) {
                alert('Error with Ajax.');
            }
        });
        return false;
    });
    $('#guestbook_send').click(function() {
        var j = 'pz'
        if($('#lang').val() == 'pl') {
            j = 'pl'
        }

        $('.guestbookBox #info').removeClass().addClass('waiting').html('<p>' + trans[j] + '</p>').show();

        $('.input input, .input textarea').blur(function(){
            $(this).removeClass();
        });

        $.ajax({
            type : 'POST',
            url : 'modules/guestbook/ajax.php?a=1',
            dataType : 'json',
            data: {
                    comment : $('#guestbook_comment').val(),
                    guest : $('#guestbook_guest').val(),
                    lang: $('#guestbook_lang').val()
            },
            success: function(data){
                var msg = '';
                var l = data.msg.length;
                var j = 0;
                for(var i = 0; i < l; i++) {
                    msg += '<p>' + data.msg[i].text + '</p>';
                    if(data.msg[i].pole !== false) {
                        $('.guestbookBox #' + data.msg[i].pole).addClass('input_error');
                        if(j == 0) {
                            $('.guestbookBox #' + data.msg[i].pole).focus();
                        }
                        j++;
                    }
                    if(data.status === true && data.accept == 1) {
                        $.ajax({
                            type : 'POST',
                            url : 'modules/guestbook/ajax.php?a=2',
                            dataType : 'html',
                            data : {
                                lang: $('#guestbook_lang').val()
                            },
                            success: function(data) {
                                $('#guestbook').html(data);
                                $('html, body').scrollTop($('#guestbook').offset().top);
                                
                            }
                        });
                    }
                }
                $('.guestbookBox #info').removeClass().addClass((data.status === true) ? 'successA' : 'errorA').html(msg).show();
                if(data.status === true) {
                    $('#guestbook_comment').val("");
                    $('#guestbook_guest').val("");
                }

            },
            error : function(XMLHttpRequest, textStatus, errorThrown) {
                alert('Error with Ajax.');
            }
        });
        return false;
    });
    $('#comment_send').click(function() {
        var j = 'pz'
        if($('#lang').val() == 'pl') {
            j = 'pl'
        }
		j = 'pl';
        $('.commentBox #cmt_info').removeClass().addClass('waiting').html('<p>' + trans[j] + '</p>').show();

        $('.input input, .input textarea').blur(function(){
            $(this).removeClass();
        });
        var ident = $('#news_ident').val();
        var lang = $('#comment_lang').val();
        $.ajax({
            type : 'POST',
            url : 'modules/comments/ajax.php',
            dataType : 'json',
            data: {
                    name : $('#name').val(),
                    come : $('#come').val(),
                    code : $('#code').val(),
                    ident : $('#news_ident').val(),
                    lang: $('#comment_lang').val()
            },
            success: function(data){
                var msg = '';
                var l = data.msg.length;
                var j = 0;
                for(var i = 0; i < l; i++) {
                    msg += '<p>' + data.msg[i].text + '</p>';
                    if(data.msg[i].pole !== false) {
                        $('.commentBox #' + data.msg[i].pole).addClass('input_error');
                        if(j == 0) {
                            $('.commentBox #' + data.msg[i].pole).focus();
                        }
                        j++;
                    }
                }
                $('.commentBox #cmt_info').removeClass().addClass((data.status === true) ? 'successA' : 'errorA').html(msg).show();
                if(data.status === true) {
                    $('#name').val("");
                    $('#come').val("");
                    $('#code').val("");
                    $('#comments_h3').addClass('comments_h');
                    $.ajax({
                      type : 'POST',
                      url: 'modules/comments/get_posts.php',
                      dataType : 'html',
                      data: {
                            ident : ident,
                            lang: lang
                        },
                      success: function(data) {
                        $('#comments_list').html(data);
                      }
                    });
                }
		var timestamp = new Date().getTime();

                $('#token').attr('src','include/token.php?' + timestamp);


            },
            error : function(XMLHttpRequest, textStatus, errorThrown) {
                alert('Error with Ajax.');
            }
        });
        return false;
    });
    $('.polls_send').click(function() {
        poll_send(this);
        return false;
    });
    $('#recsend').click(function() {
        var j = 'pz'
        if($('#reclang').val() == 'pl') {
            j = 'pl'
        }

        $('.recommendBox #info').removeClass().addClass('waiting').html('<p>' + trans[j] + '</p>').show();
        $.ajax({
                type : 'POST',
                url : 'modules/recommend/ajax.php',
                dataType : 'json',
                data: {
                        mail : $('#friend_mail').val(),
                        name : $('#your_name').val(),
                        token : $('#token').val(),
                        lang : $('#reclang').val()
                },
                success : function(data){
                    var msg = '';
                    var l = data.msg.length;
                    var j = 0;
                    for(var i = 0; i < l; i++) {
                        msg += '<p>' + data.msg[i] + '</p>';
                    }
                    $('.recommendBox #info').removeClass().addClass((data.status === true) ? 'successA' : 'errorA').html(msg).show();
                    if(data.status === true) {
                        $('#friend_mail').val("");
                        $('#your_name').val("");
                    }
                },
                error : function(XMLHttpRequest, textStatus, errorThrown) {
                        $('#waiting').attr("style", "display:none");
                        $('.recommendBox #info').removeClass().addClass('errorA')
                                .html('There was an error.').show();
                }
        });

        return false;
    });
});


