var margin = 0;
var width = 0;
var height = 0;

var doc_counter = 1;

var modal_form_newsletter = '';
var modal_form_approve = '';

var modal_notice_newsletter = '';
var modal_notice_approve = '';
var modal_notice_approve_popup = '';
var modal_notice_question = '';
var modal_notice_foppinion = '';

var modal_form_approve_popup = '';
var modal_form_approve_popup2 = '';

function main ()
{
    var left = $('body').width() - 975;
    left = Math.round(left/2);

    $('#bottom').css({'left' : left});

    if ( $('body').width() > 1152 )
    {
        $('#slider').css({'width': 1152});
        $('#pictures > img').css({'width': 1152});
    }

    if ( $('body').height() < 830 )
    {
        var top = 930 - $('body').height();
        top = 770 - top;

        $('#bottom').css({'top' : top});
    }
}

function rebuild ()
{
    if ( ($('#content').height()) < 973 )
    $('#content').css({'height': 975});

    var min = $('#content').height()+$('#menu').height()+131-$('#header').height();  //menu top és content bottom

    if ( min < 180 )
    min = 180;

    $('#wraper').css({'height': (min+92)});

    $('#content_closer').css({'top':(min-5)});

    margin = Math.round(($('html').width()- 465)/2);

    var width = $('body').width();

    if ( width > 1132 )
    {
        $('#header').css({'width': 1132});
        $('#wraper').css({'width': 1132});
    }
    else
    {
        if ( width > 994 )
        {
            $('#header').css({'width': width});
            $('#wraper').css({'width': width});
        }
        else
        {
            $('#header').css({'width': 995});
            $('#wraper').css({'width': 995});
        }

        margin = Math.round(($('#header').width()- 465)/2);
    }
   
    height = $('body').height();

    if ( height < 1090 )
    $('#sidebar').css({'position':'absolute'});

    $('#oppinion').css({'left':margin+495+'px'});
    $('#sidebar').css({'left':margin+496+'px'});

    var margin2 = Math.round((width - $('#header').width())/2);

    $('#content_closer').css({'left':margin-margin2-14});

    $('#header').css({'left':margin2-2});
    $('#wraper').css({'left':margin2-2});
    $('#crop').css({'left':margin2-2});
}

function rebuild_topmenu ()
{
    if ( $('#content').height() < 630 )
    $('#content').css({'height': 630});

    var margin = Math.round(($('html').width()- 465)/2);

    var width = $('body').width();
    
    if ( width > 1132 )
    {
        $('#header').css({'width': 1132});
        $('#wraper').css({'width': 1132});
    }
    else
    {
        $('#header').css({'width': width});
        $('#wraper').css({'width': width});
    }

    if ( ($('#content').height()+110) < $('#header').height() )
    {
        $('#header').css({'height': $('#content').height()+110});
        $('#wraper').css({'height': 0});
    }
    else
    $('#wraper').css({'height': $('#content').height()-777});

    margin = Math.round((width - $('#header').width())/2);

    $('#header').css({'left':margin-2});
    $('#wraper').css({'left':margin-2});

    $('.delimiter').hover(function(){
        $(this).attr('src', './images/elvalaszto_fel.png');
    },
     function(){
        $(this).attr('src', './images/elvalaszto.png');
    });

    $('.delimiter').click(function(){
        $('html, body').animate({scrollTop:0});
    });
}

function inic_checkbox ()
{
    $('.checkbox').click(function(){
        if ( $(this).children('.want').val() == 0 )
        {
            $(this).children('.want').val(1);
            $(this).addClass('on');

            return false;
        }
        else
        {
            $(this).children('.want').val(0);
            $(this).removeClass('on');

            return false;
        }
    });
}

function inic_radio ()
{
    $('.radio').click(function(){
        if ( $(this).children('.want').val() == 0 )
        {
            $('.radio').removeClass('on');
            $('.radio').children('.want').val(0);

            $(this).children('.want').val(1);
            $(this).addClass('on');

            return false;
        }

        return false;
    });
}

function inic_tooltip ()
{
    $('.trigger').hover(function(){
        var next = $(this).next('span.tooltip');
        next.css({'top':$(this).position().top-next.height()});
        next.css({'left':$(this).position().left});
        next.show();
    },function(){
        var next = $(this).next('span.tooltip');
        next.hide();
    });
}

