$(document).ready(function(){


$('.show_hidden').click(function(){
  $(this).next().slideToggle();
});




/*==============================================================================
spectours
==============================================================================*/
$('#specs table a').click(function(){
$('#specs .info').hide();
var url=$(this).attr('href');
url=url.substring(1);
$('#specs .load').show();
$('#spectours .content').html('&nbsp;');
$('#spectours').load(url+' #spectours .content', function(){


$('#specs .load').hide();

});


return false;
});

$('#spectours a').live('click',function(){
  $('#specs .info').hide(); 
var title=$(this).html();
var position=$(this).position();

$('#specs .info h2').html(title);

$('#specs .info').show();
var height=$('#specs .info').height()+10;
$('#specs .info').css({'top':position.top-height+'px'});


return false;
});

$('#specs .info .close').click(function(){

$('#specs .info').hide();

return false;

});








/*==============================================================================

==============================================================================*/
    $('#os_phone').keyup(function(){
        checkNumber(this);
    });

    var valR;
    if($('#os_direction').length && $('#os_date').length){
    	valDir = $('#os_direction').val();
    	valDat = $('#os_date').val();
    	trip(valDir,valDat);
    }
    
    $('#os_direction').change(function(){
    	valDir = $('#os_direction').val();
    	valDat = $('#os_date').val();
    	trip(valDir,valDat);
    });
    
    $('#os_date').change(function(){
    	valDir = $('#os_direction').val();
    	valDat = $('#os_date').val();
    	trip(valDir,valDat);
    });

    $('.checkTrip').live('click',function(){
        $('#os_numplace').val('');
        valR = $(this).attr('id');
    	bus(valR);
    });

    /*if($('.checkTrip').length){
        valR = $('.checkTrip:checked').attr('id');
        alert(valR);
    	bus(valR);
    };*/

    $('#bronButton').click(function(){
    	strH = '';
    	$('.myPlace').each(function(){
		strH += $(this).attr('id')+', ';
        });
        strH = strH.substr(0,strH.length-2);
        $('#os_numplace').val(strH);
        if(!isNaN(valR)) $('#os_tt').val(valR);
        if($('#os_name').val()=='' || $('#os_phone').val()=='' || $('#os_numplace').val()=='' || $('#os_tt').val()=='' || $('#qzxw').val()==''){
            if($('#error').length) $('#error').empty();
            if($('#listError').length) $('#listError').remove();
            str = '';
            //if($('#os_tt').val()=='' ) str += "Выберите рейс<br>";
            if(!$('.checkTrip:checked').length) str += "Выберите рейс<br>";
            if($('#os_numplace').val()=='') str += "Выберите места<br>";
            if($('#os_name').val()=='') str += "Заполните поле «Контактное лицо»<br>";
            if($('#os_phone').val()=='') str += "Заполните поле «Контактный телефон»<br>";
            if($('#os_mail').val()=='') str += "Заполните поле «E-mail»<br>";
            if($('#qzxw').val()=='') str += "Запишите результат выражения<br>";
            $('#error').text("Не все поля заполнены");
            $('#error').after("<div id='listError'>"+str+"</div>");
        }
        else{
            $('#os_time').val($('.checkTrip:checked').parent().next().text());
            $('#os_coast').val(parseInt($('#dayTime .td3').text()));
            $('#addTrip').append('<input type="hidden" value="Забронировать" name="bronButton"/>');
            $('#addTrip').submit();
        }
    });
});


/*==============================================================================

==============================================================================*/
function trip(valDir,valDat){
	if(valDir=='' || valDat==''){
    		$(".block2").empty();
    	}else{
    		$(".block2").empty();
            $(".block2").append('<img src="/_i/preloaderTrip.gif" alt="" id="preloadTrip">');
            $.ajax({
            method: "get",url: "/ajax/trips.php?valDir="+valDir+"&valDat="+valDat,
            success: function(html)
            {
                $(".block2").html(html);
                valR = $('.checkTrip:checked').attr('id');

                if(isNaN(valR)) {
                    $('#planBus').planBus();
                }
                else bus(valR);
            }
            });
    	}
}

function bus(valR){
        $("#planBus").append('<img src="/_i/preloaderBus.gif" alt="" id="preloadBus">');
    	$.ajax({
		method: "get",url: "/ajax/bus.php?valR="+valR,
		success: function(html)
		{
			$("#planBus").html(html);
            myVal = $('#os_numplace').val();
            $('#planBus').planBus({
              myPlace: myVal,
              valR: 1
            });
		}
});
}

function checkNumber(t){
    n = parseInt($(t).val());
    if(isNaN(n) || n==0) $(t).val('');
    else $(t).val(n);
}

function printblank(os_id) {
    newWin = window.open("/blank_print.php?os_id="+os_id, null, "width='900px', height='100%', top='0', left='0', resizable=1, scrollbars=1");
}

//калькулятор тура
function calc2(){
    if (document.form1.price.value != ''){

        var price = parseInt(document.form1.price.value);   //стоимость тура
        var first;   //первоначальный взнос
        var month; //ежемесячный платеж
        var credit; //размер кредита


        if (document.form1.period.value==6){




        first = Math.round(price*0.412);
        credit = price - first;

        month = Math.round(credit/6);
        credit = month*6;
        first = price - credit;

        document.form1.first.value = first;
        document.form1.credit.value = credit;
        document.form1.month.value = month;
        document.form1.overpayment.value = '0%';
        document.getElementById('calcBlock').style.visibility = 'visible';
        } else{

        var predoplata=document.form1.predoplata.value;

        first = Math.round(price*predoplata);
        credit = price - first;

        month = Math.round(credit/5);
        credit = month*5;
        first = price - credit;

        document.form1.first.value = first;
        document.form1.credit.value = credit;
        document.form1.month.value = month;
        document.form1.overpayment.value = '0%';
        document.getElementById('calcBlock').style.visibility = 'visible';

        }

    }

    return false;
}

function calc2_period(){

if (document.form1.period.value==6){
document.getElementById('predoplata').style.display = 'none';
  }else{
     document.getElementById('predoplata').style.display = 'block';
  }

}
