$(document).ready(

    function() {

        if (!$('body').hasClass("home")) {
            $('body').scrollTo(142, { axis:'y', duration:500 } );
        }

        $('.list').children('li').each(
            function(i) {
                $(this).children('.sub').attr("origHeight", $(this).children('.sub').outerHeight(true));
                $(this).children('.sub').hide();
                $(this).children('.sub').height(0);

                $(this).mouseenter (
                    function () {
                        $(this).addClass('select');

                        $(this).children('.sub').animate(
                            {
                                height: $(this).children('.sub').attr("origHeight")
                            },
                            {
                                duration: 500,
                                complete: function()
                                {
                                    $(this).show();
                                }
                            });
                    }
                );
                $(this).mouseleave (
                    function () {
                        //$(this).
                        //$(this).removeClass('select');
                        $(this).removeClass('select');

                        $(this).children('.sub').stop().animate(
                            {
                                height: "0px"
                            },
                            {
                                duration: 500,
                                complete: function()
                                {
                                    $(this).hide()
                                }
                            });

                    }
                );

            }
        );
        $('#news-list').children('.position').each(
            function (i) {
                $(this).mouseenter (
                    function () {
                        $(this).addClass('select');
                    }
                );
                $(this).mouseleave (
                    function () {
                        $(this).removeClass('select');
                    }
                );
                $(this).click (
                    function () {
                        location.href = $(this).children('.prev').children('.more').children('a').attr('href');
                    }
                );
            }
        );
        /*
        $('.news').children('.list').children('.position').each(
            function (i) {
                $(this).mouseenter (
                    function () {
                        $(this).addClass('select');
                    }
                );
                $(this).mouseleave (
                    function () {
                        $(this).removeClass('select');
                    }
                );
                $(this).click (
                    function () {
                        location.href = $(this).children('.prev').children('.more').children('a').attr('href');
                    }
                );
            }
        );
        */
        $('#news-poll').children('.question').each(
            function (i) {
                $(this).mouseenter (
                    function () {
                        $(this).addClass('hover');
                    }
                );
                $(this).mouseleave (
                    function () {
                        $(this).removeClass('hover');
                    }
                );
            }
        )

        $('#news-poll-form').submit (
            function() {
                var validForm = function (formData, jqForm, options) {
                    var count_a = 0;
                    var count_t = 0;

                    for (var i=0; i < formData.length; i++) {
                        if (formData[i].name.search('qtest') != -1) {
                            count_t++;
                        }
                        if ((formData[i].name.search('qans') != -1) && (formData[i].value.trim())) {
                            count_a++;
                        }
                    }
                    
                    if (count_a != count_t) {
                        $('#news-poll-msg').html('Prosze uzupełnić brakujące odpowiedzi!');
                        $('#news-poll-msg').show();
                        return false;
                    } else {                        
                        $('#news-poll-msg').hide();
                        $('#news-poll-msg').html('');
                        return true;
                    }

                    
                }

                var callBackFunc = function(data, textStatus) {
                    if (textStatus == 'success') {
                        if (data.search('error') != -1) {
                            //
                            /*alert('błąd 1');*/
                        } else {
                            $('#news-poll').html(data);
                        }
                    } else {
                        //
                        /*alert('błąd 2');*/
                    }
                }

                var submitOptions = {
                    beforeSubmit:  validForm,  // pre-submit callback
                    success:       callBackFunc,  // post-submit callback
                    clearForm: true               // clear all form fields after successful submit
                };

                $(this).ajaxSubmit(submitOptions);

                return false;
            }
        )

        $('.title-l').click (
            function (i) {
                $(this).addClass('select-l');
                $('.title-r').removeClass('select-r');
                $('.quotation').hide();
                $('.currency').show();
                $(this).blur();
                return false;
            }
        );
        $('.title-r').click (
            function (i) {
                $(this).addClass('select-r');
                $('.title-l').removeClass('select-l');
                $('.currency').hide();
                $('.quotation').show();
                $(this).blur();
                return false;
            }
        );

        $('.radio_ans').each(
            function (i) {
                $(this).click (
                    function () {
                        $('#poll-form').submit();
                    }
                );
            }
        );

        $('#poll-form').submit (
            function() {
                var startSending = function () {
                    return true;
                }

                var callBackFunc = function(data, textStatus) {
                    if (textStatus == 'success') {
                        if (data.search('error') != -1) {
                            //
                            /*alert('błąd 1');*/
                        } else {
                            $('#answers').html(data);
                        }
                    } else {
                        //
                        /*alert('błąd 2');*/
                    }
                }

                var submitOptions = {
                    beforeSubmit:  startSending,  // pre-submit callback
                    success:       callBackFunc,  // post-submit callback
                    clearForm: true               // clear all form fields after successful submit
                };

                $(this).ajaxSubmit(submitOptions);

                return false;
            }
        )

        $('.news').children('.list').children('.position').each(
            function (i) {
                $(this).mouseenter (
                    function () {
                        $(this).addClass('select');
                    }
                );
                $(this).mouseleave (
                    function () {
                        $(this).removeClass('select');
                    }
                );
                $(this).click (
                    function () {
                    //<div class="title-main"><div class="title-bar"><div class="title-txt">
                        location.href = $(this).children('.title-main').children('.title-bar').children('.title-txt').children('a').attr('href');
                    }
                );
            }
        );

        $('.offer').children('.private').each(
            function (i) {
                $(this).mouseenter (
                    function () {
                        $(this).addClass('select');
                    }
                );
                $(this).mouseleave (
                    function () {
                        $(this).removeClass('select');
                    }
                );
                $(this).click (
                    function () {
                        location.href = $(this).children('.more').children('a').attr('href');
                    }
                );
            }
        );

         $('.page').children('.private').each(
            function (i) {
                $(this).children('.position').each(
                function (i) {
                    $(this).mouseenter (
                        function () {
                            $(this).addClass('select');
                        }
                    );
                    $(this).mouseleave (
                        function () {
                            $(this).removeClass('select');
                        }
                    );
                    $(this).click (
                        function () {
                            location.href = $(this).children('.more').children('a').attr('href');
                        }
                    );
                    }
                )
            }
        );

        $('.prod-other').children('.position').each(
            function (i) {
                $(this).mouseenter (
                    function () {
                        $(this).addClass('select');
                    }
                );
                $(this).mouseleave (
                    function () {
                        $(this).removeClass('select');
                    }
                );
                $(this).click (
                    function () {
                        location.href = $(this).children('.prev').children('.more').children('a').attr('href');
                    }
                );
            }
        );




        $('.top').each (
            function (i) {
                $(this).click (
                    function () {
                        $('body').scrollTo(1, { axis:'y', duration:500 } );
                        return false;
                    }
                )
            }
        )

        $('.print').each (
            function (i) {
                $(this).click (
                    function () {
                        print();
                        return false;
                    }
                )
            }
        )
        $('.bookmark').jFav();

        $('.sendto').click (
            function (i) {
                var wHeight = $(document).height();
                var wWidth  = $(document).width();

                $('#send-msg').html('');

                $('#siteURL').val(location.href);
                $('#siteTitle').val($('title').html());
                //alert ($('title').html());

                $('#send-mask').css({'width':'' + wWidth + 'px', 'height': '' + wHeight + 'px'});
                $('#send-link').css({'width':'' + wWidth + 'px', 'height': '' + wHeight + 'px'});

                $('body').scrollTo(1, { axis:'y', duration:500 } );
                $('#send-mask').show();
                $('#send-link').show();

                return false;
            }
        )

        $('#query-btn').click (
            function (i) {
                var wHeight = $(document).height();
                var wWidth  = $(document).width();

                $('#query-msg').html('');

                $('#siteURL2').val(location.href);
                $('#siteTitle2').val($('title').text());

                $('#send-mask').css({'width':'' + wWidth + 'px', 'height': '' + wHeight + 'px'});
                $('#send-query').css({'width':'' + wWidth + 'px', 'height': '' + wHeight + 'px'});

                $('body').scrollTo(1, { axis:'y', duration:500 } );
                $('#send-mask').show();
                $('#send-query').show();

                return false;
            }

        )
        $('#send-cancel').click (
            function (i) {
                $('#send-msg').hide();
                $('#send-mask').hide();
                $('#send-link').hide();

                $('#send-link-form').resetForm();

                return false;
            }
        )

        $('#send-link-form').submit (
            function() {

                var validForm = function () {
                    var msg  = '';
                    var send = true;
                    if ($('#y-name').val() == '') {
                        msg  += 'Uzupełnij pole imię!<br />';
                        send = false;
                    }
                    if ($('#y-mail').val().search(/[_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3}/i) == -1 ) {
                        msg  += 'Wprowadź poprawny adres e-mail nadawcy!<br />';
                        send = false;
                    }
                    if ($('#f-mail').val().search(/[_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3}/i) == -1 ) {
                        msg  += 'Wprowadź poprawny adres e-mail odbiorcy!<br />';
                        send = false;
                    }
                    if (!send) {
                        $('#send-msg').html(msg);
                        $('#send-msg').show();
                    }
                    return send;
                }


                var startSending = function () {
                    var send = validForm();
                    if (!send) {
                        return false;
                    } else {
                        return true;
                    }
                }

                var backToPage = function () {
                    $('#send-msg').hide();
                    $('#send-mask').hide();
                    $('#send-link').hide();
                    $('#send-link-form').resetForm();
                }

                var callBackFunc = function(data, textStatus) {
                    if (textStatus == 'success') {
                        if (data.search('error') != -1) {
                            
                            $('#send-msg').html('Wysyłanie nie powiodło się! Spróbuj ponownie za chwilę.\n' );
                            $('#send-msg').show();
                            window.setTimeout(backToPage, 5000);
                        } else {
                            $('#send-msg').html('Wiadomość została wysłana! Dziękujemy za zainteresowanie.');
                            $('#send-msg').show();
                            window.setTimeout(backToPage, 5000);
                        }
                    } else {
                        $('#send-msg').html('Wysyłanie nie powiodło się! Spróbuj ponownie za chwilę.');
                        $('#send-msg').show();
                        window.setTimeout(backToPage, 5000);
                    }
                }

                var submitOptions = {
                    beforeSubmit:  startSending,  // pre-submit callback
                    success:       callBackFunc,  // post-submit callback
                    clearForm: true               // clear all form fields after successful submit
                };

                $(this).ajaxSubmit(submitOptions);

                return false;
            }
        )

        $('#query-cancel').click (
            function (i) {
                $('#query-msg').hide();
                $('#send-mask').hide();
                $('#send-query').hide();

                $('#send-query-form').resetForm();

                return false;
            }
        )

        $('#send-query-form').submit (
            function() {

                var validForm = function () {
                    var msg  = '';
                    var send = true;
                    if ($('#name2').val() == '') {
                        msg  += 'Uzupełnij pole imię!<br />';
                        send = false;
                    }
                    if ($('#surname2').val() == ''  ) {
                        msg  += 'Uzupełnij pole nazwisko!<br />';
                        send = false;
                    }
                    if ($('#email2').val().search(/[_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3}/i) == -1 ) {
                        msg  += 'Wprowadź poprawny adres e-mail!<br />';
                        send = false;
                    }
                    if ($('#mobile2').val() == ''  ) {
                        msg  += 'Uzupełnij pole telefon!<br />';
                        send = false;
                    }
                    if ($('#mesage2').val() == ''  ) {
                        msg  += 'Wprowadź treść wiadomości!<br />';
                        send = false;
                    }
                    if ($('#confirm2:checked').val() == null ) {
                        msg  += 'Musisz zaakceptować warunki!';
                        send = false;
                    }
                    if (!send) {
                        $('#query-msg').html(msg);
                        $('#query-msg').show();
                    }
                    return send;
                }


                var startSending = function () {
                    var send = validForm();
                    if (!send) {
                        return false;
                    } else {
                        return true;
                    }
                }

                var backToPage = function () {
                    $('#query-msg').hide();
                    $('#send-mask').hide();
                    $('#send-query').hide();
                    $('#send-query-form').resetForm();
                }

                var callBackFunc = function(data, textStatus) {
                    if (textStatus == 'success') {
                        if (data.search('error') != -1) {
                            $('#query-msg').html('Wysyłanie nie powiodło się! Spróbuj ponownie za chwilę.');
                            $('#query-msg').show();
                            window.setTimeout(backToPage, 5000);
                        } else {
                            $('#query-msg').html('Wiadomość została wysłana! Dziękujemy za zainteresowanie.');
                            $('#query-msg').show();
                            window.setTimeout(backToPage, 5000);
                        }
                    } else {
                        $('#query-msg').html('Wysyłanie nie powiodło się! Spróbuj ponownie za chwilę.');
                        $('#query-msg').show();
                        window.setTimeout(backToPage, 5000);
                    }
                }

                var submitOptions = {
                    beforeSubmit:  startSending,  // pre-submit callback
                    success:       callBackFunc,  // post-submit callback
                    clearForm: true               // clear all form fields after successful submit
                };

                $(this).ajaxSubmit(submitOptions);

                return false;
            }
        )
    }
);

function check_search () {
    send = true;
    if
           ($('#q').val() == 'wpisz szukaną frazę') {
//           ||
//           ($('#q').val() == 'wpisz szukaną frazę') ){
        send = false;
        alert ('Szukane słowo musi zawierac więcej niż 3 znaki!');
    }
    return send;
}

function check_newsletter () {
    send = true;
    if ($('#m').val().search(/[_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3}/i) == -1)  {
        send = false;
        alert ('Wprowadź poprawny adres e-mail!');
    }
    return send;
}