function inic_approve_form ()
{
    $("#approve_form").validator({
        'lang' : 'hu',
        errorInputEvent: null,
        onBeforeValidate : function ()
        {
             if ( $('#approve_email').val() != $('#approve_email2').val() )
             $('#approve_email2').val('');
        },
        onFail:function(){},
        onSuccess:function(e, list)
        {
            $.post('./email.php', list);

            var nev = $('#approve_name').val();

            $(':input').each(function(){
                 if ( this.type == 'text' )
                 this.value = '';
            });

            $('.checkbox').each(function(){
                $(this).removeClass('on');
            });

            $('#approve_comment').val('');

            $('.error').hide();

            nev = nev.split(' ');

            $('#notice_approve #keresztnev').text(nev[nev.length-1]);

            modal_notice_approve = $(this).overlay({
                mask: {
                    color: '#ebecff',
                    loadSpeed: 200,
                    opacity: 0.5
                },
                target: '#notice_approve',
                top: 20,
                closeOnClick: false,
                load: true,
                onLoad: function(){
                    $('.close_panel').click(function(){
                        modal_notice_approve.overlay().close();

                        $('.error').hide();
                    });
                },
                onClose: function(){
                    $('.error').hide();
                }
            });

            return false;
        }
    });
}

function inic_approve_popup_form ()
{
    $("#approve_popup_form").validator({
        'lang' : 'hu',
        errorInputEvent: null,
        onBeforeValidate : function ()
        {
             if ( $('#approve2_email').val() != $('#approve2_email2').val() )
             $('#approve2_email2').val('');
        },
        onFail:function(){},
        onSuccess:function(e, list)
        {
            $.post('./email.php', list);

            var nev = $('#approve2_name').val();

            $(':input').each(function(){
                 if ( this.type == 'text' )
                 this.value = '';
            });

            $('.checkbox').each(function(){
                $(this).removeClass('on');
            });

            $('#approve2_comment').val('');

            $('.error').hide();

            nev = nev.split(' ');

            modal_form_approve_popup.overlay().close();
            modal_form_approve_popup2.overlay().close();

            $('#notice_approve #keresztnev').text(nev[nev.length-1]);

            modal_notice_approve = $(this).overlay({
                mask: {
                    color: '#ebecff',
                    loadSpeed: 200,
                    opacity: 0.5
                },
                target: '#notice_approve',
                top: 20,
                closeOnClick: false,
                load: true,
                onLoad: function(){
                    $('.close_panel').click(function(){
                        modal_notice_approve.overlay().close();

                        $('.error').hide();
                    });
                },
                onClose: function(){
                    $('.error').hide();
                }
            });

            return false;
        }
    });
}

function inic_foppinion_form ()
{
    $("#foppinion_form").validator({
        'lang' : 'hu',
        errorInputEvent: null,
        onBeforeValidate : function ()
        {
             if ( $('#foppinion_email').val() != $('#foppinion_email2').val() )
             $('#foppinion_email2').val('');
        },
        onFail:function(){},
        onSuccess:function(e, list)
        {
            $.post('./email.php', list);

            var nev = $('#foppinion_name').val();

            $(':input').each(function(){
                 if ( this.type == 'text' )
                 this.value = '';
            });

            $('#foppinion_comment').val('');

            $('.error').hide();

            nev = nev.split(' ');

            $('#notice_foppinion #keresztnev').text(nev[nev.length-1]);

            modal_notice_foppinion = $(this).overlay({
                mask: {
                    color: '#ebecff',
                    loadSpeed: 200,
                    opacity: 0.5
                },
                target: '#notice_foppinion',
                top: 20,
                closeOnClick: false,
                load: true,
                onLoad: function(){
                    $('.close_panel').click(function(){
                        modal_notice_foppinion.overlay().close();

                        $('.error').hide();
                    });
                },
                onClose: function(){
                    $('.error').hide();
                }

            });

            return false;
        }
    });
}

function inic_question_form ()
{
    $("#question_form").validator({
        'lang' : 'hu',
        errorInputEvent: null,
        onBeforeValidate : function ()
        {
             if ( $('#question_email').val() != $('#question_email2').val() )
             $('#question_email2').val('');
        },
        onFail:function(){},
        onSuccess:function(e, list)
        {
            $.post('./email.php', list);

            var nev = $('#question_name').val();

            $(':input').each(function(){
                 if ( this.type == 'text' )
                 this.value = '';
            });

            $('.checkbox').each(function(){
                $(this).removeClass('on');
            });

            $('textarea').html('');

            $('.error').hide();

            nev = nev.split(' ');

            $('#notice_question #keresztnev').text(nev[nev.length-1]);

            modal_notice_question = $(this).overlay({
                mask: {
                    color: '#ebecff',
                    loadSpeed: 200,
                    opacity: 0.5
                },
                target: '#notice_question',
                top: 20,
                closeOnClick: false,
                load: true,
                onLoad: function(){
                    $('.close_panel').click(function(){
                        modal_notice_question.overlay().close();

                        $('.error').hide();
                    });
                },
                onClose: function(){
                    $('.error').hide();
                }
            });

            return false;
        }
    });
}

