$(document).ready(function() {
    $("#searchBlock").css('clear', 'both').css('height', '110px').css('padding', '1px 5px 0 5px').css('background-color', '#CBE0ED').css('color', '#07476D').css('margin-bottom', '2px');
    $("<h2>Quick Search</h2>").css('color', '#07476D').css('margin', '10px 0 10px 0').css('display', 'block').css('text-transform', 'uppercase').appendTo("#searchBlock");
    $('<div id="searchBlockContent" class="clearfix" style="background-color: transparent;"></div>').appendTo('#searchBlock');
    $("#searchBlockContent").append('<form method="get" action="http://www.a-prague.com/accommodation" id="ASearchForm"></form>');
    $('#ASearchForm').append('<div id="SearchLeftBlock" style="background-color: transparent;line-height:1.8" />');
    $('#SearchLeftBlock').css('width', '65%').css('float', 'left');
    $('#ASearchForm').append('<div id="SearchRightBlock" style="background-color: transparent;" />')
    $('#SearchRightBlock').css('width', '30%').css('float', 'right').css('padding', '10px 0 0 0');
    
    var days = '';
    for(var i = 1; i <= 31; i++) {
        days += '<option value="'+ i +'">' + i + '</option>';
    }
    
    var months = '';
    var yr = today.getFullYear();
    
    for(var i = today.getMonth()+1, y = 1; y <= 12; y++,i++) {
        if (i == 13) {
            i = 1;
            yr++;
        }
        strMonth = getStrFromNum(i)        
        months += '<option value="'+ yr+'-'+ strMonth +'">'+ monthNames[i] + ' ' + yr +'</option>';
    }
    
    $('#SearchLeftBlock').append('<input type="hidden" name="ad" value="aaaahotels.com" />');
    $('#SearchLeftBlock').append('<label for="checkin_day" style="display:block;float:left;width:40px;">From: </label><select name="checkin_day" id="checkin_day">'+ days +'</select>');
    $('#SearchLeftBlock').append('<select name="checkin_year_month" id="checkin_year_month">'+ months +'</select>');
    $('#SearchLeftBlock').append('<input type="hidden" name="checkin_date" id="checkin_date" />');
    $('#SearchLeftBlock').append('<div style="clear: both; height: 10px; font-size:0;"></div>');
    $('#SearchLeftBlock').append('<label for="checkout_day" style="display:block;float:left;width:40px;padding-bottom:10px;clear:both;">To:</label><select name="checkout_day" id="checkout_day">'+ days +'</select>');
    $('#SearchLeftBlock').append('<select name="checkout_year_month" id="checkout_year_month">'+ months +'</select>');
    $('#SearchLeftBlock').append('<input type="hidden" name="checkout_date" id="checkout_date" />');
    $('#SearchLeftBlock').append('<br />');
    $('#SearchRightBlock').append('<span class="inline-input"><input type="submit" name="SearchButton" id="SearchButton" value="Search" /></span>');
    $('#SearchRightBlock').append('<input type="hidden" name="do_availability_check" id="availcheck" value="on" />');
    
    $('#checkin_year_month option').each(function() {
        var strMonth = getStrFromNum(today.getMonth()+1);
        
        if($(this).val() == today.getFullYear() + '-' + strMonth) {
            $(this).attr('selected', 'selected');
            //console.log(strMonth + '/' + getStrFromNum(today.getDate()) + '/' + today.getFullYear());
            $('#checkin_date').val(strMonth + '/' + getStrFromNum(today.getDate()) + '/' + today.getFullYear());
        }
    });
    
    $('#checkout_year_month option').each(function() {
        var tommorrow = new Date(today.getFullYear(), today.getMonth(), today.getDate()+1);
        var strMonth = getStrFromNum(tommorrow.getMonth()+1);
        
        if($(this).val() == year + '-' + strMonth) {
            $(this).attr('selected', 'selected');
            $('#checkout_date').val(strMonth + '/' + getStrFromNum(tommorrow.getDate()) + '/' + tommorrow.getFullYear());
        }
    });
    
    $('#checkin_day option').each(function() {
        
        if($(this).val() == today.getDate()) {
            $(this).attr('selected', 'selected');
        }
    });
    
    initDays($('#checkout_day option'), tommorrow.getMonth()+1, tommorrow.getFullYear());
    
    $('#checkout_day option').each(function() {
        if($(this).val() == tommorrow.getDate()) {
            $(this).attr('selected', 'selected');
            $('#checkout_date').val(getStrFromNum(tommorrow.getMonth()+1) + '/' + getStrFromNum(tommorrow.getDate()) + '/' + tommorrow.getFullYear());
        }
    });
    
    initDays($('#checkin_day option'), today.getMonth()+1, today.getFullYear());
    //console.log(getStrFromNum(today.getMonth()+1) +'/'+ daysInMonth(today.getMonth(), today.getFullYear()+1) +'/' + (today.getFullYear()+1));
    //console.log(strMonth + '/' + strDay + '/' + year);    
    $('#checkin_date,#checkout_date').datepicker({ 
        beforeShow: customRange,
        onSelect: fchangedDate,
        minDate: getStrFromNum(today.getMonth()+1) + '/' + getStrFromNum(today.getDate()) + '/' + today.getFullYear(), 
        maxDate: getStrFromNum(today.getMonth()+1) +'/'+ daysInMonth(today.getMonth(), today.getFullYear()+1) +'/' + (today.getFullYear()+1), 
        alignment: 'bottomRight', /*onSelect: updateSelected, */
        showOn: "button",
        buttonImage: "images/calendar.gif",
        buttonImageOnly: true
    });
    
    // Update datepicker from three select controls 
    $('#checkin_day').change(function() {
        var yearmonth = $('#checkin_year_month').val();
        yearmonth = yearmonth.split('-');
        
        //zmena kalednare
        var newDate = new Date(parseInt(yearmonth[0]), parseInt(yearmonth[1], 10)-1, $('#checkin_day').val());
        
        $('#checkin_date').datepicker('setDate', newDate); 
        
        // pokud je datum checkinu stejne nebo vetsi nez checkout tak se posune checkout o den za nej
        //var checkoutDate = $('#checkout_date').datepicker('getDate');
        var checkoutDate = new Date($('#checkout_year_month').val().split('-')[0], ($('#checkout_year_month').val().split('-')[1]-1), $('#checkout_day').val())
        
        if(newDate > checkoutDate || +newDate == +checkoutDate) {
            
            checkoutDate.setDate(newDate.getDate()+1);
            if(checkoutDate.getDate() == 1 && newDate.getDate() != 1 && newDate.getMonth() == 11) {
                checkoutDate.setMonth(newDate.getMonth()+1);
                checkoutDate.setFullYear(newDate.getFullYear()+1);
            }
            else if(checkoutDate.getDate() == 1 && newDate.getDate() != 1) {
                checkoutDate.setMonth(newDate.getMonth()+1);
            }
            else {
                checkoutDate.setMonth(newDate.getMonth());
                if(newDate.getFullYear() > checkoutDate.getFullYear() ) {
                    checkoutDate.setFullYear(newDate.getFullYear());
                }
                
            }
            
            $('#checkout_date').datepicker('setDate', checkoutDate); 
            initDays($('#checkin_day option'), newDate.getMonth()+1 , newDate.getFullYear());
            initDays($('#checkout_day option'), checkoutDate.getMonth()+1 , checkoutDate.getFullYear());
            $('#checkout_day').val(checkoutDate.getDate());
            $('#checkout_year_month').val(checkoutDate.getFullYear() + '-' + getStrFromNum(checkoutDate.getMonth()+1));
            
        }
    });
    
    $('#checkin_year_month').change(function() {
        var yearmonth = $('#checkin_year_month').val();
        yearmonth = yearmonth.split('-');
        
        //zmena kalednare
        var newDate = new Date(parseInt(yearmonth[0]), parseInt(yearmonth[1], 10)-1, 1);
        
        $('#checkin_day').val(1);
        $('#checkin_date').datepicker('setDate', newDate); 
        
        // pokud je datum checkinu stejne nebo vetsi nez checkout tak se posune checkout o den za nej
        //var checkoutDate = $('#checkout_date').datepicker('getDate');
        var checkoutDate = new Date($('#checkout_year_month').val().split('-')[0], ($('#checkout_year_month').val().split('-')[1]-1), $('#checkout_day').val())
        
        if(newDate > checkoutDate || +newDate == +checkoutDate) {
            
            checkoutDate.setDate(newDate.getDate()+1);
            if(checkoutDate.getDate() == 1 && newDate.getDate() != 1 && newDate.getMonth() == 11) {
                checkoutDate.setMonth(newDate.getMonth()+1);
                checkoutDate.setFullYear(newDate.getFullYear()+1);
            }
            else if(checkoutDate.getDate() == 1 && newDate.getDate() != 1) {
                checkoutDate.setMonth(newDate.getMonth()+1);
            }
            else {
                checkoutDate.setMonth(newDate.getMonth());
                if(newDate.getFullYear() > checkoutDate.getFullYear() ) {
                    checkoutDate.setFullYear(newDate.getFullYear());
                }
                
            }
            
            $('#checkout_date').datepicker('setDate', checkoutDate); 
            initDays($('#checkin_day option'), newDate.getMonth()+1 , newDate.getFullYear());
            initDays($('#checkout_day option'), checkoutDate.getMonth()+1 , checkoutDate.getFullYear());
            $('#checkout_day').val(checkoutDate.getDate());
            $('#checkout_year_month').val(checkoutDate.getFullYear() + '-' + getStrFromNum(checkoutDate.getMonth()+1));
            
        }
    });
    
    $('#checkout_day,#checkout_year_month').change(function() {
        var yearmonth = $('#checkout_year_month').val();
        yearmonth = yearmonth.split('-');
        var newDate = new Date(parseInt(yearmonth[0]), parseInt(yearmonth[1], 10)-1, $('#checkout_day').val());
        
        $('#checkout_date').datepicker('setDate', newDate); 
    });
});
