(function($) {
	$(document).ready( function()
    {
       
		$("#obra").change( function() 
		{
			
			var teatro = $("#teatro").val();
			$("#peliloading").show();
			$("#teatro").empty();
			$("#teatro").append( '<option value="-1">Selecciona</option>' );
			jQuery.ez( 'combos::teatros::' + $(this).val() + '::' + teatro + '::' + $("#node").val(), {}, function(data){
				$("#peliloading").hide();
				for( var i= 0; i < data.content.teatros.result.length; i++ ) {
					var opcion = '<option value="' + data.content.teatros.result[i].id + '"';
					if ( data.content.teatros.result[i].id == data.content.selectedteatro )
					{
						opcion += ' selected="selected" '
					}
					opcion += '>' + data.content.teatros.result[i].name + '</option>';	
					
					$('#teatro').append( opcion );
				}				
				})
		});	
		
		$("#teatro").change( function() 
		{
			var obra = $("#obra").val();

			$("#obra").empty();
			$("#obra").append( '<option value="-1">Selecciona</option>' );
			jQuery.ez( 'combos::obras::' + $(this).val() + '::' + obra + '::' + $("#node").val(), {}, function(data){
				
				for( var i= 0; i < data.content.obras.result.length; i++ ) {
					var opcion = '<option value="' + data.content.obras.result[i].id + '"';
					if ( data.content.obras.result[i].id == data.content.selectedobra )
					{
						opcion += ' selected="selected" '
					}
					opcion += '>' + data.content.obras.result[i].name + '</option>';	
					$('#obra').append( opcion );
				}				
				})
		});
		
		$("#cine").submit( function(){ 			
			if ( ( $("#teatro").val() == - 1 ) && ( $("#obra").val() == - 1 ) )
			{
				alert( 'Debes escoger una obra o un teatro' );
				return false;
			}

		} );
		
	});	
})(jQuery);