function inic_newsletter_form ()
{
    $('#newsletter_email').focus(function(){
        $(this).val('');
    });

    $('#newsletter_name').focus(function(){
        $(this).val('');
    });

    $("#newsletter_form").validator({
        'lang' : 'hu',
        errorInputEvent: null,
        onFail:function(){},
        onSuccess:function(e, list)
        {
            $.post('./email.php', list);

            var nev = $('#newsletter_name').val();
            var email = $('#newsletter_email').val();

            $(':input').each(function(){
                 if ( this.type == 'text' )
                 this.value = '';
            });

            $('.error').hide();

            nev = nev.split(' ');

            $('#notice_newsletter #keresztnev').text(nev[nev.length-1]);
            $('#notice_newsletter #email_cim').text(email);

            modal_notice_newsletter = $(this).overlay({
                mask: {
                    color: '#ebecff',
                    loadSpeed: 200,
                    opacity: 0.5
                },
                target: '#notice_newsletter',
                top: 20,
                closeOnClick: false,
                load: true,
                onLoad: function(){
                    $('.close_panel').click(function(){
                        modal_notice_newsletter.overlay().close();

                        $('.error').hide();
                    });
                },
                onClose: function(){
                    $('.error').hide();
                }
            });

            return false;
        }
    });
}

function inic_newsletter_popup_form ()
{
    $("#newsletter_popup_form").validator({
        'lang' : 'hu',
        errorInputEvent: null,
        onFail:function(){},
        onSuccess:function(e, list)
        {
            $.post('./email.php', list);

            var nev = $('#newsletter_popup_name').val();
            var email = $('#newsletter_popup_email').val();

            $(':input').each(function(){
                 if ( this.type == 'text' )
                 this.value = '';
            });

            $('.error').hide();

            nev = nev.split(' ');

            modal_form_newsletter.overlay().close();

            $('#notice_newsletter #keresztnev').text(nev[nev.length-1]);
            $('#notice_newsletter #email_cim').text(email);

            modal_notice_newsletter = $(this).overlay({
                mask: {
                    color: '#ebecff',
                    loadSpeed: 200,
                    opacity: 0.5
                },
                target: '#notice_newsletter',
                top: 20,
                closeOnClick: false,
                load: true,
                onLoad: function(){
                    $('.close_panel').click(function(){
                        modal_notice_newsletter.overlay().close();

                        $('.error').hide();
                    });
                },
                onClose: function(){
                    $('.error').hide();
                }
            });

            return false;
        }
    });
}

function inic_datepicker ()
{
    $(":date").dateinput({
        format: 'yyyy-mm-dd',
        lang: 'hu',
        selectors: true,
        min: -1,
        max: 190,
        offset: [10, 20],
        speed: 'fast',
        firstDay: 1,
        onBeforeShow: function(){
            if ( $('#approve_popup_modal').css('display') == 'none' )
            {
                var all  = $('#calroot');
                $('#calroot').remove();
                $('#sidebar').prepend(all);                
            }
        }
    });
}

function show_doc (index)
{
    $('.doc_image').css({left:-999});
    //$('.doc_image').hide();
    $('#doc_image_'+index).css({left:0});
    //$('#doc_image_'+index).fadeIn('slow');

    $('.doc_panel').hide();
    $('#doc_'+index).fadeIn('slow');

    $('.doc_oppinion').hide();
    $('#doc_oppinion_'+index).fadeIn('slow');
}

function inic_modal ()
{
    modal_form_approve_popup = $('li#line_5 a').overlay({
        mask: {
            color: '#ebecff',
            loadSpeed: 200,
            opacity: 0.5
        },
        top: 20,
        target: 'div#approve_popup_modal',
        closeOnClick: false,
        onLoad: function(){
            inic_approve_popup_form();

            inic_datepicker();

            var all  = $('#calroot');
            $('#calroot').remove();
            $('#approve_popup_modal').prepend(all);

            $('.close2').click(function(){
                $('#calroot').remove();
                modal_form_approve_popup.overlay().close();

                $('.error').hide();
            }); 
        },
        onClose: function(){
            $('.error').hide();
        }
    });

    modal_form_approve_popup2 = $('a.bejelentkezes_ablak').overlay({
        mask: {
            color: '#ebecff',
            loadSpeed: 200,
            opacity: 0.5
        },
        top: 20,
        target: 'div#approve_popup_modal',
        closeOnClick: false,
        onLoad: function(){
            inic_approve_popup_form();

            inic_datepicker();

            var all  = $('#calroot');
            $('#calroot').remove();
            $('#approve_popup_modal').prepend(all);

            $('.close2').click(function(){

                $('#calroot').remove();
                modal_form_approve_popup2.overlay().close();

                $('.error').hide();
            });
        },
        onClose: function(){
            $('.error').hide();
        }
    });

    $('.close2').click(function(){

        $('#calroot').remove();
        modal_form_approve_popup.overlay().close();
        modal_form_approve_popup2.each(function(){
            $(this).overlay().close();
        });

        $('.error').hide();
    });

}
