$(document).ready(function() {
    $("#calendar").ready(function() {
        loadCalendar();
    });
    $('#paytypeA').change(function(){
        $('#cards').hide();
    });
    
    $('#paytypeB').change(function(){
        $('#cards').show();
    });
    
    if ($('#paytypeB').is(":checked"))
        $('#cards').show();
        
    $("#calendar .calendar2").live("click", function() {
        loadCalendar(this.rel);
    });
    
    $("#program").change(function() {      
        changeProgram();
        
        /*$.get($("#root").val()+"scripts/ajax-days.php", {"program":this.value, "lang":$("#lang").val()}, function(data) {        
            $("#daycount").html(data);
            $("#termin").html('<option>Vyberte...</option>');
            $("#termrow2").hide();
            $("#termrow1").show();
        });
        if($(this).children('option:selected').hasClass('linie')) {
            $("#toggler").show("slow");
        } else {
            $("#toggler").hide("slow");
        } */
    });
   
   
    $("#daycount").change(function() {
        
        var program = $("#program > option:selected").val();
        var days = $(this).children('option:selected').html();
        var termed = !isNaN(days)? $(this).children('option:selected').attr('class'): 'termed';
        
        
        $('#date_from').val('');
        $('#labeldo').html('');
        
        if (termed === 'free') {
            $("#termrow1").hide();
            $("#termrow2").show();
            $("#terminovany").val('ne');
        } else { // terminovy
            $("#termrow2").hide();
            $("#termrow1").show();
            $("#terminovany").val('ano');
            $.get($("#root").val()+"scripts/ajax-program.php", {"program":program, "lang":$("#lang").val(), 'days':days}, function(data) {
                $("#termin").html(data);
            });
        } 
/*        
        if($(this).children('option:selected').hasClass('linie')) {
            $("#toggler").show("slow");
        } else {
            $("#toggler").hide("slow");
        }
        */
    });
   
   
   
    $("#add_room").click(function() {
        var rooms = $(".room").length;
        rooms++;

        var html = '';
        html+= '<tr class="room">';
        html+= '<td><label for="room'+rooms+'">Pokoj '+rooms+'</label></td>';
        html+= '<td colspan="3" class="sleft">';
        html+= '<select name="room[]" id="room'+rooms+'">';
        html+= '<option value="0">Vyberte...</option>';
        html+= '<option value="1">dvoulůžkový / obsazený jednou osobou</option>';
        html+= '<option value="2">dvoulůžkový / jedna osoba + dítě do 2 let</option>';
        html+= '<option value="3">dvoulůžkový / 2 oddělená lůžka</option>';
        html+= '<option value="4">dvoulůžkový / 2 oddělená lůžka + dítě do 2 let</option>';
        html+= '<option value="5">dvoulůžkový / manželská postel</option>';
        html+= '<option value="6">dvoulůžkový / manželská postel + dítě do 2 let</option>';
        html+= '<option class="triple" value="7">třílůžkový</option>';
        html+= '</select>';
        html+= '</td>';
        html+= '<td><label for="roomtype'+rooms+'">Typ pokoje</label></td>';
        html+= '<td class="sleft">';
        html+= '<select id="roomtype'+rooms+'" onchange="updateDog(options[selectedIndex]);" name="roomtype[]">';
        html+= '<option value="0">Vyberte...</option>';
        html+= '<option class="triple-0 dog-0" value="894">Shiraz</option>';
        html+= '<option class="triple-1 dog-0" value="893">Shiraz Superior</option>';
        html+= '<option class="triple-1 dog-1" value="892">Chardonnay</option>';
        html+= '<option class="triple-0 dog-0" value="891">Cabernet</option>';
        html+= '<option class="triple-1 dog-0" value="890">Merlot</option>';
        html+= '<option class="triple-1 dog-0" value="889">Sauvignon</option>';
        html+= '<option class="triple-1 dog-0" value="888">Pinot</option>';
        html+= '<option class="triple-1 dog-0" value="887">Carmenere</option>';
        html+= '</select>';
        html+= '</td>';
        html+= '</tr>';

        var html = 
        $("#add_room_row").before(html);
    });

    $("#sendtofriends").click(function() {
        $(this).hide();							   
        $(this).next('fieldset').show();							   
    });
    
	
    $(".roomtype").select(function()
	{ });
	
  $('.send-friend a').click( function(){
     $('#mailform').show();
  });
});


function changeProgram( )
   {
        var program = $('#program');      
        $.get($("#root").val()+"scripts/ajax-days.php", {"program":program.val(), "lang":$("#lang").val()}, function(data) {        
          
            $("#daycount").html(data);
            $("#termin").html('<option>Vyberte...</option>');
            $("#termrow2").hide();
            $("#termrow1").show();
        });
        if($(program).children('option:selected').hasClass('linie')) {
            $("#toggler").show("slow");
        } else {
            $("#toggler").hide("slow");
        } 
   
   }


function loadCalendar(month) {
    if(!month) {
        month = "";
    }
    
    $.get($("#root").val()+"scripts/ajax-calendar.php", {"month":month,"lang":$("#lang").val()}, function(data) {
        $("#calendar").html("");
        $("#calendar").html(data);
    });
}

function updateRoomTypeList(index){

}

function showTo()
{
    var dateFrom = $('#date_from').val();
    var datum = dateFrom.split('.');
    var dnu = $("#daycount > option:selected").html();
    var date = new Date();
    date.setDate(Number(datum[0]));
    date.setMonth(Number(datum[1])-1);
    date.setFullYear(Number(datum[2]));
    date.setDate(date.getDate() + Number(dnu))
    var dateTo = date.getDate()+'.'+(Number(date.getMonth())+1)+'.'+date.getFullYear();
    $('#labeldo').html('do '+dateTo);
    $('#date_to').val(dateTo);
}
