function Page()
{
    this.carListPage = $('#PageAutoSelect');
    this.carListNode = $('#SeriesShowAll');
    
    this.calcPage = $('#PageCalcForm');
    this.carsSliderContainerNode = $('#HD2');
    this.carsSliderNode = $('#lyr1');
    this.carHeaderContainerNode = $('#HD1');
    
    this.carCompletionsContainerNode = $('#IntegContainer');
    this.carNameNode = $('#VsCarName');
    
    this.creditContainerNode = $('#CalcCredit');
    this.programsListNode = $('#programs_list');
    
    this.sendMailPage = $('#PageSendMail');
    
    this.comparePage = $('#PageCalcCompare');
    
    this.models = [];
    this.init();
}

Page.prototype.init = function()
{
    this.sliderTerm = $('#VsTermTrack').slider({max: 60, min: 12, orientation: 'horizontal', step: 1 });
    this.sliderFirstPay = $('#VsFPTrack').slider({max: 100, min: 0, orientation: 'vertical', step: 1 });
    this.sliderTermInner = $('#LsTermTrack').slider({max: 60, min: 12, orientation: 'vertical', step: 1});
    
    $('#ContainerTermCredit').click(function()
    {
        $('#LsTerm').toggle();
    });
    
    $('#print_button').click(function(){
        facade.program.print();
    });
    
    $('#sendmailpage_button').click(function(){
        facade.page.calcPage.fadeOut(400, function() {facade.page.sendMailPage.fadeIn(400);});
        document.title = "Тойота Банк :: кредитный калькулятор :: отправка запроса";
        if (pageTracker)
        {
            pageTracker._trackPageview("calculatorSendMail.html");
        }
    });
    
    $('#sendmail_button').click(function(){
        facade.page.sendMail();
    });
    
    $('#sendmail_back').click(function(){
        facade.page.sendMailPage.fadeOut(400, function() {facade.page.calcPage.fadeIn(400);});
        document.title = "Тойота Банк :: кредитный калькулятор :: расчет кредита на покупку Toyota :: параметры кредита";
    });
    
    $('#compare_add').click(function(){
        if (facade.compare.add())
        {
            facade.page.calcPage.fadeOut(400, function() {facade.page.comparePage.fadeIn(400, function() {$('#compare_show_list').show();}); });
            if (pageTracker)
            {   
                pageTracker._trackPageview("calculatorCompareViewResult.html");
            }
        }
    });
    
    $('#compare_back').click(function(){
        facade.page.comparePage.fadeOut(400, function() {facade.page.calcPage.fadeIn(400);});
    });
    
    $('#compare_print').click(function(){
        window.print();
    });
    
    $('#compare_clean').click(function(){
        $('#compare_show_list').hide();
        facade.compare.clean();
    });
    
    $('.compare_delete').live('click', function(){
        $(this).parents('.compare_item').remove();
        if ($('#compare_list > div.compare_item').size() == 0)
        {
            $('#compare_show_list').hide();
            facade.page.comparePage.fadeOut(400, function() {facade.page.calcPage.fadeIn(400);});
        }
    });
    
    $('.compare_select').live('click', function(){
        facade.compare.select($(this).attr('id').substr(15));
    });
    
    $('#compare_show_list').click(function() {
        facade.page.calcPage.fadeOut(400, function() {facade.page.comparePage.fadeIn(400);});
        if (pageTracker)
        {
            pageTracker._trackPageview("calculatorCompareViewResult.html");
        }
    });
    
    $('#help').click(function(ev){
        $('#tip_insurance').css('top', (parseInt($('#sumCredit_punkt').position().top) + 10) + 'px');
        $('#tip_send,#tip_compare').css('top', (parseInt($('#sendmailpage_button').position().top) + 35) + 'px');
        
        $('#tips').toggle();
        return false;
    });
}

Page.prototype.renderCarList = function()
{
    document.title = "Тойота Банк :: кредитный калькулятор :: расчет кредита на покупку Toyota :: выбор модели";
    this.loadCarList();
}

Page.prototype.loadCarList = function()
{
    $.ajax
    ({
        url: credit_root + '/b.php?t=MainLoadCar',
        dataType: 'json',
        success: facade.page.onCarListLoadSuccess
    });
}

Page.prototype.onCarListLoadSuccess = function(data)
{
    facade.page.carListNode.html(facade.page.getCarListHTML(data));
    facade.page.carListPage.show();
    facade.page.showCarListBanner();
    $('.small_car').click(facade.page.selectListModel);
    
    if (pageTracker)
    {
        pageTracker._trackPageview("calculatorseries.html");
    }
}

