 conf = function (str){ return confirm('Realmente deseas :\n'+str);}
 
 $(document).ready(function(){
 	// Funcion mostrar ocultar capas.
		$(function() {
				
			var links = $('span.link a');
			var linksCount = links.length;
			var fadeSpeed = "fast";
			
			links.each(function(i) {
				var current = (i+1);
					this.onclick = function () {
			 			$("#"+i).toggle();
					}
			})
		})
		
		
  	$(function(){
		if ( $("form.frm_siestta").length > 0 ) {$("form.frm_siestta").submit();}  	
		if ( $("form.frm_sgdb").length > 0 ) {$("form.frm_sgdb").submit();} 
		if ( $("form.frm_prof").length > 0 ) {$("form.frm_prof").submit();} 
 	}); 		

  /* Profesores */
  //$("#browser").treeview();
  	var url_impreso=$("#url_impreso").val();
	$('#impresos').fileTree({ root: '/', script: url_impreso, folderEvent: 'click', expandSpeed: 750, collapseSpeed: 750, multiFolder: false }, function(file) {					
					showFile(file,"add"); // en add esta view.					
				});					
	
	// Mostrar la información			
	function showFile(file,id_impreso) {
			
			$("div.info").hide();
			$("div.info_box").addClass("load"); 		
			var url=$("#located_url").val();
	    	$.post(url,{id:file, op:id_impreso}, function(j){
	    		$("#files_info").html(j);	    		
		    		 $(document).ready(function(){
						// Dentro
						$("a.edit").click(function(){
								$("div.info").hide();
								var id_impreso="";
								// Aqui si uso id_impreso								
								showFile($("#id_impreso").val(),"edit");								
							return false;
						})	 	    		 	
	    		 })
	    	$("div.info_box").removeClass("load");	
	    	})
	    	    		
	};	
	
	// Date pick
	$('.date-pick').datepicker({showOn: 'button', buttonImage: '/img/calendar_li.png', buttonImageOnly: true, firstDay: 1,   
						onSelect: function(dateText, inst) {
								var datePickerId = $(this).attr('id'); 
								var afecha = dateText.split("/");
								var fecha=new Date(""+afecha[2]+"/"+afecha[1]+"/"+afecha[0]);
								//alert(fecha);
								if (datePickerId == "fecha_aula")
								{
									var text = "aula";
								}
								else {
									var text = "material";
								}
								if (fecha.getDay() == 4)
								{
									$("div.franja_"+text+"_j").show();
									$("div.franja_"+text+"_j select").attr("disabled", false);
									$("div.franja_"+text+"").hide();
									$("div.franja_"+text+" select").attr("disabled", true);
								}
								else
								{
									$("div.franja_"+text+"_j").hide();
									$("div.franja_"+text+"_j select").attr("disabled", true);
									$("div.franja_"+text+"").show();			
									$("div.franja_"+text+" select").attr("disabled", false);
								}
						}}, 
						$.datepicker.regional['es']);
	
	
	/* Reservas */
	$(".tooltip").tooltip();
	
	 $("#aula").change(function () {
		 	var aula = $(this).val();	
		 	if (aula == "") return false;
			var url = $("#located_url_aula").val();
	    	$.post(url, {id:aula}, function(j){
	    		$("#aula_reserva").html(j);
	    		
	    		/* Funcion para abrir, cerrar faq */
	    		$("#reservas li").toggle( 
	    		    function() {$(this).addClass("active")},
	    		    function() {$(this).removeClass();$(this).addClass("date");} 
	    		);  	 
	    		
	    		$(".tooltip").tooltip();
	    	})		 
	 })
	 
	 $("#material").change(function () {
		 	var material = $(this).val();	
		 	if (material == "") return false;
			var url = $("#located_url_material").val();
	    	$.post(url, {id:material}, function(j){
	    		$("#material_reserva").html(j);
	    		
	    		/* Funcion para abrir, cerrar faq */
	    		$("#reservas li").toggle( 
	    		    function() {$(this).addClass("active")},
	    		    function() {$(this).removeClass();$(this).addClass("date");} 
	    		);  	 
	    		
	    		$(".tooltip").tooltip();
	    	})		 
	 })	 

	 
	 /* Franjas horarias */
	 $(".selectaula").change(function () {
		var franja = $(this).val();
		
		if (franja !="")
		{
		    var datos = $(this).val().split("-");
		    	 
		    var horas_i = datos[0];
		    var hora_i = horas_i.split(":");
		    	$("#horasi_aula").val(hora_i[0]);
		    	$("#minutosi_aula").val(hora_i[1]);
		    	
		    	var horas_f = datos[1];
		    	var hora_f = horas_f.split(":");
		    	$("#horasf_aula").val(hora_f[0]);
		    	$("#minutosf_aula").val(hora_f[1]);	    	
		}
	 });
	
	 // Si tocamos las horas, init select
	$("input[class=horas_aula]").each(function(){
			$(this).change(function() {
				$("#franja_aula").val("");
				$("#franja_aula_j").val("");
			})	
	});	 
	 
	 $(".selectmaterial").change(function () {
			var franja = $(this).val();
			
			if (franja !="")
			{
			    var datos = $(this).val().split("-");
			    	 
			    var horas_i = datos[0];
			    var hora_i = horas_i.split(":");
			    	$("#horasi_material").val(hora_i[0]);
			    	$("#minutosi_material").val(hora_i[1]);
			    	
			    	var horas_f = datos[1];
			    	var hora_f = horas_f.split(":");
			    	$("#horasf_material").val(hora_f[0]);
			    	$("#minutosf_material").val(hora_f[1]);	    	
			}
		 });	
	 
	 // Si tocamos las horas, init select
		$("input[class=horas_material]").each(function(){
				$(this).change(function() {
					$("#franja_material").val("");
					$("#franja_material_j").val("");
				})	
		});		 
	 
	/*jstree*/
	var jstreeurl=$("#jstreeurl").val();
	var jstreeurlupdate=$("#jstreeurlupdate").val();
	var jstreeurlsearch=$("#jstreeurlsearch").val();
	
	$("#jstreefiles").jstree({
		
		"plugins" : [ "themes", "html_data" ,"ui", "crrm", "cookies", "dnd", "search", "types", "contextmenu"],
		"html_data" : {			
			"ajax" : { 
				"url" : jstreeurl,
				"type": "get",
				"data" : function (n) { 
						// the result is fed to the AJAX request `data` option
						return { 
							"dir" : n.attr ? "/"+n.attr("id").replace("grupo_","") : "/"
						}; 
					}
				
			}
		},
		
		"types" : {
		
				"max_depth" : -2,
				"max_children" : -2,
				"valid_children" : [ "none"],
				"types" : {
					// The default type
					"default" : {
						// I want this type to have no children (so only leaf nodes)
						// In my case - those are files
						"valid_children" : "none",
						// If we specify an icon for the default type it WILL OVERRIDE the theme icons
						"icon" : {
							"image" : "http://www.iessantaursula.org/img/tree/file.png"
						}
					},
					// The default type
					"files-doc" : {
						// I want this type to have no children (so only leaf nodes)
						// In my case - those are files
						"valid_children" : "none",
						// If we specify an icon for the default type it WILL OVERRIDE the theme icons
						"icon" : {
							"image" : "http://www.iessantaursula.org/img/tree/doc.png"
						}
					},
					"files-txt" : {
						// I want this type to have no children (so only leaf nodes)
						// In my case - those are files
						"valid_children" : "none",
						// If we specify an icon for the default type it WILL OVERRIDE the theme icons
						"icon" : {
							"image" : "http://www.iessantaursula.org/img/tree/txt.png"
						}
					},
					"files-docx" : {
						// I want this type to have no children (so only leaf nodes)
						// In my case - those are files
						"valid_children" : "none",
						// If we specify an icon for the default type it WILL OVERRIDE the theme icons
						"icon" : {
							"image" : "http://www.iessantaursula.org/img/tree/doc.png"
						}
					},
					"files-pdf" : {
						// I want this type to have no children (so only leaf nodes)
						// In my case - those are files
						"valid_children" : "none",
						// If we specify an icon for the default type it WILL OVERRIDE the theme icons
						"icon" : {
							"image" : "http://www.iessantaursula.org/img/tree/pdf.png"
						}
					},
					"files-ppt" : {
						// I want this type to have no children (so only leaf nodes)
						// In my case - those are files
						"valid_children" : "none",
						// If we specify an icon for the default type it WILL OVERRIDE the theme icons
						"icon" : {
							"image" : "http://www.iessantaursula.org/img/tree/ppt.png"
						}
					},
					"files-pptx" : {
						// I want this type to have no children (so only leaf nodes)
						// In my case - those are files
						"valid_children" : "none",						
						// If we specify an icon for the default type it WILL OVERRIDE the theme icons
						"icon" : {
							"image" : "http://www.iessantaursula.org/img/tree/ppt.png"
						}
					},
					// The `folder` type
					"folder" : {
						// can have files and other folders inside of it, but NOT `drive` nodes
						"valid_children" : [ "files-doc","files-pdf" ,"files-ppt","files-pptx","files-docx","files-txt","default"],
						"icon" : {
							"image" : "http://www.iessantaursula.org/img/tree/folder.png"							
						},
						
						"start_drag" : false,
						"move_node" : false,
						"delete_node" : false,
						"remove" : false,
						"select_node" : true
					}
				}
			},
			"ui" : {
				"select_limit" : 1			
			},
			"contextmenu":{

				"items" : {
					// Some key
					"rename" : false,
					"create" :false,
					"remove" :false,
					"ccp": false,
					"cut":{
						    "label" : "Cortar",

							"action" : function(obj) {
									this.cut(obj);
							},
							"seperator_after" : true,
							"seperator_before" : false,
							"icon" : false 
					},
					"paste":{
						    "label" : "Pegar",

							"action" : function(obj) {
									this.paste(obj);
							},
							"seperator_after" : true,
							"seperator_before" : false,
							"icon" : false 
					}
					

					
				}

			
			},			
			"themes" : {
				"theme" : "default",
				"dots" : false,
				"icons" : true 
			},
			// Configuring the search plugin
			"search" : {
				// As this has been a common question - async search
				// Same as above - the `ajax` config option is actually jQuery's object (only `data` can be a function)
				"case_insensitive" : true,
				"ajax" : {
					"url" :jstreeurlsearch,
					"type":"get",
					// You get the search string as a parameter
					"data" : function (str) {						
						return { 							
							"txt" : str 
						}; 
					}
					
				}
			}
	}).bind("move_node.jstree", function (e, data) {
			data.rslt.o.each(function (i) {
				
				$.ajax({
					async : false,
					type: 'POST',
					url: jstreeurlupdate,
					data : { 
						
						"id_documento" : $(this).attr("id").replace("doc_",""), 
						"id_grupo" : data.rslt.np.attr("id").replace("grupo_","")
						
					},
					success : function (r) {						
							$('#jstreefiles').jstree('refresh',-1);						
					}
					
				});
			});

	 }).bind("select_node.jstree", function (e,data) {
			var rel= data.rslt.obj.attr("rel");
			var node_id;
			if (rel=='folder')
				node_id ="/g:"+data.rslt.obj.attr("id").replace("grupo_","");	
			else
				node_id ="/f:"+data.rslt.obj.attr("id").replace("doc_","");	
		
			showFile(node_id,"add");

	 });
	 
	 
	 $("#btnsearch").click(function () {
		
		$("#jstreefiles").jstree("search",$("#search").val());
	});
	
	$("#btnlimpiar").click(function () {
		$("#search").val("");
		$('#jstreefiles').jstree('closed_node',-1);	
	});
	

	
 	// Mensajes
    //$("div.msgok").fadeOut(5000);
    //$("div.errors").fadeOut(5000); 	 	
	
});

