$(document).ready(function(){
    
    $('#lightbox a').lightBox({
        imageLoading:'/i/jquery/lightbox-images/lightbox-ico-loading.gif',
        imageBtnClose:'/i/jquery/lightbox-images/lightbox-btn-close.gif',
        imageBtnPrev:'/i/jquery/lightbox-images/lightbox-btn-prev.gif',
        imageBtnNext:'/i/jquery/lightbox-images/lightbox-btn-next.gif',
        imageBlank:'/i/jquery/lightbox-images/lightbox-blank.gif',
        txtImage:'Изображение',
        txtOf:'из'
    });
    
    $('.captcha').click(function(){
        $(this).attr('src', '/captcha/rand-'+parseInt(Math.random()*999999999)+'/');
    });
    
    $('.fields').focus(function(){
        $('#div_msg').empty()
    });
    
    if (document.documentElement.clientHeight>$('#l_bigbg').height()) {
        $('#td_content').height(document.documentElement.clientHeight-368);
    }
    else if ($('#l_bigbg').height()>$('#table_content').height()) {
        $('#td_content').height($('#l_bigbg').height()-$('#table_content').height()+10);
    }
    
    $('#show_form').click(function(){
        $('#send_button').hide();
        $('#send_form').slideDown();
    });
    
    $('.img_country').mouseover(function(){
        var div = $('.div_country[i="'+$(this).attr('i')+'"]');
        div.css('top', $(this).offset().top);
        div.css('left', $(this).offset().left+$(this).width());
        div.show();
    });
    $('.img_country').mouseout(function(){
        var id = $(this).attr('i');
        $('.div_country[i="'+id+'"]').hide()
    });
    $('.div_country').mouseover(function(){
        $(this).show();
    });
    $('.div_country').mouseout(function(){
        $(this).hide();
    });
    
});

function query(url, div, data) {
    
    $.ajax({
        cache:false,
        type:'POST',
        dataType:'json',
        url:url,
        data:data,
        success:function(response){
            $('#'+div).html(response.html);
        }
    });
    
}

function checknum(e) {
    
    return (e.keyCode >= 48 && e.keyCode <= 57)
    
}

function fixPNG(element) {
    
    if (/MSIE (5\.5|6).+Win/.test(navigator.userAgent)) {
        var src;
        if (element.tagName=='IMG') {
            if (/\.png$/.test(element.src)) {
                src = element.src;
                element.src = "/i/spacer.gif";
            }
        }
        else {
            src = element.currentStyle.backgroundImage.match(/url\("(.+\.png)"\)/i)
            if (src) {
                src = src[1];
                element.runtimeStyle.backgroundImage="none";
            }
        }
        if (src) {
            element.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "',sizingMethod='scale')";
        }
    }
    
}

function guestbook_send(id) {
    
    $.ajax({
        cache:false,
        dataType:'json',
        type:'POST',
        url:'/guestbook/send/id-'+id+'/',
        data:{name:$('#name').val(), email:$('#email').val(), text:$('#text').val(), keystring:$('#keystring').length?$('#keystring').val():''},
        success:function(response){
            $('#div_msg').html(response.msg);
            if(response.result) {
                $('input[class!=button]').val('');
                $('textarea').val('');
            }
            else {
                if ($('#keystring').length) {
                    $('#keystring').val('');
                    query('/captcha/reload/', 'div_captcha', null);
                }
            }
        }
    });
    
}

function routes_send(id) {
    
    $.ajax({
        cache:false,
        dataType:'json',
        type:'POST',
        url:'/routes/send/id-'+id+'/',
        data:{name:$('#name').val(), time:$('#time').val(), period:$('#period').val(), cost:$('#cost').val(), description:$('#description').val(), contact:$('#contact').val(), keystring:$('#keystring').length?$('#keystring').val():''},
        success:function(response){
            $('#div_msg').html(response.msg);
            if(response.result) {
                $('input[class!=button]').val('');
                $('textarea').val('');
            }
            else {
                if ($('#keystring').length) {
                    $('#keystring').val('');
                    query('/captcha/reload/', 'div_captcha', null);
                }
            }
        }
    });
    
}
