if (location.href.substr(7).indexOf('/') != -1) {
    var url = location.href.substr(7, location.href.substr(7).indexOf('/'));
} else {
    var url = saalbook.local;
}

jQuery(function() {
        jQuery("#dp_from").datepicker({
            onSelect: function(dateText, inst) {
                jQuery('#dp_to').load("http://" + url + "/calendardata/dpto?date=" + dateText);
                $('#NoHutAvailable').css('display', 'none');
		        disableSets();
            },
            minDate: new Date()
        });
        jQuery("#dp_from").change(function() {
            jQuery('#dp_to').load("http://" + url + "/calendardata/dpto?date=" + dateText);
	        disableSets();
        });

        jQuery('#fieldset-Services input.amount').change(function() {
            if (jQuery(this).val() != '') {
                (jQuery('#svc_' + (this.id).substr(7, 1)).attr('checked', 'checked'));
            } else {
                (jQuery('#svc_' + (this.id).substr(7, 1)).attr('checked', false));
            }
        });

        jQuery('#persons').change(function() {
            var children = jQuery('#children').val();
            if (children == 0) {
                return;
            }

            if (this.value == 0 || this.value == 1) {
                jQuery('#children').val(0);
                jQuery('#ChildrenTab').hide();
                return;
            }

            if (children >= this.value) {
                jQuery('#children').val(this.value - 1);
            }

            if (jQuery('#children').val() > 0) {
                children = jQuery('#children').val();
                jQuery('#ChildrenTab').show();
                jQuery('#ChildrenTab tr').hide();
                jQuery('#ChildrenTab tr:first').show();

                for (var x=1; x <= children; x++) {
                    jQuery('#ChRow' + x.toString()).show();
                }
            } else {
                jQuery('#ChildrenTab').hide();
            }
        });

        jQuery('#children').change(function() {
            var persons = jQuery('#persons').val();
            if (persons == 0 || persons == 1) {
                this.value = 0;
                return;
            }

            if (persons <= this.value) {
                this.value = persons -1;
            }

            if (this.value > 0) {
                jQuery('#ChildrenTab').show();
                jQuery('#ChildrenTab tr').hide();
                jQuery('#ChildrenTab tr:first').show();

                for (var x=1; x <= this.value; x++) {
                    jQuery('#ChRow' + x.toString()).show();
                }
            } else {
                jQuery('#ChildrenTab').hide();
            }

        });

        // In case of reload make children visible
        var childrenCount = jQuery('#children').val();
        if (childrenCount > 0) {
            jQuery('#ChildrenTab').show();
            jQuery('#ChildrenTab tr').hide();
            jQuery('#ChildrenTab tr:first').show();

            for (var x=1; x <= childrenCount; x++) {
                jQuery('#ChRow' + x.toString()).show();
            }
        }

        // Tooltips
        jQuery('#Content acronym').tooltip({
            showUrl: false
        });

        // Check all disabled checkboxes
        jQuery('#fieldset-Services input[type=checkbox][disabled=disabled]').attr('checked', 'checked');

        // Update Booking length
        setTimeout('updateBookingLength()', 100);

	// Add click function to radio "Huettenauswahl"
	jQuery('#FsHutPicker input[type="radio"]').each(function() {
	    $(this).click(function() {
			enableSets(3);
	    });
	});

	jQuery('#FrmHutConfig input[type="reset"]').click(function() {
	    disableSets();
	});
});

function getHutRental() {
	// proof if hut is checked
	var hutid = $('#FsHutPicker input:radio:checked').val();
	if($('#dp_from').val() != '' && $('#dp_to').val() != '' && hutid > 0) {
	    $.getJSON(
	    	'/booking/hutrental',
	    	{dp_from : $('#dp_from').val(), dp_to : $('#dp_to').val(), hut_id: hutid},
	    	function(_json) {
	    		$('#price_value').html(_json.price);
	    		$('#price_text').html(_json.message);
	    		calc();
	    	}
	    );
	} else {
		// set price to null
	}
}

function checkFreeHuts() {
	
	$('#FsHutPicker input[type="radio"]').each(function() {
		$(this).attr('disabled', true);
	});

    $.ajax({
	type : 'POST',
	data : ({dp_from : $('#dp_from').val(), dp_to : $('#dp_to').val()}),
	url : '/booking/freehuts',
	success : function(_response) {
		var obj = eval(_response);
		if(obj.length > 0) {
            $('#NoHutAvailable').css('display', 'none');
			jQuery.each(obj, function(index, hutid) {
				$('#FsHutPicker input[id="hut_id-'+hutid+'"]').attr('disabled', false);
			});
		} else {
			$('#NoHutAvailable').css('display', 'block');
		}
	}
    });
}

// Set Opacity = 0.3 and disable all input fields
function disableSets() {
    jQuery('#FsHutPicker input[type="radio"]').each(function() {
        $(this).attr('checked', false);
        $(this).attr('disabled', true);
    });

    if($('#dp_to').val() == '' || $('#dp_to').val() <= 0) {
    	jQuery('#FsHutPicker input[type="radio"]').each(function() {
    	    $(this).attr('disabled', true);
    	});
    	$('#FsHutPicker').css('opacity', 0.3);
    }

	$('#FsPersons').css('opacity', 0.3);
	jQuery('#FsPersons select, #ChildrenTab input').each(function() {
	    $(this).attr('disabled', true);
	});
}

// Set Opacity = 1 and enable all input fields
function enableSets(p_which) {
    switch(p_which) {
        case 2:
            $('#FsHutPicker').css('opacity', 1);
            break;
            
        case 3:
            jQuery('#FsPersons select, #ChildrenTab input').each(function() {
                $(this).attr('disabled', false);
            });
            $('#FsPersons').css('opacity', 1);       
            break;
    }
}

function updateBookingLength() {
    if (jQuery('#dp_from').val() != '') {
        var dateText = jQuery('#dp_from').val();
        var oldVal = jQuery('#dp_to').val();
        jQuery('#dp_to').load("http://" + url + "/calendardata/dpto?date=" + dateText + "&old=" + oldVal, function() {
        	if(typeof calc == 'function') {
        		getHutRental();
            }
        });
    }
}
