/* Main script */

// Preload common images
var img = new Image();
img.src = '/images/rg_help.png';
var img1 = new Image();
img1.src = '/images/rg_help.gif';

$(document).ready(function(){
    $(document).pngFix();
    
    // Call request
    $('INPUT[name=cr-phone],INPUT[name=cr-name]').one('focus', function(){
            $(this).val('');    
    });
    
    $('.call-request').click(function(e){
        e.preventDefault();
        
        // Check data
        if($('INPUT[name=cr-phone]').val() == 'номер телефона' || $('INPUT[name=cr-name]').val() == 'ваше имя')
        {
            alert('Вы не ввели номер телефона или ваше имя!');
        }
        else
        {
            $(this).after('<div class="cr-stdby fc-hidden">отправляю...</div>')
            $(this).slideUp("fast", function(){
                $(this).remove();
                $('.cr-stdby').slideDown("fast");
				
                $.post("/lib/log.php", { action: 'economy', phone: $('INPUT[name=cr-phone]').val(), fio: $('INPUT[name=cr-name]').val() }, function(){
                    $('.cr-stdby').fadeOut("fast", function(){
                        $(this).replaceWith('<div class="cr-end">Благодарим за проявленный интерес, мы свяжемся с вами в течение 15 минут (в рабочее время).</div>');
                    });
                });
            });
        }
    }); 
    
    // Set regions panel
    showRegionsPanel();
    
    // Prepare page elements that depend on region
    preparePageElements();
}); 

// Show regions panel according to it's size
function showRegionsPanel()
{
    var top = $('.top-region-panel').height() - 34;
    $('.top-region-panel').css('top', '-' + top + 'px');
    $('.top-region-panel').show();

    // Slide in/out    
    $('.trp-change').click(function(e){
        e.preventDefault();
        toggleRegionsPanel();
    });
    
    // Region select
    $('.trp-regions A').click(function(e){
        e.preventDefault();
        $('.trp-results B').html($(this).html());
        $('.trp-results B').attr('rel', $(this).attr('rel'));
        toggleRegionsPanel();
        togglePageElements($(this).attr('rel'));
        refreshCalc();
        
        // Set global region
        $.post("/index.php", { action: 'set_region', rgcode: $(this).attr('rel')});
    });
}

// Slide in/out regions panel
function toggleRegionsPanel()
{
    var top = $('.top-region-panel').height() - 34;
    if($('.trp-change').hasClass('trp-active'))
    {
        $('.top-region-panel').animate({top: '-' + top});
        $('.trp-change').removeClass('trp-active');
        $('.trp-change').html('изменить');
        $('.top-region-panel .darr').html('&darr;');
    }
    else
    {
        $('.top-region-panel').animate({top: 0});    
        $('.trp-change').addClass('trp-active');
        $('.trp-change').html('свернуть');
        $('.top-region-panel .darr').html('&uarr;');
    }
}

// Toggle region dependent page elements
function togglePageElements(rcode)
{
    appendRegionNotice(rcode);
    if($('.region-dep:visible').size() > 0)
    {
        $('.region-dep:visible').fadeOut("normal", function(){
            $('.region-' + rcode).fadeIn("normal");
        });
    }
    else
    {
        $('.region-' + rcode).fadeIn("normal");
    }
}

// Prepare region dependent page elements
function preparePageElements()
{
    var rcode = $('.trp-results B').attr('rel');
    if($('.region-dep:visible').size() > 0)
    {
        $('.region-dep:visible').hide();
        $('.region-' + rcode).show();
    }
    else
    {
        $('.region-' + rcode).show();
    }
    appendRegionNotice(rcode);
}

// Append notice and link to region change
function appendRegionNotice(rcode)
{
    // If already set
    if($('.rg-notice').size() == 0) $('BODY').append('<div class="rg-notice"></div>');
    $('.rg-notice').html('<a href="#" class="inner-link">как изменить регион</a>Выбранный регион: <b>' + $('.trp-results B').html() + '</b>');
    var rgn = $('.rg-notice').remove();
    

    if($('.region-' + rcode + ' H2:first').size() > 0)
    {
        rgn.insertAfter('.region-' + rcode + ' H2:first');
    }
    else
    {
        rgn.insertBefore('.region-' + rcode + ':not(.top-phone,.mpi):first');
    }
    
    $('.rg-notice A').live('click', function(e){
        e.preventDefault();
        $("html:not(:animated),body:not(:animated)").animate({ scrollTop: 0}, 200, function(){
                $('.top-region-panel').animate({"opacity": "toggle"}, 400, function(){
                    $(this).animate({"opacity": "toggle"}, 400, function(){
                        $(this).animate({"opacity": "toggle"}, 400, function(){
                            $(this).fadeIn("slow");
                        });
                    });
                });
        });    
    });
}

// Refreshes calculator
function refreshCalc()
{
    if($('.calculator:visible').size() > 0)
    {
        $('.calculator').remove();
        placeCalc();
    }
}