Page.prototype.getCarListHTML = function(a)
{
    var buf ='<div id="569x74-lfs-premium" style=" display:none; width:569px;height:74px; border:0px solid red;"></div><br>';
    for(var i=0; i < a.length; i++)
    {
        facade.page.models[i + 1] = {name: a[i].NameAuto};
        
        buf += '<div id="listmodel' + (i + 1) + '" class="small_car" style="cursor: pointer;">';
        buf += '<div style="float:none;height:75px;" >';
        buf += '<div style="float:left; width:197px;">';
        buf += '<span class="car_name_small" ' + (a[i].id == 117 ? 'style="font-size:13px;"' : '') + ' >' + a[i].NameAuto + '</span>';
        buf += '<span class="os_poyasnenie"><font style="color:#000000;">Ежемесячный платеж<sup>*</sup></font> (от/до, USD)</span>';
        buf += '</div>';
        buf += '<div style="padding-top:0px; float:left; width:369px;"><img align="left" style="padding-top:2px;" src="/userfiles/cAuto/' + a[i].csPic + '" border="0" />';
        
        /* ONLY FOR TOYOTA
        if (a[i].AwardImage)
        {
            buf += '<a title="' + a[i].AwardText + '" href="#"><img align="left" border="0" style="padding: 2px 0 0 6px; width: 47px; height: 47px;" src="' + a[i].AwardImage + '" /></a>';
        }
        */
        
        buf += '<input class="bot_but_r" style="float:right;" value="Рассчитать кредит" />';
        buf += '</div>';
        buf += '</div>';
        buf += '<div class="polzun_red" style="position:relative; top:-25px;background-position:center left;width:' + (a[i].RedLine*1 + 56) +  'px; margin-left:' + parseInt(parseInt(a[i].WhiteSpace) + parseInt(27)) + 'px;">&nbsp;</div>';
        buf += '<table border=0 style="font-size:1px;line-height:8px;"><tr><td width="'+a[i].WhiteSpace+'"></td><td><div class="arrows_bot" style="position:relative; top:-25px;">'+a[i].MinCostInteg+'</div></td><td width="'+(a[i].RedLine)+'"></td><td><div class="arrows_bot" style="position:relative; top:-25px;">'+a[i].MaxCostInteg+'</div></td></tr></table>';
        buf += '</div>';
    }
    
    return buf;
}

Page.prototype.selectListModel = function()
{
    var model = facade.page.models[ $(this).attr('id').substr(9) ];
    facade.car = new Car(model);
    facade.car.renderToCalc();
    
    new dw_scrollObj('wn', 'lyr1', 't1');
    facade.page.carListPage.fadeOut(400, function() { facade.page.calcPage.fadeIn(400) });
}

Page.prototype.sendMail = function()
{
    var CitiesDC = {'Екатеринбург':true , 'Казань':true , 'Краснодар':true , 'Самара':true , 'Челябинск':true };
    
    var message = "";
    var Name        = $('#FormSendMail input[name=Name]');
    var Phone       = $('#FormSendMail input[name=Phone]');
    var PostAddress = $('#FormSendMail input[name=PostAddress]');
    var Comment     = $('#FormSendMail input[name=Comment]');
    var City        = $('#FormSendMail select[name=city]');
    var Dealer		= $('#FormSendMail select[name=dealer]');
    var Comment     = $('#FormSendMail textarea[name=Comment]');
    var RequestMail = $('#FormSendMail input[name=RequestMail]');
    
    var inf_source  = $('#FormSendMail select[name=inf_source]');
    var inf_source_other  = $('#FormSendMail input[name=inf_source_other]');
    
    if(!Name.val() || Name.val().length < 8)
    {
        message += "Вы не заполнили ФИО\n";
    }
    if(!Phone.val() || Phone.val().length < 7)
    {
        message += "Вы не заполнили телефон\n";
    }
    if(!PostAddress.val() || PostAddress.val().length < 6)
    {
        message += "Вы не заполнили почтовый адрес\n";
    }
    if(!City.val())
    {
        message += "Не выбран город\n";
    }
    if (!Dealer.val() && (City.val() in CitiesDC))
    {
        message += "Не указан дилер\n";
    }
    if (!inf_source.val() && !inf_source_other.val())
    {
        message += "Не выбран источник информации.";
    }
    
    if(message)
    {
        alert("Ошибка заполнения формы:\n" + message);
        return;
    }
    
    
    var data = facade.program.exportData();
    data.mail_name = Name.val();
    data.mail_phone = Phone.val();
    data.mail_email = PostAddress.val();
    data.mail_city = City.val();
    data.mail_dealer = Dealer.val();
    data.mail_inf_source = inf_source.val();
    data.mail_inf_source_other = inf_source_other.val();
    data.mail_comment = Comment.val();
    data.mail_request_mail = (RequestMail.attr('checked') ? 1 : 0);
    
    $.ajax(
    {
        url: credit_root + '/b.php?t=SendMail',
        type: 'post',
        dataType: 'json',
        data: data,
        success: function(data)
        {
            alert( data.request ? 'Ваш запрос уже обрабатывается' : 'Ваше сообщение отправлено' );
            if (data.request == 0)
            {
                if (pageTracker)
                {
                    pageTracker._trackPageview("calculatorRequestSent.html");
                }
            }
        }
    });
}

Page.prototype.showCarListBanner = function()
{
   // var so = new SWFObject("569x74-lfs-premium.swf", "569x74-lfs-premium", "569", "74", "7", "red"); 
   // so.addParam("wmode", "transparent");
   // so.write("569x74-lfs-premium"); 
}
